Skip to content

Commit

Permalink
Fix backwards compat of custom attributes that don't refer to comptim…
Browse files Browse the repository at this point in the history
…e fns
  • Loading branch information
jfecher committed Jul 31, 2024
1 parent 01f93cf commit e592a10
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
11 changes: 8 additions & 3 deletions compiler/noirc_frontend/src/elaborator/comptime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,17 @@ impl<'context> Elaborator<'context> {
_ => return Ok(()),
};

let definition = self.interner.try_definition(definition_id);
let Some(DefinitionKind::Function(function)) = definition.map(|d| &d.kind) else {
let Some(definition) = self.interner.try_definition(definition_id) else {
// If there's no such function, don't return an error.
// This preserves backwards compatibility in allowing custom attributes that
// do not refer to comptime functions.
return Ok(());
};

let DefinitionKind::Function(function) = definition.kind else {
return Err((ResolverError::NonFunctionInAnnotation { span }.into(), self.file));
};

let function = *function;
let mut interpreter = self.setup_interpreter();
let mut arguments =
Self::handle_attribute_arguments(&mut interpreter, function, arguments, location)
Expand Down
9 changes: 0 additions & 9 deletions compiler/noirc_frontend/src/hir/resolution/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ pub enum ResolverError {
MacroIsNotComptime { span: Span },
#[error("Annotation name must refer to a comptime function")]
NonFunctionInAnnotation { span: Span },
#[error("Unknown annotation")]
UnknownAnnotation { span: Span },
}

impl ResolverError {
Expand Down Expand Up @@ -460,13 +458,6 @@ impl<'a> From<&'a ResolverError> for Diagnostic {
*span,
)
},
ResolverError::UnknownAnnotation { span } => {
Diagnostic::simple_warning(
"Unknown annotation".into(),
"No matching comptime function found in scope".into(),
*span,
)
},
}
}
}
11 changes: 6 additions & 5 deletions test_programs/compile_success_empty/attribute_args/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#[attr_with_args(a b, c d)]
#[varargs(one, two)]
#[varargs(one, two, three, four)]
#[attr_with_args(1, 2)]
#[varargs(1, 2)]
#[varargs(1, 2, 3, 4)]
struct Foo {}

comptime fn attr_with_args(s: StructDefinition, a: Quoted, b: Quoted) {
comptime fn attr_with_args(s: StructDefinition, a: Field, b: Field) {
// Ensure all variables are in scope.
// We can't print them since that breaks the test runner.
let _ = s;
let _ = a;
let _ = b;
}

comptime fn varargs(s: StructDefinition, t: [Quoted]) {
#[varargs]
comptime fn varargs(s: StructDefinition, t: [Field]) {
let _ = s;
for _ in t {}
assert(t.len() < 5);
Expand Down

0 comments on commit e592a10

Please sign in to comment.