-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(frontend): Resolve object types from method calls a single time (#…
…5131) # Description ## Problem\* Resolves #5065 Probably resolves #4732 but need to test it or have aztec team test it. ## Summary\* When working with a program like such where `MyType` implements `MyTrait`: ```rust fn foo<T>() -> T where T: MyTrait { MyTrait::new() } fn concise_regression() -> MyType { Wrapper::new(foo()).unwrap() } ``` We should be able to infer the return type of `foo`. We currently always push trait constraints onto a a `Vec<(TraitConstraint, ExprId)>`. We need to do this as we can have multiple trait constraints that need to be handled during monomorphization due to generics. However, when working with a method call this can cause us to store an old trait constraint that does not necessarily apply to the expression. The nested function call in `concise_regression` initially adds a trait constraint simply for `foo` due to the call to `Wrapper::new(foo())` and then another constraint for `Wrapper::new(foo()).unwrap()`. The call to `Wrapper::new(foo())` cannot be bound to anything unless we introduce an intermediate variable. This felt like it would be overly complex and we just need to follow the accurate trait constraint for a function call expression. Taking the test in the issue and this PR we have the following trait constraints on master for the `foo` expression: ``` TraitConstraint { typ: '23646 -> '23647, trait_id: TraitId( ModuleId { krate: Root( 1, ), local_id: LocalModuleId( Index( 1, ), ), }, ), trait_generics: [], }, TraitConstraint { typ: '23648 -> '23649 -> '23650 -> MyType, trait_id: TraitId( ModuleId { krate: Root( 1, ), local_id: LocalModuleId( Index( 1, ), ), }, ), trait_generics: [], } ``` This is occurring due to an unnecessary type check on a method call's object type. This is cause a repeated trait constraint where one has incorrect type variables that cannot be resolved. I have altered how MethodCall's and Call's are resolved as to avoid repeated type checks on the object type. ## 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: jfecher <jake@aztecprotocol.com>
- Loading branch information
Showing
5 changed files
with
171 additions
and
46 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/regression_5065_failure/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 = "regression_5065_failure" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.30.0" | ||
|
||
[dependencies] |
40 changes: 40 additions & 0 deletions
40
test_programs/compile_failure/regression_5065_failure/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,40 @@ | ||
struct Wrapper<T> { | ||
_value: T, | ||
} | ||
|
||
impl<T> Wrapper<T> { | ||
fn new(value: T) -> Self { | ||
Self { _value: value } | ||
} | ||
|
||
fn unwrap(self) -> T { | ||
self._value | ||
} | ||
} | ||
|
||
trait MyTrait { | ||
fn new() -> Self; | ||
} | ||
|
||
struct MyType {} | ||
|
||
impl MyTrait for MyType { | ||
fn new() -> Self { | ||
MyType {} | ||
} | ||
} | ||
|
||
fn foo<T>() -> T where T: MyTrait { | ||
MyTrait::new() | ||
} | ||
|
||
struct BadType {} | ||
|
||
// Check that we get "No matching impl found for `BadType: MyTrait`" | ||
fn concise_regression() -> BadType { | ||
Wrapper::new(foo()).unwrap() | ||
} | ||
|
||
fn main() { | ||
let _ = concise_regression(); | ||
} |
7 changes: 7 additions & 0 deletions
7
test_programs/compile_success_empty/regression_5065/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 = "regression_5065" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.30.0" | ||
|
||
[dependencies] |
45 changes: 45 additions & 0 deletions
45
test_programs/compile_success_empty/regression_5065/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,45 @@ | ||
struct Wrapper<T> { | ||
_value: T, | ||
} | ||
|
||
impl<T> Wrapper<T> { | ||
fn new_wrapper(value: T) -> Self { | ||
Self { _value: value } | ||
} | ||
|
||
fn unwrap(self) -> T { | ||
self._value | ||
} | ||
} | ||
|
||
trait MyTrait { | ||
fn new() -> Self; | ||
} | ||
|
||
struct MyType {} | ||
|
||
impl MyTrait for MyType { | ||
fn new() -> Self { | ||
MyType {} | ||
} | ||
} | ||
|
||
fn foo<T>() -> T where T: MyTrait { | ||
MyTrait::new() | ||
} | ||
|
||
// fn verbose_but_compiles() -> MyType { | ||
// let a = Wrapper::new_wrapper(foo()); | ||
// a.unwrap() | ||
// } | ||
|
||
// Check that are able to infer the return type of the call to `foo` | ||
fn concise_regression() -> MyType { | ||
Wrapper::new_wrapper(foo()).unwrap() | ||
// Wrapper::unwrap(Wrapper::new_wrapper(foo())) | ||
} | ||
|
||
fn main() { | ||
// let _ = verbose_but_compiles(); | ||
let _ = concise_regression(); | ||
} |