Skip to content

Commit

Permalink
feat: Allow inserting new structs and into programs from attributes (#…
Browse files Browse the repository at this point in the history
…5927)

# Description

## Problem\*

Resolves #5913

## Summary\*

Allows inserting new structs and impls into the program from comptime
attributes.

## Additional Context

Inserting new structs wasn't allowed originally since functions wouldn't
be able to refer to them in their signature. This use case isn't needed
for aztec currently so I'm adding this feature now and we can look into
expanding it later. See #5926

## 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 4, 2024
1 parent 2ca2e5c commit 94e661e
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 165 deletions.
20 changes: 18 additions & 2 deletions compiler/noirc_frontend/src/elaborator/comptime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,14 +385,30 @@ impl<'context> Elaborator<'context> {
self.errors.push(error);
}
}
TopLevelStatement::Struct(struct_def) => {
if let Some((type_id, the_struct)) = dc_mod::collect_struct(
self.interner,
self.def_maps.get_mut(&self.crate_id).unwrap(),
struct_def,
self.file,
self.local_module,
self.crate_id,
&mut self.errors,
) {
generated_items.types.insert(type_id, the_struct);
}
}
TopLevelStatement::Impl(r#impl) => {
let module = self.module_id();
dc_mod::collect_impl(self.interner, generated_items, r#impl, self.file, module);
}

// Assume that an error has already been issued
TopLevelStatement::Error => (),

TopLevelStatement::Module(_)
| TopLevelStatement::Import(..)
| TopLevelStatement::Struct(_)
| TopLevelStatement::Trait(_)
| TopLevelStatement::Impl(_)
| TopLevelStatement::TypeAlias(_)
| TopLevelStatement::SubModule(_)
| TopLevelStatement::InnerAttribute(_) => {
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/hir/comptime/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ impl<'a> From<&'a InterpreterError> for CustomDiagnostic {
InterpreterError::UnsupportedTopLevelItemUnquote { item, location } => {
let msg = "Unsupported statement type to unquote".into();
let secondary =
"Only functions, globals, and trait impls can be unquoted here".into();
"Only functions, structs, globals, and impls can be unquoted here".into();
let mut error = CustomDiagnostic::simple_error(msg, secondary, location.span);
error.add_note(format!("Unquoted item was:\n{item}"));
error
Expand Down
Loading

0 comments on commit 94e661e

Please sign in to comment.