From 3833e60748507012f2001d9cbb004ec00a74d1d4 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Thu, 31 May 2018 22:31:13 +0800 Subject: [PATCH 1/7] fix wrong replacing --- src/librustc_borrowck/borrowck/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_borrowck/borrowck/mod.rs b/src/librustc_borrowck/borrowck/mod.rs index e063880028fc9..938f821649002 100644 --- a/src/librustc_borrowck/borrowck/mod.rs +++ b/src/librustc_borrowck/borrowck/mod.rs @@ -1201,7 +1201,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> { db.span_label( let_span, format!("consider changing this to `{}`", - snippet.replace("ref ", "ref mut ")) + snippet.replacen("ref ", "ref mut ", 1)) ); } } From 9d42c805eb4eff848173b09b22a75d4983029faa Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Thu, 31 May 2018 23:00:58 +0800 Subject: [PATCH 2/7] update test --- src/test/ui/suggestions/issue-51244.rs | 14 ++++++++++++++ src/test/ui/suggestions/issue-51244.stderr | 11 +++++++++++ 2 files changed, 25 insertions(+) create mode 100644 src/test/ui/suggestions/issue-51244.rs create mode 100644 src/test/ui/suggestions/issue-51244.stderr diff --git a/src/test/ui/suggestions/issue-51244.rs b/src/test/ui/suggestions/issue-51244.rs new file mode 100644 index 0000000000000..efb8a03a45cbc --- /dev/null +++ b/src/test/ui/suggestions/issue-51244.rs @@ -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 or the MIT license +// , 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; +} diff --git a/src/test/ui/suggestions/issue-51244.stderr b/src/test/ui/suggestions/issue-51244.stderr new file mode 100644 index 0000000000000..b6663c3789ebc --- /dev/null +++ b/src/test/ui/suggestions/issue-51244.stderr @@ -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; + | -------------- consider changing this to `ref mut my_ref @ _` +LL | *my_ref = 0; + | ^^^^^^^^^^^ cannot borrow as mutable + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0594`. From 78e1b3d44f0cadc09eeab1b381ccb5b60028e498 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Fri, 1 Jun 2018 12:08:31 +0800 Subject: [PATCH 3/7] lint with ref_span --- src/librustc_borrowck/borrowck/mod.rs | 12 +++++------- src/test/ui/suggestions/issue-51244.stderr | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/librustc_borrowck/borrowck/mod.rs b/src/librustc_borrowck/borrowck/mod.rs index 938f821649002..8544216bc28c7 100644 --- a/src/librustc_borrowck/borrowck/mod.rs +++ b/src/librustc_borrowck/borrowck/mod.rs @@ -1196,14 +1196,12 @@ 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 { + let ref_span = self.tcx.sess.codemap().span_until_whitespace(let_span); + if let Ok(_) = self.tcx.sess.codemap().span_to_snippet(let_span) { db.span_label( - let_span, - format!("consider changing this to `{}`", - snippet.replacen("ref ", "ref mut ", 1)) - ); - } + ref_span, + format!("consider changing this to `{}`", "ref mut")); + }; } ty::BindByValue(..) => { if let (Some(local_ty), is_implicit_self) = self.local_ty(node_id) { diff --git a/src/test/ui/suggestions/issue-51244.stderr b/src/test/ui/suggestions/issue-51244.stderr index b6663c3789ebc..f653a2a3359b1 100644 --- a/src/test/ui/suggestions/issue-51244.stderr +++ b/src/test/ui/suggestions/issue-51244.stderr @@ -2,7 +2,7 @@ error[E0594]: cannot assign to immutable borrowed content `*my_ref` --> $DIR/issue-51244.rs:13:5 | LL | let ref my_ref @ _ = 0; - | -------------- consider changing this to `ref mut my_ref @ _` + | --- consider changing this to `ref mut` LL | *my_ref = 0; | ^^^^^^^^^^^ cannot borrow as mutable From 62c4544b097277a0c84fdb04c2e6459be25f4e26 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Fri, 1 Jun 2018 19:00:15 +0800 Subject: [PATCH 4/7] replace ref --- src/librustc_borrowck/borrowck/mod.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/librustc_borrowck/borrowck/mod.rs b/src/librustc_borrowck/borrowck/mod.rs index 8544216bc28c7..63ff71aef021d 100644 --- a/src/librustc_borrowck/borrowck/mod.rs +++ b/src/librustc_borrowck/borrowck/mod.rs @@ -1196,11 +1196,16 @@ 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 ref_span = self.tcx.sess.codemap().span_until_whitespace(let_span); - if let Ok(_) = self.tcx.sess.codemap().span_to_snippet(let_span) { + 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_label( - ref_span, - format!("consider changing this to `{}`", "ref mut")); + let_span, + format!("consider changing this to `{}`", replace_str) + ); }; } ty::BindByValue(..) => { From 3fa7b5ad5e51867982c576904da0b0054ba62dc7 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Fri, 1 Jun 2018 21:03:24 +0800 Subject: [PATCH 5/7] update test --- src/test/ui/suggestions/issue-51244.rs | 2 +- src/test/ui/suggestions/issue-51244.stderr | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/ui/suggestions/issue-51244.rs b/src/test/ui/suggestions/issue-51244.rs index efb8a03a45cbc..50a21184a98b9 100644 --- a/src/test/ui/suggestions/issue-51244.rs +++ b/src/test/ui/suggestions/issue-51244.rs @@ -10,5 +10,5 @@ fn main() { let ref my_ref @ _ = 0; - *my_ref = 0; + *my_ref = 0; //~ ERROR cannot assign to immutable borrowed content `*my_ref` [E0594] } diff --git a/src/test/ui/suggestions/issue-51244.stderr b/src/test/ui/suggestions/issue-51244.stderr index f653a2a3359b1..b4ce29756235b 100644 --- a/src/test/ui/suggestions/issue-51244.stderr +++ b/src/test/ui/suggestions/issue-51244.stderr @@ -2,8 +2,8 @@ error[E0594]: cannot assign to immutable borrowed content `*my_ref` --> $DIR/issue-51244.rs:13:5 | LL | let ref my_ref @ _ = 0; - | --- consider changing this to `ref mut` -LL | *my_ref = 0; + | -------------- consider changing this to `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 From d49cc4680b7051d27771743ed5657c673a0dd84c Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Sun, 3 Jun 2018 12:06:21 +0800 Subject: [PATCH 6/7] add nll stderr --- src/test/ui/suggestions/issue-51244.nll.stderr | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/test/ui/suggestions/issue-51244.nll.stderr diff --git a/src/test/ui/suggestions/issue-51244.nll.stderr b/src/test/ui/suggestions/issue-51244.nll.stderr new file mode 100644 index 0000000000000..95b71c41cfa7b --- /dev/null +++ b/src/test/ui/suggestions/issue-51244.nll.stderr @@ -0,0 +1,11 @@ +error[E0594]: cannot assign to data in a `&` reference + --> $DIR/issue-51244.rs:13: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 immutable borrowed content `*my_ref` [E0594] + | ^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0594`. From 43cc08bb9891c86760d9523346b9698a87487a80 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Thu, 14 Jun 2018 09:12:50 +0800 Subject: [PATCH 7/7] span_suggestion --- src/librustc_borrowck/borrowck/mod.rs | 5 +++-- src/test/ui/nll/issue-51244.rs | 16 ++++++++++++++++ .../issue-51244.stderr} | 4 ++-- .../ui/rfc-2005-default-binding-mode/enum.stderr | 6 +++--- .../explicit-mut.stderr | 6 +++--- src/test/ui/suggestions/issue-51244.stderr | 2 +- 6 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 src/test/ui/nll/issue-51244.rs rename src/test/ui/{suggestions/issue-51244.nll.stderr => nll/issue-51244.stderr} (72%) diff --git a/src/librustc_borrowck/borrowck/mod.rs b/src/librustc_borrowck/borrowck/mod.rs index 63ff71aef021d..e5e51b4abab13 100644 --- a/src/librustc_borrowck/borrowck/mod.rs +++ b/src/librustc_borrowck/borrowck/mod.rs @@ -1202,9 +1202,10 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> { } else { snippet }; - db.span_label( + db.span_suggestion( let_span, - format!("consider changing this to `{}`", replace_str) + "use a mutable reference instead", + replace_str, ); }; } diff --git a/src/test/ui/nll/issue-51244.rs b/src/test/ui/nll/issue-51244.rs new file mode 100644 index 0000000000000..56d9449c4679d --- /dev/null +++ b/src/test/ui/nll/issue-51244.rs @@ -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 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 ref my_ref @ _ = 0; + *my_ref = 0; //~ ERROR cannot assign to data in a `&` reference [E0594] +} diff --git a/src/test/ui/suggestions/issue-51244.nll.stderr b/src/test/ui/nll/issue-51244.stderr similarity index 72% rename from src/test/ui/suggestions/issue-51244.nll.stderr rename to src/test/ui/nll/issue-51244.stderr index 95b71c41cfa7b..f1f47fc61ce8b 100644 --- a/src/test/ui/suggestions/issue-51244.nll.stderr +++ b/src/test/ui/nll/issue-51244.stderr @@ -1,9 +1,9 @@ error[E0594]: cannot assign to data in a `&` reference - --> $DIR/issue-51244.rs:13:5 + --> $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 immutable borrowed content `*my_ref` [E0594] +LL | *my_ref = 0; //~ ERROR cannot assign to data in a `&` reference [E0594] | ^^^^^^^^^^^ error: aborting due to previous error diff --git a/src/test/ui/rfc-2005-default-binding-mode/enum.stderr b/src/test/ui/rfc-2005-default-binding-mode/enum.stderr index a7f3b507508e8..26d51e9338152 100644 --- a/src/test/ui/rfc-2005-default-binding-mode/enum.stderr +++ b/src/test/ui/rfc-2005-default-binding-mode/enum.stderr @@ -2,7 +2,7 @@ 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 @@ -10,7 +10,7 @@ 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 @@ -18,7 +18,7 @@ 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 diff --git a/src/test/ui/rfc-2005-default-binding-mode/explicit-mut.stderr b/src/test/ui/rfc-2005-default-binding-mode/explicit-mut.stderr index f2b9bde41ab33..2f5eb8a3d8ecc 100644 --- a/src/test/ui/rfc-2005-default-binding-mode/explicit-mut.stderr +++ b/src/test/ui/rfc-2005-default-binding-mode/explicit-mut.stderr @@ -2,7 +2,7 @@ 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 @@ -10,7 +10,7 @@ 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 @@ -18,7 +18,7 @@ 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 diff --git a/src/test/ui/suggestions/issue-51244.stderr b/src/test/ui/suggestions/issue-51244.stderr index b4ce29756235b..997a74295e565 100644 --- a/src/test/ui/suggestions/issue-51244.stderr +++ b/src/test/ui/suggestions/issue-51244.stderr @@ -2,7 +2,7 @@ error[E0594]: cannot assign to immutable borrowed content `*my_ref` --> $DIR/issue-51244.rs:13:5 | LL | let ref my_ref @ _ = 0; - | -------------- consider changing this to `ref mut my_ref @ _` + | -------------- 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