Skip to content

Commit 329fcd4

Browse files
committed
auto merge of #12338 : edwardw/rust/hygienic-break-continue, r=cmr
Makes labelled loops hygiene by performing renaming of the labels defined in e.g. `'x: loop { ... }` and then used in break and continue statements within loop body so that they act hygienically when used with macros. Closes #12262.
2 parents cbed332 + 386db05 commit 329fcd4

18 files changed

+262
-28
lines changed

src/librustc/middle/cfg/construct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ impl CFGBuilder {
490490

491491
fn find_scope(&self,
492492
expr: @ast::Expr,
493-
label: Option<ast::Name>) -> LoopScope {
493+
label: Option<ast::Ident>) -> LoopScope {
494494
match label {
495495
None => {
496496
return *self.loop_scopes.last().unwrap();

src/librustc/middle/dataflow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ impl<'a, O:DataFlowOperator> PropagationContext<'a, O> {
770770

771771
fn find_scope<'a>(&self,
772772
expr: &ast::Expr,
773-
label: Option<ast::Name>,
773+
label: Option<ast::Ident>,
774774
loop_scopes: &'a mut ~[LoopScope]) -> &'a mut LoopScope {
775775
let index = match label {
776776
None => {

src/librustc/middle/liveness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ impl Liveness {
747747
}
748748

749749
pub fn find_loop_scope(&self,
750-
opt_label: Option<Name>,
750+
opt_label: Option<Ident>,
751751
id: NodeId,
752752
sp: Span)
753753
-> NodeId {

src/librustc/middle/resolve.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -5206,13 +5206,13 @@ impl Resolver {
52065206
ExprLoop(_, Some(label)) => {
52075207
self.with_label_rib(|this| {
52085208
let def_like = DlDef(DefLabel(expr.id));
5209-
// plain insert (no renaming)
52105209
{
52115210
let mut label_ribs = this.label_ribs.borrow_mut();
52125211
let rib = label_ribs.get()[label_ribs.get().len() -
52135212
1];
52145213
let mut bindings = rib.bindings.borrow_mut();
5215-
bindings.get().insert(label.name, def_like);
5214+
let renamed = mtwt_resolve(label);
5215+
bindings.get().insert(renamed, def_like);
52165216
}
52175217

52185218
visit::walk_expr(this, expr, ());
@@ -5223,11 +5223,12 @@ impl Resolver {
52235223

52245224
ExprBreak(Some(label)) | ExprAgain(Some(label)) => {
52255225
let mut label_ribs = self.label_ribs.borrow_mut();
5226-
match self.search_ribs(label_ribs.get(), label, expr.span) {
5226+
let renamed = mtwt_resolve(label);
5227+
match self.search_ribs(label_ribs.get(), renamed, expr.span) {
52275228
None =>
52285229
self.resolve_error(expr.span,
52295230
format!("use of undeclared label `{}`",
5230-
token::get_name(label))),
5231+
token::get_ident(label))),
52315232
Some(DlDef(def @ DefLabel(_))) => {
52325233
// Since this def is a label, it is never read.
52335234
self.record_def(expr.id, (def, LastMod(AllPublic)))

src/librustc/middle/trans/controlflow.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use util::ppaux::Repr;
2525
use middle::trans::type_::Type;
2626

2727
use syntax::ast;
28-
use syntax::ast::Name;
28+
use syntax::ast::Ident;
2929
use syntax::ast_util;
3030
use syntax::codemap::Span;
3131
use syntax::parse::token::InternedString;
@@ -260,7 +260,7 @@ pub fn trans_loop<'a>(bcx:&'a Block<'a>,
260260

261261
pub fn trans_break_cont<'a>(bcx: &'a Block<'a>,
262262
expr_id: ast::NodeId,
263-
opt_label: Option<Name>,
263+
opt_label: Option<Ident>,
264264
exit: uint)
265265
-> &'a Block<'a> {
266266
let _icx = push_ctxt("trans_break_cont");
@@ -293,14 +293,14 @@ pub fn trans_break_cont<'a>(bcx: &'a Block<'a>,
293293

294294
pub fn trans_break<'a>(bcx: &'a Block<'a>,
295295
expr_id: ast::NodeId,
296-
label_opt: Option<Name>)
296+
label_opt: Option<Ident>)
297297
-> &'a Block<'a> {
298298
return trans_break_cont(bcx, expr_id, label_opt, cleanup::EXIT_BREAK);
299299
}
300300

301301
pub fn trans_cont<'a>(bcx: &'a Block<'a>,
302302
expr_id: ast::NodeId,
303-
label_opt: Option<Name>)
303+
label_opt: Option<Ident>)
304304
-> &'a Block<'a> {
305305
return trans_break_cont(bcx, expr_id, label_opt, cleanup::EXIT_LOOP);
306306
}

src/libsyntax/ast.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,13 @@ impl Eq for Ident {
5858
// if it should be non-hygienic (most things are), just compare the
5959
// 'name' fields of the idents. Or, even better, replace the idents
6060
// with Name's.
61-
fail!("not allowed to compare these idents: {:?}, {:?}.
62-
Probably related to issue \\#6993", self, other);
61+
//
62+
// On the other hand, if the comparison does need to be hygienic,
63+
// one example and its non-hygienic counterpart would be:
64+
// syntax::parse::token::mtwt_token_eq
65+
// syntax::ext::tt::macro_parser::token_name_eq
66+
fail!("not allowed to compare these idents: {:?}, {:?}. \
67+
Probably related to issue \\#6993", self, other);
6368
}
6469
}
6570
fn ne(&self, other: &Ident) -> bool {
@@ -564,8 +569,8 @@ pub enum Expr_ {
564569
ExprPath(Path),
565570

566571
ExprAddrOf(Mutability, @Expr),
567-
ExprBreak(Option<Name>),
568-
ExprAgain(Option<Name>),
572+
ExprBreak(Option<Ident>),
573+
ExprAgain(Option<Ident>),
569574
ExprRet(Option<@Expr>),
570575

571576
/// Gets the log level for the enclosing module

src/libsyntax/ext/expand.rs

+29-2
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ pub fn expand_expr(e: @ast::Expr, fld: &mut MacroExpander) -> @ast::Expr {
139139
// Expand any interior macros etc.
140140
// NB: we don't fold pats yet. Curious.
141141
let src_expr = fld.fold_expr(src_expr).clone();
142+
// Rename label before expansion.
143+
let (opt_ident, src_loop_block) = rename_loop_label(opt_ident, src_loop_block, fld);
142144
let src_loop_block = fld.fold_block(src_loop_block);
143145

144146
let span = e.span;
@@ -165,8 +167,7 @@ pub fn expand_expr(e: @ast::Expr, fld: &mut MacroExpander) -> @ast::Expr {
165167

166168
// `None => break ['<ident>];`
167169
let none_arm = {
168-
// FIXME #6993: this map goes away:
169-
let break_expr = fld.cx.expr(span, ast::ExprBreak(opt_ident.map(|x| x.name)));
170+
let break_expr = fld.cx.expr(span, ast::ExprBreak(opt_ident));
170171
let none_pat = fld.cx.pat_ident(span, none_ident);
171172
fld.cx.arm(span, ~[none_pat], break_expr)
172173
};
@@ -199,10 +200,36 @@ pub fn expand_expr(e: @ast::Expr, fld: &mut MacroExpander) -> @ast::Expr {
199200
fld.cx.expr_match(span, discrim, ~[arm])
200201
}
201202

203+
ast::ExprLoop(loop_block, opt_ident) => {
204+
let (opt_ident, loop_block) =
205+
rename_loop_label(opt_ident, loop_block, fld);
206+
let loop_block = fld.fold_block(loop_block);
207+
fld.cx.expr(e.span, ast::ExprLoop(loop_block, opt_ident))
208+
}
209+
202210
_ => noop_fold_expr(e, fld)
203211
}
204212
}
205213

214+
// Rename loop label and its all occurrences inside the loop body
215+
fn rename_loop_label(opt_ident: Option<Ident>,
216+
loop_block: P<Block>,
217+
fld: &mut MacroExpander) -> (Option<Ident>, P<Block>) {
218+
match opt_ident {
219+
Some(label) => {
220+
// Generate fresh label and add to the existing pending renames
221+
let new_label = fresh_name(&label);
222+
let rename = (label, new_label);
223+
fld.extsbox.info().pending_renames.push(rename);
224+
let mut pending_renames = ~[rename];
225+
let mut rename_fld = renames_to_fold(&mut pending_renames);
226+
(Some(rename_fld.fold_ident(label)),
227+
rename_fld.fold_block(loop_block))
228+
}
229+
None => (None, loop_block)
230+
}
231+
}
232+
206233
// eval $e with a new exts frame:
207234
macro_rules! with_exts_frame (
208235
($extsboxexpr:expr,$macros_escape:expr,$e:expr) =>

src/libsyntax/ext/tt/macro_parser.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,9 @@ pub fn parse_or_else<R: Reader>(sess: @ParseSess,
218218
// perform a token equality check, ignoring syntax context (that is, an unhygienic comparison)
219219
pub fn token_name_eq(t1 : &Token, t2 : &Token) -> bool {
220220
match (t1,t2) {
221-
(&token::IDENT(id1,_),&token::IDENT(id2,_)) =>
222-
id1.name == id2.name,
221+
(&token::IDENT(id1,_),&token::IDENT(id2,_))
222+
| (&token::LIFETIME(id1),&token::LIFETIME(id2)) =>
223+
id1.name == id2.name,
223224
_ => *t1 == *t2
224225
}
225226
}

src/libsyntax/fold.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,23 @@ fn fold_arg_<T: Folder>(a: &Arg, fld: &mut T) -> Arg {
353353

354354
// build a new vector of tts by appling the Folder's fold_ident to
355355
// all of the identifiers in the token trees.
356+
//
357+
// This is part of hygiene magic. As far as hygiene is concerned, there
358+
// are three types of let pattern bindings or loop labels:
359+
// - those defined and used in non-macro part of the program
360+
// - those used as part of macro invocation arguments
361+
// - those defined and used inside macro definitions
362+
// Lexically, type 1 and 2 are in one group and type 3 the other. If they
363+
// clash, in order for let and loop label to work hygienically, one group
364+
// or the other needs to be renamed. The problem is that type 2 and 3 are
365+
// parsed together (inside the macro expand function). After being parsed and
366+
// AST being constructed, they can no longer be distinguished from each other.
367+
//
368+
// For that reason, type 2 let bindings and loop labels are actually renamed
369+
// in the form of tokens instead of AST nodes, here. There are wasted effort
370+
// since many token::IDENT are not necessary part of let bindings and most
371+
// token::LIFETIME are certainly not loop labels. But we can't tell in their
372+
// token form. So this is less ideal and hacky but it works.
356373
pub fn fold_tts<T: Folder>(tts: &[TokenTree], fld: &mut T) -> ~[TokenTree] {
357374
tts.map(|tt| {
358375
match *tt {
@@ -376,6 +393,7 @@ fn maybe_fold_ident<T: Folder>(t: &token::Token, fld: &mut T) -> token::Token {
376393
token::IDENT(id, followed_by_colons) => {
377394
token::IDENT(fld.fold_ident(id), followed_by_colons)
378395
}
396+
token::LIFETIME(id) => token::LIFETIME(fld.fold_ident(id)),
379397
_ => (*t).clone()
380398
}
381399
}
@@ -802,8 +820,8 @@ pub fn noop_fold_expr<T: Folder>(e: @Expr, folder: &mut T) -> @Expr {
802820
}
803821
ExprPath(ref pth) => ExprPath(folder.fold_path(pth)),
804822
ExprLogLevel => ExprLogLevel,
805-
ExprBreak(opt_ident) => ExprBreak(opt_ident),
806-
ExprAgain(opt_ident) => ExprAgain(opt_ident),
823+
ExprBreak(opt_ident) => ExprBreak(opt_ident.map(|x| folder.fold_ident(x))),
824+
ExprAgain(opt_ident) => ExprAgain(opt_ident.map(|x| folder.fold_ident(x))),
807825
ExprRet(ref e) => {
808826
ExprRet(e.map(|x| folder.fold_expr(x)))
809827
}

src/libsyntax/parse/parser.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1822,7 +1822,7 @@ impl Parser {
18221822
let ex = if Parser::token_is_lifetime(&self.token) {
18231823
let lifetime = self.get_lifetime();
18241824
self.bump();
1825-
ExprAgain(Some(lifetime.name))
1825+
ExprAgain(Some(lifetime))
18261826
} else {
18271827
ExprAgain(None)
18281828
};
@@ -1885,7 +1885,7 @@ impl Parser {
18851885
if Parser::token_is_lifetime(&self.token) {
18861886
let lifetime = self.get_lifetime();
18871887
self.bump();
1888-
ex = ExprBreak(Some(lifetime.name));
1888+
ex = ExprBreak(Some(lifetime));
18891889
} else {
18901890
ex = ExprBreak(None);
18911891
}
@@ -2579,7 +2579,7 @@ impl Parser {
25792579
let ex = if Parser::token_is_lifetime(&self.token) {
25802580
let lifetime = self.get_lifetime();
25812581
self.bump();
2582-
ExprAgain(Some(lifetime.name))
2582+
ExprAgain(Some(lifetime))
25832583
} else {
25842584
ExprAgain(None)
25852585
};

src/libsyntax/parse/token.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -704,8 +704,8 @@ pub fn is_reserved_keyword(tok: &Token) -> bool {
704704

705705
pub fn mtwt_token_eq(t1 : &Token, t2 : &Token) -> bool {
706706
match (t1,t2) {
707-
(&IDENT(id1,_),&IDENT(id2,_)) =>
708-
ast_util::mtwt_resolve(id1) == ast_util::mtwt_resolve(id2),
707+
(&IDENT(id1,_),&IDENT(id2,_)) | (&LIFETIME(id1),&LIFETIME(id2)) =>
708+
ast_util::mtwt_resolve(id1) == ast_util::mtwt_resolve(id2),
709709
_ => *t1 == *t2
710710
}
711711
}

src/libsyntax/print/pprust.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,7 @@ pub fn print_expr(s: &mut State, expr: &ast::Expr) -> io::IoResult<()> {
14711471
try!(space(&mut s.s));
14721472
for ident in opt_ident.iter() {
14731473
try!(word(&mut s.s, "'"));
1474-
try!(print_name(s, *ident));
1474+
try!(print_ident(s, *ident));
14751475
try!(space(&mut s.s));
14761476
}
14771477
}
@@ -1480,7 +1480,7 @@ pub fn print_expr(s: &mut State, expr: &ast::Expr) -> io::IoResult<()> {
14801480
try!(space(&mut s.s));
14811481
for ident in opt_ident.iter() {
14821482
try!(word(&mut s.s, "'"));
1483-
try!(print_name(s, *ident));
1483+
try!(print_ident(s, *ident));
14841484
try!(space(&mut s.s))
14851485
}
14861486
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2012-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+
#[feature(macro_rules)];
12+
13+
macro_rules! foo {
14+
() => { break 'x; }
15+
}
16+
17+
pub fn main() {
18+
'x: loop { foo!() } //~ ERROR use of undeclared label `x`
19+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2012-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+
#[feature(macro_rules)];
12+
13+
macro_rules! foo {
14+
($e: expr) => { 'x: loop { $e } }
15+
}
16+
17+
pub fn main() {
18+
foo!(break 'x); //~ ERROR use of undeclared label `x`
19+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2012-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+
#[feature(macro_rules)];
12+
13+
macro_rules! foo {
14+
() => { break 'x; }
15+
}
16+
17+
pub fn main() {
18+
'x: for _ in range(0,1) {
19+
foo!() //~ ERROR use of undeclared label `x`
20+
};
21+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2012-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+
#[feature(macro_rules)];
12+
13+
macro_rules! foo {
14+
($e: expr) => { 'x: for _ in range(0,1) { $e } }
15+
}
16+
17+
pub fn main() {
18+
foo!(break 'x); //~ ERROR use of undeclared label `x`
19+
}

0 commit comments

Comments
 (0)