rust-lang / rust-lang/rust

Cell::swap(&cell_t, &mut t) should suggest Cell::from_mut not Cell::replace(...)

Open
#128,587 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics D-lack-of-suggestion T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
use std::cell::Cell;

fn example<T>(cell: &Cell<T>, other: &mut T) {
    cell.swap(other);
}
Current output
error[E0308]: mismatched types
   --> src/lib.rs:4:15
    |
3   | fn example<T>(cell: &Cell<T>, other: &mut T) {
    |            - found this type parameter
4   |     cell.swap(other);
    |          ---- ^^^^^ expected `&Cell<T>`, found `&mut T`
    |          |
    |          arguments to this method are incorrect
    |
    = note:      expected reference `&Cell<T>`
            found mutable reference `&mut T`
note: method defined here
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/cell.rs:451:12
    |
451 |     pub fn swap(&self, other: &Self) {
    |            ^^^^
note: you might have meant to use method `replace`
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/cell.rs:494:5
    |
494 |     pub fn replace(&self, val: T) -> T {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Desired output
error[E0308]: mismatched types
   --> src/lib.rs:4:15
    |
3   | fn example<T>(cell: &Cell<T>, other: &mut T) {
    |            - found this type parameter
4   |     cell.swap(other);
    |          ---- ^^^^^ expected `&Cell<T>`, found `&mut T`
    |          |
    |          arguments to this method are incorrect
    |
    = note:      expected reference `&Cell<T>`
            found mutable reference `&mut T`
note: method defined here
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/cell.rs:451:12
    |
451 |     pub fn swap(&self, other: &Self) {
    |            ^^^^
help: consider calling `Cell::from_mut` here:
    |
4   -     cell.swap(other);
4   +     cell.swap(Cell::from_mut(other));
Rationale and extra context

The desired update is what the programmer intended to do:

use std::cell::Cell;

fn example<T>(cell: &Cell<T>, other: &mut T) {
    cell.swap(Cell::from_mut(other));
}

Moreover, you can't move the T from behind the &mut T, so the current help is not actionable.

Motivating URLO topic.

Other cases

No response

Rust Version
Rust playground

Stable channel
Build using the Stable version: 1.80.0

Beta channel
Build using the Beta version: 1.81.0-beta.2
(2024-07-25 08328a323ecd80b443a8)

Nightly channel
Build using the Nightly version: 1.82.0-nightly
(2024-08-02 fd8d6fbe505ecf913f5e)
Anything else?

This could be generalized to suggest Cell::from_mut wherever a &mut T is passed and a &Cell<T> would work.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example in src/lib.rs with Rust 1.80.0 and compare the current diagnostic with the requested help suggesting Cell::from_mut rather than replace. Trace the compiler diagnostic path for the Cell::swap(&Cell, &mut T) mismatch, and consider the issue complete when the output suggests Cell::from_mut(other) with the shown replacement.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.