Clean up tyc pass

This commit is contained in:
Pranav Gaddamadugu 2022-10-03 10:40:59 -07:00
parent 80d8880292
commit fef22d6d5a
3 changed files with 25 additions and 27 deletions

View File

@ -101,14 +101,13 @@ impl<'a> ProgramVisitor<'a> for TypeChecker<'a> {
fn visit_function(&mut self, function: &'a Function) { fn visit_function(&mut self, function: &'a Function) {
// Check that the function's annotations are valid. // Check that the function's annotations are valid.
// Note that Leo does not natively support any specific annotations.
for annotation in function.annotations.iter() { for annotation in function.annotations.iter() {
match annotation.identifier.name { self.emit_err(TypeCheckerError::unknown_annotation(annotation, annotation.span))
// Set `is_program_function` to true if the corresponding annotation is found.
sym::program => self.is_program_function = true,
_ => self.emit_err(TypeCheckerError::unknown_annotation(annotation, annotation.span)),
}
} }
self.is_transition_function = matches!(function.call_type, CallType::Transition);
// Lookup function metadata in the symbol table. // Lookup function metadata in the symbol table.
// Note that this unwrap is safe since function metadata is stored in a prior pass. // Note that this unwrap is safe since function metadata is stored in a prior pass.
let function_index = self let function_index = self
@ -139,14 +138,14 @@ impl<'a> ProgramVisitor<'a> for TypeChecker<'a> {
self.assert_type_is_valid(input_var.span(), &input_var.type_()); self.assert_type_is_valid(input_var.span(), &input_var.type_());
self.assert_not_tuple(input_var.span(), &input_var.type_()); self.assert_not_tuple(input_var.span(), &input_var.type_());
match self.is_program_function { match self.is_transition_function {
// If the function is a program function, then check that the parameter mode is not a constant. // If the function is a transition function, then check that the parameter mode is not a constant.
true if input_var.mode() == Mode::Const => self.emit_err( true if input_var.mode() == Mode::Const => self.emit_err(
TypeCheckerError::program_function_inputs_cannot_be_const(input_var.span()), TypeCheckerError::transition_function_inputs_cannot_be_const(input_var.span()),
), ),
// If the function is not a program function, then check that the parameters do not have an associated mode. // If the function is not a program function, then check that the parameters do not have an associated mode.
false if input_var.mode() != Mode::None => self.emit_err( false if input_var.mode() != Mode::None => self.emit_err(
TypeCheckerError::helper_function_inputs_cannot_have_modes(input_var.span()), TypeCheckerError::regular_function_inputs_cannot_have_modes(input_var.span()),
), ),
_ => {} // Do nothing. _ => {} // Do nothing.
} }
@ -206,9 +205,9 @@ impl<'a> ProgramVisitor<'a> for TypeChecker<'a> {
// The function;s finalize block does not have a finalize statement. // The function;s finalize block does not have a finalize statement.
self.has_finalize = false; self.has_finalize = false;
// Check that the function is a program function. // Check that the function is a transition function.
if !self.is_program_function { if !self.is_transition_function {
self.emit_err(TypeCheckerError::only_program_functions_can_have_finalize( self.emit_err(TypeCheckerError::only_transition_functions_can_have_finalize(
finalize.span, finalize.span,
)); ));
} }
@ -285,7 +284,7 @@ impl<'a> ProgramVisitor<'a> for TypeChecker<'a> {
// Exit the function's scope. // Exit the function's scope.
self.exit_scope(function_index); self.exit_scope(function_index);
// Unset `is_program_function` flag. // Unset `is_transition_function` flag.
self.is_program_function = false; self.is_transition_function = false;
} }
} }

View File

@ -35,9 +35,8 @@ pub struct TypeChecker<'a> {
pub(crate) has_return: bool, pub(crate) has_return: bool,
/// Whether or not the function that we are currently traversing has a finalize statement. /// Whether or not the function that we are currently traversing has a finalize statement.
pub(crate) has_finalize: bool, pub(crate) has_finalize: bool,
/// Whether or not we are currently traversing a program function. /// Whether or not we are currently traversing a transition function.
/// A "program function" is a function that can be invoked by a user or another program. pub(crate) is_transition_function: bool,
pub(crate) is_program_function: bool,
/// Whether or not we are currently traversing a finalize block. /// Whether or not we are currently traversing a finalize block.
pub(crate) is_finalize: bool, pub(crate) is_finalize: bool,
} }
@ -89,7 +88,7 @@ impl<'a> TypeChecker<'a> {
/// Returns a new type checker given a symbol table and error handler. /// Returns a new type checker given a symbol table and error handler.
pub fn new(symbol_table: SymbolTable, handler: &'a Handler) -> Self { pub fn new(symbol_table: SymbolTable, handler: &'a Handler) -> Self {
Self { Self {
is_program_function: false, is_transition_function: false,
symbol_table: RefCell::new(symbol_table), symbol_table: RefCell::new(symbol_table),
handler, handler,
function: None, function: None,

View File

@ -279,14 +279,14 @@ create_messages!(
unknown_annotation { unknown_annotation {
args: (annotation: impl Display), args: (annotation: impl Display),
msg: format!("Unknown annotation: `{annotation}`."), msg: format!("Unknown annotation: `{annotation}`."),
help: Some("Use a valid annotation. The Leo compiler supports: `@program`".to_string()), help: None,
} }
@formatted @formatted
helper_function_inputs_cannot_have_modes { regular_function_inputs_cannot_have_modes {
args: (), args: (),
msg: format!("Helper functions cannot have modes associated with their inputs."), msg: format!("Standard functions cannot have modes associated with their inputs."),
help: Some("Consider removing the mode or adding a `@program` annotation to the function.".to_string()), help: Some("Consider removing the mode or using the keyword `transition` instead of `function`.".to_string()),
} }
@formatted @formatted
@ -304,10 +304,10 @@ create_messages!(
} }
@formatted @formatted
only_program_functions_can_have_finalize { only_transition_functions_can_have_finalize {
args: (), args: (),
msg: format!("Only program functions can have a `finalize` block."), msg: format!("Only transition functions can have a `finalize` block."),
help: Some("Remove the `finalize` block or add a `@program` annotation to the function.".to_string()), help: Some("Remove the `finalize` block or use the keyword `transition` instead of `function`.".to_string()),
} }
@formatted @formatted
@ -367,9 +367,9 @@ create_messages!(
} }
@formatted @formatted
program_function_inputs_cannot_be_const { transition_function_inputs_cannot_be_const {
args: (), args: (),
msg: format!("Program functions cannot have constant inputs."), msg: format!("Transition functions cannot have constant inputs."),
help: None, help: None,
} }