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

Invalid suggestion to introduce as_ref() for Option used in a place expression #99426

Closed
nagisa opened this issue Jul 18, 2022 · 5 comments · Fixed by #100186
Closed

Invalid suggestion to introduce as_ref() for Option used in a place expression #99426

nagisa opened this issue Jul 18, 2022 · 5 comments · Fixed by #100186
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-papercut Diagnostics: An error or lint that needs small tweaks.

Comments

@nagisa
Copy link
Member

nagisa commented Jul 18, 2022

Consider the following code

struct Foo {
    val: i32
}

fn func1() {
    let mut foo = Some(Foo{val: 0});
    let ptr = &mut foo;
    ptr.unwrap().val += 1;
}

Here the diagnostic suggests to add as_ref as such:

note: this function takes ownership of the receiver `self`, which moves `*ptr`
help: consider calling `.as_ref()` to borrow the type's contents
  |
8 |     ptr.as_ref().unwrap().val += 1;
  |         +++++++++

This suggestion is incorrect – as_ref will never (AFAIK) produce a mutable reference necessary for the assignment. We should suggest as_mut where we know that a mutable reference will be required, instead.

@nagisa nagisa added A-diagnostics Area: Messages for errors, warnings, and lints D-papercut Diagnostics: An error or lint that needs small tweaks. labels Jul 18, 2022
@petrochenkov
Copy link
Contributor

I've seen a PR fixing something similar (#96537), but it was closed due to inactivity.

@nagisa
Copy link
Member Author

nagisa commented Jul 19, 2022 via email

@compiler-errors
Copy link
Member

Right, originally this was an issue for wrong placement of as_ref which is still a bug on stable but has been fixed in nightly at some point,

Yeah, I fixed the .as_ref placement recently in #98603

where we know that a mutable reference will be required

I looked into this, but couldn't find out from the borrowck diagnostic machinery how to tell whether a place is being accessed mutably or not. I can take another look at this though.

@rustbot claim

@compiler-errors
Copy link
Member

Worst case I could just change it to say ".as_ref() or .as_mut()".

@nagisa
Copy link
Member Author

nagisa commented Jul 19, 2022

I looked into this, but couldn't find out from the borrowck diagnostic machinery how to tell whether a place is being accessed mutably or not. I can take another look at this though.

Ah, so I didn’t mean to imply that the relevant code knows this. A reasonable rule of thumb would be to see what context this expression is in. If it is a part of a place expression on the lhs of an assignment, as_mut is what user will need. I don’t know if this information is already available or, if not, what it would take to pass this around. And, yeah, I guess it wouldn’t be 100% right in all instances, so perhaps just saying “either-or” is good enough here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-papercut Diagnostics: An error or lint that needs small tweaks.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants