Skip to content

Commit

Permalink
Auto merge of #44922 - zilbuz:issue-44596/E0594, r=pnkfelix
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Oct 3, 2017
2 parents 8891044 + d328d26 commit 835e3e5
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 48 deletions.
20 changes: 5 additions & 15 deletions src/librustc_borrowck/borrowck/check_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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();
}
}
6 changes: 1 addition & 5 deletions src/librustc_borrowck/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion src/librustc_borrowck/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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...
}
18 changes: 4 additions & 14 deletions src/librustc_mir/borrow_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
37 changes: 29 additions & 8 deletions src/librustc_mir/util/borrowck_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/test/compile-fail/E0594.rs
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
7 changes: 6 additions & 1 deletion src/test/compile-fail/issue-5500-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
// 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
}

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!()
}

0 comments on commit 835e3e5

Please sign in to comment.