Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trait methods are not found on mut references #4095

Closed
Tracked by #2568
Thunkar opened this issue Jan 19, 2024 · 1 comment · Fixed by #4152
Closed
Tracked by #2568

Trait methods are not found on mut references #4095

Thunkar opened this issue Jan 19, 2024 · 1 comment · Fixed by #4152
Assignees
Labels
bug Something isn't working

Comments

@Thunkar
Copy link
Contributor

Thunkar commented Jan 19, 2024

Aim

Use a Trait implemented method on a mutable reference of an object. The trait method uses &mut self on its definition:

trait SomeTrait {
    fn set_value(&mut self, new_value: Field) -> ();
}

struct AType {
    x: Field
}

impl AType {
    fn new(x: Field) -> Self {
        AType { x }
    }

    fn set_a_different_value(&mut self, new_value: Field) -> () {
        self.x = new_value;
    }
}

impl SomeTrait for AType {
    fn set_value(&mut self, new_value: Field) -> () {
        self.x = new_value;
    }
}

fn fail(a_mut_ref: &mut AType) {
    a_mut_ref.set_value(1); // --> Fails with: No matching impl found for `&mut AType: SomeTrait`
    AType::set_value(a_mut_ref, 1); // --> Workaround!
}

fn works(a_mut_ref: &mut AType) {
    a_mut_ref.set_a_different_value(2);
}

fn main() {
    let mut 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!

fn fail(a_mut_ref: &mut AType) {
    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

@Thunkar Thunkar added the bug Something isn't working label Jan 19, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Jan 19, 2024
@jfecher jfecher self-assigned this Jan 24, 2024
@jfecher
Copy link
Contributor

jfecher commented Jan 25, 2024

The test in this example works in #4152

github-merge-queue bot pushed a commit that referenced this issue Jan 26, 2024
# 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.
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants