-
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: Add some metaprogramming methods on
TypeDefinition
(#5310)
# Description ## Problem\* Resolves #5285 ## Summary\* Implements `as_type`, `generics`, and `fields` on the `TypeDefinition` type. The `Type` type isn't really supported yet so `as_type` and `fields` return `Quoted` token streams instead of `Type`s for now. ## Additional Context A few bugs still need to be fixed after this PR for the vertical slice to work. ## 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 <[email protected]>
- Loading branch information
1 parent
9b59f6b
commit a818d95
Showing
10 changed files
with
272 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod type_def; |
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,16 @@ | ||
impl TypeDefinition { | ||
/// Return a syntactic version of this type definition as a type. | ||
/// For example, `as_type(quote { type Foo<A, B> { ... } })` would return `Foo<A, B>` | ||
#[builtin(type_def_as_type)] | ||
fn as_type(self) -> Quoted {} | ||
|
||
/// Return each generic on this type. The names of these generics are unchanged | ||
/// so users may need to keep name collisions in mind if this is used directly in a macro. | ||
#[builtin(type_def_generics)] | ||
fn generics(self) -> [Quoted] {} | ||
|
||
/// Returns (name, type) pairs of each field in this type. Each type is as-is | ||
/// with any generic arguments unchanged. | ||
#[builtin(type_def_fields)] | ||
fn fields(self) -> [(Quoted, Quoted)] {} | ||
} |
7 changes: 7 additions & 0 deletions
7
test_programs/compile_success_empty/comptime_type_definition/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 = "comptime_type_definition" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.31.0" | ||
|
||
[dependencies] |
13 changes: 13 additions & 0 deletions
13
test_programs/compile_success_empty/comptime_type_definition/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,13 @@ | ||
fn main() {} | ||
|
||
#[my_comptime_fn] | ||
struct MyType<A, B, C> { | ||
field1: [A; 10], | ||
field2: (B, C), | ||
} | ||
|
||
comptime fn my_comptime_fn(typ: TypeDefinition) { | ||
let _ = typ.as_type(); | ||
assert_eq(typ.generics().len(), 3); | ||
assert_eq(typ.fields().len(), 2); | ||
} |
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() {} |