-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Implement interpretation of member access on tuples (#5311)
# Description ## Problem\* Resolves <!-- Link to GitHub Issue --> ## Summary\* Fixes an issue in the interpreter where the interpreter would error for member access expressions on tuples. This prevented e.g. `my_tuple.0`. ## 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. --------- Co-authored-by: Tom French <[email protected]>
- Loading branch information
1 parent
32f66f3
commit b94662e
Showing
4 changed files
with
78 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "derive_impl" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.30.0" | ||
|
||
[dependencies] |
44 changes: 44 additions & 0 deletions
44
test_programs/compile_success_empty/derive_impl/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,44 @@ | ||
comptime fn derive_default(typ: TypeDefinition) -> Quoted { | ||
let generics: [Quoted] = typ.generics(); | ||
assert_eq( | ||
generics.len(), 0, "derive_default: Deriving Default on generic types is currently unimplemented" | ||
); | ||
|
||
let type_name = typ.as_type(); | ||
let fields = typ.fields(); | ||
|
||
let fields = join(make_field_exprs(fields)); | ||
|
||
quote { | ||
impl Default for $type_name { | ||
fn default() -> Self { | ||
Self { $fields } | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[derive_default] | ||
struct Foo { | ||
x: Field, | ||
y: u32, | ||
} | ||
|
||
comptime fn make_field_exprs(fields: [(Quoted, Quoted)]) -> [Quoted] { | ||
let mut result = &[]; | ||
for my_field in fields { | ||
let name = my_field.0; | ||
result = result.push_back(quote { $name: Default::default(), }); | ||
} | ||
result | ||
} | ||
|
||
comptime fn join(slice: [Quoted]) -> Quoted { | ||
let mut result = quote {}; | ||
for elem in slice { | ||
result = quote { $result $elem }; | ||
} | ||
result | ||
} | ||
|
||
fn main() {} |