Skip to content

Commit d3e9881

Browse files
committed
MIR-borrowck: fix diagnostics for closures
1 parent 45caff8 commit d3e9881

File tree

5 files changed

+301
-9
lines changed

5 files changed

+301
-9
lines changed

src/librustc/hir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1949,7 +1949,7 @@ impl ForeignItem_ {
19491949
}
19501950

19511951
/// A free variable referred to in a function.
1952-
#[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
1952+
#[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable)]
19531953
pub struct Freevar {
19541954
/// The variable being accessed free.
19551955
pub def: Def,

src/librustc/mir/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,10 @@ impl<'tcx> Mir<'tcx> {
267267
let block = &self[location.block];
268268
let stmts = &block.statements;
269269
let idx = location.statement_index;
270-
if location.statement_index < stmts.len() {
270+
if idx < stmts.len() {
271271
&stmts[idx].source_info
272272
} else {
273-
assert!(location.statement_index == stmts.len());
273+
assert!(idx == stmts.len());
274274
&block.terminator().source_info
275275
}
276276
}

src/librustc_mir/borrow_check.rs

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,8 +1169,70 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
11691169
err.emit();
11701170
}
11711171

1172+
/// Finds the span of arguments of aclosure (within `maybe_closure_span`) and its usage of
1173+
/// the local assigned at `location`.
1174+
fn find_closure_span(
1175+
&self,
1176+
maybe_closure_span: Span,
1177+
location: Location,
1178+
) -> Option<(Span, Span)> {
1179+
use rustc::hir::ExprClosure;
1180+
use rustc::mir::AggregateKind;
1181+
1182+
let local = if let StatementKind::Assign(Lvalue::Local(local), _) =
1183+
self.mir[location.block].statements[location.statement_index].kind
1184+
{
1185+
local
1186+
} else {
1187+
return None;
1188+
};
1189+
1190+
for stmt in &self.mir[location.block].statements[location.statement_index + 1..] {
1191+
if maybe_closure_span != stmt.source_info.span {
1192+
break;
1193+
}
1194+
1195+
if let StatementKind::Assign(_, Rvalue::Aggregate(ref kind, ref lvs)) = stmt.kind {
1196+
if let AggregateKind::Closure(def_id, _) = **kind {
1197+
debug!("find_closure_span: found closure {:?}", lvs);
1198+
1199+
return if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
1200+
let args_span = if let ExprClosure(_, _, _, span, _) =
1201+
self.tcx.hir.expect_expr(node_id).node
1202+
{
1203+
span
1204+
} else {
1205+
return None;
1206+
};
1207+
1208+
self.tcx
1209+
.with_freevars(node_id, |freevars| {
1210+
for (v, lv) in freevars.iter().zip(lvs) {
1211+
if let Operand::Consume(Lvalue::Local(l)) = *lv {
1212+
if local == l {
1213+
debug!(
1214+
"find_closure_span: found captured local {:?}",
1215+
l
1216+
);
1217+
return Some(v.span);
1218+
}
1219+
}
1220+
}
1221+
None
1222+
})
1223+
.map(|var_span| (args_span, var_span))
1224+
} else {
1225+
None
1226+
};
1227+
}
1228+
}
1229+
}
1230+
1231+
None
1232+
}
1233+
11721234
fn report_conflicting_borrow(&mut self,
1173-
_context: Context,
1235+
context: Context,
11741236
common_prefix: &Lvalue,
11751237
(lvalue, span): (&Lvalue, Span),
11761238
gen_borrow_kind: BorrowKind,
@@ -1183,38 +1245,60 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
11831245

11841246
let issued_span = self.retrieve_borrow_span(issued_borrow);
11851247

1248+
let new_closure_span = self.find_closure_span(span, context.loc);
1249+
let span = new_closure_span.map(|(args, _)| args).unwrap_or(span);
1250+
let old_closure_span = self.find_closure_span(issued_span, issued_borrow.location);
1251+
let issued_span = old_closure_span.map(|(args, _)| args).unwrap_or(issued_span);
1252+
1253+
let desc_lvalue = self.describe_lvalue(lvalue);
1254+
11861255
// FIXME: supply non-"" `opt_via` when appropriate
11871256
let mut err = match (gen_borrow_kind, "immutable", "mutable",
11881257
issued_borrow.kind, "immutable", "mutable") {
11891258
(BorrowKind::Shared, lft, _, BorrowKind::Mut, _, rgt) |
11901259
(BorrowKind::Mut, _, lft, BorrowKind::Shared, rgt, _) =>
11911260
self.tcx.cannot_reborrow_already_borrowed(
1192-
span, &self.describe_lvalue(lvalue), "", lft, issued_span,
1261+
span, &desc_lvalue, "", lft, issued_span,
11931262
"it", rgt, "", end_issued_loan_span, Origin::Mir),
11941263

11951264
(BorrowKind::Mut, _, _, BorrowKind::Mut, _, _) =>
11961265
self.tcx.cannot_mutably_borrow_multiply(
1197-
span, &self.describe_lvalue(lvalue), "", issued_span,
1266+
span, &desc_lvalue, "", issued_span,
11981267
"", end_issued_loan_span, Origin::Mir),
11991268

12001269
(BorrowKind::Unique, _, _, BorrowKind::Unique, _, _) =>
12011270
self.tcx.cannot_uniquely_borrow_by_two_closures(
1202-
span, &self.describe_lvalue(lvalue), issued_span,
1271+
span, &desc_lvalue, issued_span,
12031272
end_issued_loan_span, Origin::Mir),
12041273

12051274
(BorrowKind::Unique, _, _, _, _, _) =>
12061275
self.tcx.cannot_uniquely_borrow_by_one_closure(
1207-
span, &self.describe_lvalue(lvalue), "",
1276+
span, &desc_lvalue, "",
12081277
issued_span, "it", "", end_issued_loan_span, Origin::Mir),
12091278

12101279
(_, _, _, BorrowKind::Unique, _, _) =>
12111280
self.tcx.cannot_reborrow_already_uniquely_borrowed(
1212-
span, &self.describe_lvalue(lvalue), "it", "",
1281+
span, &desc_lvalue, "it", "",
12131282
issued_span, "", end_issued_loan_span, Origin::Mir),
12141283

12151284
(BorrowKind::Shared, _, _, BorrowKind::Shared, _, _) =>
12161285
unreachable!(),
12171286
};
1287+
1288+
if let Some((_, var_span)) = old_closure_span {
1289+
err.span_label(
1290+
var_span,
1291+
format!("previous borrow occurs due to use of `{}` in closure", desc_lvalue),
1292+
);
1293+
}
1294+
1295+
if let Some((_, var_span)) = new_closure_span {
1296+
err.span_label(
1297+
var_span,
1298+
format!("borrow occurs due to use of `{}` in closure", desc_lvalue),
1299+
);
1300+
}
1301+
12181302
err.emit();
12191303
}
12201304

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2014 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+
// Tests that two closures cannot simultaneously have mutable
12+
// access to the variable, whether that mutable access be used
13+
// for direct assignment or for taking mutable ref. Issue #6801.
14+
15+
// compile-flags: -Z emit-end-regions -Z borrowck-mir
16+
17+
#![feature(box_syntax)]
18+
19+
fn to_fn_mut<F: FnMut()>(f: F) -> F { f }
20+
21+
fn a() {
22+
let mut x = 3;
23+
let c1 = to_fn_mut(|| x = 4);
24+
let c2 = to_fn_mut(|| x = 5); //~ ERROR cannot borrow `x` as mutable more than once
25+
}
26+
27+
fn set(x: &mut isize) {
28+
*x = 4;
29+
}
30+
31+
fn b() {
32+
let mut x = 3;
33+
let c1 = to_fn_mut(|| set(&mut x));
34+
let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
35+
}
36+
37+
fn c() {
38+
let mut x = 3;
39+
let c1 = to_fn_mut(|| x = 5);
40+
let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
41+
}
42+
43+
fn d() {
44+
let mut x = 3;
45+
let c1 = to_fn_mut(|| x = 5);
46+
let c2 = to_fn_mut(|| { let _y = to_fn_mut(|| set(&mut x)); }); // (nested closure)
47+
//~^ ERROR cannot borrow `x` as mutable more than once
48+
}
49+
50+
fn g() {
51+
struct Foo {
52+
f: Box<isize>
53+
}
54+
55+
let mut x: Box<_> = box Foo { f: box 3 };
56+
let c1 = to_fn_mut(|| set(&mut *x.f));
57+
let c2 = to_fn_mut(|| set(&mut *x.f));
58+
//~^ ERROR cannot borrow `x` as mutable more than once
59+
}
60+
61+
fn main() {
62+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
error[E0499]: cannot borrow `x` as mutable more than once at a time (Ast)
2+
--> $DIR/borrowck-closures-two-mut.rs:24:24
3+
|
4+
23 | let c1 = to_fn_mut(|| x = 4);
5+
| -- - previous borrow occurs due to use of `x` in closure
6+
| |
7+
| first mutable borrow occurs here
8+
24 | let c2 = to_fn_mut(|| x = 5); //~ ERROR cannot borrow `x` as mutable more than once
9+
| ^^ - borrow occurs due to use of `x` in closure
10+
| |
11+
| second mutable borrow occurs here
12+
25 | }
13+
| - first borrow ends here
14+
15+
error[E0499]: cannot borrow `x` as mutable more than once at a time (Ast)
16+
--> $DIR/borrowck-closures-two-mut.rs:34:24
17+
|
18+
33 | let c1 = to_fn_mut(|| set(&mut x));
19+
| -- - previous borrow occurs due to use of `x` in closure
20+
| |
21+
| first mutable borrow occurs here
22+
34 | let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
23+
| ^^ - borrow occurs due to use of `x` in closure
24+
| |
25+
| second mutable borrow occurs here
26+
35 | }
27+
| - first borrow ends here
28+
29+
error[E0499]: cannot borrow `x` as mutable more than once at a time (Ast)
30+
--> $DIR/borrowck-closures-two-mut.rs:40:24
31+
|
32+
39 | let c1 = to_fn_mut(|| x = 5);
33+
| -- - previous borrow occurs due to use of `x` in closure
34+
| |
35+
| first mutable borrow occurs here
36+
40 | let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
37+
| ^^ - borrow occurs due to use of `x` in closure
38+
| |
39+
| second mutable borrow occurs here
40+
41 | }
41+
| - first borrow ends here
42+
43+
error[E0499]: cannot borrow `x` as mutable more than once at a time (Ast)
44+
--> $DIR/borrowck-closures-two-mut.rs:46:24
45+
|
46+
45 | let c1 = to_fn_mut(|| x = 5);
47+
| -- - previous borrow occurs due to use of `x` in closure
48+
| |
49+
| first mutable borrow occurs here
50+
46 | let c2 = to_fn_mut(|| { let _y = to_fn_mut(|| set(&mut x)); }); // (nested closure)
51+
| ^^ - borrow occurs due to use of `x` in closure
52+
| |
53+
| second mutable borrow occurs here
54+
47 | //~^ ERROR cannot borrow `x` as mutable more than once
55+
48 | }
56+
| - first borrow ends here
57+
58+
error[E0499]: cannot borrow `x` as mutable more than once at a time (Ast)
59+
--> $DIR/borrowck-closures-two-mut.rs:57:24
60+
|
61+
56 | let c1 = to_fn_mut(|| set(&mut *x.f));
62+
| -- - previous borrow occurs due to use of `x` in closure
63+
| |
64+
| first mutable borrow occurs here
65+
57 | let c2 = to_fn_mut(|| set(&mut *x.f));
66+
| ^^ - borrow occurs due to use of `x` in closure
67+
| |
68+
| second mutable borrow occurs here
69+
58 | //~^ ERROR cannot borrow `x` as mutable more than once
70+
59 | }
71+
| - first borrow ends here
72+
73+
error[E0499]: cannot borrow `x` as mutable more than once at a time (Mir)
74+
--> $DIR/borrowck-closures-two-mut.rs:24:24
75+
|
76+
23 | let c1 = to_fn_mut(|| x = 4);
77+
| -- - previous borrow occurs due to use of `x` in closure
78+
| |
79+
| first mutable borrow occurs here
80+
24 | let c2 = to_fn_mut(|| x = 5); //~ ERROR cannot borrow `x` as mutable more than once
81+
| ^^ - borrow occurs due to use of `x` in closure
82+
| |
83+
| second mutable borrow occurs here
84+
25 | }
85+
| - first borrow ends here
86+
87+
error[E0499]: cannot borrow `x` as mutable more than once at a time (Mir)
88+
--> $DIR/borrowck-closures-two-mut.rs:34:24
89+
|
90+
33 | let c1 = to_fn_mut(|| set(&mut x));
91+
| -- - previous borrow occurs due to use of `x` in closure
92+
| |
93+
| first mutable borrow occurs here
94+
34 | let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
95+
| ^^ - borrow occurs due to use of `x` in closure
96+
| |
97+
| second mutable borrow occurs here
98+
35 | }
99+
| - first borrow ends here
100+
101+
error[E0499]: cannot borrow `x` as mutable more than once at a time (Mir)
102+
--> $DIR/borrowck-closures-two-mut.rs:40:24
103+
|
104+
39 | let c1 = to_fn_mut(|| x = 5);
105+
| -- - previous borrow occurs due to use of `x` in closure
106+
| |
107+
| first mutable borrow occurs here
108+
40 | let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
109+
| ^^ - borrow occurs due to use of `x` in closure
110+
| |
111+
| second mutable borrow occurs here
112+
41 | }
113+
| - first borrow ends here
114+
115+
error[E0499]: cannot borrow `x` as mutable more than once at a time (Mir)
116+
--> $DIR/borrowck-closures-two-mut.rs:46:24
117+
|
118+
45 | let c1 = to_fn_mut(|| x = 5);
119+
| -- - previous borrow occurs due to use of `x` in closure
120+
| |
121+
| first mutable borrow occurs here
122+
46 | let c2 = to_fn_mut(|| { let _y = to_fn_mut(|| set(&mut x)); }); // (nested closure)
123+
| ^^ - borrow occurs due to use of `x` in closure
124+
| |
125+
| second mutable borrow occurs here
126+
47 | //~^ ERROR cannot borrow `x` as mutable more than once
127+
48 | }
128+
| - first borrow ends here
129+
130+
error[E0499]: cannot borrow `x` as mutable more than once at a time (Mir)
131+
--> $DIR/borrowck-closures-two-mut.rs:57:24
132+
|
133+
56 | let c1 = to_fn_mut(|| set(&mut *x.f));
134+
| -- - previous borrow occurs due to use of `x` in closure
135+
| |
136+
| first mutable borrow occurs here
137+
57 | let c2 = to_fn_mut(|| set(&mut *x.f));
138+
| ^^ - borrow occurs due to use of `x` in closure
139+
| |
140+
| second mutable borrow occurs here
141+
58 | //~^ ERROR cannot borrow `x` as mutable more than once
142+
59 | }
143+
| - first borrow ends here
144+
145+
error: aborting due to 10 previous errors
146+

0 commit comments

Comments
 (0)