Skip to content

Commit

Permalink
fix: Error when comptime functions are used in runtime code (#5976)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

Adds an error during monomorphization if any comptime functions fall
through the cracks and aren't executed at compile-time.

## Additional Context



## 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 Sep 9, 2024
1 parent ec442d4 commit ec24917
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 8 additions & 0 deletions compiler/noirc_frontend/src/monomorphization/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ pub enum MonomorphizationError {
NoDefaultType { location: Location },
InternalError { message: &'static str, location: Location },
InterpreterError(InterpreterError),
ComptimeFnInRuntimeCode { name: String, location: Location },
}

impl MonomorphizationError {
fn location(&self) -> Location {
match self {
MonomorphizationError::UnknownArrayLength { location, .. }
| MonomorphizationError::InternalError { location, .. }
| MonomorphizationError::ComptimeFnInRuntimeCode { location, .. }
| MonomorphizationError::NoDefaultType { location, .. } => *location,
MonomorphizationError::InterpreterError(error) => error.get_location(),
}
Expand Down Expand Up @@ -43,6 +45,12 @@ impl MonomorphizationError {
}
MonomorphizationError::InterpreterError(error) => return error.into(),
MonomorphizationError::InternalError { message, .. } => message.to_string(),
MonomorphizationError::ComptimeFnInRuntimeCode { name, location } => {
let message = format!("Comptime function {name} used in runtime code");
let secondary =
"Comptime functions must be in a comptime block to be called".into();
return CustomDiagnostic::simple_error(message, secondary, location.span);
}
};

let location = self.location();
Expand Down
12 changes: 10 additions & 2 deletions compiler/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub fn monomorphize_debug(
let impl_bindings = perform_impl_bindings(interner, trait_method, next_fn_id, location)
.map_err(MonomorphizationError::InterpreterError)?;

monomorphizer.function(next_fn_id, new_id)?;
monomorphizer.function(next_fn_id, new_id, location)?;
undo_instantiation_bindings(impl_bindings);
undo_instantiation_bindings(bindings);
}
Expand Down Expand Up @@ -278,7 +278,10 @@ impl<'interner> Monomorphizer<'interner> {
) -> Result<FunctionSignature, MonomorphizationError> {
let new_main_id = self.next_function_id();
assert_eq!(new_main_id, Program::main_id());
self.function(main_id, new_main_id)?;

let location = self.interner.function_meta(&main_id).location;
self.function(main_id, new_main_id, location)?;

self.return_location =
self.interner.function(&main_id).block(self.interner).statements().last().and_then(
|x| match self.interner.statement(x) {
Expand All @@ -294,6 +297,7 @@ impl<'interner> Monomorphizer<'interner> {
&mut self,
f: node_interner::FuncId,
id: FuncId,
location: Location,
) -> Result<(), MonomorphizationError> {
if let Some((self_type, trait_id)) = self.interner.get_function_trait(&f) {
let the_trait = self.interner.get_trait(trait_id);
Expand All @@ -313,6 +317,10 @@ impl<'interner> Monomorphizer<'interner> {
let modifiers = self.interner.function_modifiers(&f);
let name = self.interner.function_name(&f).to_owned();

if modifiers.is_comptime {
return Err(MonomorphizationError::ComptimeFnInRuntimeCode { name, location });
}

let body_expr_id = self.interner.function(&f).as_expr();
let body_return_type = self.interner.id_type(body_expr_id);
let return_type = match meta.return_type() {
Expand Down

0 comments on commit ec24917

Please sign in to comment.