Skip to content

Commit 791e7bc

Browse files
committed
Auto merge of #28170 - nagisa:loopctl-label-spans, r=alexcrichton
r? @alexcrichton
2 parents 35b1454 + c493084 commit 791e7bc

File tree

16 files changed

+73
-43
lines changed

16 files changed

+73
-43
lines changed

src/librustc/middle/cfg/construct.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -284,15 +284,15 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
284284
}
285285

286286
hir::ExprBreak(label) => {
287-
let loop_scope = self.find_scope(expr, label);
287+
let loop_scope = self.find_scope(expr, label.map(|l| l.node));
288288
let b = self.add_ast_node(expr.id, &[pred]);
289289
self.add_exiting_edge(expr, b,
290290
loop_scope, loop_scope.break_index);
291291
self.add_unreachable_node()
292292
}
293293

294294
hir::ExprAgain(label) => {
295-
let loop_scope = self.find_scope(expr, label);
295+
let loop_scope = self.find_scope(expr, label.map(|l| l.node));
296296
let a = self.add_ast_node(expr.id, &[pred]);
297297
self.add_exiting_edge(expr, a,
298298
loop_scope, loop_scope.continue_index);

src/librustc/middle/liveness.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
10491049

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

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

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

10681068
// Now that we know the label we're going to,
10691069
// look it up in the continue loop nodes table

src/librustc_back/svh.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ mod svh_visitor {
277277
ExprRange(..) => SawExprRange,
278278
ExprPath(ref qself, _) => SawExprPath(qself.as_ref().map(|q| q.position)),
279279
ExprAddrOf(m, _) => SawExprAddrOf(m),
280-
ExprBreak(id) => SawExprBreak(id.map(|id| id.name.as_str())),
281-
ExprAgain(id) => SawExprAgain(id.map(|id| id.name.as_str())),
280+
ExprBreak(id) => SawExprBreak(id.map(|id| id.node.name.as_str())),
281+
ExprAgain(id) => SawExprAgain(id.map(|id| id.node.name.as_str())),
282282
ExprRet(..) => SawExprRet,
283283
ExprInlineAsm(ref asm) => SawExprInlineAsm(asm),
284284
ExprStruct(..) => SawExprStruct,

src/librustc_front/fold.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1124,8 +1124,14 @@ pub fn noop_fold_expr<T: Folder>(Expr {id, node, span}: Expr, folder: &mut T) ->
11241124
});
11251125
ExprPath(qself, folder.fold_path(path))
11261126
}
1127-
ExprBreak(opt_ident) => ExprBreak(opt_ident.map(|x| folder.fold_ident(x))),
1128-
ExprAgain(opt_ident) => ExprAgain(opt_ident.map(|x| folder.fold_ident(x))),
1127+
ExprBreak(opt_ident) => ExprBreak(opt_ident.map(|label|
1128+
respan(folder.new_span(label.span),
1129+
folder.fold_ident(label.node)))
1130+
),
1131+
ExprAgain(opt_ident) => ExprAgain(opt_ident.map(|label|
1132+
respan(folder.new_span(label.span),
1133+
folder.fold_ident(label.node)))
1134+
),
11291135
ExprRet(e) => ExprRet(e.map(|x| folder.fold_expr(x))),
11301136
ExprInlineAsm(InlineAsm {
11311137
inputs,

src/librustc_front/hir.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -730,9 +730,9 @@ pub enum Expr_ {
730730
/// A referencing operation (`&a` or `&mut a`)
731731
ExprAddrOf(Mutability, P<Expr>),
732732
/// A `break`, with an optional label to break
733-
ExprBreak(Option<Ident>),
733+
ExprBreak(Option<SpannedIdent>),
734734
/// A `continue`, with an optional label
735-
ExprAgain(Option<Ident>),
735+
ExprAgain(Option<SpannedIdent>),
736736
/// A `return`, with an optional value to be returned
737737
ExprRet(Option<P<Expr>>),
738738

src/librustc_front/print/pprust.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1587,15 +1587,15 @@ impl<'a> State<'a> {
15871587
try!(word(&mut self.s, "break"));
15881588
try!(space(&mut self.s));
15891589
if let Some(ident) = opt_ident {
1590-
try!(self.print_ident(ident));
1590+
try!(self.print_ident(ident.node));
15911591
try!(space(&mut self.s));
15921592
}
15931593
}
15941594
hir::ExprAgain(opt_ident) => {
15951595
try!(word(&mut self.s, "continue"));
15961596
try!(space(&mut self.s));
15971597
if let Some(ident) = opt_ident {
1598-
try!(self.print_ident(ident));
1598+
try!(self.print_ident(ident.node));
15991599
try!(space(&mut self.s))
16001600
}
16011601
}

src/librustc_resolve/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3759,12 +3759,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
37593759
}
37603760

37613761
ExprBreak(Some(label)) | ExprAgain(Some(label)) => {
3762-
let renamed = mtwt::resolve(label);
3762+
let renamed = mtwt::resolve(label.node);
37633763
match self.search_label(renamed) {
37643764
None => {
37653765
resolve_error(self,
3766-
expr.span,
3767-
ResolutionError::UndeclaredLabel(&label.name.as_str()))
3766+
label.span,
3767+
ResolutionError::UndeclaredLabel(&label.node.name.as_str()))
37683768
}
37693769
Some(DlDef(def @ DefLabel(_))) => {
37703770
// Since this def is a label, it is never read.

src/librustc_trans/trans/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -937,10 +937,10 @@ fn trans_rvalue_stmt_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
937937
trans_into(bcx, &**e, Ignore)
938938
}
939939
hir::ExprBreak(label_opt) => {
940-
controlflow::trans_break(bcx, expr, label_opt)
940+
controlflow::trans_break(bcx, expr, label_opt.map(|l| l.node))
941941
}
942942
hir::ExprAgain(label_opt) => {
943-
controlflow::trans_cont(bcx, expr, label_opt)
943+
controlflow::trans_cont(bcx, expr, label_opt.map(|l| l.node))
944944
}
945945
hir::ExprRet(ref ex) => {
946946
// Check to see if the return expression itself is reachable.

src/libsyntax/ast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -891,9 +891,9 @@ pub enum Expr_ {
891891
/// A referencing operation (`&a` or `&mut a`)
892892
ExprAddrOf(Mutability, P<Expr>),
893893
/// A `break`, with an optional label to break
894-
ExprBreak(Option<Ident>),
894+
ExprBreak(Option<SpannedIdent>),
895895
/// A `continue`, with an optional label
896-
ExprAgain(Option<Ident>),
896+
ExprAgain(Option<SpannedIdent>),
897897
/// A `return`, with an optional value to be returned
898898
ExprRet(Option<P<Expr>>),
899899

src/libsyntax/fold.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1299,8 +1299,14 @@ pub fn noop_fold_expr<T: Folder>(Expr {id, node, span}: Expr, folder: &mut T) ->
12991299
});
13001300
ExprPath(qself, folder.fold_path(path))
13011301
}
1302-
ExprBreak(opt_ident) => ExprBreak(opt_ident.map(|x| folder.fold_ident(x))),
1303-
ExprAgain(opt_ident) => ExprAgain(opt_ident.map(|x| folder.fold_ident(x))),
1302+
ExprBreak(opt_ident) => ExprBreak(opt_ident.map(|label|
1303+
respan(folder.new_span(label.span),
1304+
folder.fold_ident(label.node)))
1305+
),
1306+
ExprAgain(opt_ident) => ExprAgain(opt_ident.map(|label|
1307+
respan(folder.new_span(label.span),
1308+
folder.fold_ident(label.node)))
1309+
),
13041310
ExprRet(e) => ExprRet(e.map(|x| folder.fold_expr(x))),
13051311
ExprInlineAsm(InlineAsm {
13061312
inputs,

src/libsyntax/parse/parser.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -2143,9 +2143,12 @@ impl<'a> Parser<'a> {
21432143
}
21442144
if try!(self.eat_keyword(keywords::Continue) ){
21452145
let ex = if self.token.is_lifetime() {
2146-
let lifetime = self.get_lifetime();
2146+
let ex = ExprAgain(Some(Spanned{
2147+
node: self.get_lifetime(),
2148+
span: self.span
2149+
}));
21472150
try!(self.bump());
2148-
ExprAgain(Some(lifetime))
2151+
ex
21492152
} else {
21502153
ExprAgain(None)
21512154
};
@@ -2161,7 +2164,6 @@ impl<'a> Parser<'a> {
21612164
UnsafeBlock(ast::UserProvided));
21622165
}
21632166
if try!(self.eat_keyword(keywords::Return) ){
2164-
// RETURN expression
21652167
if self.token.can_begin_expr() {
21662168
let e = try!(self.parse_expr_nopanic());
21672169
hi = e.span.hi;
@@ -2170,11 +2172,12 @@ impl<'a> Parser<'a> {
21702172
ex = ExprRet(None);
21712173
}
21722174
} else if try!(self.eat_keyword(keywords::Break) ){
2173-
// BREAK expression
21742175
if self.token.is_lifetime() {
2175-
let lifetime = self.get_lifetime();
2176+
ex = ExprBreak(Some(Spanned {
2177+
node: self.get_lifetime(),
2178+
span: self.span
2179+
}));
21762180
try!(self.bump());
2177-
ex = ExprBreak(Some(lifetime));
21782181
} else {
21792182
ex = ExprBreak(None);
21802183
}

src/libsyntax/print/pprust.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1911,15 +1911,15 @@ impl<'a> State<'a> {
19111911
try!(word(&mut self.s, "break"));
19121912
try!(space(&mut self.s));
19131913
if let Some(ident) = opt_ident {
1914-
try!(self.print_ident(ident));
1914+
try!(self.print_ident(ident.node));
19151915
try!(space(&mut self.s));
19161916
}
19171917
}
19181918
ast::ExprAgain(opt_ident) => {
19191919
try!(word(&mut self.s, "continue"));
19201920
try!(space(&mut self.s));
19211921
if let Some(ident) = opt_ident {
1922-
try!(self.print_ident(ident));
1922+
try!(self.print_ident(ident.node));
19231923
try!(space(&mut self.s))
19241924
}
19251925
}

src/test/compile-fail/hygienic-label-1.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
// except according to those terms.
1010

1111
macro_rules! foo {
12-
() => { break 'x; }
12+
() => { break 'x; } //~ ERROR use of undeclared label `'x`
1313
}
1414

1515
pub fn main() {
16-
'x: loop { foo!() } //~ ERROR use of undeclared label `'x`
16+
'x: loop { foo!() }
1717
}

src/test/compile-fail/hygienic-label-3.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
// except according to those terms.
1010

1111
macro_rules! foo {
12-
() => { break 'x; }
12+
() => { break 'x; } //~ ERROR use of undeclared label `'x`
1313
}
1414

1515
pub fn main() {
1616
'x: for _ in 0..1 {
17-
foo!() //~ ERROR use of undeclared label `'x`
17+
foo!()
1818
};
1919
}

src/test/compile-fail/issue-28105.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -11,13 +11,8 @@
1111
// Make sure that a continue span actually contains the keyword.
1212

1313
fn main() {
14-
'a: loop {
15-
if false {
16-
continue //~ ERROR use of undeclared label
17-
'b;
18-
} else {
19-
break //~ ERROR use of undeclared label
20-
'c;
21-
}
22-
}
14+
continue //~ ERROR `continue` outside of loop
15+
;
16+
break //~ ERROR `break` outside of loop
17+
;
2318
}

src/test/compile-fail/issue-28109.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2012-2015 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+
// Make sure that label for continue and break is spanned correctly
12+
13+
fn main() {
14+
continue
15+
'b //~ ERROR use of undeclared label
16+
;
17+
break
18+
'c //~ ERROR use of undeclared label
19+
;
20+
}

0 commit comments

Comments
 (0)