Skip to content

Commit

Permalink
Auto merge of #37360 - jseyfried:fix_label_scope, r=nrc
Browse files Browse the repository at this point in the history
resolve: fix label scopes

Fixes #37353 (turns an ICE back into an error).
r? @nrc
  • Loading branch information
bors authored Oct 25, 2016
2 parents affc3b7 + dcdab2d commit 67f26f7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use rustc::util::nodemap::{NodeMap, NodeSet, FnvHashMap, FnvHashSet};

use syntax::ext::hygiene::{Mark, SyntaxContext};
use syntax::ast::{self, FloatTy};
use syntax::ast::{CRATE_NODE_ID, Name, NodeId, Ident, IntTy, UintTy};
use syntax::ast::{CRATE_NODE_ID, Name, NodeId, Ident, SpannedIdent, IntTy, UintTy};
use syntax::ext::base::SyntaxExtension;
use syntax::parse::token::{self, keywords};
use syntax::util::lev_distance::find_best_match_for_name;
Expand Down Expand Up @@ -2278,7 +2278,7 @@ impl<'a> Resolver<'a> {
}

fn fresh_binding(&mut self,
ident: &ast::SpannedIdent,
ident: &SpannedIdent,
pat_id: NodeId,
outer_pat_id: NodeId,
pat_src: PatternSource,
Expand Down Expand Up @@ -2842,11 +2842,11 @@ impl<'a> Resolver<'a> {
} SuggestionType::NotFound
}

fn resolve_labeled_block(&mut self, label: Option<Ident>, id: NodeId, block: &Block) {
fn resolve_labeled_block(&mut self, label: Option<SpannedIdent>, id: NodeId, block: &Block) {
if let Some(label) = label {
let def = Def::Label(id);
self.with_label_rib(|this| {
this.label_ribs.last_mut().unwrap().bindings.insert(label, def);
this.label_ribs.last_mut().unwrap().bindings.insert(label.node, def);
this.visit_block(block);
});
} else {
Expand Down Expand Up @@ -3039,19 +3039,6 @@ impl<'a> Resolver<'a> {
visit::walk_expr(self, expr);
}

ExprKind::Loop(_, Some(label)) | ExprKind::While(.., Some(label)) => {
self.with_label_rib(|this| {
let def = Def::Label(expr.id);

{
let rib = this.label_ribs.last_mut().unwrap();
rib.bindings.insert(label.node, def);
}

visit::walk_expr(this, expr);
})
}

ExprKind::Break(Some(label)) | ExprKind::Continue(Some(label)) => {
match self.search_label(label.node) {
None => {
Expand Down Expand Up @@ -3081,12 +3068,19 @@ impl<'a> Resolver<'a> {
optional_else.as_ref().map(|expr| self.visit_expr(expr));
}

ExprKind::Loop(ref block, label) => self.resolve_labeled_block(label, expr.id, &block),

ExprKind::While(ref subexpression, ref block, label) => {
self.visit_expr(subexpression);
self.resolve_labeled_block(label, expr.id, &block);
}

ExprKind::WhileLet(ref pattern, ref subexpression, ref block, label) => {
self.visit_expr(subexpression);
self.value_ribs.push(Rib::new(NormalRibKind));
self.resolve_pattern(pattern, PatternSource::WhileLet, &mut FnvHashMap());

self.resolve_labeled_block(label.map(|l| l.node), expr.id, block);
self.resolve_labeled_block(label, expr.id, block);

self.value_ribs.pop();
}
Expand All @@ -3096,7 +3090,7 @@ impl<'a> Resolver<'a> {
self.value_ribs.push(Rib::new(NormalRibKind));
self.resolve_pattern(pattern, PatternSource::For, &mut FnvHashMap());

self.resolve_labeled_block(label.map(|l| l.node), expr.id, block);
self.resolve_labeled_block(label, expr.id, block);

self.value_ribs.pop();
}
Expand Down
3 changes: 3 additions & 0 deletions src/test/compile-fail/resolve-label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ fn f() {
}
}
}

// issue #37353
loop { 'w: while break 'w { } } //~ ERROR use of undeclared label
}

fn main() {}

0 comments on commit 67f26f7

Please sign in to comment.