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

vicious "add &" then "remove &" cycle of suggestions #102892

Closed
pnkfelix opened this issue Oct 10, 2022 · 1 comment · Fixed by #102951
Closed

vicious "add &" then "remove &" cycle of suggestions #102892

pnkfelix opened this issue Oct 10, 2022 · 1 comment · Fixed by #102951
Assignees
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

@pnkfelix
Copy link
Member

pnkfelix commented Oct 10, 2022

Given the following code (playground):

#![allow(dead_code, unused_variables)]

use std::sync::Arc;

#[derive(Debug)]
struct A;
#[derive(Debug)]
struct B;

fn process_without_annot(arc: &Arc<(A, B)>) {
    let (a, b) = **arc; // suggests putting `&**arc` here; with that, fixed!
}

fn process_with_annot(arc: &Arc<(A, B)>) {
    let (a, b): (A, B) = **arc; // suggests putting `&**arc` here too
}

The current output is:

error[[E0507]](https://doc.rust-lang.org/nightly/error-index.html#E0507): cannot move out of an `Arc`
  --> src/lib.rs:11:18
   |
11 |     let (a, b) = **arc; // suggests putting `&**arc` here; with that, fixed!
   |          -  -    ^^^^^ help: consider borrowing here: `&**arc`
   |          |  |
   |          |  ...and here
   |          data moved here
   |
   = note: move occurs because these variables have types that don't implement the `Copy` trait

error[[E0507]](https://doc.rust-lang.org/nightly/error-index.html#E0507): cannot move out of an `Arc`
  --> src/lib.rs:15:26
   |
15 |     let (a, b): (A, B) = **arc; // suggests putting `&**arc` here too
   |          -  -            ^^^^^ help: consider borrowing here: `&**arc`
   |          |  |
   |          |  ...and here
   |          data moved here
   |
   = note: move occurs because these variables have types that don't implement the `Copy` trait

To be clear, both of the above are fine suggestions.

Here's the problem:

When you make the suggested change on the second version (the one with the type annotation in the interior of the code):

fn process_with_annot_partially_fixed(arc: &Arc<(A, B)>) {
    let (a, b): (A, B) = &**arc; // ... but suggests *removing* the `&` here!
    //          ~~~~~~
    // Shouldn't it suggest the alternative (correct) fix of changing the 
    // type annotation?
}

The current output is:

error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
  --> src/lib.rs:19:26
   |
19 |     let (a, b): (A, B) = &**arc; // ... but suggests *removing* the `&` here!
   |                 ------   ^^^^^^ expected tuple, found `&(A, B)`
   |                 |
   |                 expected due to this
   |
   = note:  expected tuple `(A, B)`
           found reference `&(A, B)`
help: consider removing the borrow
   |
19 -     let (a, b): (A, B) = &**arc; // ... but suggests *removing* the `&` here!
19 +     let (a, b): (A, B) = **arc; // ... but suggests *removing* the `&` here!
   |

Ideally the output should look like:

help: consider removing the borrow
   |
19 -     let (a, b): (A, B) = &**arc; // ... but suggests *removing* the `&` here!
19 +     let (a, b): (A, B) = **arc; // ... but suggests *removing* the `&` here!
   |
help: alternatively, consider changing the type annotation:
   |
19 -     let (a, b): (A, B) = **arc; // ... but suggests *removing* the `&` here!
19 +     let (a, b): &(A, B) = **arc; // ... but suggests *removing* the `&` here!
   |

(Really, for types where the right-hand side won't be able to be moved into the left-hand side, it would be better to not suggest adding the & at all. But my immediate goal is to at least include the alternative suggestion.)

@pnkfelix pnkfelix 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 Oct 10, 2022
@pnkfelix pnkfelix changed the title vicious add & then remove & circle of suggestions vicious "add &" then "remove &" cycle of suggestions Oct 10, 2022
@SparrowLii
Copy link
Member

@rustbot claim

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Oct 25, 2022
…bank

suggest type annotation for local statement initialed by ref expression

In a local statement with a type declaration, if a ref expression is used on the right side and not used on the left side, in addition to removing the `&` and `&mut` on the right side, we can add them on the left side alternatively
Fixes rust-lang#102892
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Oct 25, 2022
…bank

suggest type annotation for local statement initialed by ref expression

In a local statement with a type declaration, if a ref expression is used on the right side and not used on the left side, in addition to removing the `&` and `&mut` on the right side, we can add them on the left side alternatively
Fixes rust-lang#102892
@bors bors closed this as completed in bf6bfcd Oct 26, 2022
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