Skip to content

Commit 7e9a2fe

Browse files
committed
Add support for labeled while loops.
1 parent cf1381c commit 7e9a2fe

21 files changed

+96
-24
lines changed

src/librustc/lint/builtin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl LintPass for WhileTrue {
5959

6060
fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
6161
match e.node {
62-
ast::ExprWhile(cond, _) => {
62+
ast::ExprWhile(cond, _, _) => {
6363
match cond.node {
6464
ast::ExprLit(lit) => {
6565
match lit.node {
@@ -1057,7 +1057,7 @@ impl LintPass for UnnecessaryParens {
10571057
fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
10581058
let (value, msg, struct_lit_needs_parens) = match e.node {
10591059
ast::ExprIf(cond, _, _) => (cond, "`if` condition", true),
1060-
ast::ExprWhile(cond, _) => (cond, "`while` condition", true),
1060+
ast::ExprWhile(cond, _, _) => (cond, "`while` condition", true),
10611061
ast::ExprMatch(head, _) => (head, "`match` head expression", true),
10621062
ast::ExprRet(Some(value)) => (value, "`return` value", false),
10631063
ast::ExprAssign(_, value) => (value, "assigned value", false),

src/librustc/middle/cfg/construct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ impl<'a> CFGBuilder<'a> {
225225
self.add_node(expr.id, [then_exit, else_exit]) // 4, 5
226226
}
227227

228-
ast::ExprWhile(ref cond, ref body) => {
228+
ast::ExprWhile(ref cond, ref body, _) => {
229229
//
230230
// [pred]
231231
// |

src/librustc/middle/check_loop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl<'a> Visitor<Context> for CheckLoopVisitor<'a> {
3535

3636
fn visit_expr(&mut self, e: &ast::Expr, cx:Context) {
3737
match e.node {
38-
ast::ExprWhile(ref e, ref b) => {
38+
ast::ExprWhile(ref e, ref b, _) => {
3939
self.visit_expr(&**e, cx);
4040
self.visit_block(&**b, Loop);
4141
}

src/librustc/middle/expr_use_visitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ impl<'d,'t,TYPER:mc::Typer> ExprUseVisitor<'d,'t,TYPER> {
391391
self.walk_block(&**blk);
392392
}
393393

394-
ast::ExprWhile(ref cond_expr, ref blk) => {
394+
ast::ExprWhile(ref cond_expr, ref blk, _) => {
395395
self.consume_expr(&**cond_expr);
396396
self.walk_block(&**blk);
397397
}

src/librustc/middle/liveness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ impl<'a> Liveness<'a> {
10171017
self.propagate_through_expr(&**cond, ln)
10181018
}
10191019

1020-
ExprWhile(ref cond, ref blk) => {
1020+
ExprWhile(ref cond, ref blk, _) => {
10211021
self.propagate_through_loop(expr,
10221022
WhileLoop(cond.clone()),
10231023
&**blk,

src/librustc/middle/region.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ fn resolve_expr(visitor: &mut RegionResolutionVisitor,
521521
visitor.region_maps.mark_as_terminating_scope(body.id);
522522
}
523523

524-
ast::ExprWhile(expr, body) => {
524+
ast::ExprWhile(expr, body, _) => {
525525
visitor.region_maps.mark_as_terminating_scope(expr.id);
526526
visitor.region_maps.mark_as_terminating_scope(body.id);
527527
}

src/librustc/middle/resolve.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5313,7 +5313,7 @@ impl<'a> Resolver<'a> {
53135313
visit::walk_expr(self, expr, ());
53145314
}
53155315

5316-
ExprLoop(_, Some(label)) => {
5316+
ExprLoop(_, Some(label)) | ExprWhile(_, _, Some(label)) => {
53175317
self.with_label_rib(|this| {
53185318
let def_like = DlDef(DefLabel(expr.id));
53195319

src/librustc/middle/trans/debuginfo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3570,7 +3570,7 @@ fn populate_scope_map(cx: &CrateContext,
35703570
}
35713571
}
35723572

3573-
ast::ExprWhile(ref cond_exp, ref loop_body) => {
3573+
ast::ExprWhile(ref cond_exp, ref loop_body, _) => {
35743574
walk_expr(cx, &**cond_exp, scope_stack, scope_map);
35753575

35763576
with_new_scope(cx,

src/librustc/middle/trans/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ fn trans_rvalue_stmt_unadjusted<'a>(bcx: &'a Block<'a>,
662662
ast::ExprRet(ex) => {
663663
controlflow::trans_ret(bcx, ex)
664664
}
665-
ast::ExprWhile(ref cond, ref body) => {
665+
ast::ExprWhile(ref cond, ref body, _) => {
666666
controlflow::trans_while(bcx, expr.id, &**cond, &**body)
667667
}
668668
ast::ExprForLoop(ref pat, ref head, ref body, _) => {

src/librustc/middle/typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3333,7 +3333,7 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
33333333
check_then_else(fcx, &**cond, &**then_blk, opt_else_expr.clone(),
33343334
id, expr.span, expected);
33353335
}
3336-
ast::ExprWhile(ref cond, ref body) => {
3336+
ast::ExprWhile(ref cond, ref body, _) => {
33373337
check_expr_has_type(fcx, &**cond, ty::mk_bool());
33383338
check_block_no_value(fcx, &**body);
33393339
let cond_ty = fcx.expr_ty(&**cond);

src/librustc/middle/typeck/check/regionck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ fn visit_expr(rcx: &mut Rcx, expr: &ast::Expr) {
599599
rcx.set_repeating_scope(repeating_scope);
600600
}
601601

602-
ast::ExprWhile(ref cond, ref body) => {
602+
ast::ExprWhile(ref cond, ref body, _) => {
603603
let repeating_scope = rcx.set_repeating_scope(cond.id);
604604
rcx.visit_expr(&**cond, ());
605605

src/libsyntax/ast.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,8 @@ pub enum Expr_ {
494494
ExprLit(Gc<Lit>),
495495
ExprCast(Gc<Expr>, P<Ty>),
496496
ExprIf(Gc<Expr>, P<Block>, Option<Gc<Expr>>),
497-
ExprWhile(Gc<Expr>, P<Block>),
497+
// FIXME #6993: change to Option<Name> ... or not, if these are hygienic.
498+
ExprWhile(Gc<Expr>, P<Block>, Option<Ident>),
498499
// FIXME #6993: change to Option<Name> ... or not, if these are hygienic.
499500
ExprForLoop(Gc<Pat>, Gc<Expr>, P<Block>, Option<Ident>),
500501
// Conditionless loop (can be exited with break, cont, or ret)

src/libsyntax/ext/expand.rs

+6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ fn expand_expr(e: Gc<ast::Expr>, fld: &mut MacroExpander) -> Gc<ast::Expr> {
6262
}
6363
}
6464

65+
ast::ExprWhile(cond, body, opt_ident) => {
66+
let cond = fld.fold_expr(cond);
67+
let (body, opt_ident) = expand_loop_block(body, opt_ident, fld);
68+
fld.cx.expr(e.span, ast::ExprWhile(cond, body, opt_ident))
69+
}
70+
6571
ast::ExprLoop(loop_block, opt_ident) => {
6672
let (loop_block, opt_ident) = expand_loop_block(loop_block, opt_ident, fld);
6773
fld.cx.expr(e.span, ast::ExprLoop(loop_block, opt_ident))

src/libsyntax/fold.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -932,18 +932,20 @@ pub fn noop_fold_expr<T: Folder>(e: Gc<Expr>, folder: &mut T) -> Gc<Expr> {
932932
folder.fold_block(tr),
933933
fl.map(|x| folder.fold_expr(x)))
934934
}
935-
ExprWhile(cond, body) => {
936-
ExprWhile(folder.fold_expr(cond), folder.fold_block(body))
935+
ExprWhile(cond, body, opt_ident) => {
936+
ExprWhile(folder.fold_expr(cond),
937+
folder.fold_block(body),
938+
opt_ident.map(|i| folder.fold_ident(i)))
937939
}
938-
ExprForLoop(pat, iter, body, ref maybe_ident) => {
940+
ExprForLoop(pat, iter, body, ref opt_ident) => {
939941
ExprForLoop(folder.fold_pat(pat),
940942
folder.fold_expr(iter),
941943
folder.fold_block(body),
942-
maybe_ident.map(|i| folder.fold_ident(i)))
944+
opt_ident.map(|i| folder.fold_ident(i)))
943945
}
944946
ExprLoop(body, opt_ident) => {
945947
ExprLoop(folder.fold_block(body),
946-
opt_ident.map(|x| folder.fold_ident(x)))
948+
opt_ident.map(|i| folder.fold_ident(i)))
947949
}
948950
ExprMatch(expr, ref arms) => {
949951
ExprMatch(folder.fold_expr(expr),

src/libsyntax/parse/parser.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1985,7 +1985,7 @@ impl<'a> Parser<'a> {
19851985
return self.parse_for_expr(None);
19861986
},
19871987
_ if self.eat_keyword(keywords::While) => {
1988-
return self.parse_while_expr();
1988+
return self.parse_while_expr(None);
19891989
},
19901990
_ if Parser::token_is_lifetime(&self.token) => {
19911991
let lifetime = self.get_lifetime();
@@ -1995,8 +1995,10 @@ impl<'a> Parser<'a> {
19951995
return self.parse_for_expr(Some(lifetime))
19961996
} else if self.eat_keyword(keywords::Loop) {
19971997
return self.parse_loop_expr(Some(lifetime))
1998+
} else if self.eat_keyword(keywords::While) {
1999+
return self.parse_while_expr(Some(lifetime))
19982000
} else {
1999-
self.fatal("expected `for` or `loop` after a label")
2001+
self.fatal("expected `for`, `loop`, or `while` after a label")
20002002
}
20012003
},
20022004
_ if self.eat_keyword(keywords::Loop) => {
@@ -2687,12 +2689,12 @@ impl<'a> Parser<'a> {
26872689
self.mk_expr(lo, hi, ExprForLoop(pat, expr, loop_block, opt_ident))
26882690
}
26892691

2690-
pub fn parse_while_expr(&mut self) -> Gc<Expr> {
2692+
pub fn parse_while_expr(&mut self, opt_ident: Option<ast::Ident>) -> Gc<Expr> {
26912693
let lo = self.last_span.lo;
26922694
let cond = self.parse_expr_res(RESTRICT_NO_STRUCT_LITERAL);
26932695
let body = self.parse_block();
26942696
let hi = body.span.hi;
2695-
return self.mk_expr(lo, hi, ExprWhile(cond, body));
2697+
return self.mk_expr(lo, hi, ExprWhile(cond, body, opt_ident));
26962698
}
26972699

26982700
pub fn parse_loop_expr(&mut self, opt_ident: Option<ast::Ident>) -> Gc<Expr> {

src/libsyntax/print/pprust.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,11 @@ impl<'a> State<'a> {
13671367
ast::ExprIf(ref test, ref blk, elseopt) => {
13681368
try!(self.print_if(&**test, &**blk, elseopt, false));
13691369
}
1370-
ast::ExprWhile(ref test, ref blk) => {
1370+
ast::ExprWhile(ref test, ref blk, opt_ident) => {
1371+
for ident in opt_ident.iter() {
1372+
try!(self.print_ident(*ident));
1373+
try!(self.word_space(":"));
1374+
}
13711375
try!(self.head("while"));
13721376
try!(self.print_expr(&**test));
13731377
try!(space(&mut self.s));

src/libsyntax/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ pub fn walk_expr<E: Clone, V: Visitor<E>>(visitor: &mut V, expression: &Expr, en
771771
visitor.visit_block(&**if_block, env.clone());
772772
walk_expr_opt(visitor, optional_else, env.clone())
773773
}
774-
ExprWhile(ref subexpression, ref block) => {
774+
ExprWhile(ref subexpression, ref block, _) => {
775775
visitor.visit_expr(&**subexpression, env.clone());
776776
visitor.visit_block(&**block, env.clone())
777777
}

src/test/run-pass/hygienic-labels-in-let.rs

+17
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ macro_rules! loop_x {
1919
}
2020
}
2121

22+
macro_rules! while_true {
23+
($e: expr) => {
24+
// $e shouldn't be able to interact with this 'x
25+
'x: while 1i + 1 == 2 { $e }
26+
}
27+
}
28+
2229
macro_rules! run_once {
2330
($e: expr) => {
2431
// ditto
@@ -49,6 +56,16 @@ pub fn main() {
4956
};
5057
assert_eq!(k, 1i);
5158

59+
let l: int = {
60+
'x: for _ in range(0i, 1) {
61+
// ditto
62+
while_true!(break 'x);
63+
i += 1;
64+
}
65+
i + 1
66+
};
67+
assert_eq!(l, 1i);
68+
5269
let n: int = {
5370
'x: for _ in range(0i, 1) {
5471
// ditto

src/test/run-pass/hygienic-labels.rs

+12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ macro_rules! run_once {
2424
}
2525
}
2626

27+
macro_rules! while_x {
28+
($e: expr) => {
29+
// ditto
30+
'x: while 1i + 1 == 2 { $e }
31+
}
32+
}
33+
2734
pub fn main() {
2835
'x: for _ in range(0i, 1) {
2936
// this 'x should refer to the outer loop, lexically
@@ -37,6 +44,11 @@ pub fn main() {
3744
fail!("break doesn't act hygienically inside infinite loop");
3845
}
3946

47+
'x: while 1i + 1 == 2 {
48+
while_x!(break 'x);
49+
fail!("break doesn't act hygienically inside infinite while loop");
50+
}
51+
4052
'x: for _ in range(0i, 1) {
4153
// ditto
4254
run_once!(continue 'x);

src/test/run-pass/labeled-break.rs

+6
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ pub fn main() {
2020
break 'bar;
2121
}
2222
}
23+
24+
'foobar: while 1i + 1 == 2 {
25+
loop {
26+
break 'foobar;
27+
}
28+
}
2329
}

src/test/run-pass/while-label.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2014 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+
12+
pub fn main() {
13+
let mut i = 100i;
14+
'w: while 1i + 1 == 2 {
15+
i -= 1;
16+
if i == 95 {
17+
break 'w;
18+
fail!("Should have broken out of loop");
19+
}
20+
}
21+
assert_eq!(i, 95);
22+
}

0 commit comments

Comments
 (0)