Skip to content

Commit d328d26

Browse files
committed
mir-borrowck: Factorize error message for cannot_assign_static() between AST and MIR borrowck
1 parent 5c8066b commit d328d26

File tree

9 files changed

+54
-14
lines changed

9 files changed

+54
-14
lines changed

Diff for: src/librustc_borrowck/borrowck/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -759,11 +759,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
759759

760760
let mut db = match err.cause {
761761
MutabilityViolation => {
762-
struct_span_err!(self.tcx.sess,
763-
error_span,
764-
E0594,
765-
"cannot assign to {}",
766-
descr)
762+
self.cannot_assign(error_span, &descr, Origin::Ast)
767763
}
768764
BorrowViolation(euv::ClosureCapture(_)) => {
769765
struct_span_err!(self.tcx.sess, error_span, E0595,

Diff for: src/librustc_borrowck/diagnostics.rs

-1
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,5 @@ b.resume();
742742

743743
register_diagnostics! {
744744
// E0385, // {} in an aliasable location
745-
E0594, // cannot assign to {}
746745
E0598, // lifetime of {} is too short to guarantee its contents can be...
747746
}

Diff for: src/librustc_mir/borrow_check.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,6 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
10101010
fn report_assignment_to_static(&mut self, _context: Context, (lvalue, span): (&Lvalue, Span)) {
10111011
let mut err = self.tcx.cannot_assign_static(
10121012
span, &self.describe_lvalue(lvalue), Origin::Mir);
1013-
// FIXME: add span labels for borrow and assignment points
10141013
err.emit();
10151014
}
10161015
}

Diff for: src/librustc_mir/diagnostics.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1005,5 +1005,6 @@ register_diagnostics! {
10051005
E0493, // destructors cannot be evaluated at compile-time
10061006
E0524, // two closures require unique access to `..` at the same time
10071007
E0526, // shuffle indices are not constant
1008+
E0594, // cannot assign to {}
10081009
E0625, // thread-local statics cannot be accessed at compile-time
10091010
}

Diff for: src/librustc_mir/util/borrowck_errors.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,17 @@ pub trait BorrowckErrors {
179179
desc, OGN=o)
180180
}
181181

182+
fn cannot_assign(&self, span: Span, desc: &str, o: Origin) -> DiagnosticBuilder
183+
{
184+
struct_span_err!(self, span, E0594,
185+
"cannot assign to {}{OGN}",
186+
desc, OGN=o)
187+
}
188+
182189
fn cannot_assign_static(&self, span: Span, desc: &str, o: Origin)
183190
-> DiagnosticBuilder
184191
{
185-
self.struct_span_err(span, &format!("cannot assign to immutable static item {}{OGN}",
186-
desc, OGN=o))
192+
self.cannot_assign(span, &format!("immutable static item `{}`", desc), o)
187193
}
188194
}
189195

Diff for: src/test/compile-fail/E0594.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// revisions: ast mir
12+
//[mir]compile-flags: -Z emit-end-regions -Z borrowck-mir
13+
14+
static NUM: i32 = 18;
15+
16+
fn main() {
17+
NUM = 20; //[ast]~ ERROR E0594
18+
//[mir]~^ ERROR cannot assign to immutable static item (Ast)
19+
//[mir]~| ERROR cannot assign to immutable static item `NUM` (Mir)
20+
}

Diff for: src/test/compile-fail/borrowck/borrowck-assign-to-constants.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// revisions: ast mir
12+
//[mir]compile-flags: -Z emit-end-regions -Z borrowck-mir
13+
1114
static foo: isize = 5;
1215

1316
fn main() {
1417
// assigning to various global constants
15-
foo = 6; //~ ERROR cannot assign to immutable static item
18+
foo = 6; //[ast]~ ERROR cannot assign to immutable static item
19+
//[mir]~^ ERROR cannot assign to immutable static item (Ast)
20+
//[mir]~| ERROR cannot assign to immutable static item `foo` (Mir)
1621
}

Diff for: src/test/compile-fail/borrowck/borrowck-overloaded-index-ref-index.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// revisions: ast mir
12+
//[mir]compile-flags: -Z emit-end-regions -Z borrowck-mir
13+
1114
use std::ops::{Index, IndexMut};
1215

1316
struct Foo {
@@ -57,12 +60,18 @@ fn main() {
5760
let mut s = "hello".to_string();
5861
let rs = &mut s;
5962
println!("{}", f[&s]);
60-
//~^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
63+
//[ast]~^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
64+
//[mir]~^^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable (Ast)
65+
//[mir]~| ERROR cannot borrow `s` as immutable because it is also borrowed as mutable (Mir)
6166
f[&s] = 10;
62-
//~^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
67+
//[ast]~^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
68+
//[mir]~^^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable (Ast)
69+
//[mir]~| ERROR cannot borrow `s` as immutable because it is also borrowed as mutable (Mir)
6370
let s = Bar {
6471
x: 1,
6572
};
6673
s[2] = 20;
67-
//~^ ERROR cannot assign to immutable indexed content
74+
//[ast]~^ ERROR cannot assign to immutable indexed content
75+
//[mir]~^^ ERROR cannot assign to immutable indexed content
76+
// FIXME Error for MIR
6877
}

Diff for: src/test/compile-fail/issue-5500-1.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// revisions: ast mir
12+
//[mir]compile-flags: -Z emit-end-regions -Z borrowck-mir
13+
1114
struct TrieMapIterator<'a> {
1215
node: &'a usize
1316
}
1417

1518
fn main() {
1619
let a = 5;
1720
let _iter = TrieMapIterator{node: &a};
18-
_iter.node = & //~ ERROR cannot assign to immutable field
21+
_iter.node = & //[ast]~ ERROR cannot assign to immutable field
22+
//[mir]~^ ERROR cannot assign to immutable field `_iter.node` (Ast)
23+
// FIXME Error for MIR
1924
panic!()
2025
}

0 commit comments

Comments
 (0)