Skip to content

Commit 4dacf27

Browse files
committed
rollup merge of rust-lang#20242: sanxiyn/break-from-fn
Fix rust-lang#19331.
2 parents 21f661a + fbda51e commit 4dacf27

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/librustc_resolve/lib.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4008,6 +4008,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
40084008
}
40094009
}
40104010

4011+
/// Searches the current set of local scopes and
4012+
/// applies translations for closures.
40114013
fn search_ribs(&self,
40124014
ribs: &[Rib],
40134015
name: Name,
@@ -4029,6 +4031,27 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
40294031
None
40304032
}
40314033

4034+
/// Searches the current set of local scopes for labels.
4035+
/// Stops after meeting a closure.
4036+
fn search_label(&self, name: Name) -> Option<DefLike> {
4037+
for rib in self.label_ribs.iter().rev() {
4038+
match rib.kind {
4039+
NormalRibKind => {
4040+
// Continue
4041+
}
4042+
_ => {
4043+
// Do not resolve labels across function boundary
4044+
return None
4045+
}
4046+
}
4047+
let result = rib.bindings.get(&name).cloned();
4048+
if result.is_some() {
4049+
return result
4050+
}
4051+
}
4052+
None
4053+
}
4054+
40324055
fn resolve_crate(&mut self, krate: &ast::Crate) {
40334056
debug!("(resolving crate) starting");
40344057

@@ -5835,8 +5858,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
58355858

58365859
ExprBreak(Some(label)) | ExprAgain(Some(label)) => {
58375860
let renamed = mtwt::resolve(label);
5838-
match self.search_ribs(self.label_ribs[],
5839-
renamed, expr.span) {
5861+
match self.search_label(renamed) {
58405862
None => {
58415863
self.resolve_error(
58425864
expr.span,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
fn f() {
12+
'l: loop {
13+
fn g() {
14+
loop {
15+
break 'l; //~ ERROR use of undeclared label
16+
}
17+
}
18+
}
19+
}
20+
21+
fn main() {}

0 commit comments

Comments
 (0)