Skip to content

Commit

Permalink
Also preserve warnings when resetting scope for backtracking.
Browse files Browse the repository at this point in the history
  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.
  • Loading branch information
KtorZ committed May 16, 2024
1 parent eadf709 commit 27b3536
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion crates/aiken-lang/src/tipo/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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,
Expand All @@ -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);
}

Expand Down

0 comments on commit 27b3536

Please sign in to comment.