-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Apply trait constraints from method calls (#4152)
# Description ## Problem\* Resolves #4124 Resolves #4095 ## Summary\* We were never applying trait constraints from method calls before. These have been handled for other identifiers since #4000, but not for method calls which desugar to a function identifier that is called, then type checked with its own special function. I've fixed this by removing the special function and recursively type checking the function call they desugar to instead. This way we have less code duplication and only need to fix things in one spot in the future. ## Additional Context It is a good day when you get to fix a bug by removing code. This is a draft currently because I still need: - [x] To add `&mut` implicitly where applicable to the function calls that are now checked recursively - [x] To add the test case I'm using locally ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[Exceptional Case]** 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.
- Loading branch information
Showing
8 changed files
with
149 additions
and
169 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,7 @@ | ||
[package] | ||
name = "regression_4124" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.22.0" | ||
|
||
[dependencies] |
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 @@ | ||
value = 0 |
39 changes: 39 additions & 0 deletions
39
test_programs/execution_success/regression_4124/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,39 @@ | ||
use dep::std::option::Option; | ||
|
||
trait MyDeserialize<N> { | ||
fn deserialize(fields: [Field; N]) -> Self; | ||
} | ||
|
||
impl MyDeserialize<1> for Field { | ||
fn deserialize(fields: [Field; 1]) -> Self { | ||
fields[0] | ||
} | ||
} | ||
|
||
pub fn storage_read<N>() -> [Field; N] { | ||
dep::std::unsafe::zeroed() | ||
} | ||
|
||
struct PublicState<T> { | ||
storage_slot: Field, | ||
} | ||
|
||
impl<T> PublicState<T> { | ||
pub fn new(storage_slot: Field) -> Self { | ||
assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1."); | ||
PublicState { storage_slot } | ||
} | ||
|
||
pub fn read<T_SERIALIZED_LEN>(_self: Self) -> T where T: MyDeserialize<T_SERIALIZED_LEN> { | ||
// storage_read returns slice here | ||
let fields: [Field; T_SERIALIZED_LEN] = storage_read(); | ||
T::deserialize(fields) | ||
} | ||
} | ||
|
||
fn main(value: Field) { | ||
let ps: PublicState<Field> = PublicState::new(27); | ||
|
||
// error here | ||
assert(ps.read() == value); | ||
} |