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: Change panic to error in interpreter #5446

Merged
merged 1 commit into from
Jul 8, 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
8 changes: 8 additions & 0 deletions compiler/noirc_frontend/src/hir/comptime/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
NonComptimeFnCallInSameCrate { function: String, location: Location },
NoImpl { location: Location },
NoMatchingImplFound { error: NoMatchingImplFoundError, file: FileId },
ImplMethodTypeMismatch { expected: Type, actual: Type, location: Location },

Unimplemented { item: String, location: Location },

Expand Down Expand Up @@ -114,6 +115,7 @@
| InterpreterError::NonComptimeFnCallInSameCrate { location, .. }
| InterpreterError::Unimplemented { location, .. }
| InterpreterError::NoImpl { location, .. }
| InterpreterError::ImplMethodTypeMismatch { location, .. }
| InterpreterError::BreakNotInLoop { location, .. }
| InterpreterError::ContinueNotInLoop { location, .. } => *location,
InterpreterError::FailedToParseMacro { error, file, .. } => {
Expand Down Expand Up @@ -293,7 +295,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 298 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 Expand Up @@ -344,6 +346,12 @@
let msg = "No impl found due to prior type error".into();
CustomDiagnostic::simple_error(msg, String::new(), location.span)
}
InterpreterError::ImplMethodTypeMismatch { expected, actual, location } => {
let msg = format!(
"Impl method type {actual} does not unify with trait method type {expected}"
);
CustomDiagnostic::simple_error(msg, String::new(), location.span)
}
InterpreterError::NoMatchingImplFound { error, .. } => error.into(),
InterpreterError::Break => unreachable!("Uncaught InterpreterError::Break"),
InterpreterError::Continue => unreachable!("Uncaught InterpreterError::Continue"),
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/hir/comptime/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
let trait_method = self.interner.get_trait_method_id(function);

perform_instantiation_bindings(&instantiation_bindings);
let impl_bindings = perform_impl_bindings(self.interner, trait_method, function);
let impl_bindings = perform_impl_bindings(self.interner, trait_method, function, location)?;
let result = self.call_function_inner(function, arguments, location);
undo_instantiation_bindings(impl_bindings);
undo_instantiation_bindings(instantiation_bindings);
Expand Down 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
28 changes: 19 additions & 9 deletions compiler/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct Monomorphizer<'interner> {

/// Queue of functions to monomorphize next each item in the queue is a tuple of:
/// (old_id, new_monomorphized_id, any type bindings to apply, the trait method if old_id is from a trait impl)
queue: VecDeque<(node_interner::FuncId, FuncId, TypeBindings, Option<TraitMethodId>)>,
queue: VecDeque<(node_interner::FuncId, FuncId, TypeBindings, Option<TraitMethodId>, Location)>,

/// When a function finishes being monomorphized, the monomorphized ast::Function is
/// stored here along with its FuncId.
Expand Down Expand Up @@ -124,11 +124,15 @@ pub fn monomorphize_debug(
let function_sig = monomorphizer.compile_main(main)?;

while !monomorphizer.queue.is_empty() {
let (next_fn_id, new_id, bindings, trait_method) = monomorphizer.queue.pop_front().unwrap();
let (next_fn_id, new_id, bindings, trait_method, location) =
monomorphizer.queue.pop_front().unwrap();
monomorphizer.locals.clear();

perform_instantiation_bindings(&bindings);
let impl_bindings = perform_impl_bindings(monomorphizer.interner, trait_method, next_fn_id);
let interner = &monomorphizer.interner;
let impl_bindings = perform_impl_bindings(interner, trait_method, next_fn_id, location)
.map_err(MonomorphizationError::InterpreterError)?;

monomorphizer.function(next_fn_id, new_id)?;
undo_instantiation_bindings(impl_bindings);
undo_instantiation_bindings(bindings);
Expand Down Expand Up @@ -1275,9 +1279,10 @@ impl<'interner> Monomorphizer<'interner> {
let new_id = self.next_function_id();
self.define_function(id, function_type.clone(), turbofish_generics, new_id);

let location = self.interner.expr_location(&expr_id);
let bindings = self.interner.get_instantiation_bindings(expr_id);
let bindings = self.follow_bindings(bindings);
self.queue.push_back((id, new_id, bindings, trait_method));
self.queue.push_back((id, new_id, bindings, trait_method, location));
new_id
}

Expand Down Expand Up @@ -1747,7 +1752,8 @@ pub fn perform_impl_bindings(
interner: &NodeInterner,
trait_method: Option<TraitMethodId>,
impl_method: node_interner::FuncId,
) -> TypeBindings {
location: Location,
) -> Result<TypeBindings, InterpreterError> {
let mut bindings = TypeBindings::new();

if let Some(trait_method) = trait_method {
Expand All @@ -1767,14 +1773,18 @@ pub fn perform_impl_bindings(
let type_bindings = generics.iter().map(replace_type_variable).collect();
let impl_method_type = impl_method_type.force_substitute(&type_bindings);

trait_method_type.try_unify(&impl_method_type, &mut bindings).unwrap_or_else(|_| {
unreachable!("Impl method type {} does not unify with trait method type {} during monomorphization", impl_method_type, trait_method_type)
});
trait_method_type.try_unify(&impl_method_type, &mut bindings).map_err(|_| {
InterpreterError::ImplMethodTypeMismatch {
expected: trait_method_type.clone(),
actual: impl_method_type,
location,
}
})?;

perform_instantiation_bindings(&bindings);
}

bindings
Ok(bindings)
}

pub fn resolve_trait_method(
Expand Down
Loading