You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use a Trait implemented method on a mutable reference of an object. The trait method uses &mut self on its definition:
traitSomeTrait{fnset_value(&mutself,new_value:Field) -> ();}structAType{x:Field}implAType{fnnew(x:Field) -> Self{AType{ x }}fnset_a_different_value(&mutself,new_value:Field) -> (){self.x = new_value;}}implSomeTraitforAType{fnset_value(&mutself,new_value:Field) -> (){self.x = new_value;}}fnfail(a_mut_ref:&mutAType){
a_mut_ref.set_value(1);// --> Fails with: No matching impl found for `&mut AType: SomeTrait`AType::set_value(a_mut_ref,1);// --> Workaround!}fnworks(a_mut_ref:&mutAType){
a_mut_ref.set_a_different_value(2);}fnmain(){letmut a_mut_ref = AType::new(0);works(&mut a_mut_ref);fail(&mut a_mut_ref);assert(a_mut_ref.x == 1);}
Expected Behavior
The above code should compile
Bug
The compiler throws a No matching impl found for &mut AType: SomeTrait error if trying to use the method on the mutable reference directly. However, desugaring the expression works!
fnfail(a_mut_ref:&mutAType){
a_mut_ref.set_value(1);// --> Fails with: No matching impl found for `&mut AType: SomeTrait`AType::set_value(a_mut_ref,1);// --> Workaround!}
To Reproduce
See reproduction
Installation Method
Compiled from source
Nargo Version
nargo version = 0.22.0 (git version hash: 5bf66e1889c91b2fac1c4a20dc04734d0ddf03ae)
Additional Context
No response
Would you like to submit a PR for this Issue?
No
Support Needs
No response
The text was updated successfully, but these errors were encountered:
# Description
## Problem\*
Resolves#4124Resolves#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.
Aim
Use a Trait implemented method on a mutable reference of an object. The trait method uses
&mut self
on its definition:Expected Behavior
The above code should compile
Bug
The compiler throws a
No matching impl found for &mut AType: SomeTrait
error if trying to use the method on the mutable reference directly. However, desugaring the expression works!To Reproduce
See reproduction
Installation Method
Compiled from source
Nargo Version
nargo version = 0.22.0 (git version hash: 5bf66e1889c91b2fac1c4a20dc04734d0ddf03ae)
Additional Context
No response
Would you like to submit a PR for this Issue?
No
Support Needs
No response
The text was updated successfully, but these errors were encountered: