Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use proper span for break and continue labels #28170

Merged
merged 4 commits into from
Sep 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/librustc/middle/cfg/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,15 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
}

hir::ExprBreak(label) => {
let loop_scope = self.find_scope(expr, label);
let loop_scope = self.find_scope(expr, label.map(|l| l.node));
let b = self.add_ast_node(expr.id, &[pred]);
self.add_exiting_edge(expr, b,
loop_scope, loop_scope.break_index);
self.add_unreachable_node()
}

hir::ExprAgain(label) => {
let loop_scope = self.find_scope(expr, label);
let loop_scope = self.find_scope(expr, label.map(|l| l.node));
let a = self.add_ast_node(expr.id, &[pred]);
self.add_exiting_edge(expr, a,
loop_scope, loop_scope.continue_index);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {

hir::ExprBreak(opt_label) => {
// Find which label this break jumps to
let sc = self.find_loop_scope(opt_label, expr.id, expr.span);
let sc = self.find_loop_scope(opt_label.map(|l| l.node), expr.id, expr.span);

// Now that we know the label we're going to,
// look it up in the break loop nodes table
Expand All @@ -1063,7 +1063,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {

hir::ExprAgain(opt_label) => {
// Find which label this expr continues to
let sc = self.find_loop_scope(opt_label, expr.id, expr.span);
let sc = self.find_loop_scope(opt_label.map(|l| l.node), expr.id, expr.span);

// Now that we know the label we're going to,
// look it up in the continue loop nodes table
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_back/svh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ mod svh_visitor {
ExprRange(..) => SawExprRange,
ExprPath(ref qself, _) => SawExprPath(qself.as_ref().map(|q| q.position)),
ExprAddrOf(m, _) => SawExprAddrOf(m),
ExprBreak(id) => SawExprBreak(id.map(|id| id.name.as_str())),
ExprAgain(id) => SawExprAgain(id.map(|id| id.name.as_str())),
ExprBreak(id) => SawExprBreak(id.map(|id| id.node.name.as_str())),
ExprAgain(id) => SawExprAgain(id.map(|id| id.node.name.as_str())),
ExprRet(..) => SawExprRet,
ExprInlineAsm(ref asm) => SawExprInlineAsm(asm),
ExprStruct(..) => SawExprStruct,
Expand Down
10 changes: 8 additions & 2 deletions src/librustc_front/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,8 +1124,14 @@ pub fn noop_fold_expr<T: Folder>(Expr {id, node, span}: Expr, folder: &mut T) ->
});
ExprPath(qself, folder.fold_path(path))
}
ExprBreak(opt_ident) => ExprBreak(opt_ident.map(|x| folder.fold_ident(x))),
ExprAgain(opt_ident) => ExprAgain(opt_ident.map(|x| folder.fold_ident(x))),
ExprBreak(opt_ident) => ExprBreak(opt_ident.map(|label|
respan(folder.new_span(label.span),
folder.fold_ident(label.node)))
),
ExprAgain(opt_ident) => ExprAgain(opt_ident.map(|label|
respan(folder.new_span(label.span),
folder.fold_ident(label.node)))
),
ExprRet(e) => ExprRet(e.map(|x| folder.fold_expr(x))),
ExprInlineAsm(InlineAsm {
inputs,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_front/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,9 +730,9 @@ pub enum Expr_ {
/// A referencing operation (`&a` or `&mut a`)
ExprAddrOf(Mutability, P<Expr>),
/// A `break`, with an optional label to break
ExprBreak(Option<Ident>),
ExprBreak(Option<SpannedIdent>),
/// A `continue`, with an optional label
ExprAgain(Option<Ident>),
ExprAgain(Option<SpannedIdent>),
/// A `return`, with an optional value to be returned
ExprRet(Option<P<Expr>>),

Expand Down
4 changes: 2 additions & 2 deletions src/librustc_front/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1587,15 +1587,15 @@ impl<'a> State<'a> {
try!(word(&mut self.s, "break"));
try!(space(&mut self.s));
if let Some(ident) = opt_ident {
try!(self.print_ident(ident));
try!(self.print_ident(ident.node));
try!(space(&mut self.s));
}
}
hir::ExprAgain(opt_ident) => {
try!(word(&mut self.s, "continue"));
try!(space(&mut self.s));
if let Some(ident) = opt_ident {
try!(self.print_ident(ident));
try!(self.print_ident(ident.node));
try!(space(&mut self.s))
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3759,12 +3759,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}

ExprBreak(Some(label)) | ExprAgain(Some(label)) => {
let renamed = mtwt::resolve(label);
let renamed = mtwt::resolve(label.node);
match self.search_label(renamed) {
None => {
resolve_error(self,
expr.span,
ResolutionError::UndeclaredLabel(&label.name.as_str()))
label.span,
ResolutionError::UndeclaredLabel(&label.node.name.as_str()))
}
Some(DlDef(def @ DefLabel(_))) => {
// Since this def is a label, it is never read.
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_trans/trans/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,10 +937,10 @@ fn trans_rvalue_stmt_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
trans_into(bcx, &**e, Ignore)
}
hir::ExprBreak(label_opt) => {
controlflow::trans_break(bcx, expr, label_opt)
controlflow::trans_break(bcx, expr, label_opt.map(|l| l.node))
}
hir::ExprAgain(label_opt) => {
controlflow::trans_cont(bcx, expr, label_opt)
controlflow::trans_cont(bcx, expr, label_opt.map(|l| l.node))
}
hir::ExprRet(ref ex) => {
// Check to see if the return expression itself is reachable.
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,9 +891,9 @@ pub enum Expr_ {
/// A referencing operation (`&a` or `&mut a`)
ExprAddrOf(Mutability, P<Expr>),
/// A `break`, with an optional label to break
ExprBreak(Option<Ident>),
ExprBreak(Option<SpannedIdent>),
/// A `continue`, with an optional label
ExprAgain(Option<Ident>),
ExprAgain(Option<SpannedIdent>),
/// A `return`, with an optional value to be returned
ExprRet(Option<P<Expr>>),

Expand Down
10 changes: 8 additions & 2 deletions src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1299,8 +1299,14 @@ pub fn noop_fold_expr<T: Folder>(Expr {id, node, span}: Expr, folder: &mut T) ->
});
ExprPath(qself, folder.fold_path(path))
}
ExprBreak(opt_ident) => ExprBreak(opt_ident.map(|x| folder.fold_ident(x))),
ExprAgain(opt_ident) => ExprAgain(opt_ident.map(|x| folder.fold_ident(x))),
ExprBreak(opt_ident) => ExprBreak(opt_ident.map(|label|
respan(folder.new_span(label.span),
folder.fold_ident(label.node)))
),
ExprAgain(opt_ident) => ExprAgain(opt_ident.map(|label|
respan(folder.new_span(label.span),
folder.fold_ident(label.node)))
),
ExprRet(e) => ExprRet(e.map(|x| folder.fold_expr(x))),
ExprInlineAsm(InlineAsm {
inputs,
Expand Down
15 changes: 9 additions & 6 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2143,9 +2143,12 @@ impl<'a> Parser<'a> {
}
if try!(self.eat_keyword(keywords::Continue) ){
let ex = if self.token.is_lifetime() {
let lifetime = self.get_lifetime();
let ex = ExprAgain(Some(Spanned{
node: self.get_lifetime(),
span: self.span
}));
try!(self.bump());
ExprAgain(Some(lifetime))
ex
} else {
ExprAgain(None)
};
Expand All @@ -2161,7 +2164,6 @@ impl<'a> Parser<'a> {
UnsafeBlock(ast::UserProvided));
}
if try!(self.eat_keyword(keywords::Return) ){
// RETURN expression
if self.token.can_begin_expr() {
let e = try!(self.parse_expr_nopanic());
hi = e.span.hi;
Expand All @@ -2170,11 +2172,12 @@ impl<'a> Parser<'a> {
ex = ExprRet(None);
}
} else if try!(self.eat_keyword(keywords::Break) ){
// BREAK expression
if self.token.is_lifetime() {
let lifetime = self.get_lifetime();
ex = ExprBreak(Some(Spanned {
node: self.get_lifetime(),
span: self.span
}));
try!(self.bump());
ex = ExprBreak(Some(lifetime));
} else {
ex = ExprBreak(None);
}
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1911,15 +1911,15 @@ impl<'a> State<'a> {
try!(word(&mut self.s, "break"));
try!(space(&mut self.s));
if let Some(ident) = opt_ident {
try!(self.print_ident(ident));
try!(self.print_ident(ident.node));
try!(space(&mut self.s));
}
}
ast::ExprAgain(opt_ident) => {
try!(word(&mut self.s, "continue"));
try!(space(&mut self.s));
if let Some(ident) = opt_ident {
try!(self.print_ident(ident));
try!(self.print_ident(ident.node));
try!(space(&mut self.s))
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/hygienic-label-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
// except according to those terms.

macro_rules! foo {
() => { break 'x; }
() => { break 'x; } //~ ERROR use of undeclared label `'x`
}

pub fn main() {
'x: loop { foo!() } //~ ERROR use of undeclared label `'x`
'x: loop { foo!() }
}
4 changes: 2 additions & 2 deletions src/test/compile-fail/hygienic-label-3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
// except according to those terms.

macro_rules! foo {
() => { break 'x; }
() => { break 'x; } //~ ERROR use of undeclared label `'x`
}

pub fn main() {
'x: for _ in 0..1 {
foo!() //~ ERROR use of undeclared label `'x`
foo!()
};
}
15 changes: 5 additions & 10 deletions src/test/compile-fail/issue-28105.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand All @@ -11,13 +11,8 @@
// Make sure that a continue span actually contains the keyword.

fn main() {
'a: loop {
if false {
continue //~ ERROR use of undeclared label
'b;
} else {
break //~ ERROR use of undeclared label
'c;
}
}
continue //~ ERROR `continue` outside of loop
;
break //~ ERROR `break` outside of loop
;
}
20 changes: 20 additions & 0 deletions src/test/compile-fail/issue-28109.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2012-2015 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.

// Make sure that label for continue and break is spanned correctly

fn main() {
continue
'b //~ ERROR use of undeclared label
;
break
'c //~ ERROR use of undeclared label
;
}