Skip to content

MIR borrowck: missing suggestion to use a reference #46627

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

Closed
arielb1 opened this issue Dec 10, 2017 · 0 comments
Closed

MIR borrowck: missing suggestion to use a reference #46627

arielb1 opened this issue Dec 10, 2017 · 0 comments
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints A-NLL Area: Non-lexical lifetimes (NLL) C-enhancement Category: An issue proposing an enhancement or a PR with one. NLL-diagnostics Working towards the "diagnostic parity" goal

Comments

@arielb1
Copy link
Contributor

arielb1 commented Dec 10, 2017

When moving out of a value into a new let binding, AST borrowck suggests using a reference, but MIR borrowck does not.

e.g. for E0508:

struct NonCopy;

fn main() {
    let array = [NonCopy; 1];
    let _value = array[0];  //[ast]~ ERROR [E0508]
                            //[mir]~^ ERROR [E0508]
}

We have the following error message

error[E0508]: cannot move out of type `[NonCopy; 1]`, a non-copy array (Ast)
  --> /home/ariel/Rust/rust-master/src/test/compile-fail/E0508.rs:18:18
   |
18 |     let _value = array[0];  //[ast]~ ERROR [E0508]
   |                  ^^^^^^^^
   |                  |
   |                  cannot move out of here
   |                  help: consider using a reference instead: `&array[0]`

error[E0508]: cannot move out of type `[NonCopy; 1]`, a non-copy array (Mir)
  --> /home/ariel/Rust/rust-master/src/test/compile-fail/E0508.rs:18:18
   |
18 |     let _value = array[0];  //[ast]~ ERROR [E0508]
   |                  ^^^^^^^^ cannot move out of here

error: aborting due to 2 previous errors

See that the help is only in the AST. We should port that help message to MIR borrowck.

The help message is generated in this place in AST borrowck:

Some(&MovePlace { pat_source: PatternSource::LetDecl(ref e), .. }) => {
// ignore patterns that are found at the top-level of a `let`;
// see `get_pattern_source()` for details
let initializer =
e.init.as_ref().expect("should have an initializer to get an error");
if let Ok(snippet) = bccx.tcx.sess.codemap().span_to_snippet(initializer.span) {
err.span_suggestion(initializer.span,
"consider using a reference instead",
format!("&{}", snippet));
}
}

@arielb1 arielb1 added A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints WG-compiler-nll labels Dec 10, 2017
@pnkfelix pnkfelix added the A-NLL Area: Non-lexical lifetimes (NLL) label Jan 10, 2018
@XAMPPRocky XAMPPRocky added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Mar 26, 2018
@nikomatsakis nikomatsakis added the NLL-diagnostics Working towards the "diagnostic parity" goal label Apr 3, 2018
bors added a commit that referenced this issue Jun 29, 2018
[NLL] Better move errors

Make a number of changes to improve the quality of NLL cannot move errors.

* Group errors that occur in the same `match` with the same cause.
* Suggest `ref`, `&` or removing `*` to avoid the move.
* Show the place being matched on.

Differences from AST borrowck:

* `&` is suggested over `ref` when matching on a place that can't be moved from.
* Removing `*` is suggested instead of adding `&` when applicable.
* Sub-pattern spans aren't used, this would probably need Spans on Places.

Closes #45699
Closes #46627
Closes #51187
Closes #51189

r? @pnkfelix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints A-NLL Area: Non-lexical lifetimes (NLL) C-enhancement Category: An issue proposing an enhancement or a PR with one. NLL-diagnostics Working towards the "diagnostic parity" goal
Projects
None yet
Development

No branches or pull requests

4 participants