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

Mismatched type [E0308] offers Intention for fix but does not actual apply fix #2472

Closed
schoenja opened this issue Apr 14, 2018 · 3 comments · Fixed by #5550
Closed

Mismatched type [E0308] offers Intention for fix but does not actual apply fix #2472

schoenja opened this issue Apr 14, 2018 · 3 comments · Fixed by #5550
Labels
bug subsystem::code insight General label for issues related to code understanding: highlighting, completion, annotation, etc.

Comments

@schoenja
Copy link

I am getting an mismatched types [E0308] expected '&mut String', found '&String' error with this code on line 4 (code is from this tutorial)

fn main() {
    let mut s = String::from("hello");

    change(&s);
}

fn change(some_string: &mut String) {
    some_string.push_str(", world");
}

The intention offers to Convert to &mut String using 'BorrowMut' trait.

Expected behaviour:
The line is changed to change(&mut s);.

Actual behaviour:
The line is not changed.

Additional information:
IntelliJ IDEA 2018.1.1 (Ultimate Edition)
Build #IU-181.4445.78, built on April 9, 2018
JRE: 1.8.0_162-b12 amd64
JVM: OpenJDK 64-Bit Server VM by Oracle Corporation
Linux 4.15.15-1-ARCH
Rust plugin: Version: 0.2.0.2092-181

@vlad20012 vlad20012 added subsystem::code insight General label for issues related to code understanding: highlighting, completion, annotation, etc. bug labels Apr 16, 2018
@vlad20012
Copy link
Member

c.c. #1730
c.c. @oleg-semenov

@oleg-semenov
Copy link
Contributor

hm it works little bit differently for me, it replaces &s in the line 4 with (&s).borrow_mut() which is still incorrect; i didn't try to match any versions though; I can make a bug-fix that adds a check that the expression is mutable before suggesting applying BorrowMut trait. So BorrowMut won't be suggested in this case.

As for the desired &s => &mut s fix we just currently don't have a quick-fix/intention for that. So far all quick-fixes/intentions for types mismatches don't try to replace or delete anything from the affected source-code; they add stuff before or around it. I don't know if we want to keep this "policy" (or it's just a coincidence). But i think in this case we can add a simple quick-fix/intention that in cases like this can try to check the mutability of s and suggest replacing & with &mut. So, more precisely it would be like if expected type it &mut T actual type is & T and the expression is mutable suggest replacing & with &mut.

@vlad20012 what do you think?

oleg-semenov added a commit to oleg-semenov/intellij-rust that referenced this issue May 12, 2018
This commit makes the quick-fixes that use BororwMut and AsMut traits
to be applicable only for truly mutable expressions. This fixes part of
the: intellij-rust#2472
@oleg-semenov
Copy link
Contributor

here is a bug-fix for BorrowMut: #2526

bors bot added a commit that referenced this issue Jun 18, 2018
2526: ANN: Only allow BororwMut and AsMut quick-fixes on mutable expressions r=vlad20012 a=oleg-semenov

This commit makes the quick-fixes that use BororwMut and AsMut traits
to be applicable only for truly mutable expressions. This fixes part of
the: #2472

<!--
Hello and thank you for the pull request!

We don't have any strict rules about pull requests, but you might check
https://github.com/intellij-rust/intellij-rust/blob/master/CONTRIBUTING.md
for some hints!

Note that we need an electronic CLA for contributions:
https://github.com/intellij-rust/intellij-rust/blob/master/CONTRIBUTING.md#cla

After you sign the CLA, please add your name to
https://github.com/intellij-rust/intellij-rust/blob/master/CONTRIBUTORS.txt

:)
-->


Co-authored-by: oleg-semenov <olegsa@gmail.com>
Undin pushed a commit that referenced this issue Jun 18, 2018
This commit makes the quick-fixes that use BororwMut and AsMut traits
to be applicable only for truly mutable expressions. This fixes part of
the: #2472

(cherry picked from commit 1bbcc77)
rrevenantt pushed a commit to rrevenantt/intellij-rust that referenced this issue May 30, 2019
This commit makes the quick-fixes that use BororwMut and AsMut traits
to be applicable only for truly mutable expressions. This fixes part of
the: intellij-rust#2472
@bors bors bot closed this as completed in e0daaa6 Jun 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug subsystem::code insight General label for issues related to code understanding: highlighting, completion, annotation, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants