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

Fix wrong test replacemen in ref mut suggestion #51249

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/librustc_borrowck/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1196,14 +1196,18 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
let let_span = self.tcx.hir.span(node_id);
match self.local_binding_mode(node_id) {
ty::BindByReference(..) => {
let snippet = self.tcx.sess.codemap().span_to_snippet(let_span);
if let Ok(snippet) = snippet {
db.span_label(
if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(let_span) {
let replace_str = if snippet.starts_with("ref ") {
snippet.replacen("ref ", "ref mut ", 1)
} else {
snippet
};
db.span_suggestion(
let_span,
format!("consider changing this to `{}`",
snippet.replace("ref ", "ref mut "))
"use a mutable reference instead",
replace_str,
);
}
};
}
ty::BindByValue(..) => {
if let (Some(local_ty), is_implicit_self) = self.local_ty(node_id) {
Expand Down
16 changes: 16 additions & 0 deletions src/test/ui/nll/issue-51244.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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 ref my_ref @ _ = 0;
*my_ref = 0; //~ ERROR cannot assign to data in a `&` reference [E0594]
}
11 changes: 11 additions & 0 deletions src/test/ui/nll/issue-51244.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0594]: cannot assign to data in a `&` reference
--> $DIR/issue-51244.rs:15:5
|
LL | let ref my_ref @ _ = 0;
| -------------- help: consider changing this to be a mutable reference: `&mut ef my_ref @ _`
LL | *my_ref = 0; //~ ERROR cannot assign to data in a `&` reference [E0594]
| ^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0594`.
6 changes: 3 additions & 3 deletions src/test/ui/rfc-2005-default-binding-mode/enum.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ error[E0594]: cannot assign to immutable borrowed content `*x`
--> $DIR/enum.rs:19:5
|
LL | let Wrap(x) = &Wrap(3);
| - consider changing this to `x`
| - help: use a mutable reference instead: `x`
LL | *x += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot borrow as mutable

error[E0594]: cannot assign to immutable borrowed content `*x`
--> $DIR/enum.rs:23:9
|
LL | if let Some(x) = &Some(3) {
| - consider changing this to `x`
| - help: use a mutable reference instead: `x`
LL | *x += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot borrow as mutable

error[E0594]: cannot assign to immutable borrowed content `*x`
--> $DIR/enum.rs:29:9
|
LL | while let Some(x) = &Some(3) {
| - consider changing this to `x`
| - help: use a mutable reference instead: `x`
LL | *x += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot borrow as mutable

Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/rfc-2005-default-binding-mode/explicit-mut.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ error[E0594]: cannot assign to immutable borrowed content `*n`
--> $DIR/explicit-mut.rs:17:13
|
LL | Some(n) => {
| - consider changing this to `n`
| - help: use a mutable reference instead: `n`
LL | *n += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot borrow as mutable

error[E0594]: cannot assign to immutable borrowed content `*n`
--> $DIR/explicit-mut.rs:25:13
|
LL | Some(n) => {
| - consider changing this to `n`
| - help: use a mutable reference instead: `n`
LL | *n += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot borrow as mutable

error[E0594]: cannot assign to immutable borrowed content `*n`
--> $DIR/explicit-mut.rs:33:13
|
LL | Some(n) => {
| - consider changing this to `n`
| - help: use a mutable reference instead: `n`
LL | *n += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot borrow as mutable

Expand Down
14 changes: 14 additions & 0 deletions src/test/ui/suggestions/issue-51244.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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.

fn main() {
let ref my_ref @ _ = 0;
*my_ref = 0; //~ ERROR cannot assign to immutable borrowed content `*my_ref` [E0594]
}
11 changes: 11 additions & 0 deletions src/test/ui/suggestions/issue-51244.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0594]: cannot assign to immutable borrowed content `*my_ref`
--> $DIR/issue-51244.rs:13:5
|
LL | let ref my_ref @ _ = 0;
| -------------- help: use a mutable reference instead: `ref mut my_ref @ _`
LL | *my_ref = 0; //~ ERROR cannot assign to immutable borrowed content `*my_ref` [E0594]
| ^^^^^^^^^^^ cannot borrow as mutable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0594`.