-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend): Where clause on impl (#5320)
# Description ## Problem\* Resolves #4508 ## Summary\* This enables the simple functionality of being able to declare a where clause directly on a struct implementation such as below: ```rust impl<T> MyStruct<T> where T: MyEq { fn my_eq(self, other: Self) -> bool { (self.a == other.a) & self.b.my_eq(other.b) } } ``` The code above is essentially syntactic sugar where we now every method in the struct impl a where clause. As the logic for resolving trait constraints already exists this PR really just updates the parser to accept `where` clauses and during definition collection attaches them to each method in an impl. ## Additional Context ## Documentation\* Check one: - [ ] No documentation needed. - [X] 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. --------- Co-authored-by: jfecher <jake@aztecprotocol.com>
- Loading branch information
Showing
14 changed files
with
91 additions
and
7 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
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
7 changes: 7 additions & 0 deletions
7
test_programs/compile_success_empty/impl_where_clause/Nargo.toml
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "impl_where_clause" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.31.0" | ||
|
||
[dependencies] |
34 changes: 34 additions & 0 deletions
34
test_programs/compile_success_empty/impl_where_clause/src/main.nr
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
struct MyStruct<T> { | ||
a: u32, | ||
b: T, | ||
} | ||
|
||
struct InnerStruct { | ||
a: Field, | ||
b: Field, | ||
} | ||
|
||
trait MyEq { | ||
fn my_eq(self, other: Self) -> bool; | ||
} | ||
|
||
impl MyEq for InnerStruct { | ||
fn my_eq(self, other: InnerStruct) -> bool { | ||
(self.a == other.a) & (self.b == other.b) | ||
} | ||
} | ||
|
||
impl<T> MyStruct<T> where T: MyEq { | ||
fn my_eq(self, other: Self) -> bool { | ||
(self.a == other.a) & self.b.my_eq(other.b) | ||
} | ||
} | ||
|
||
fn main() { | ||
let inner = InnerStruct { a: 1, b: 2 }; | ||
let my_struct = MyStruct { a: 5, b: inner }; | ||
assert(my_struct.my_eq(my_struct)); | ||
|
||
let mut my_struct_new = MyStruct { a: 5, b: InnerStruct { a: 10, b: 15 } }; | ||
assert(my_struct_new.my_eq(my_struct_new)); | ||
} |
2 changes: 1 addition & 1 deletion
2
...s_empty/impl_with_where_clause/Nargo.toml → ...y/trait_impl_with_where_clause/Nargo.toml
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[package] | ||
name = "impl_with_where_clause" | ||
name = "trait_impl_with_where_clause" | ||
type = "bin" | ||
authors = [""] | ||
|
||
|
File renamed without changes.
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