Skip to content

Commit a9ac272

Browse files
committed
auto merge of #9103 : jbclements/rust/let-var-hygiene, r=erickt
update AST so that ExprBreak and ExprCont expressions contain names, not idents. Fixes #9047 and makes progress on #6993. Simplifies the compiler very slightly, should make it (infinitesimally) faster.
2 parents 917d3c2 + 969181b commit a9ac272

File tree

14 files changed

+56
-27
lines changed

14 files changed

+56
-27
lines changed

src/librustc/middle/cfg/construct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ impl CFGBuilder {
488488

489489
fn find_scope(&self,
490490
expr: @ast::Expr,
491-
label: Option<ast::Ident>) -> LoopScope {
491+
label: Option<ast::Name>) -> LoopScope {
492492
match label {
493493
None => {
494494
return *self.loop_scopes.last();

src/librustc/middle/dataflow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ impl<'self, O:DataFlowOperator> PropagationContext<'self, O> {
867867

868868
fn find_scope<'a>(&self,
869869
expr: @ast::Expr,
870-
label: Option<ast::Ident>,
870+
label: Option<ast::Name>,
871871
loop_scopes: &'a mut ~[LoopScope]) -> &'a mut LoopScope {
872872
let index = match label {
873873
None => {

src/librustc/middle/liveness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ impl Liveness {
756756
}
757757

758758
pub fn find_loop_scope(&self,
759-
opt_label: Option<Ident>,
759+
opt_label: Option<Name>,
760760
id: NodeId,
761761
sp: Span)
762762
-> NodeId {

src/librustc/middle/resolve.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -5157,15 +5157,13 @@ impl Resolver {
51575157
ExprForLoop(*) => fail!("non-desugared expr_for_loop"),
51585158

51595159
ExprBreak(Some(label)) | ExprAgain(Some(label)) => {
5160-
let name = label.name;
5161-
match self.search_ribs(self.label_ribs, name, expr.span,
5160+
match self.search_ribs(self.label_ribs, label, expr.span,
51625161
DontAllowCapturingSelf) {
51635162
None =>
51645163
self.resolve_error(expr.span,
51655164
fmt!("use of undeclared label \
51665165
`%s`",
5167-
self.session.str_of(
5168-
label))),
5166+
interner_get(label))),
51695167
Some(DlDef(def @ DefLabel(_))) => {
51705168
self.record_def(expr.id, def)
51715169
}

src/librustc/middle/trans/base.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ use std::vec;
7676
use std::local_data;
7777
use extra::time;
7878
use extra::sort;
79-
use syntax::ast::Ident;
79+
use syntax::ast::Name;
8080
use syntax::ast_map::{path, path_elt_to_str, path_name, path_pretty_name};
8181
use syntax::ast_util::{local_def};
8282
use syntax::attr;
@@ -1189,7 +1189,7 @@ pub fn scope_block(bcx: @mut Block,
11891189

11901190
pub fn loop_scope_block(bcx: @mut Block,
11911191
loop_break: @mut Block,
1192-
loop_label: Option<Ident>,
1192+
loop_label: Option<Name>,
11931193
n: &str,
11941194
opt_node_info: Option<NodeInfo>) -> @mut Block {
11951195
return new_block(bcx.fcx, Some(bcx), Some(@mut ScopeInfo {

src/librustc/middle/trans/common.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use std::cast;
3838
use std::hashmap::{HashMap};
3939
use std::libc::{c_uint, c_longlong, c_ulonglong, c_char};
4040
use std::vec;
41-
use syntax::ast::Ident;
41+
use syntax::ast::{Name,Ident};
4242
use syntax::ast_map::{path, path_elt, path_pretty_name};
4343
use syntax::codemap::Span;
4444
use syntax::parse::token;
@@ -463,7 +463,7 @@ pub fn block_cleanups(bcx: @mut Block) -> ~[cleanup] {
463463
pub struct ScopeInfo {
464464
parent: Option<@mut ScopeInfo>,
465465
loop_break: Option<@mut Block>,
466-
loop_label: Option<Ident>,
466+
loop_label: Option<Name>,
467467
// A list of functions that must be run at when leaving this
468468
// block, cleaning up any variables that were introduced in the
469469
// block.

src/librustc/middle/trans/controlflow.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use util::ppaux;
2222
use middle::trans::type_::Type;
2323

2424
use syntax::ast;
25-
use syntax::ast::Ident;
25+
use syntax::ast::Name;
2626
use syntax::ast_util;
2727
use syntax::codemap::Span;
2828

@@ -188,7 +188,7 @@ pub fn trans_while(bcx: @mut Block, cond: @ast::Expr, body: &ast::Block) -> @mut
188188

189189
pub fn trans_loop(bcx:@mut Block,
190190
body: &ast::Block,
191-
opt_label: Option<Ident>)
191+
opt_label: Option<Name>)
192192
-> @mut Block {
193193
let _icx = push_ctxt("trans_loop");
194194
let next_bcx = sub_block(bcx, "next");
@@ -201,7 +201,7 @@ pub fn trans_loop(bcx:@mut Block,
201201
}
202202

203203
pub fn trans_break_cont(bcx: @mut Block,
204-
opt_label: Option<Ident>,
204+
opt_label: Option<Name>,
205205
to_end: bool)
206206
-> @mut Block {
207207
let _icx = push_ctxt("trans_break_cont");
@@ -254,11 +254,11 @@ pub fn trans_break_cont(bcx: @mut Block,
254254
return bcx;
255255
}
256256

257-
pub fn trans_break(bcx: @mut Block, label_opt: Option<Ident>) -> @mut Block {
257+
pub fn trans_break(bcx: @mut Block, label_opt: Option<Name>) -> @mut Block {
258258
return trans_break_cont(bcx, label_opt, true);
259259
}
260260

261-
pub fn trans_cont(bcx: @mut Block, label_opt: Option<Ident>) -> @mut Block {
261+
pub fn trans_cont(bcx: @mut Block, label_opt: Option<Name>) -> @mut Block {
262262
return trans_break_cont(bcx, label_opt, false);
263263
}
264264

src/librustc/middle/trans/expr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,8 @@ fn trans_rvalue_stmt_unadjusted(bcx: @mut Block, expr: @ast::Expr) -> @mut Block
617617
return controlflow::trans_while(bcx, cond, body);
618618
}
619619
ast::ExprLoop(ref body, opt_label) => {
620-
return controlflow::trans_loop(bcx, body, opt_label);
620+
// FIXME #6993: map can go away when ast.rs is changed
621+
return controlflow::trans_loop(bcx, body, opt_label.map(|x| x.name));
621622
}
622623
ast::ExprAssign(dst, src) => {
623624
let src_datum = unpack_datum!(

src/libsyntax/ast.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -522,10 +522,12 @@ pub enum Expr_ {
522522
ExprCast(@Expr, Ty),
523523
ExprIf(@Expr, Block, Option<@Expr>),
524524
ExprWhile(@Expr, Block),
525+
// FIXME #6993: change to Option<Name>
525526
ExprForLoop(@Pat, @Expr, Block, Option<Ident>),
526527
/* Conditionless loop (can be exited with break, cont, or ret)
527528
Same semantics as while(true) { body }, but typestate knows that the
528529
(implicit) condition is always true. */
530+
// FIXME #6993: change to Option<Name>
529531
ExprLoop(Block, Option<Ident>),
530532
ExprMatch(@Expr, ~[Arm]),
531533
ExprFnBlock(fn_decl, Block),
@@ -541,8 +543,8 @@ pub enum Expr_ {
541543
/// The special identifier `self`.
542544
ExprSelf,
543545
ExprAddrOf(Mutability, @Expr),
544-
ExprBreak(Option<Ident>),
545-
ExprAgain(Option<Ident>),
546+
ExprBreak(Option<Name>),
547+
ExprAgain(Option<Name>),
546548
ExprRet(Option<@Expr>),
547549

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

src/libsyntax/ext/expand.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ pub fn expand_expr(extsbox: @mut SyntaxEnv,
113113

114114
// Desugar expr_for_loop
115115
// From: `['<ident>:] for <src_pat> in <src_expr> <src_loop_block>`
116+
// FIXME #6993 : change type of opt_ident to Option<Name>
116117
ast::ExprForLoop(src_pat, src_expr, ref src_loop_block, opt_ident) => {
117118
// Expand any interior macros etc.
118119
// NB: we don't fold pats yet. Curious.
@@ -144,7 +145,8 @@ pub fn expand_expr(extsbox: @mut SyntaxEnv,
144145

145146
// `None => break ['<ident>];`
146147
let none_arm = {
147-
let break_expr = cx.expr(span, ast::ExprBreak(opt_ident));
148+
// FIXME #6993: this map goes away:
149+
let break_expr = cx.expr(span, ast::ExprBreak(opt_ident.map(|x| x.name)));
148150
let none_pat = cx.pat_ident(span, none_ident);
149151
cx.arm(span, ~[none_pat], break_expr)
150152
};

src/libsyntax/fold.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -688,10 +688,13 @@ pub fn noop_fold_expr(e: &Expr_, fld: @ast_fold) -> Expr_ {
688688
ExprPath(ref pth) => ExprPath(fld.fold_path(pth)),
689689
ExprSelf => ExprSelf,
690690
ExprBreak(ref opt_ident) => {
691-
ExprBreak(opt_ident.map_move(|x| fld.fold_ident(x)))
691+
// FIXME #6993: add fold_name to fold.... then cut out the
692+
// bogus Name->Ident->Name conversion.
693+
ExprBreak(opt_ident.map_move(|x| fld.fold_ident(Ident::new(x)).name))
692694
}
693695
ExprAgain(ref opt_ident) => {
694-
ExprAgain(opt_ident.map_move(|x| fld.fold_ident(x)))
696+
// FIXME #6993: add fold_name to fold....
697+
ExprAgain(opt_ident.map_move(|x| fld.fold_ident(Ident::new(x)).name))
695698
}
696699
ExprRet(ref e) => {
697700
ExprRet(e.map_move(|x| fld.fold_expr(x)))

src/libsyntax/parse/parser.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1849,7 +1849,7 @@ impl Parser {
18491849
if self.token_is_lifetime(&*self.token) {
18501850
let lifetime = self.get_lifetime(&*self.token);
18511851
self.bump();
1852-
ex = ExprBreak(Some(lifetime));
1852+
ex = ExprBreak(Some(lifetime.name));
18531853
} else {
18541854
ex = ExprBreak(None);
18551855
}
@@ -2585,7 +2585,7 @@ impl Parser {
25852585
let ex = if self.token_is_lifetime(&*self.token) {
25862586
let lifetime = self.get_lifetime(&*self.token);
25872587
self.bump();
2588-
ExprAgain(Some(lifetime))
2588+
ExprAgain(Some(lifetime.name))
25892589
} else {
25902590
ExprAgain(None)
25912591
};

src/libsyntax/print/pprust.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use codemap::{CodeMap, BytePos};
1919
use codemap;
2020
use diagnostic;
2121
use parse::classify::expr_is_simple_block;
22-
use parse::token::{ident_interner, ident_to_str};
22+
use parse::token::{ident_interner, ident_to_str, interner_get};
2323
use parse::{comments, token};
2424
use parse;
2525
use print::pp::{break_offset, word, space, zerobreak, hardbreak};
@@ -1394,7 +1394,7 @@ pub fn print_expr(s: @ps, expr: &ast::Expr) {
13941394
space(s.s);
13951395
for ident in opt_ident.iter() {
13961396
word(s.s, "'");
1397-
print_ident(s, *ident);
1397+
print_name(s, *ident);
13981398
space(s.s);
13991399
}
14001400
}
@@ -1403,7 +1403,7 @@ pub fn print_expr(s: @ps, expr: &ast::Expr) {
14031403
space(s.s);
14041404
for ident in opt_ident.iter() {
14051405
word(s.s, "'");
1406-
print_ident(s, *ident);
1406+
print_name(s, *ident);
14071407
space(s.s)
14081408
}
14091409
}
@@ -1503,6 +1503,10 @@ pub fn print_ident(s: @ps, ident: ast::Ident) {
15031503
word(s.s, ident_to_str(&ident));
15041504
}
15051505

1506+
pub fn print_name(s: @ps, name: ast::Name) {
1507+
word(s.s, interner_get(name));
1508+
}
1509+
15061510
pub fn print_for_decl(s: @ps, loc: &ast::Local, coll: &ast::Expr) {
15071511
print_local_decl(s, loc);
15081512
space(s.s);

src/test/run-pass/issue-9047.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2013 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+
fn decode() -> ~str {
12+
'outer: loop {
13+
let mut ch_start: uint;
14+
break 'outer;
15+
}
16+
~""
17+
}
18+
19+
fn main() {}

0 commit comments

Comments
 (0)