Skip to content

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

Open
@khuey

Description

@khuey

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions