-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: error on unbound generics in structs (#5619)
# Description ## Problem Resolves #5436 ## Summary The monomorphizer checks that no types exist that aren't unbound and can't have a default type. This is also the case for structs, but this check is done on field types after they are paired with generics. For example: ```rust struct Foo<T> { x: T } fn main() { let _ = Foo { x: 1 }; } ``` Here we pair `T` with the generic type of 1, then it's defaulted to `Field`, no error. However, it can happen that a generic parameter isn't mentioned at all in a struct field. For example: ```rust struct Foo<T> { } fn main() { let _ = Foo {}; } ``` Here `T` isn't paired with anything, so it's left unbound. However, because we only check that the resulting field types aren't unbound, no error happens. The solution in this PR is to check that generic parameters aren't unbound. This required a method like `convert_type`, called `check_type`, that only checks for this errors. The reason is that I wanted to avoid converting types twice. But let me know if that would be fine (or if there's another solution for this). ## Additional Context In Rust if a generic parameter isn't used in a field, an error about that is made. However, no error happens if the generic parameter is a `const` (similar to a `let N: u32` parameter in Noir) so here I chose to error un unbound generics, not check if a generic is unused. ## 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: jfecher <[email protected]>
- Loading branch information
Showing
5 changed files
with
140 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
7 changes: 7 additions & 0 deletions
7
test_programs/compile_failure/type_annotation_needed_on_struct_constructor/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 = "type_annotation_needed_on_struct_constructor" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.31.0" | ||
|
||
[dependencies] |
6 changes: 6 additions & 0 deletions
6
test_programs/compile_failure/type_annotation_needed_on_struct_constructor/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,6 @@ | ||
struct Foo<T> { | ||
} | ||
|
||
fn main() { | ||
let foo = Foo {}; | ||
} |
7 changes: 7 additions & 0 deletions
7
test_programs/compile_failure/type_annotation_needed_on_struct_new/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 = "type_annotation_needed_on_struct_new" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.31.0" | ||
|
||
[dependencies] |
12 changes: 12 additions & 0 deletions
12
test_programs/compile_failure/type_annotation_needed_on_struct_new/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,12 @@ | ||
struct Foo<T> { | ||
} | ||
|
||
impl <T> Foo<T> { | ||
fn new() -> Foo<T> { | ||
Foo {} | ||
} | ||
} | ||
|
||
fn main() { | ||
let foo = Foo::new(); | ||
} |