Skip to content

Commit a379780

Browse files
committed
Auto merge of #44811 - zilbuz:issue-44596/E0506, r=arielb1
MIR-borrowck: Adding notes to E0506 This PR adds notes to the MIR borrowck error E0506. Part of #44596
2 parents 46ef620 + b683538 commit a379780

14 files changed

+162
-40
lines changed

src/librustc_mir/borrow_check.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
412412
WriteKind::StorageDead |
413413
WriteKind::Mutate =>
414414
this.report_illegal_mutation_of_borrowed(
415-
context, lvalue_span),
415+
context, lvalue_span, borrow),
416416
WriteKind::Move =>
417417
this.report_move_out_while_borrowed(
418418
context, lvalue_span, borrow),
@@ -987,10 +987,19 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
987987
err.emit();
988988
}
989989

990-
fn report_illegal_mutation_of_borrowed(&mut self, _: Context, (lvalue, span): (&Lvalue, Span)) {
990+
fn report_illegal_mutation_of_borrowed(&mut self,
991+
_: Context,
992+
(lvalue, span): (&Lvalue, Span),
993+
loan: &BorrowData) {
994+
let describe_lvalue = self.describe_lvalue(lvalue);
995+
let borrow_span = self.retrieve_borrow_span(loan);
996+
991997
let mut err = self.tcx.cannot_assign_to_borrowed(
992998
span, &self.describe_lvalue(lvalue), Origin::Mir);
993-
// FIXME: add span labels for borrow and assignment points
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));
1002+
9941003
err.emit();
9951004
}
9961005

src/test/compile-fail/E0506.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@
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 FancyNum {
1215
num: u8,
1316
}
1417

1518
fn main() {
1619
let mut fancy_num = FancyNum { num: 5 };
1720
let fancy_ref = &fancy_num;
18-
fancy_num = FancyNum { num: 6 }; //~ ERROR E0506
21+
fancy_num = FancyNum { num: 6 }; //[ast]~ ERROR E0506
22+
//[mir]~^ ERROR (Mir) [E0506]
23+
//[mir]~| ERROR (Ast) [E0506]
1924

2025
println!("Num: {}, Ref: {}", fancy_num.num, fancy_ref.num);
2126
}

src/test/compile-fail/borrowck/borrowck-assign-comp.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
struct point { x: isize, y: isize }
1215

1316
fn a() {
@@ -17,7 +20,9 @@ fn a() {
1720
// This assignment is illegal because the field x is not
1821
// inherently mutable; since `p` was made immutable, `p.x` is now
1922
// immutable. Otherwise the type of &_q.x (&isize) would be wrong.
20-
p.x = 5; //~ ERROR cannot assign to `p.x`
23+
p.x = 5; //[ast]~ ERROR cannot assign to `p.x`
24+
//[mir]~^ ERROR cannot assign to `p.x` because it is borrowed (Ast)
25+
//[mir]~| ERROR cannot assign to `p.0` because it is borrowed (Mir)
2126
q.x;
2227
}
2328

@@ -27,7 +32,9 @@ fn c() {
2732

2833
let mut p = point {x: 3, y: 4};
2934
let q = &p.y;
30-
p = point {x: 5, y: 7};//~ ERROR cannot assign to `p`
35+
p = point {x: 5, y: 7};//[ast]~ ERROR cannot assign to `p`
36+
//[mir]~^ ERROR cannot assign to `p` because it is borrowed (Ast)
37+
//[mir]~| ERROR cannot assign to `p` because it is borrowed (Mir)
3138
p.x; // silence warning
3239
*q; // stretch loan
3340
}
@@ -38,7 +45,9 @@ fn d() {
3845

3946
let mut p = point {x: 3, y: 4};
4047
let q = &p.y;
41-
p.y = 5; //~ ERROR cannot assign to `p.y`
48+
p.y = 5; //[ast]~ ERROR cannot assign to `p.y`
49+
//[mir]~^ ERROR cannot assign to `p.y` because it is borrowed (Ast)
50+
//[mir]~| ERROR cannot assign to `p.1` because it is borrowed (Mir)
4251
*q;
4352
}
4453

src/test/compile-fail/borrowck/borrowck-closures-mut-and-imm.rs

+28-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
// Tests that two closures cannot simultaneously have mutable
1212
// and immutable access to the variable. Issue #6801.
1313

14+
// ignore-tidy-linelength
15+
// revisions: ast mir
16+
//[mir]compile-flags: -Z emit-end-regions -Z borrowck-mir
17+
1418
#![feature(box_syntax)]
1519

1620
fn get(x: &isize) -> isize {
@@ -24,37 +28,49 @@ fn set(x: &mut isize) {
2428
fn a() {
2529
let mut x = 3;
2630
let c1 = || x = 4;
27-
let c2 = || x * 5; //~ ERROR cannot borrow `x`
31+
let c2 = || x * 5; //[ast]~ ERROR cannot borrow `x`
32+
//[mir]~^ ERROR cannot borrow `x` as immutable because it is also borrowed as mutable (Ast)
33+
//[mir]~| ERROR cannot borrow `x` as immutable because it is also borrowed as mutable (Mir)
2834
}
2935

3036
fn b() {
3137
let mut x = 3;
3238
let c1 = || set(&mut x);
33-
let c2 = || get(&x); //~ ERROR cannot borrow `x`
39+
let c2 = || get(&x); //[ast]~ ERROR cannot borrow `x`
40+
//[mir]~^ ERROR cannot borrow `x` as immutable because it is also borrowed as mutable (Ast)
41+
//[mir]~| ERROR cannot borrow `x` as immutable because it is also borrowed as mutable (Mir)
3442
}
3543

3644
fn c() {
3745
let mut x = 3;
3846
let c1 = || set(&mut x);
39-
let c2 = || x * 5; //~ ERROR cannot borrow `x`
47+
let c2 = || x * 5; //[ast]~ ERROR cannot borrow `x`
48+
//[mir]~^ ERROR cannot borrow `x` as immutable because it is also borrowed as mutable (Ast)
49+
//[mir]~| ERROR cannot borrow `x` as immutable because it is also borrowed as mutable (Mir)
4050
}
4151

4252
fn d() {
4353
let mut x = 3;
4454
let c2 = || x * 5;
45-
x = 5; //~ ERROR cannot assign
55+
x = 5; //[ast]~ ERROR cannot assign
56+
//[mir]~^ ERROR cannot assign to `x` because it is borrowed (Ast)
57+
//[mir]~| ERROR cannot assign to `x` because it is borrowed (Mir)
4658
}
4759

4860
fn e() {
4961
let mut x = 3;
5062
let c1 = || get(&x);
51-
x = 5; //~ ERROR cannot assign
63+
x = 5; //[ast]~ ERROR cannot assign
64+
//[mir]~^ ERROR cannot assign to `x` because it is borrowed (Ast)
65+
//[mir]~| ERROR cannot assign to `x` because it is borrowed (Mir)
5266
}
5367

5468
fn f() {
5569
let mut x: Box<_> = box 3;
5670
let c1 = || get(&*x);
57-
*x = 5; //~ ERROR cannot assign
71+
*x = 5; //[ast]~ ERROR cannot assign
72+
//[mir]~^ ERROR cannot assign to `*x` because it is borrowed (Ast)
73+
//[mir]~| ERROR cannot assign to `(*x)` because it is borrowed (Mir)
5874
}
5975

6076
fn g() {
@@ -64,7 +80,9 @@ fn g() {
6480

6581
let mut x: Box<_> = box Foo { f: box 3 };
6682
let c1 = || get(&*x.f);
67-
*x.f = 5; //~ ERROR cannot assign to `*x.f`
83+
*x.f = 5; //[ast]~ ERROR cannot assign to `*x.f`
84+
//[mir]~^ ERROR cannot assign to `*x.f` because it is borrowed (Ast)
85+
//[mir]~| ERROR cannot assign to `(*(*x).0)` because it is borrowed (Mir)
6886
}
6987

7088
fn h() {
@@ -74,7 +92,9 @@ fn h() {
7492

7593
let mut x: Box<_> = box Foo { f: box 3 };
7694
let c1 = || get(&*x.f);
77-
let c2 = || *x.f = 5; //~ ERROR cannot borrow `x` as mutable
95+
let c2 = || *x.f = 5; //[ast]~ ERROR cannot borrow `x` as mutable
96+
//[mir]~^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable (Ast)
97+
//[mir]~| ERROR cannot borrow `x` as mutable because it is also borrowed as immutable (Mir)
7898
}
7999

80100
fn main() {

src/test/compile-fail/borrowck/borrowck-imm-ref-to-mut-rec-field-issue-3162-c.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
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
fn main() {
1215
let mut _a = 3;
1316
let _b = &mut _a;
1417
{
1518
let _c = &*_b;
16-
_a = 4; //~ ERROR cannot assign to `_a`
19+
_a = 4; //[ast]~ ERROR cannot assign to `_a`
20+
//[mir]~^ ERROR cannot assign to `_a` because it is borrowed (Ast)
21+
//[mir]~| ERROR cannot assign to `_a` because it is borrowed (Mir)
1722
}
1823
}

src/test/compile-fail/borrowck/borrowck-lend-flow-match.rs

+6-1
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
#![allow(unused_variables)]
1215
#![allow(unused_assignments)]
1316

@@ -22,7 +25,9 @@ fn separate_arms() {
2225
x = Some(0);
2326
}
2427
Some(ref __isize) => {
25-
x = Some(1); //~ ERROR cannot assign
28+
x = Some(1); //[ast]~ ERROR cannot assign
29+
//[mir]~^ ERROR cannot assign to `x` because it is borrowed (Ast)
30+
//[mir]~| ERROR cannot assign to `x` because it is borrowed (Mir)
2631
}
2732
}
2833
x.clone(); // just to prevent liveness warnings

src/test/compile-fail/borrowck/borrowck-overloaded-index-and-overloaded-deref.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
// operator. The accounting of the all the implicit things going on
1414
// here is rather subtle. Issue #20232.
1515

16+
// revisions: ast mir
17+
//[mir]compile-flags: -Z emit-end-regions -Z borrowck-mir
18+
1619
use std::ops::{Deref, Index};
1720

1821
struct MyVec<T> { x: T }
@@ -39,7 +42,9 @@ fn main() {
3942
let mut v = MyVec { x: MyPtr { x: Foo { f: 22 } } };
4043
let i = &v[0].f;
4144
v = MyVec { x: MyPtr { x: Foo { f: 23 } } };
42-
//~^ ERROR cannot assign to `v`
45+
//[ast]~^ ERROR cannot assign to `v`
46+
//[mir]~^^ ERROR cannot assign to `v` because it is borrowed (Ast)
47+
//[mir]~| ERROR cannot assign to `v` because it is borrowed (Mir)
4348
read(*i);
4449
}
4550

src/test/compile-fail/borrowck/borrowck-pat-reassign-binding.rs

+6-1
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
fn main() {
1215
let mut x: Option<isize> = None;
1316
match x {
@@ -17,7 +20,9 @@ fn main() {
1720
}
1821
Some(ref i) => {
1922
// But on this branch, `i` is an outstanding borrow
20-
x = Some(*i+1); //~ ERROR cannot assign to `x`
23+
x = Some(*i+1); //[ast]~ ERROR cannot assign to `x`
24+
//[mir]~^ ERROR cannot assign to `x` because it is borrowed (Ast)
25+
//[mir]~| ERROR cannot assign to `x` because it is borrowed (Mir)
2126
}
2227
}
2328
x.clone(); // just to prevent liveness warnings

src/test/compile-fail/borrowck/borrowck-union-borrow.rs

+38-12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
// except according to those terms.
1010

1111
// ignore-tidy-linelength
12+
// revisions: ast mir
13+
//[mir]compile-flags: -Z emit-end-regions -Z borrowck-mir
1214

1315
#[derive(Clone, Copy)]
1416
union U {
@@ -30,11 +32,15 @@ fn main() {
3032
}
3133
{
3234
let ra = &u.a;
33-
let rma = &mut u.a; //~ ERROR cannot borrow `u.a` as mutable because it is also borrowed as immutable
35+
let rma = &mut u.a; //[ast]~ ERROR cannot borrow `u.a` as mutable because it is also borrowed as immutable
36+
//[mir]~^ ERROR cannot borrow `u.a` as mutable because it is also borrowed as immutable (Ast)
37+
//[mir]~| ERROR cannot borrow `u.0` as mutable because it is also borrowed as immutable (Mir)
3438
}
3539
{
3640
let ra = &u.a;
37-
u.a = 1; //~ ERROR cannot assign to `u.a` because it is borrowed
41+
u.a = 1; //[ast]~ ERROR cannot assign to `u.a` because it is borrowed
42+
//[mir]~^ ERROR cannot assign to `u.a` because it is borrowed (Ast)
43+
//[mir]~| ERROR cannot assign to `u.0` because it is borrowed (Mir)
3844
}
3945
// Imm borrow, other field
4046
{
@@ -47,45 +53,65 @@ fn main() {
4753
}
4854
{
4955
let ra = &u.a;
50-
let rmb = &mut u.b; //~ ERROR cannot borrow `u` (via `u.b`) as mutable because `u` is also borrowed as immutable (via `u.a`)
56+
let rmb = &mut u.b; //[ast]~ ERROR cannot borrow `u` (via `u.b`) as mutable because `u` is also borrowed as immutable (via `u.a`)
57+
//[mir]~^ ERROR cannot borrow `u` (via `u.b`) as mutable because `u` is also borrowed as immutable (via `u.a`) (Ast)
58+
// FIXME Error for MIR (needs support for union)
5159
}
5260
{
5361
let ra = &u.a;
54-
u.b = 1; //~ ERROR cannot assign to `u.b` because it is borrowed
62+
u.b = 1; //[ast]~ ERROR cannot assign to `u.b` because it is borrowed
63+
//[mir]~^ ERROR cannot assign to `u.b` because it is borrowed (Ast)
64+
// FIXME Error for MIR (needs support for union)
5565
}
5666
// Mut borrow, same field
5767
{
5868
let rma = &mut u.a;
59-
let ra = &u.a; //~ ERROR cannot borrow `u.a` as immutable because it is also borrowed as mutable
69+
let ra = &u.a; //[ast]~ ERROR cannot borrow `u.a` as immutable because it is also borrowed as mutable
70+
//[mir]~^ ERROR cannot borrow `u.a` as immutable because it is also borrowed as mutable (Ast)
71+
//[mir]~| ERROR cannot borrow `u.0` as immutable because it is also borrowed as mutable (Mir)
6072
}
6173
{
6274
let ra = &mut u.a;
63-
let a = u.a; //~ ERROR cannot use `u.a` because it was mutably borrowed
75+
let a = u.a; //[ast]~ ERROR cannot use `u.a` because it was mutably borrowed
76+
//[mir]~^ ERROR cannot use `u.a` because it was mutably borrowed (Ast)
77+
//[mir]~| ERROR cannot use `u.0` because it was mutably borrowed (Mir)
6478
}
6579
{
6680
let rma = &mut u.a;
67-
let rma2 = &mut u.a; //~ ERROR cannot borrow `u.a` as mutable more than once at a time
81+
let rma2 = &mut u.a; //[ast]~ ERROR cannot borrow `u.a` as mutable more than once at a time
82+
//[mir]~^ ERROR cannot borrow `u.a` as mutable more than once at a time (Ast)
83+
//[mir]~| ERROR cannot borrow `u.0` as mutable more than once at a time (Mir)
6884
}
6985
{
7086
let rma = &mut u.a;
71-
u.a = 1; //~ ERROR cannot assign to `u.a` because it is borrowed
87+
u.a = 1; //[ast]~ ERROR cannot assign to `u.a` because it is borrowed
88+
//[mir]~^ ERROR cannot assign to `u.a` because it is borrowed (Ast)
89+
//[mir]~| ERROR cannot assign to `u.0` because it is borrowed (Mir)
7290
}
7391
// Mut borrow, other field
7492
{
7593
let rma = &mut u.a;
76-
let rb = &u.b; //~ ERROR cannot borrow `u` (via `u.b`) as immutable because `u` is also borrowed as mutable (via `u.a`)
94+
let rb = &u.b; //[ast]~ ERROR cannot borrow `u` (via `u.b`) as immutable because `u` is also borrowed as mutable (via `u.a`)
95+
//[mir]~^ ERROR cannot borrow `u` (via `u.b`) as immutable because `u` is also borrowed as mutable (via `u.a`) (Ast)
96+
// FIXME Error for MIR (needs support for union)
7797
}
7898
{
7999
let ra = &mut u.a;
80-
let b = u.b; //~ ERROR cannot use `u.b` because it was mutably borrowed
100+
let b = u.b; //[ast]~ ERROR cannot use `u.b` because it was mutably borrowed
101+
//[mir]~^ ERROR cannot use `u.b` because it was mutably borrowed (Ast)
102+
// FIXME Error for MIR (needs support for union)
81103
}
82104
{
83105
let rma = &mut u.a;
84-
let rmb2 = &mut u.b; //~ ERROR cannot borrow `u` (via `u.b`) as mutable more than once at a time
106+
let rmb2 = &mut u.b; //[ast]~ ERROR cannot borrow `u` (via `u.b`) as mutable more than once at a time
107+
//[mir]~^ ERROR cannot borrow `u` (via `u.b`) as mutable more than once at a time (Ast)
108+
// FIXME Error for MIR (needs support for union)
85109
}
86110
{
87111
let rma = &mut u.a;
88-
u.b = 1; //~ ERROR cannot assign to `u.b` because it is borrowed
112+
u.b = 1; //[ast]~ ERROR cannot assign to `u.b` because it is borrowed
113+
//[mir]~^ ERROR cannot assign to `u.b` because it is borrowed (Ast)
114+
// FIXME Error for MIR (needs support for union)
89115
}
90116
}
91117
}

src/test/compile-fail/borrowck/borrowck-vec-pattern-move-tail.rs

+6-1
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
#![feature(slice_patterns)]
1215

1316
fn main() {
@@ -17,7 +20,9 @@ fn main() {
1720
_ => unreachable!()
1821
};
1922
println!("t[0]: {}", t[0]);
20-
a[2] = 0; //~ ERROR cannot assign to `a[..]` because it is borrowed
23+
a[2] = 0; //[ast]~ ERROR cannot assign to `a[..]` because it is borrowed
24+
//[mir]~^ ERROR cannot assign to `a[..]` because it is borrowed (Ast)
25+
// FIXME Error for MIR (error missed)
2126
println!("t[0]: {}", t[0]);
2227
t[0];
2328
}

0 commit comments

Comments
 (0)