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: Error when comptime types are used in runtime code #5987

Merged
merged 3 commits into from
Sep 10, 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
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() {
jfecher marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -2333,7 +2333,7 @@
}

#[test]
fn underflowing_u8() {

Check warning on line 2336 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (underflowing)
let src = r#"
fn main() {
let _: u8 = -1;
Expand Down Expand Up @@ -2371,7 +2371,7 @@
}

#[test]
fn underflowing_i8() {

Check warning on line 2374 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (underflowing)
let src = r#"
fn main() {
let _: i8 = -129;
Expand Down Expand Up @@ -3451,3 +3451,14 @@
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
Loading