From 27b3536f0976306123e1284d8755ca636fedb8b0 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Thu, 16 May 2024 23:20:52 +0200 Subject: [PATCH] Also preserve warnings when resetting scope for backtracking. This is crucial as some checks regarding variable usages depends on warnings; so we may accidentally remove variables from the AST as a consequence of backtracking for deep inferrence. --- crates/aiken-lang/src/tipo/expr.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/aiken-lang/src/tipo/expr.rs b/crates/aiken-lang/src/tipo/expr.rs index 6045150d..e0abb1ee 100644 --- a/crates/aiken-lang/src/tipo/expr.rs +++ b/crates/aiken-lang/src/tipo/expr.rs @@ -71,6 +71,8 @@ pub(crate) fn infer_function( .function_types() .unwrap_or_else(|| panic!("Preregistered type for fn {name} was not a fn")); + let warnings = environment.warnings.clone(); + // ━━━ open new scope ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ let initial_scope = environment.open_new_scope(); @@ -106,10 +108,12 @@ pub(crate) fn infer_function( // callee. Otherwise, identifiers present in the caller's scope may become available to the // callee. if let Err(Error::MustInferFirst { function, .. }) = inferred { + // Reset the environment & scope. hydrators.insert(name.to_string(), expr_typer.hydrator); - environment.close_scope(initial_scope); + *environment.warnings = warnings; + // Backtrack and infer callee first. infer_function( &function, environment.current_module, @@ -119,6 +123,7 @@ pub(crate) fn infer_function( tracing, )?; + // Then, try again the entire function definition. return infer_function(fun, module_name, hydrators, environment, lines, tracing); }