Skip to content

Commit 835e3e5

Browse files
committed
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
2 parents 8891044 + d328d26 commit 835e3e5

File tree

10 files changed

+84
-48
lines changed

10 files changed

+84
-48
lines changed

src/librustc_borrowck/borrowck/check_loans.rs

+5-15
Original file line numberDiff line numberDiff line change
@@ -640,14 +640,10 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
640640
UseOk => { }
641641
UseWhileBorrowed(loan_path, loan_span) => {
642642
let desc = self.bccx.loan_path_to_string(copy_path);
643-
self.bccx.cannot_use_when_mutably_borrowed(span, &desc, Origin::Ast)
644-
.span_label(loan_span,
645-
format!("borrow of `{}` occurs here",
646-
&self.bccx.loan_path_to_string(&loan_path))
647-
)
648-
.span_label(span,
649-
format!("use of borrowed `{}`",
650-
&self.bccx.loan_path_to_string(&loan_path)))
643+
self.bccx.cannot_use_when_mutably_borrowed(
644+
span, &desc,
645+
loan_span, &self.bccx.loan_path_to_string(&loan_path),
646+
Origin::Ast)
651647
.emit();
652648
}
653649
}
@@ -865,13 +861,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
865861
loan_path: &LoanPath<'tcx>,
866862
loan: &Loan) {
867863
self.bccx.cannot_assign_to_borrowed(
868-
span, &self.bccx.loan_path_to_string(loan_path), Origin::Ast)
869-
.span_label(loan.span,
870-
format!("borrow of `{}` occurs here",
871-
self.bccx.loan_path_to_string(loan_path)))
872-
.span_label(span,
873-
format!("assignment to borrowed `{}` occurs here",
874-
self.bccx.loan_path_to_string(loan_path)))
864+
span, loan.span, &self.bccx.loan_path_to_string(loan_path), Origin::Ast)
875865
.emit();
876866
}
877867
}

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,

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
}

src/librustc_mir/borrow_check.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -931,14 +931,11 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
931931
_context: Context,
932932
(lvalue, span): (&Lvalue, Span),
933933
borrow : &BorrowData) {
934-
let described_lvalue = self.describe_lvalue(lvalue);
935-
let borrow_span = self.retrieve_borrow_span(borrow);
936934

937935
let mut err = self.tcx.cannot_use_when_mutably_borrowed(
938-
span, &described_lvalue, Origin::Mir);
939-
940-
err.span_label(borrow_span, format!("borrow of `{}` occurs here", described_lvalue));
941-
err.span_label(span, format!("use of borrowed `{}`", described_lvalue));
936+
span, &self.describe_lvalue(lvalue),
937+
self.retrieve_borrow_span(borrow), &self.describe_lvalue(&borrow.lvalue),
938+
Origin::Mir);
942939

943940
err.emit();
944941
}
@@ -991,14 +988,8 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
991988
_: Context,
992989
(lvalue, span): (&Lvalue, Span),
993990
loan: &BorrowData) {
994-
let describe_lvalue = self.describe_lvalue(lvalue);
995-
let borrow_span = self.retrieve_borrow_span(loan);
996-
997991
let mut err = self.tcx.cannot_assign_to_borrowed(
998-
span, &self.describe_lvalue(lvalue), Origin::Mir);
999-
1000-
err.span_label(borrow_span, format!("borrow of `{}` occurs here", describe_lvalue));
1001-
err.span_label(span, format!("assignment to borrowed `{}` occurs here", describe_lvalue));
992+
span, self.retrieve_borrow_span(loan), &self.describe_lvalue(lvalue), Origin::Mir);
1002993

1003994
err.emit();
1004995
}
@@ -1019,7 +1010,6 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
10191010
fn report_assignment_to_static(&mut self, _context: Context, (lvalue, span): (&Lvalue, Span)) {
10201011
let mut err = self.tcx.cannot_assign_static(
10211012
span, &self.describe_lvalue(lvalue), Origin::Mir);
1022-
// FIXME: add span labels for borrow and assignment points
10231013
err.emit();
10241014
}
10251015
}

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
}

src/librustc_mir/util/borrowck_errors.rs

+29-8
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,22 @@ pub trait BorrowckErrors {
5757
desc, OGN=o)
5858
}
5959

60-
fn cannot_use_when_mutably_borrowed(&self, span: Span, desc: &str, o: Origin)
60+
fn cannot_use_when_mutably_borrowed(&self,
61+
span: Span,
62+
desc: &str,
63+
borrow_span: Span,
64+
borrow_desc: &str,
65+
o: Origin)
6166
-> DiagnosticBuilder
6267
{
63-
struct_span_err!(self, span, E0503,
68+
let mut err = struct_span_err!(self, span, E0503,
6469
"cannot use `{}` because it was mutably borrowed{OGN}",
65-
desc, OGN=o)
70+
desc, OGN=o);
71+
72+
err.span_label(borrow_span, format!("borrow of `{}` occurs here", borrow_desc));
73+
err.span_label(span, format!("use of borrowed `{}`", borrow_desc));
74+
75+
err
6676
}
6777

6878
fn cannot_act_on_uninitialized_variable(&self,
@@ -140,12 +150,17 @@ pub trait BorrowckErrors {
140150
desc_new, msg_new, kind_new, noun_old, kind_old, msg_old, OGN=o)
141151
}
142152

143-
fn cannot_assign_to_borrowed(&self, span: Span, desc: &str, o: Origin)
153+
fn cannot_assign_to_borrowed(&self, span: Span, borrow_span: Span, desc: &str, o: Origin)
144154
-> DiagnosticBuilder
145155
{
146-
struct_span_err!(self, span, E0506,
156+
let mut err = struct_span_err!(self, span, E0506,
147157
"cannot assign to `{}` because it is borrowed{OGN}",
148-
desc, OGN=o)
158+
desc, OGN=o);
159+
160+
err.span_label(borrow_span, format!("borrow of `{}` occurs here", desc));
161+
err.span_label(span, format!("assignment to borrowed `{}` occurs here", desc));
162+
163+
err
149164
}
150165

151166
fn cannot_move_into_closure(&self, span: Span, desc: &str, o: Origin)
@@ -164,11 +179,17 @@ pub trait BorrowckErrors {
164179
desc, OGN=o)
165180
}
166181

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+
167189
fn cannot_assign_static(&self, span: Span, desc: &str, o: Origin)
168190
-> DiagnosticBuilder
169191
{
170-
self.struct_span_err(span, &format!("cannot assign to immutable static item {}{OGN}",
171-
desc, OGN=o))
192+
self.cannot_assign(span, &format!("immutable static item `{}`", desc), o)
172193
}
173194
}
174195

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+
}

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
}

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
}

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)