Skip to content

Commit

Permalink
Do not resolve labels across function boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
sanxiyn committed Dec 26, 2014
1 parent 5ba6102 commit 9449161
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/librustc_resolve/lib.rs
Expand Up @@ -3934,6 +3934,30 @@ impl<'a> Resolver<'a> {
None
}

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();
match result {
Some(_) => {
return result
}
None => {
// Continue
}
}
}
None
}

fn resolve_crate(&mut self, krate: &ast::Crate) {
debug!("(resolving crate) starting");

Expand Down Expand Up @@ -5752,8 +5776,7 @@ impl<'a> Resolver<'a> {

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,
Expand Down
21 changes: 21 additions & 0 deletions src/test/compile-fail/resolve-label.rs
@@ -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() {}

0 comments on commit 9449161

Please sign in to comment.