Skip to content

Commit a9b9782

Browse files
committed
Rollup merge of rust-lang#33345 - birkenfeld:issue-31754, r=pnkfelix
middle: reset loop labels while visiting closure This should fix rust-lang#31754 and follow-up rust-lang#25343. Before the latter, the closure was visited twice in the context of the enclosing fn, which made even a single closure with a loop label emit a warning. With this change, the closure is still visited within the context of the main fn (which is intended, since it is not a separate item) but resets the found loop labels while being visited. Fixes: rust-lang#31754 Note: I amended the test file from rust-lang#25343, but I don't know if the original or amended test are effective, since as far as I could see, compiletest's run-pass tests do not check for zero warnings emitted? /cc @Manishearth
2 parents fc299c0 + 6fed013 commit a9b9782

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/librustc/middle/resolve_lifetime.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,12 @@ impl<'a, 'v> Visitor<'v> for LifetimeContext<'a> {
193193
})
194194
}
195195
FnKind::Closure(_) => {
196-
self.add_scope_and_walk_fn(fk, fd, b, s, fn_id)
196+
// Closures have their own set of labels, save labels just
197+
// like for foreign items above.
198+
let saved = replace(&mut self.labels_in_fn, vec![]);
199+
let result = self.add_scope_and_walk_fn(fk, fd, b, s, fn_id);
200+
replace(&mut self.labels_in_fn, saved);
201+
result
197202
}
198203
}
199204
}

src/test/run-pass/issue-25343.rs

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

11+
#[allow(unused)]
1112
fn main() {
1213
|| {
1314
'label: loop {
1415
}
1516
};
17+
18+
// More cases added from issue 31754
19+
20+
'label2: loop {
21+
break;
22+
}
23+
24+
let closure = || {
25+
'label2: loop {}
26+
};
27+
28+
fn inner_fn() {
29+
'label2: loop {}
30+
}
1631
}

0 commit comments

Comments
 (0)