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 mutating comptime variables in non-comptime code #6003

Merged
merged 2 commits into from
Sep 11, 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
7 changes: 7 additions & 0 deletions compiler/noirc_frontend/src/elaborator/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ impl<'context> Elaborator<'context> {
} else {
if let Some(definition) = self.interner.try_definition(ident.id) {
mutable = definition.mutable;

if definition.comptime && !self.in_comptime_context() {
self.push_err(ResolverError::MutatingComptimeInNonComptimeContext {
name: definition.name.clone(),
span: ident.location.span,
});
}
}

let typ = self.interner.definition_type(ident.id).instantiate(self.interner).0;
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 @@ -128,6 +128,8 @@ pub enum ResolverError {
QuoteInRuntimeCode { span: Span },
#[error("Comptime-only type `{typ}` cannot be used in runtime code")]
ComptimeTypeInRuntimeCode { typ: String, span: Span },
#[error("Comptime variable `{name}` cannot be mutated in a non-comptime context")]
MutatingComptimeInNonComptimeContext { name: String, span: Span },
}

impl ResolverError {
Expand Down Expand Up @@ -522,6 +524,13 @@ impl<'a> From<&'a ResolverError> for Diagnostic {
*span,
)
},
ResolverError::MutatingComptimeInNonComptimeContext { name, span } => {
Diagnostic::simple_error(
format!("Comptime variable `{name}` cannot be mutated in a non-comptime context"),
format!("`{name}` mutated here"),
*span,
)
},
}
}
}
Loading