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

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

Open
QuineDot opened this issue Aug 3, 2024 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-lack-of-suggestion Diagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@QuineDot
Copy link

QuineDot commented Aug 3, 2024

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.

@QuineDot QuineDot 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 Aug 3, 2024
@fmease fmease added the D-lack-of-suggestion Diagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic. label Mar 20, 2025
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-lack-of-suggestion Diagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants