Skip to content

Commit 2e88c36

Browse files
committed
Auto merge of #28642 - petrochenkov:name3, r=nrc
This PR removes random remaining `Ident`s outside of libsyntax and performs general cleanup In particular, interfaces of `Name` and `Ident` are tidied up, `Name`s and `Ident`s being small `Copy` aggregates are always passed to functions by value, and `Ident`s are never used as keys in maps, because `Ident` comparisons are tricky. Although this PR closes #6993 there's still work related to it: - `Name` can be made `NonZero` to compress numerous `Option<Name>`s and `Option<Ident>`s but it requires const unsafe functions. - Implementation of `PartialEq` on `Ident` should be eliminated and replaced with explicit hygienic, non-hygienic or member-wise comparisons. - Finally, large parts of AST can potentially be converted to `Name`s in the same way as HIR to clearly separate identifiers used in hygienic and non-hygienic contexts. r? @nrc
2 parents 9169e6c + f284cbc commit 2e88c36

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+335
-399
lines changed

src/grammar/verify.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use syntax::parse::lexer::TokenAndSpan;
3535

3636
fn parse_token_list(file: &str) -> HashMap<String, token::Token> {
3737
fn id() -> token::Token {
38-
token::Ident(ast::Ident { name: Name(0), ctxt: 0, }, token::Plain)
38+
token::Ident(ast::Ident::with_empty_ctxt(Name(0))), token::Plain)
3939
}
4040

4141
let mut res = HashMap::new();
@@ -75,7 +75,7 @@ fn parse_token_list(file: &str) -> HashMap<String, token::Token> {
7575
"RPAREN" => token::CloseDelim(token::Paren),
7676
"SLASH" => token::BinOp(token::Slash),
7777
"COMMA" => token::Comma,
78-
"LIFETIME" => token::Lifetime(ast::Ident { name: Name(0), ctxt: 0 }),
78+
"LIFETIME" => token::Lifetime(ast::Ident::with_empty_ctxt(Name(0))),
7979
"CARET" => token::BinOp(token::Caret),
8080
"TILDE" => token::Tilde,
8181
"IDENT" => id(),
@@ -208,9 +208,9 @@ fn parse_antlr_token(s: &str, tokens: &HashMap<String, token::Token>, surrogate_
208208
token::Literal(token::ByteStr(..), n) => token::Literal(token::ByteStr(nm), n),
209209
token::Literal(token::ByteStrRaw(..), n) => token::Literal(token::ByteStrRaw(fix(content),
210210
count(content)), n),
211-
token::Ident(..) => token::Ident(ast::Ident { name: nm, ctxt: 0 },
211+
token::Ident(..) => token::Ident(ast::Ident::with_empty_ctxt(nm)),
212212
token::ModName),
213-
token::Lifetime(..) => token::Lifetime(ast::Ident { name: nm, ctxt: 0 }),
213+
token::Lifetime(..) => token::Lifetime(ast::Ident::with_empty_ctxt(nm)),
214214
ref t => t.clone()
215215
};
216216

src/librustc/metadata/creader.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ impl<'a> CrateReader<'a> {
482482
let span = mk_sp(lo, p.last_span.hi);
483483
p.abort_if_errors();
484484
macros.push(ast::MacroDef {
485-
ident: name.ident(),
485+
ident: ast::Ident::with_empty_ctxt(name),
486486
attrs: attrs,
487487
id: ast::DUMMY_NODE_ID,
488488
span: span,

src/librustc/metadata/encoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,9 @@ fn encode_info_for_mod(ecx: &EncodeContext,
520520
});
521521

522522
if let hir::ItemImpl(..) = item.node {
523-
let (ident, did) = (item.name, item.id);
523+
let (name, did) = (item.name, item.id);
524524
debug!("(encoding info for module) ... encoding impl {} ({}/{})",
525-
ident,
525+
name,
526526
did, ecx.tcx.map.node_to_string(did));
527527

528528
rbml_w.wr_tagged_u64(tag_mod_impl, def_to_u64(DefId::local(did)));

src/librustc/metadata/tydecode.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ impl<'a,'tcx> TyDecoder<'a,'tcx> {
184184
}
185185
'[' => {
186186
let def = self.parse_def(RegionParameter);
187-
let ident = token::str_to_ident(&self.parse_str(']'));
188-
ty::BrNamed(def, ident.name)
187+
let name = token::intern(&self.parse_str(']'));
188+
ty::BrNamed(def, name)
189189
}
190190
'f' => {
191191
let id = self.parse_u32();
@@ -219,12 +219,12 @@ impl<'a,'tcx> TyDecoder<'a,'tcx> {
219219
assert_eq!(self.next(), '|');
220220
let index = self.parse_u32();
221221
assert_eq!(self.next(), '|');
222-
let nm = token::str_to_ident(&self.parse_str(']'));
222+
let name = token::intern(&self.parse_str(']'));
223223
ty::ReEarlyBound(ty::EarlyBoundRegion {
224224
param_id: node_id,
225225
space: space,
226226
index: index,
227-
name: nm.name
227+
name: name
228228
})
229229
}
230230
'f' => {
@@ -598,7 +598,7 @@ impl<'a,'tcx> TyDecoder<'a,'tcx> {
598598
ty::ProjectionPredicate {
599599
projection_ty: ty::ProjectionTy {
600600
trait_ref: self.parse_trait_ref(),
601-
item_name: token::str_to_ident(&self.parse_str('|')).name,
601+
item_name: token::intern(&self.parse_str('|')),
602602
},
603603
ty: self.parse_ty(),
604604
}

src/librustc/middle/cfg/construct.rs

+3-3
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.map(|l| l.node));
287+
let loop_scope = self.find_scope(expr, label.map(|l| l.node.name));
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.map(|l| l.node));
295+
let loop_scope = self.find_scope(expr, label.map(|l| l.node.name));
296296
let a = self.add_ast_node(expr.id, &[pred]);
297297
self.add_exiting_edge(expr, a,
298298
loop_scope, loop_scope.continue_index);
@@ -585,7 +585,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
585585

586586
fn find_scope(&self,
587587
expr: &hir::Expr,
588-
label: Option<ast::Ident>) -> LoopScope {
588+
label: Option<ast::Name>) -> LoopScope {
589589
if label.is_none() {
590590
return *self.loop_scopes.last().unwrap();
591591
}

src/librustc/middle/dataflow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl<'a, 'tcx, O:DataFlowOperator> pprust::PpAnn for DataFlowContext<'a, 'tcx, O
114114
ps: &mut pprust::State,
115115
node: pprust::AnnNode) -> io::Result<()> {
116116
let id = match node {
117-
pprust::NodeIdent(_) | pprust::NodeName(_) => 0,
117+
pprust::NodeName(_) => 0,
118118
pprust::NodeExpr(expr) => expr.id,
119119
pprust::NodeBlock(blk) => blk.id,
120120
pprust::NodeItem(_) | pprust::NodeSubItem(_) => 0,

src/librustc/middle/entry.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn entry_point_type(item: &Item, depth: usize) -> EntryPointType {
8585
EntryPointType::Start
8686
} else if attr::contains_name(&item.attrs, "main") {
8787
EntryPointType::MainAttr
88-
} else if item.name == "main" {
88+
} else if item.name.as_str() == "main" {
8989
if depth == 1 {
9090
// This is a top-level function so can be 'main'
9191
EntryPointType::MainNamed

src/librustc/middle/infer/error_reporting.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1015,12 +1015,12 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
10151015
},
10161016
None => None
10171017
};
1018-
let (fn_decl, generics, unsafety, constness, ident, expl_self, span)
1018+
let (fn_decl, generics, unsafety, constness, name, expl_self, span)
10191019
= node_inner.expect("expect item fn");
10201020
let rebuilder = Rebuilder::new(self.tcx, fn_decl, expl_self,
10211021
generics, same_regions, &life_giver);
10221022
let (fn_decl, expl_self, generics) = rebuilder.rebuild();
1023-
self.give_expl_lifetime_param(&fn_decl, unsafety, constness, ident,
1023+
self.give_expl_lifetime_param(&fn_decl, unsafety, constness, name,
10241024
expl_self.as_ref(), &generics, span);
10251025
}
10261026
}
@@ -1127,7 +1127,7 @@ impl<'a, 'tcx> Rebuilder<'a, 'tcx> {
11271127
names.push(lt_name);
11281128
}
11291129
names.sort();
1130-
let name = token::str_to_ident(&names[0]).name;
1130+
let name = token::intern(&names[0]);
11311131
return (name_to_dummy_lifetime(name), Kept);
11321132
}
11331133
return (self.life_giver.give_lifetime(), Fresh);
@@ -1938,8 +1938,7 @@ impl LifeGiver {
19381938
let mut s = String::from("'");
19391939
s.push_str(&num_to_string(self.counter.get()));
19401940
if !self.taken.contains(&s) {
1941-
lifetime = name_to_dummy_lifetime(
1942-
token::str_to_ident(&s[..]).name);
1941+
lifetime = name_to_dummy_lifetime(token::intern(&s[..]));
19431942
self.generated.borrow_mut().push(lifetime);
19441943
break;
19451944
}

src/librustc/middle/intrinsicck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<'a, 'tcx> IntrinsicCheckingVisitor<'a, 'tcx> {
5555
ty::TyBareFn(_, ref bfty) => bfty.abi == RustIntrinsic,
5656
_ => return false
5757
};
58-
intrinsic && self.tcx.item_name(def_id) == "transmute"
58+
intrinsic && self.tcx.item_name(def_id).as_str() == "transmute"
5959
}
6060

6161
fn check_transmute(&self, span: Span, from: Ty<'tcx>, to: Ty<'tcx>, id: ast::NodeId) {

src/librustc/middle/liveness.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ fn visit_fn(ir: &mut IrMaps,
383383
&*arg.pat,
384384
|_bm, arg_id, _x, path1| {
385385
debug!("adding argument {}", arg_id);
386-
let name = path1.node.name;
386+
let name = path1.node;
387387
fn_maps.add_variable(Arg(arg_id, name));
388388
})
389389
};
@@ -416,7 +416,7 @@ fn visit_fn(ir: &mut IrMaps,
416416
fn visit_local(ir: &mut IrMaps, local: &hir::Local) {
417417
pat_util::pat_bindings(&ir.tcx.def_map, &*local.pat, |_, p_id, sp, path1| {
418418
debug!("adding local variable {}", p_id);
419-
let name = path1.node.name;
419+
let name = path1.node;
420420
ir.add_live_node_for_node(p_id, VarDefNode(sp));
421421
ir.add_variable(Local(LocalInfo {
422422
id: p_id,
@@ -431,7 +431,7 @@ fn visit_arm(ir: &mut IrMaps, arm: &hir::Arm) {
431431
pat_util::pat_bindings(&ir.tcx.def_map, &**pat, |bm, p_id, sp, path1| {
432432
debug!("adding local variable {} from match with bm {:?}",
433433
p_id, bm);
434-
let name = path1.node.name;
434+
let name = path1.node;
435435
ir.add_live_node_for_node(p_id, VarDefNode(sp));
436436
ir.add_variable(Local(LocalInfo {
437437
id: p_id,
@@ -688,7 +688,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
688688
}
689689

690690
fn find_loop_scope(&self,
691-
opt_label: Option<ast::Ident>,
691+
opt_label: Option<ast::Name>,
692692
id: NodeId,
693693
sp: Span)
694694
-> NodeId {
@@ -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.map(|l| l.node), expr.id, expr.span);
1052+
let sc = self.find_loop_scope(opt_label.map(|l| l.node.name), 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.map(|l| l.node), expr.id, expr.span);
1066+
let sc = self.find_loop_scope(opt_label.map(|l| l.node.name), 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
@@ -1553,8 +1553,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
15531553
|_bm, p_id, sp, path1| {
15541554
let var = self.variable(p_id, sp);
15551555
// Ignore unused self.
1556-
let ident = path1.node;
1557-
if ident.name != special_idents::self_.name {
1556+
let name = path1.node;
1557+
if name != special_idents::self_.name {
15581558
self.warn_about_unused(sp, p_id, entry_ln, var);
15591559
}
15601560
})

src/librustc/middle/pat_util.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ use util::nodemap::FnvHashMap;
1616
use syntax::ast;
1717
use rustc_front::hir;
1818
use rustc_front::util::walk_pat;
19-
use syntax::codemap::{Span, Spanned, DUMMY_SP};
19+
use syntax::codemap::{respan, Span, Spanned, DUMMY_SP};
2020

21-
pub type PatIdMap = FnvHashMap<ast::Ident, ast::NodeId>;
21+
pub type PatIdMap = FnvHashMap<ast::Name, ast::NodeId>;
2222

2323
// This is used because same-named variables in alternative patterns need to
2424
// use the NodeId of their namesake in the first pattern.
@@ -109,12 +109,26 @@ pub fn pat_is_binding_or_wild(dm: &DefMap, pat: &hir::Pat) -> bool {
109109
/// Call `it` on every "binding" in a pattern, e.g., on `a` in
110110
/// `match foo() { Some(a) => (), None => () }`
111111
pub fn pat_bindings<I>(dm: &DefMap, pat: &hir::Pat, mut it: I) where
112+
I: FnMut(hir::BindingMode, ast::NodeId, Span, &Spanned<ast::Name>),
113+
{
114+
walk_pat(pat, |p| {
115+
match p.node {
116+
hir::PatIdent(binding_mode, ref pth, _) if pat_is_binding(dm, p) => {
117+
it(binding_mode, p.id, p.span, &respan(pth.span, pth.node.name));
118+
}
119+
_ => {}
120+
}
121+
true
122+
});
123+
}
124+
125+
pub fn pat_bindings_hygienic<I>(dm: &DefMap, pat: &hir::Pat, mut it: I) where
112126
I: FnMut(hir::BindingMode, ast::NodeId, Span, &Spanned<ast::Ident>),
113127
{
114128
walk_pat(pat, |p| {
115129
match p.node {
116130
hir::PatIdent(binding_mode, ref pth, _) if pat_is_binding(dm, p) => {
117-
it(binding_mode, p.id, p.span, pth);
131+
it(binding_mode, p.id, p.span, &respan(pth.span, pth.node));
118132
}
119133
_ => {}
120134
}
@@ -182,10 +196,10 @@ pub fn pat_contains_bindings_or_wild(dm: &DefMap, pat: &hir::Pat) -> bool {
182196
contains_bindings
183197
}
184198

185-
pub fn simple_identifier<'a>(pat: &'a hir::Pat) -> Option<&'a ast::Ident> {
199+
pub fn simple_name<'a>(pat: &'a hir::Pat) -> Option<ast::Name> {
186200
match pat.node {
187201
hir::PatIdent(hir::BindByValue(_), ref path1, None) => {
188-
Some(&path1.node)
202+
Some(path1.node.name)
189203
}
190204
_ => {
191205
None
@@ -197,7 +211,7 @@ pub fn def_to_path(tcx: &ty::ctxt, id: DefId) -> hir::Path {
197211
tcx.with_path(id, |path| hir::Path {
198212
global: false,
199213
segments: path.last().map(|elem| hir::PathSegment {
200-
identifier: ast::Ident::new(elem.name()),
214+
identifier: ast::Ident::with_empty_ctxt(elem.name()),
201215
parameters: hir::PathParameters::none(),
202216
}).into_iter().collect(),
203217
span: DUMMY_SP,

src/librustc/middle/resolve_lifetime.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ struct LifetimeContext<'a> {
7373
trait_ref_hack: bool,
7474

7575
// List of labels in the function/method currently under analysis.
76-
labels_in_fn: Vec<(ast::Ident, Span)>,
76+
labels_in_fn: Vec<(ast::Name, Span)>,
7777
}
7878

7979
enum ScopeChain<'a> {
@@ -381,7 +381,7 @@ fn extract_labels<'v, 'a>(ctxt: &mut LifetimeContext<'a>, b: &'v hir::Block) {
381381
struct GatherLabels<'a> {
382382
sess: &'a Session,
383383
scope: Scope<'a>,
384-
labels_in_fn: &'a mut Vec<(ast::Ident, Span)>,
384+
labels_in_fn: &'a mut Vec<(ast::Name, Span)>,
385385
}
386386

387387
let mut gather = GatherLabels {
@@ -403,9 +403,9 @@ fn extract_labels<'v, 'a>(ctxt: &mut LifetimeContext<'a>, b: &'v hir::Block) {
403403
if let Some(label) = expression_label(ex) {
404404
for &(prior, prior_span) in &self.labels_in_fn[..] {
405405
// FIXME (#24278): non-hygienic comparison
406-
if label.name == prior.name {
406+
if label == prior {
407407
signal_shadowing_problem(self.sess,
408-
label.name,
408+
label,
409409
original_label(prior_span),
410410
shadower_label(ex.span));
411411
}
@@ -426,17 +426,17 @@ fn extract_labels<'v, 'a>(ctxt: &mut LifetimeContext<'a>, b: &'v hir::Block) {
426426
}
427427
}
428428

429-
fn expression_label(ex: &hir::Expr) -> Option<ast::Ident> {
429+
fn expression_label(ex: &hir::Expr) -> Option<ast::Name> {
430430
match ex.node {
431431
hir::ExprWhile(_, _, Some(label)) |
432-
hir::ExprLoop(_, Some(label)) => Some(label),
432+
hir::ExprLoop(_, Some(label)) => Some(label.name),
433433
_ => None,
434434
}
435435
}
436436

437437
fn check_if_label_shadows_lifetime<'a>(sess: &'a Session,
438438
mut scope: Scope<'a>,
439-
label: ast::Ident,
439+
label: ast::Name,
440440
label_span: Span) {
441441
loop {
442442
match *scope {
@@ -447,10 +447,10 @@ fn extract_labels<'v, 'a>(ctxt: &mut LifetimeContext<'a>, b: &'v hir::Block) {
447447
LateScope(lifetimes, s) => {
448448
for lifetime_def in lifetimes {
449449
// FIXME (#24278): non-hygienic comparison
450-
if label.name == lifetime_def.lifetime.name {
450+
if label == lifetime_def.lifetime.name {
451451
signal_shadowing_problem(
452452
sess,
453-
label.name,
453+
label,
454454
original_lifetime(&lifetime_def.lifetime),
455455
shadower_label(label_span));
456456
return;
@@ -703,7 +703,7 @@ impl<'a> LifetimeContext<'a> {
703703
{
704704
for &(label, label_span) in &self.labels_in_fn {
705705
// FIXME (#24278): non-hygienic comparison
706-
if lifetime.name == label.name {
706+
if lifetime.name == label {
707707
signal_shadowing_problem(self.sess,
708708
lifetime.name,
709709
original_label(label_span),

src/librustc/middle/stability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ impl<'a, 'v, 'tcx> Visitor<'v> for Checker<'a, 'tcx> {
336336
// When compiling with --test we don't enforce stability on the
337337
// compiler-generated test module, demarcated with `DUMMY_SP` plus the
338338
// name `__test`
339-
if item.span == DUMMY_SP && item.name == "__test" { return }
339+
if item.span == DUMMY_SP && item.name.as_str() == "__test" { return }
340340

341341
check_item(self.tcx, item, true,
342342
&mut |id, sp, stab| self.check(id, sp, stab));

src/librustc_borrowck/borrowck/gather_loans/gather_moves.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ pub fn gather_move_from_pat<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
9999
let pat_span_path_opt = match move_pat.node {
100100
hir::PatIdent(_, ref path1, _) => {
101101
Some(MoveSpanAndPath{span: move_pat.span,
102-
ident: path1.node})
102+
name: path1.node.name})
103103
},
104104
_ => None,
105105
};

0 commit comments

Comments
 (0)