Skip to content

Commit

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

## Problem\*

Resolves #5983

## Summary\*

Errors when a comptime-only type is used in runtime code.
Currently this is just the various quoted types but in the future would
also include user-defined `comptime` structs.

## 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 10, 2024
1 parent d6f60d7 commit 3d39196
Show file tree
Hide file tree
Showing 19 changed files with 153 additions and 115 deletions.
10 changes: 9 additions & 1 deletion compiler/noirc_frontend/src/elaborator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,15 @@ impl<'context> Elaborator<'context> {
let fields = self.resolve_type_inner(*fields, kind);
Type::FmtString(Box::new(resolved_size), Box::new(fields))
}
Quoted(quoted) => Type::Quoted(quoted),
Quoted(quoted) => {
let in_function = matches!(self.current_item, Some(DependencyId::Function(_)));
if in_function && !self.in_comptime_context() {
let span = typ.span;
let typ = quoted.to_string();
self.push_err(ResolverError::ComptimeTypeInRuntimeCode { span, typ });
}
Type::Quoted(quoted)
}
Unit => Type::Unit,
Unspecified => {
let span = typ.span;
Expand Down
9 changes: 9 additions & 0 deletions compiler/noirc_frontend/src/hir/resolution/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ pub enum ResolverError {
OverflowInType { lhs: u32, op: crate::BinaryTypeOperator, rhs: u32, span: Span },
#[error("`quote` cannot be used in runtime code")]
QuoteInRuntimeCode { span: Span },
#[error("Comptime-only type `{typ}` cannot be used in runtime code")]
ComptimeTypeInRuntimeCode { typ: String, span: Span },
}

impl ResolverError {
Expand Down Expand Up @@ -513,6 +515,13 @@ impl<'a> From<&'a ResolverError> for Diagnostic {
*span,
)
},
ResolverError::ComptimeTypeInRuntimeCode { typ, span } => {
Diagnostic::simple_error(
format!("Comptime-only type `{typ}` cannot be used in runtime code"),
"Comptime-only type used here".to_string(),
*span,
)
},
}
}
}
7 changes: 7 additions & 0 deletions compiler/noirc_frontend/src/monomorphization/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum MonomorphizationError {
InternalError { message: &'static str, location: Location },
InterpreterError(InterpreterError),
ComptimeFnInRuntimeCode { name: String, location: Location },
ComptimeTypeInRuntimeCode { typ: String, location: Location },
}

impl MonomorphizationError {
Expand All @@ -17,6 +18,7 @@ impl MonomorphizationError {
MonomorphizationError::UnknownArrayLength { location, .. }
| MonomorphizationError::InternalError { location, .. }
| MonomorphizationError::ComptimeFnInRuntimeCode { location, .. }
| MonomorphizationError::ComptimeTypeInRuntimeCode { location, .. }
| MonomorphizationError::NoDefaultType { location, .. } => *location,
MonomorphizationError::InterpreterError(error) => error.get_location(),
}
Expand Down Expand Up @@ -51,6 +53,11 @@ impl MonomorphizationError {
"Comptime functions must be in a comptime block to be called".into();
return CustomDiagnostic::simple_error(message, secondary, location.span);
}
MonomorphizationError::ComptimeTypeInRuntimeCode { typ, location } => {
let message = format!("Comptime-only type `{typ}` used in runtime code");
let secondary = "Comptime type used here".into();
return CustomDiagnostic::simple_error(message, secondary, location.span);
}
};

let location = self.location();
Expand Down
5 changes: 4 additions & 1 deletion compiler/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,10 @@ impl<'interner> Monomorphizer<'interner> {
let message = "Unexpected Type::Error found during monomorphization";
return Err(MonomorphizationError::InternalError { message, location });
}
HirType::Quoted(_) => unreachable!("Tried to translate Code type into runtime code"),
HirType::Quoted(typ) => {
let typ = typ.to_string();
return Err(MonomorphizationError::ComptimeTypeInRuntimeCode { typ, location });
}
})
}

Expand Down
11 changes: 11 additions & 0 deletions compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3451,3 +3451,14 @@ fn constrained_reference_to_unconstrained() {
panic!("Expected an error about passing a constrained reference to unconstrained");
};
}

#[test]
fn comptime_type_in_runtime_code() {
let source = "pub fn foo(_f: FunctionDefinition) {}";
let errors = get_program_errors(source);
assert_eq!(errors.len(), 1);
assert!(matches!(
errors[0].0,
CompilationError::ResolverError(ResolverError::ComptimeTypeInRuntimeCode { .. })
));
}
Loading

0 comments on commit 3d39196

Please sign in to comment.