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

Wrong fix suggested for missing as_mut in the presence of RefCell<Option<>> #96438

Open
khuey opened this issue Apr 26, 2022 · 3 comments
Open
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@khuey
Copy link
Contributor

khuey commented Apr 26, 2022

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=d77c477c52217e25856306f659a26e42

use std::cell::RefCell;

fn foo(cell: &RefCell<Option<Vec<()>>>) {
    cell.borrow_mut().unwrap().pop().unwrap();
}

The current output is:

Compiling playground v0.0.1 (/playground)
error[[E0507]](https://doc.rust-lang.org/nightly/error-index.html#E0507): cannot move out of dereference of `RefMut<'_, Option<Vec<()>>>`
 [--> src/lib.rs:4:5
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=d77c477c52217e25856306f659a26e42#)  |
4 |     cell.borrow_mut().unwrap().pop().unwrap();
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^ move occurs because value has type `Option<Vec<()>>`, which does not implement the `Copy` trait
  |
help: consider borrowing the `Option`'s content
  |
4 |     cell.borrow_mut().unwrap().as_ref().pop().unwrap();
  |                               +++++++++

For more information about this error, try `rustc --explain E0507`.
error: could not compile `playground` due to previous error

There are two separate issues here:

  1. The suggested as_ref is in the wrong place and should be after the borrow_mut(), not after the unwrap().
  2. The suggested as_ref really should be an as_mut
@khuey khuey added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 26, 2022
@George-lewis
Copy link
Contributor

George-lewis commented Apr 29, 2022

I think I might be able to take this one if that's alright. Was investigating it a bit yesterday, will post a pr shortly

@khuey
Copy link
Contributor Author

khuey commented Aug 25, 2022

The first half of this is fixed, as_ref is now suggested in the correct spot. But it still should be as_mut that's suggested.

@khuey
Copy link
Contributor Author

khuey commented Nov 5, 2022

This is getting better, still seems like the compiler could figure out that as_mut is needed though.

Compiling playground v0.0.1 (/playground)
error[[E0507]](https://doc.rust-lang.org/nightly/error-index.html#E0507): cannot move out of dereference of `RefMut<'_, Option<Vec<()>>>`
 --> src/lib.rs:4:5
  |
4 |     cell.borrow_mut().unwrap().pop().unwrap();
  |     ^^^^^^^^^^^^^^^^^^--------
  |     |                 |
  |     |                 value moved due to this method call
  |     help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
  |     move occurs because value has type `Option<Vec<()>>`, which does not implement the `Copy` trait
  |
note: this function takes ownership of the receiver `self`, which moves value

For more information about this error, try `rustc --explain E0507`.
error: could not compile `playground` due to previous error

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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants