Skip to content

MIR borrowck: missing suggestion to use a reference #46627

Closed
@arielb1

Description

@arielb1

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));
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-NLLArea: Non-lexical lifetimes (NLL)A-borrow-checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.NLL-diagnosticsWorking towards the "diagnostic parity" goal

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions