From a49b75d2f342f5904d89f34f46dfd621c20da7f7 Mon Sep 17 00:00:00 2001 From: ashtneoi Date: Mon, 9 Jul 2018 13:16:02 -0700 Subject: [PATCH 1/2] Add test case from issue #51515 --- src/test/ui/suggestions/issue-51515.rs | 18 ++++++++++++++++++ src/test/ui/suggestions/issue-51515.stderr | 17 +++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/test/ui/suggestions/issue-51515.rs create mode 100644 src/test/ui/suggestions/issue-51515.stderr diff --git a/src/test/ui/suggestions/issue-51515.rs b/src/test/ui/suggestions/issue-51515.rs new file mode 100644 index 0000000000000..b5bbf48ddfac8 --- /dev/null +++ b/src/test/ui/suggestions/issue-51515.rs @@ -0,0 +1,18 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(nll)] + +fn main() { + let foo = &16; + *foo = 32; + let bar = foo; + *bar = 64; +} diff --git a/src/test/ui/suggestions/issue-51515.stderr b/src/test/ui/suggestions/issue-51515.stderr new file mode 100644 index 0000000000000..7cdbef1badc76 --- /dev/null +++ b/src/test/ui/suggestions/issue-51515.stderr @@ -0,0 +1,17 @@ +error[E0594]: cannot assign to `*foo` which is behind a `&` reference + --> $DIR/issue-51515.rs:15:5 + | +LL | let foo = &16; + | --- help: consider changing this to be a mutable reference: `&mut 16` +LL | *foo = 32; + | ^^^^^^^^^ `foo` is a `&` reference, so the data it refers to cannot be written + +error[E0594]: cannot assign to `*bar` which is behind a `&` reference + --> $DIR/issue-51515.rs:17:5 + | +LL | *bar = 64; + | ^^^^^^^^^ `bar` is a `&` reference, so the data it refers to cannot be written + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0594`. From dc8ae26c1ee4f95e3baef603506fbaa95cb2ccd1 Mon Sep 17 00:00:00 2001 From: ashtneoi Date: Mon, 9 Jul 2018 13:33:57 -0700 Subject: [PATCH 2/2] Fix issue #51515 and update test --- src/librustc_mir/borrow_check/mod.rs | 6 ++++-- src/test/ui/suggestions/issue-51515.rs | 6 ++++++ src/test/ui/suggestions/issue-51515.stderr | 8 ++++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/librustc_mir/borrow_check/mod.rs b/src/librustc_mir/borrow_check/mod.rs index a5db0d15d8ab3..1a66a2d2cb902 100644 --- a/src/librustc_mir/borrow_check/mod.rs +++ b/src/librustc_mir/borrow_check/mod.rs @@ -1905,8 +1905,10 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { // highlighted text will always be `&` and // thus can transform to `&mut` by slicing off // first ASCII character and prepending "&mut ". - let borrowed_expr = src[1..].to_string(); - return (assignment_rhs_span, format!("&mut {}", borrowed_expr)); + if src.starts_with('&') { + let borrowed_expr = src[1..].to_string(); + return (assignment_rhs_span, format!("&mut {}", borrowed_expr)); + } } } diff --git a/src/test/ui/suggestions/issue-51515.rs b/src/test/ui/suggestions/issue-51515.rs index b5bbf48ddfac8..3e0a3b757a3d6 100644 --- a/src/test/ui/suggestions/issue-51515.rs +++ b/src/test/ui/suggestions/issue-51515.rs @@ -12,7 +12,13 @@ fn main() { let foo = &16; + //~^ HELP consider changing this to be a mutable reference + //~| SUGGESTION &mut 16 *foo = 32; + //~^ ERROR cannot assign to `*foo` which is behind a `&` reference let bar = foo; + //~^ HELP consider changing this to be a mutable reference + //~| SUGGESTION &mut i32 *bar = 64; + //~^ ERROR cannot assign to `*bar` which is behind a `&` reference } diff --git a/src/test/ui/suggestions/issue-51515.stderr b/src/test/ui/suggestions/issue-51515.stderr index 7cdbef1badc76..3e7349b5acabf 100644 --- a/src/test/ui/suggestions/issue-51515.stderr +++ b/src/test/ui/suggestions/issue-51515.stderr @@ -1,14 +1,18 @@ error[E0594]: cannot assign to `*foo` which is behind a `&` reference - --> $DIR/issue-51515.rs:15:5 + --> $DIR/issue-51515.rs:17:5 | LL | let foo = &16; | --- help: consider changing this to be a mutable reference: `&mut 16` +... LL | *foo = 32; | ^^^^^^^^^ `foo` is a `&` reference, so the data it refers to cannot be written error[E0594]: cannot assign to `*bar` which is behind a `&` reference - --> $DIR/issue-51515.rs:17:5 + --> $DIR/issue-51515.rs:22:5 | +LL | let bar = foo; + | --- help: consider changing this to be a mutable reference: `&mut i32` +... LL | *bar = 64; | ^^^^^^^^^ `bar` is a `&` reference, so the data it refers to cannot be written