Skip to content

Commit

Permalink
chore: add test to check that duplicate definitions generated from ma…
Browse files Browse the repository at this point in the history
…cros throws error (#6351)

…

# Description

## Problem\*

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

## Summary\*

This PR adds a test to sanity check that we will throw an error on
duplicate named functions being injected by macros as reported in
https://aztecprotocol.slack.com/archives/C0183F0V42V/p1729855935233669

## 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
TomAFrench authored Oct 25, 2024
1 parent d8e549a commit 8bc8e65
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions compiler/noirc_frontend/src/tests/metaprogramming.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
use crate::hir::{
def_collector::dc_crate::CompilationError, resolution::errors::ResolverError,
type_check::TypeCheckError,
use noirc_errors::Spanned;

use crate::{
ast::Ident,
hir::{
def_collector::{
dc_crate::CompilationError,
errors::{DefCollectorErrorKind, DuplicateType},
},
resolution::errors::ResolverError,
type_check::TypeCheckError,
},
};

use super::{assert_no_errors, get_program_errors};
Expand Down Expand Up @@ -96,3 +105,39 @@ fn allows_references_to_structs_generated_by_macros() {

assert_no_errors(src);
}

#[test]
fn errors_if_macros_inject_functions_with_name_collisions() {
let src = r#"
comptime fn make_colliding_functions(_s: StructDefinition) -> Quoted {
quote {
fn foo() {}
}
}
#[make_colliding_functions]
struct Foo {}
#[make_colliding_functions]
struct Bar {}
fn main() {
let _ = Foo {};
let _ = Bar {};
foo();
}
"#;

let errors = get_program_errors(src);
assert_eq!(errors.len(), 1);
assert!(matches!(
&errors[0].0,
CompilationError::DefinitionError(
DefCollectorErrorKind::Duplicate {
typ: DuplicateType::Function,
first_def: Ident(Spanned { contents, .. }),
..
},
) if contents == "foo"
));
}

0 comments on commit 8bc8e65

Please sign in to comment.