Skip to content

Commit

Permalink
fix: Replace panic in monomorphization with an error (noir-lang#5305)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

## Summary\*

Replaces an `unwrap` and panic in the monomorphizer with an ICE issued
to the user instead. This isn't expected to be issued normally, but is
being issued currently when a `comptime let` variable is used in runtime
code since `comptime let` is still unimplemented in the evaluator.

## Additional Context

This does not fix the underlying `comptime let` error, only the
resulting panic.

## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
jfecher authored Jun 21, 2024
1 parent ec728dd commit 49e1b0c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 3 additions & 0 deletions compiler/noirc_frontend/src/monomorphization/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ use crate::hir::comptime::InterpreterError;
pub enum MonomorphizationError {
UnknownArrayLength { location: Location },
TypeAnnotationsNeeded { location: Location },
InternalError { message: &'static str, location: Location },
InterpreterError(InterpreterError),
}

impl MonomorphizationError {
fn location(&self) -> Location {
match self {
MonomorphizationError::UnknownArrayLength { location }
| MonomorphizationError::InternalError { location, .. }
| MonomorphizationError::TypeAnnotationsNeeded { location } => *location,
MonomorphizationError::InterpreterError(error) => error.get_location(),
}
Expand All @@ -36,6 +38,7 @@ impl MonomorphizationError {
}
MonomorphizationError::TypeAnnotationsNeeded { .. } => "Type annotations needed",
MonomorphizationError::InterpreterError(error) => return (&error).into(),
MonomorphizationError::InternalError { message, .. } => message,
};

let location = self.location();
Expand Down
6 changes: 5 additions & 1 deletion compiler/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,11 @@ impl<'interner> Monomorphizer<'interner> {
DefinitionKind::Local(_) => match self.lookup_captured_expr(ident.id) {
Some(expr) => expr,
None => {
let ident = self.local_ident(&ident)?.unwrap();
let Some(ident) = self.local_ident(&ident)? else {
let location = self.interner.id_location(expr_id);
let message = "ICE: Variable not found during monomorphization";
return Err(MonomorphizationError::InternalError { location, message });
};
ast::Expression::Ident(ident)
}
},
Expand Down

0 comments on commit 49e1b0c

Please sign in to comment.