Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't panic when using undefined variables in the interpreter #5381

Merged
merged 4 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions compiler/noirc_frontend/src/hir/comptime/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
ArgumentCountMismatch { expected: usize, actual: usize, location: Location },
TypeMismatch { expected: Type, value: Value, location: Location },
NonComptimeVarReferenced { name: String, location: Location },
michaeljklein marked this conversation as resolved.
Show resolved Hide resolved
VariableNotInScope { location: Location },
IntegerOutOfRangeForType { value: FieldElement, typ: Type, location: Location },
ErrorNodeEncountered { location: Location },
NonFunctionCalled { value: Value, location: Location },
Expand Down Expand Up @@ -83,6 +84,7 @@
InterpreterError::ArgumentCountMismatch { location, .. }
| InterpreterError::TypeMismatch { location, .. }
| InterpreterError::NonComptimeVarReferenced { location, .. }
| InterpreterError::VariableNotInScope { location, .. }
| InterpreterError::IntegerOutOfRangeForType { location, .. }
| InterpreterError::ErrorNodeEncountered { location, .. }
| InterpreterError::NonFunctionCalled { location, .. }
Expand Down Expand Up @@ -152,6 +154,11 @@
let secondary = "Non-comptime variables can't be used in comptime code".to_string();
CustomDiagnostic::simple_error(msg, secondary, location.span)
}
InterpreterError::VariableNotInScope { location } => {
let msg = "Variable not in scope".to_string();
let secondary = "Could not find variable".to_string();
CustomDiagnostic::simple_error(msg, secondary, location.span)
}
InterpreterError::IntegerOutOfRangeForType { value, typ, location } => {
let int = match value.try_into_u128() {
Some(int) => int.to_string(),
Expand Down Expand Up @@ -286,7 +293,7 @@
let message = format!("Failed to parse macro's token stream into {rule}");
let tokens = vecmap(&tokens.0, ToString::to_string).join(" ");

// 10 is an aribtrary number of tokens here chosen to fit roughly onto one line

Check warning on line 296 in compiler/noirc_frontend/src/hir/comptime/errors.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (aribtrary)
let token_stream = if tokens.len() > 10 {
format!("The resulting token stream was: {tokens}")
} else {
Expand Down
27 changes: 17 additions & 10 deletions compiler/noirc_frontend/src/hir/comptime/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
}
} else {
let name = self.interner.function_name(&function);
unreachable!("Non-builtin, lowlevel or oracle builtin fn '{name}'")

Check warning on line 150 in compiler/noirc_frontend/src/hir/comptime/interpreter.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lowlevel)
}
}

Expand Down Expand Up @@ -298,8 +298,7 @@
return Ok(());
}
}
let name = self.interner.definition(id).name.clone();
Err(InterpreterError::NonComptimeVarReferenced { name, location })
Err(InterpreterError::VariableNotInScope { location })
}

pub(super) fn lookup(&self, ident: &HirIdent) -> IResult<Value> {
Expand All @@ -313,12 +312,12 @@
}
}

// Justification for `NonComptimeVarReferenced`:
// If we have an id to lookup at all that means name resolution successfully
// found another variable in scope for this name. If the name is in scope
// but unknown by the interpreter it must be because it was not a comptime variable.
let name = self.interner.definition(id).name.clone();
Err(InterpreterError::NonComptimeVarReferenced { name, location })
if id == DefinitionId::dummy_id() {
Err(InterpreterError::VariableNotInScope { location })
} else {
let name = self.interner.definition_name(id).to_string();
Err(InterpreterError::NonComptimeVarReferenced { name, location })
}
}

/// Evaluate an expression and return the result
Expand Down Expand Up @@ -354,7 +353,10 @@
}

pub(super) fn evaluate_ident(&mut self, ident: HirIdent, id: ExprId) -> IResult<Value> {
let definition = self.interner.definition(ident.id);
let definition = self.interner.try_definition(ident.id).ok_or_else(|| {
let location = self.interner.expr_location(&id);
InterpreterError::VariableNotInScope { location }
})?;

if let ImplKind::TraitMethod(method, _, _) = ident.impl_kind {
let method_id = resolve_trait_method(self.interner, method, id)?;
Expand All @@ -375,7 +377,12 @@
if let Ok(value) = self.lookup(&ident) {
Ok(value)
} else {
let let_ = self.interner.get_global_let_statement(*global_id).unwrap();
let let_ =
self.interner.get_global_let_statement(*global_id).ok_or_else(|| {
let location = self.interner.expr_location(&id);
InterpreterError::VariableNotInScope { location }
})?;

if let_.comptime {
self.evaluate_let(let_.clone())?;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "comptime_var_not_defined"
type = "bin"
authors = [""]
compiler_version = ">=0.31.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
comptime {
foo();
}
}
Loading