about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-10-03 16:42:26 +0000
committerbors <bors@rust-lang.org>2017-10-03 16:42:26 +0000
commit835e3e50781af375e6fef2d17e1bfa50d4e4aba3 (patch)
tree4d9a4fd2506bcb14b3c77990a7f6078305343d44 /src
parent8891044e89a22eeed1fcea957140072c26fcc62e (diff)
parentd328d264aac91f8f5d0514d52d598277389e14e0 (diff)
downloadrust-835e3e50781af375e6fef2d17e1bfa50d4e4aba3.tar.gz
rust-835e3e50781af375e6fef2d17e1bfa50d4e4aba3.zip
Auto merge of #44922 - zilbuz:issue-44596/E0594, r=pnkfelix
MIR borrowck: move span_label to `borrowck_errors.rs`

The calls to `span_label` are moved and factorized for:
* E0503 (`cannot_use_when_mutably_borrowed()`)
* E0506 (`cannot_assign_to_borrowed()`)

Additionnally, the error E0594 (`cannot_assign_static()`) has been factorized between `check_loan.rs` and `borrowc_check.rs`.

Part of #44596
Diffstat (limited to 'src')
-rw-r--r--src/librustc_borrowck/borrowck/check_loans.rs20
-rw-r--r--src/librustc_borrowck/borrowck/mod.rs6
-rw-r--r--src/librustc_borrowck/diagnostics.rs1
-rw-r--r--src/librustc_mir/borrow_check.rs18
-rw-r--r--src/librustc_mir/diagnostics.rs1
-rw-r--r--src/librustc_mir/util/borrowck_errors.rs37
-rw-r--r--src/test/compile-fail/E0594.rs20
-rw-r--r--src/test/compile-fail/borrowck/borrowck-assign-to-constants.rs7
-rw-r--r--src/test/compile-fail/borrowck/borrowck-overloaded-index-ref-index.rs15
-rw-r--r--src/test/compile-fail/issue-5500-1.rs7
10 files changed, 84 insertions, 48 deletions
diff --git a/src/librustc_borrowck/borrowck/check_loans.rs b/src/librustc_borrowck/borrowck/check_loans.rs
index 985257c2810..834bde660a8 100644
--- a/src/librustc_borrowck/borrowck/check_loans.rs
+++ b/src/librustc_borrowck/borrowck/check_loans.rs
@@ -640,14 +640,10 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
             UseOk => { }
             UseWhileBorrowed(loan_path, loan_span) => {
                 let desc = self.bccx.loan_path_to_string(copy_path);
-                self.bccx.cannot_use_when_mutably_borrowed(span, &desc, Origin::Ast)
-                    .span_label(loan_span,
-                               format!("borrow of `{}` occurs here",
-                                       &self.bccx.loan_path_to_string(&loan_path))
-                               )
-                    .span_label(span,
-                               format!("use of borrowed `{}`",
-                                        &self.bccx.loan_path_to_string(&loan_path)))
+                self.bccx.cannot_use_when_mutably_borrowed(
+                        span, &desc,
+                        loan_span, &self.bccx.loan_path_to_string(&loan_path),
+                        Origin::Ast)
                     .emit();
             }
         }
@@ -865,13 +861,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
                                    loan_path: &LoanPath<'tcx>,
                                    loan: &Loan) {
         self.bccx.cannot_assign_to_borrowed(
-            span, &self.bccx.loan_path_to_string(loan_path), Origin::Ast)
-            .span_label(loan.span,
-                       format!("borrow of `{}` occurs here",
-                               self.bccx.loan_path_to_string(loan_path)))
-            .span_label(span,
-                       format!("assignment to borrowed `{}` occurs here",
-                               self.bccx.loan_path_to_string(loan_path)))
+            span, loan.span, &self.bccx.loan_path_to_string(loan_path), Origin::Ast)
             .emit();
     }
 }
diff --git a/src/librustc_borrowck/borrowck/mod.rs b/src/librustc_borrowck/borrowck/mod.rs
index ef93e0365e6..a3f1340d429 100644
--- a/src/librustc_borrowck/borrowck/mod.rs
+++ b/src/librustc_borrowck/borrowck/mod.rs
@@ -759,11 +759,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
 
                 let mut db = match err.cause {
                     MutabilityViolation => {
-                        struct_span_err!(self.tcx.sess,
-                                         error_span,
-                                         E0594,
-                                         "cannot assign to {}",
-                                         descr)
+                        self.cannot_assign(error_span, &descr, Origin::Ast)
                     }
                     BorrowViolation(euv::ClosureCapture(_)) => {
                         struct_span_err!(self.tcx.sess, error_span, E0595,
diff --git a/src/librustc_borrowck/diagnostics.rs b/src/librustc_borrowck/diagnostics.rs
index 74133c821f0..29c35e23d4e 100644
--- a/src/librustc_borrowck/diagnostics.rs
+++ b/src/librustc_borrowck/diagnostics.rs
@@ -742,6 +742,5 @@ b.resume();
 
 register_diagnostics! {
 //    E0385, // {} in an aliasable location
-    E0594, // cannot assign to {}
     E0598, // lifetime of {} is too short to guarantee its contents can be...
 }
diff --git a/src/librustc_mir/borrow_check.rs b/src/librustc_mir/borrow_check.rs
index 902e2de841f..a8c81b4fc9f 100644
--- a/src/librustc_mir/borrow_check.rs
+++ b/src/librustc_mir/borrow_check.rs
@@ -931,14 +931,11 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
                                          _context: Context,
                                          (lvalue, span): (&Lvalue, Span),
                                          borrow : &BorrowData) {
-        let described_lvalue = self.describe_lvalue(lvalue);
-        let borrow_span = self.retrieve_borrow_span(borrow);
 
         let mut err = self.tcx.cannot_use_when_mutably_borrowed(
-            span, &described_lvalue, Origin::Mir);
-
-        err.span_label(borrow_span, format!("borrow of `{}` occurs here", described_lvalue));
-        err.span_label(span, format!("use of borrowed `{}`", described_lvalue));
+            span, &self.describe_lvalue(lvalue),
+            self.retrieve_borrow_span(borrow), &self.describe_lvalue(&borrow.lvalue),
+            Origin::Mir);
 
         err.emit();
     }
@@ -991,14 +988,8 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
                                            _: Context,
                                            (lvalue, span): (&Lvalue, Span),
                                            loan: &BorrowData) {
-        let describe_lvalue = self.describe_lvalue(lvalue);
-        let borrow_span = self.retrieve_borrow_span(loan);
-
         let mut err = self.tcx.cannot_assign_to_borrowed(
-            span, &self.describe_lvalue(lvalue), Origin::Mir);
-
-        err.span_label(borrow_span, format!("borrow of `{}` occurs here", describe_lvalue));
-        err.span_label(span, format!("assignment to borrowed `{}` occurs here", describe_lvalue));
+            span, self.retrieve_borrow_span(loan), &self.describe_lvalue(lvalue), Origin::Mir);
 
         err.emit();
     }
@@ -1019,7 +1010,6 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
     fn report_assignment_to_static(&mut self, _context: Context, (lvalue, span): (&Lvalue, Span)) {
         let mut err = self.tcx.cannot_assign_static(
             span, &self.describe_lvalue(lvalue), Origin::Mir);
-        // FIXME: add span labels for borrow and assignment points
         err.emit();
     }
 }
diff --git a/src/librustc_mir/diagnostics.rs b/src/librustc_mir/diagnostics.rs
index 2c4afb0aa0e..950bdff1d0f 100644
--- a/src/librustc_mir/diagnostics.rs
+++ b/src/librustc_mir/diagnostics.rs
@@ -1005,5 +1005,6 @@ register_diagnostics! {
     E0493, // destructors cannot be evaluated at compile-time
     E0524, // two closures require unique access to `..` at the same time
     E0526, // shuffle indices are not constant
+    E0594, // cannot assign to {}
     E0625, // thread-local statics cannot be accessed at compile-time
 }
diff --git a/src/librustc_mir/util/borrowck_errors.rs b/src/librustc_mir/util/borrowck_errors.rs
index 9de30726586..eddcb89c344 100644
--- a/src/librustc_mir/util/borrowck_errors.rs
+++ b/src/librustc_mir/util/borrowck_errors.rs
@@ -57,12 +57,22 @@ pub trait BorrowckErrors {
                          desc, OGN=o)
     }
 
-    fn cannot_use_when_mutably_borrowed(&self, span: Span, desc: &str, o: Origin)
+    fn cannot_use_when_mutably_borrowed(&self,
+                                        span: Span,
+                                        desc: &str,
+                                        borrow_span: Span,
+                                        borrow_desc: &str,
+                                        o: Origin)
                                         -> DiagnosticBuilder
     {
-        struct_span_err!(self, span, E0503,
+        let mut err = struct_span_err!(self, span, E0503,
                          "cannot use `{}` because it was mutably borrowed{OGN}",
-                         desc, OGN=o)
+                         desc, OGN=o);
+
+        err.span_label(borrow_span, format!("borrow of `{}` occurs here", borrow_desc));
+        err.span_label(span, format!("use of borrowed `{}`", borrow_desc));
+
+        err
     }
 
     fn cannot_act_on_uninitialized_variable(&self,
@@ -140,12 +150,17 @@ pub trait BorrowckErrors {
                          desc_new, msg_new, kind_new, noun_old, kind_old, msg_old, OGN=o)
     }
 
-    fn cannot_assign_to_borrowed(&self, span: Span, desc: &str, o: Origin)
+    fn cannot_assign_to_borrowed(&self, span: Span, borrow_span: Span, desc: &str, o: Origin)
                                  -> DiagnosticBuilder
     {
-        struct_span_err!(self, span, E0506,
+        let mut err = struct_span_err!(self, span, E0506,
                          "cannot assign to `{}` because it is borrowed{OGN}",
-                         desc, OGN=o)
+                         desc, OGN=o);
+
+        err.span_label(borrow_span, format!("borrow of `{}` occurs here", desc));
+        err.span_label(span, format!("assignment to borrowed `{}` occurs here", desc));
+
+        err
     }
 
     fn cannot_move_into_closure(&self, span: Span, desc: &str, o: Origin)
@@ -164,11 +179,17 @@ pub trait BorrowckErrors {
                          desc, OGN=o)
     }
 
+    fn cannot_assign(&self, span: Span, desc: &str, o: Origin) -> DiagnosticBuilder
+    {
+        struct_span_err!(self, span, E0594,
+                         "cannot assign to {}{OGN}",
+                         desc, OGN=o)
+    }
+
     fn cannot_assign_static(&self, span: Span, desc: &str, o: Origin)
                             -> DiagnosticBuilder
     {
-        self.struct_span_err(span, &format!("cannot assign to immutable static item {}{OGN}",
-                                            desc, OGN=o))
+        self.cannot_assign(span, &format!("immutable static item `{}`", desc), o)
     }
 }
 
diff --git a/src/test/compile-fail/E0594.rs b/src/test/compile-fail/E0594.rs
new file mode 100644
index 00000000000..8d33d658de9
--- /dev/null
+++ b/src/test/compile-fail/E0594.rs
@@ -0,0 +1,20 @@
+// Copyright 2017 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.
+
+// revisions: ast mir
+//[mir]compile-flags: -Z emit-end-regions -Z borrowck-mir
+
+static NUM: i32 = 18;
+
+fn main() {
+    NUM = 20; //[ast]~ ERROR E0594
+              //[mir]~^ ERROR cannot assign to immutable static item (Ast)
+              //[mir]~| ERROR cannot assign to immutable static item `NUM` (Mir)
+}
diff --git a/src/test/compile-fail/borrowck/borrowck-assign-to-constants.rs b/src/test/compile-fail/borrowck/borrowck-assign-to-constants.rs
index 1b5b1899e0d..3c93a391a6b 100644
--- a/src/test/compile-fail/borrowck/borrowck-assign-to-constants.rs
+++ b/src/test/compile-fail/borrowck/borrowck-assign-to-constants.rs
@@ -8,9 +8,14 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+// revisions: ast mir
+//[mir]compile-flags: -Z emit-end-regions -Z borrowck-mir
+
 static foo: isize = 5;
 
 fn main() {
     // assigning to various global constants
-    foo = 6; //~ ERROR cannot assign to immutable static item
+    foo = 6; //[ast]~ ERROR cannot assign to immutable static item
+             //[mir]~^ ERROR cannot assign to immutable static item (Ast)
+             //[mir]~| ERROR cannot assign to immutable static item `foo` (Mir)
 }
diff --git a/src/test/compile-fail/borrowck/borrowck-overloaded-index-ref-index.rs b/src/test/compile-fail/borrowck/borrowck-overloaded-index-ref-index.rs
index 4c50caf4976..7f165e00edb 100644
--- a/src/test/compile-fail/borrowck/borrowck-overloaded-index-ref-index.rs
+++ b/src/test/compile-fail/borrowck/borrowck-overloaded-index-ref-index.rs
@@ -8,6 +8,9 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+// revisions: ast mir
+//[mir]compile-flags: -Z emit-end-regions -Z borrowck-mir
+
 use std::ops::{Index, IndexMut};
 
 struct Foo {
@@ -57,12 +60,18 @@ fn main() {
     let mut s = "hello".to_string();
     let rs = &mut s;
     println!("{}", f[&s]);
-    //~^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
+    //[ast]~^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
+    //[mir]~^^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable (Ast)
+    //[mir]~| ERROR cannot borrow `s` as immutable because it is also borrowed as mutable (Mir)
     f[&s] = 10;
-    //~^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
+    //[ast]~^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
+    //[mir]~^^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable (Ast)
+    //[mir]~| ERROR cannot borrow `s` as immutable because it is also borrowed as mutable (Mir)
     let s = Bar {
         x: 1,
     };
     s[2] = 20;
-    //~^ ERROR cannot assign to immutable indexed content
+    //[ast]~^ ERROR cannot assign to immutable indexed content
+    //[mir]~^^ ERROR cannot assign to immutable indexed content
+    // FIXME Error for MIR
 }
diff --git a/src/test/compile-fail/issue-5500-1.rs b/src/test/compile-fail/issue-5500-1.rs
index 7e5809cdee0..2c2c32c0e88 100644
--- a/src/test/compile-fail/issue-5500-1.rs
+++ b/src/test/compile-fail/issue-5500-1.rs
@@ -8,6 +8,9 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+// revisions: ast mir
+//[mir]compile-flags: -Z emit-end-regions -Z borrowck-mir
+
 struct TrieMapIterator<'a> {
     node: &'a usize
 }
@@ -15,6 +18,8 @@ struct TrieMapIterator<'a> {
 fn main() {
     let a = 5;
     let _iter = TrieMapIterator{node: &a};
-    _iter.node = & //~ ERROR cannot assign to immutable field
+    _iter.node = & //[ast]~ ERROR cannot assign to immutable field
+                   //[mir]~^ ERROR cannot assign to immutable field `_iter.node` (Ast)
+                   // FIXME Error for MIR
     panic!()
 }