about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-07-10 20:36:13 +0000
committerbors <bors@rust-lang.org>2018-07-10 20:36:13 +0000
commite5f6498d3d5c9dac841009d7b49738923826af75 (patch)
tree0c1fa1bfd3253f783c2834f384b393006db0e781
parent90bd83c9fca7b22056ec1ee0f00f095230bdb5c7 (diff)
parentdc8ae26c1ee4f95e3baef603506fbaa95cb2ccd1 (diff)
downloadrust-e5f6498d3d5c9dac841009d7b49738923826af75.tar.gz
rust-e5f6498d3d5c9dac841009d7b49738923826af75.zip
Auto merge of #51612 - ashtneoi:51515-missing-first-char, r=pnkfelix
NLL: fix E0594 "change to mutable ref" suggestion

Fix #51515.
Fix #51879.

Questions:
- [x] Is this the right place to fix this? It feels brittle, being so close to the frontend. **It's probably fine.**
- [ ] Have I missed any other cases that trigger this behavior?
- [x] Is it okay to use HELP and SUGGESTION in the UI test? **Yes.**
- [x] Do I need more tests for this? **No.**
-rw-r--r--src/librustc_mir/borrow_check/mod.rs6
-rw-r--r--src/test/ui/suggestions/issue-51515.rs24
-rw-r--r--src/test/ui/suggestions/issue-51515.stderr21
3 files changed, 49 insertions, 2 deletions
diff --git a/src/librustc_mir/borrow_check/mod.rs b/src/librustc_mir/borrow_check/mod.rs
index a5db0d15d8a..1a66a2d2cb9 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 `&<expr>` 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
new file mode 100644
index 00000000000..3e0a3b757a3
--- /dev/null
+++ b/src/test/ui/suggestions/issue-51515.rs
@@ -0,0 +1,24 @@
+// 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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(nll)]
+
+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
new file mode 100644
index 00000000000..3e7349b5aca
--- /dev/null
+++ b/src/test/ui/suggestions/issue-51515.stderr
@@ -0,0 +1,21 @@
+error[E0594]: cannot assign to `*foo` which is behind a `&` reference
+  --> $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: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
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0594`.