Skip to content

Commit 6fed013

Browse files
committed
middle: reset loop labels while visiting closure
This should fix #31754 and follow-up #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: #31754
1 parent 855fb61 commit 6fed013

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)