-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Insert trait impls into the program from type annotations (#5327)
# Description ## Problem\* Resolves the vertical slice from this comment #4594 (comment) ## Summary\* This PR lets us actually insert the generated trait impl from the called macro into the program. Currently we only support inserting trait impls, functions, and globals to keep things simple. Each time these are inserted we have to call the def collection code on them so I wanted to avoid adding them all at once in a larger PR. ## Additional Context We can now generate impls for simple traits on a type! See the `derive_impl` test for details. The call site currently looks like this: ```rs #[derive_default] struct Foo { x: Field, y: Bar, } #[derive_default] struct Bar {} fn main() { let _foo: Foo = Default::default(); } ``` If `Bar` doesn't also derive `Default` the error that is issued is in the code to derive the impl unfortunately: ``` error: No matching impl found for `Bar: Default` ┌─ /.../derive_impl/src/main.nr:33:50 │ 33 │ result = result.push_back(quote { $name: Default::default(), }); │ ---------------- No impl for `Bar: Default` │ ``` Since we only support unquoting a few items at top-level currently, here is what it looks like when we try to unquote a different item. In this case, a non-trait impl: ``` error: Unsupported statement type to unquote ┌─ /.../derive_impl/src/main.nr:23:1 │ 23 │ ╭ #[derive_default] 24 │ │ struct Foo { 25 │ │ x: Field, 26 │ │ y: Bar, 27 │ │ } │ ╰─' Only functions, globals, and trait impls can be unquoted here │ = Unquoted item was: impl Foo { Attributes { function: None, secondary: [] } fn bar(self: Self) -> Self { self } } ``` ## Documentation\* Check one: - [ ] No documentation needed. - [ ] Documentation included in this PR. - [x] **[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. --------- Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
- Loading branch information
1 parent
083070e
commit efdd818
Showing
12 changed files
with
298 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.