rollup merge of #20242: sanxiyn/break-from-fn

Fix #19331.
This commit is contained in:
Alex Crichton 2014-12-29 16:36:15 -08:00
commit 4dacf2780f
2 changed files with 45 additions and 2 deletions

View file

@ -4008,6 +4008,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
}
/// Searches the current set of local scopes and
/// applies translations for closures.
fn search_ribs(&self,
ribs: &[Rib],
name: Name,
@ -4029,6 +4031,27 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
None
}
/// Searches the current set of local scopes for labels.
/// Stops after meeting a closure.
fn search_label(&self, name: Name) -> Option<DefLike> {
for rib in self.label_ribs.iter().rev() {
match rib.kind {
NormalRibKind => {
// Continue
}
_ => {
// Do not resolve labels across function boundary
return None
}
}
let result = rib.bindings.get(&name).cloned();
if result.is_some() {
return result
}
}
None
}
fn resolve_crate(&mut self, krate: &ast::Crate) {
debug!("(resolving crate) starting");
@ -5835,8 +5858,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
ExprBreak(Some(label)) | ExprAgain(Some(label)) => {
let renamed = mtwt::resolve(label);
match self.search_ribs(self.label_ribs[],
renamed, expr.span) {
match self.search_label(renamed) {
None => {
self.resolve_error(
expr.span,

View file

@ -0,0 +1,21 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
fn f() {
'l: loop {
fn g() {
loop {
break 'l; //~ ERROR use of undeclared label
}
}
}
}
fn main() {}