Skip to content

Commit 1e504d3

Browse files
committed
Auto merge of #51072 - petrochenkov:ifield, r=eddyb
Use `Ident`s for fields in HIR Continuation of #49718, part of #49300
2 parents 1594c6c + 1e4269c commit 1e504d3

File tree

51 files changed

+208
-186
lines changed

Some content is hidden

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

51 files changed

+208
-186
lines changed

src/doc/unstable-book/src/language-features/plugin.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ impl LintPass for Pass {
208208
209209
impl EarlyLintPass for Pass {
210210
fn check_item(&mut self, cx: &EarlyContext, it: &ast::Item) {
211-
if it.ident.name.as_str() == "lintme" {
211+
if it.ident.as_str() == "lintme" {
212212
cx.span_lint(TEST_LINT, it.span, "item is named 'lintme'");
213213
}
214214
}

src/libproc_macro/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1214,14 +1214,14 @@ impl TokenTree {
12141214
SingleQuote => op!('\''),
12151215

12161216
Ident(ident, false) => {
1217-
tt!(self::Ident::new(&ident.name.as_str(), Span(span)))
1217+
tt!(self::Ident::new(&ident.as_str(), Span(span)))
12181218
}
12191219
Ident(ident, true) => {
1220-
tt!(self::Ident::new_raw(&ident.name.as_str(), Span(span)))
1220+
tt!(self::Ident::new_raw(&ident.as_str(), Span(span)))
12211221
}
12221222
Lifetime(ident) => {
12231223
let ident = ident.without_first_quote();
1224-
stack.push(tt!(self::Ident::new(&ident.name.as_str(), Span(span))));
1224+
stack.push(tt!(self::Ident::new(&ident.as_str(), Span(span))));
12251225
tt!(Punct::new('\'', Spacing::Joint))
12261226
}
12271227
Literal(lit, suffix) => tt!(self::Literal { lit, suffix, span: Span(span) }),

src/librustc/hir/intravisit.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
//! example generator inference, and possibly also HIR borrowck.
4343
4444
use rustc_target::spec::abi::Abi;
45-
use syntax::ast::{NodeId, CRATE_NODE_ID, Name, Attribute};
45+
use syntax::ast::{NodeId, CRATE_NODE_ID, Ident, Name, Attribute};
4646
use syntax_pos::Span;
4747
use hir::*;
4848
use hir::def::Def;
@@ -248,6 +248,9 @@ pub trait Visitor<'v> : Sized {
248248
fn visit_name(&mut self, _span: Span, _name: Name) {
249249
// Nothing to do.
250250
}
251+
fn visit_ident(&mut self, ident: Ident) {
252+
walk_ident(self, ident)
253+
}
251254
fn visit_mod(&mut self, m: &'v Mod, _s: Span, n: NodeId) {
252255
walk_mod(self, m, n)
253256
}
@@ -413,6 +416,10 @@ pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
413416
walk_list!(visitor, visit_ty, &local.ty);
414417
}
415418

419+
pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) {
420+
visitor.visit_name(ident.span, ident.name);
421+
}
422+
416423
pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
417424
visitor.visit_name(label.span, label.name);
418425
}
@@ -662,7 +669,7 @@ pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
662669
visitor.visit_qpath(qpath, pattern.id, pattern.span);
663670
for field in fields {
664671
visitor.visit_id(field.node.id);
665-
visitor.visit_name(field.span, field.node.name);
672+
visitor.visit_ident(field.node.ident);
666673
visitor.visit_pat(&field.node.pat)
667674
}
668675
}
@@ -915,7 +922,7 @@ pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V, struct_definition: &
915922
pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V, struct_field: &'v StructField) {
916923
visitor.visit_id(struct_field.id);
917924
visitor.visit_vis(&struct_field.vis);
918-
visitor.visit_name(struct_field.span, struct_field.name);
925+
visitor.visit_ident(struct_field.ident);
919926
visitor.visit_ty(&struct_field.ty);
920927
walk_list!(visitor, visit_attribute, &struct_field.attrs);
921928
}
@@ -970,7 +977,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
970977
visitor.visit_qpath(qpath, expression.id, expression.span);
971978
for field in fields {
972979
visitor.visit_id(field.id);
973-
visitor.visit_name(field.name.span, field.name.node);
980+
visitor.visit_ident(field.ident);
974981
visitor.visit_expr(&field.expr)
975982
}
976983
walk_list!(visitor, visit_expr, optional_base);
@@ -1035,9 +1042,9 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
10351042
visitor.visit_expr(right_expression);
10361043
visitor.visit_expr(left_expression)
10371044
}
1038-
ExprField(ref subexpression, ref name) => {
1045+
ExprField(ref subexpression, ident) => {
10391046
visitor.visit_expr(subexpression);
1040-
visitor.visit_name(name.span, name.node);
1047+
visitor.visit_ident(ident);
10411048
}
10421049
ExprIndex(ref main_expression, ref index_expression) => {
10431050
visitor.visit_expr(main_expression);

src/librustc/hir/lowering.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -2072,11 +2072,11 @@ impl<'a> LoweringContext<'a> {
20722072
hir::StructField {
20732073
span: f.span,
20742074
id: self.lower_node_id(f.id).node_id,
2075-
name: self.lower_ident(match f.ident {
2075+
ident: match f.ident {
20762076
Some(ident) => ident,
20772077
// FIXME(jseyfried) positional field hygiene
20782078
None => Ident::new(Symbol::intern(&index.to_string()), f.span),
2079-
}),
2079+
},
20802080
vis: self.lower_visibility(&f.vis, None),
20812081
ty: self.lower_ty(&f.ty, ImplTraitContext::Disallowed),
20822082
attrs: self.lower_attrs(&f.attrs),
@@ -2086,7 +2086,7 @@ impl<'a> LoweringContext<'a> {
20862086
fn lower_field(&mut self, f: &Field) -> hir::Field {
20872087
hir::Field {
20882088
id: self.next_id().node_id,
2089-
name: respan(f.ident.span, self.lower_ident(f.ident)),
2089+
ident: f.ident,
20902090
expr: P(self.lower_expr(&f.expr)),
20912091
span: f.span,
20922092
is_shorthand: f.is_shorthand,
@@ -2848,7 +2848,7 @@ impl<'a> LoweringContext<'a> {
28482848
span: f.span,
28492849
node: hir::FieldPat {
28502850
id: self.next_id().node_id,
2851-
name: self.lower_ident(f.node.ident),
2851+
ident: f.node.ident,
28522852
pat: self.lower_pat(&f.node.pat),
28532853
is_shorthand: f.node.is_shorthand,
28542854
},
@@ -3091,10 +3091,7 @@ impl<'a> LoweringContext<'a> {
30913091
P(self.lower_expr(el)),
30923092
P(self.lower_expr(er)),
30933093
),
3094-
ExprKind::Field(ref el, ident) => hir::ExprField(
3095-
P(self.lower_expr(el)),
3096-
respan(ident.span, self.lower_ident(ident)),
3097-
),
3094+
ExprKind::Field(ref el, ident) => hir::ExprField(P(self.lower_expr(el)), ident),
30983095
ExprKind::Index(ref el, ref er) => {
30993096
hir::ExprIndex(P(self.lower_expr(el)), P(self.lower_expr(er)))
31003097
}
@@ -3134,7 +3131,8 @@ impl<'a> LoweringContext<'a> {
31343131
let expr = P(self.lower_expr(&e));
31353132
let unstable_span =
31363133
self.allow_internal_unstable(CompilerDesugaringKind::DotFill, e.span);
3137-
self.field(Symbol::intern(s), expr, unstable_span)
3134+
let ident = Ident::new(Symbol::intern(s), unstable_span);
3135+
self.field(ident, expr, unstable_span)
31383136
})
31393137
.collect::<P<[hir::Field]>>();
31403138

@@ -3749,10 +3747,10 @@ impl<'a> LoweringContext<'a> {
37493747
}
37503748
}
37513749

3752-
fn field(&mut self, name: Name, expr: P<hir::Expr>, span: Span) -> hir::Field {
3750+
fn field(&mut self, ident: Ident, expr: P<hir::Expr>, span: Span) -> hir::Field {
37533751
hir::Field {
37543752
id: self.next_id().node_id,
3755-
name: Spanned { node: name, span },
3753+
ident,
37563754
span,
37573755
expr,
37583756
is_shorthand: false,

src/librustc/hir/map/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ impl<'hir> Map<'hir> {
909909
NodeImplItem(ii) => ii.name,
910910
NodeTraitItem(ti) => ti.name,
911911
NodeVariant(v) => v.node.name,
912-
NodeField(f) => f.name,
912+
NodeField(f) => f.ident.name,
913913
NodeLifetime(lt) => lt.name.name(),
914914
NodeTyParam(tp) => tp.name,
915915
NodeBinding(&Pat { node: PatKind::Binding(_,_,l,_), .. }) => l.node,
@@ -1105,7 +1105,7 @@ impl<T:Named> Named for Spanned<T> { fn name(&self) -> Name { self.node.name() }
11051105
impl Named for Item { fn name(&self) -> Name { self.name } }
11061106
impl Named for ForeignItem { fn name(&self) -> Name { self.name } }
11071107
impl Named for Variant_ { fn name(&self) -> Name { self.name } }
1108-
impl Named for StructField { fn name(&self) -> Name { self.name } }
1108+
impl Named for StructField { fn name(&self) -> Name { self.ident.name } }
11091109
impl Named for TraitItem { fn name(&self) -> Name { self.name } }
11101110
impl Named for ImplItem { fn name(&self) -> Name { self.name } }
11111111

@@ -1291,7 +1291,7 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
12911291
}
12921292
Some(NodeField(ref field)) => {
12931293
format!("field {} in {}{}",
1294-
field.name,
1294+
field.ident,
12951295
path_str(), id_str)
12961296
}
12971297
Some(NodeAnonConst(_)) => {

src/librustc/hir/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use mir::mono::Linkage;
3535
use syntax_pos::{Span, DUMMY_SP};
3636
use syntax::codemap::{self, Spanned};
3737
use rustc_target::spec::abi::Abi;
38-
use syntax::ast::{self, Name, NodeId, DUMMY_NODE_ID, AsmDialect};
38+
use syntax::ast::{self, Ident, Name, NodeId, DUMMY_NODE_ID, AsmDialect};
3939
use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy, MetaItem};
4040
use syntax::attr::InlineAttr;
4141
use syntax::ext::hygiene::SyntaxContext;
@@ -866,7 +866,7 @@ impl Pat {
866866
pub struct FieldPat {
867867
pub id: NodeId,
868868
/// The identifier for the field
869-
pub name: Name,
869+
pub ident: Ident,
870870
/// The pattern the field is destructured to
871871
pub pat: P<Pat>,
872872
pub is_shorthand: bool,
@@ -1211,7 +1211,7 @@ pub struct Arm {
12111211
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
12121212
pub struct Field {
12131213
pub id: NodeId,
1214-
pub name: Spanned<Name>,
1214+
pub ident: Ident,
12151215
pub expr: P<Expr>,
12161216
pub span: Span,
12171217
pub is_shorthand: bool,
@@ -1414,7 +1414,7 @@ pub enum Expr_ {
14141414
/// For example, `a += 1`.
14151415
ExprAssignOp(BinOp, P<Expr>, P<Expr>),
14161416
/// Access of a named (`obj.foo`) or unnamed (`obj.0`) struct or tuple field
1417-
ExprField(P<Expr>, Spanned<Name>),
1417+
ExprField(P<Expr>, Ident),
14181418
/// An indexing operation (`foo[2]`)
14191419
ExprIndex(P<Expr>, P<Expr>),
14201420

@@ -1973,7 +1973,7 @@ impl Visibility {
19731973
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
19741974
pub struct StructField {
19751975
pub span: Span,
1976-
pub name: Name,
1976+
pub ident: Ident,
19771977
pub vis: Visibility,
19781978
pub id: NodeId,
19791979
pub ty: P<Ty>,
@@ -1983,7 +1983,7 @@ pub struct StructField {
19831983
impl StructField {
19841984
// Still necessary in couple of places
19851985
pub fn is_positional(&self) -> bool {
1986-
let first = self.name.as_str().as_bytes()[0];
1986+
let first = self.ident.as_str().as_bytes()[0];
19871987
first >= b'0' && first <= b'9'
19881988
}
19891989
}

src/librustc/hir/print.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ impl<'a> State<'a> {
857857
self.maybe_print_comment(field.span.lo())?;
858858
self.print_outer_attributes(&field.attrs)?;
859859
self.print_visibility(&field.vis)?;
860-
self.print_name(field.name)?;
860+
self.print_ident(field.ident)?;
861861
self.word_nbsp(":")?;
862862
self.print_type(&field.ty)?;
863863
self.s.word(",")?;
@@ -1166,7 +1166,7 @@ impl<'a> State<'a> {
11661166
|s, field| {
11671167
s.ibox(indent_unit)?;
11681168
if !field.is_shorthand {
1169-
s.print_name(field.name.node)?;
1169+
s.print_ident(field.ident)?;
11701170
s.word_space(":")?;
11711171
}
11721172
s.print_expr(&field.expr)?;
@@ -1406,10 +1406,10 @@ impl<'a> State<'a> {
14061406
self.word_space("=")?;
14071407
self.print_expr_maybe_paren(&rhs, prec)?;
14081408
}
1409-
hir::ExprField(ref expr, name) => {
1409+
hir::ExprField(ref expr, ident) => {
14101410
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX)?;
14111411
self.s.word(".")?;
1412-
self.print_name(name.node)?;
1412+
self.print_ident(ident)?;
14131413
}
14141414
hir::ExprIndex(ref expr, ref index) => {
14151415
self.print_expr_maybe_paren(&expr, parser::PREC_POSTFIX)?;
@@ -1561,13 +1561,17 @@ impl<'a> State<'a> {
15611561
self.s.word(&i.to_string())
15621562
}
15631563

1564-
pub fn print_name(&mut self, name: ast::Name) -> io::Result<()> {
1565-
if name.to_ident().is_raw_guess() {
1566-
self.s.word(&format!("r#{}", name))?;
1564+
pub fn print_ident(&mut self, ident: ast::Ident) -> io::Result<()> {
1565+
if ident.is_raw_guess() {
1566+
self.s.word(&format!("r#{}", ident.name))?;
15671567
} else {
1568-
self.s.word(&name.as_str())?;
1568+
self.s.word(&ident.as_str())?;
15691569
}
1570-
self.ann.post(self, NodeName(&name))
1570+
self.ann.post(self, NodeName(&ident.name))
1571+
}
1572+
1573+
pub fn print_name(&mut self, name: ast::Name) -> io::Result<()> {
1574+
self.print_ident(name.to_ident())
15711575
}
15721576

15731577
pub fn print_for_decl(&mut self, loc: &hir::Local, coll: &hir::Expr) -> io::Result<()> {
@@ -1773,7 +1777,7 @@ impl<'a> State<'a> {
17731777
|s, f| {
17741778
s.cbox(indent_unit)?;
17751779
if !f.node.is_shorthand {
1776-
s.print_name(f.node.name)?;
1780+
s.print_ident(f.node.ident)?;
17771781
s.word_nbsp(":")?;
17781782
}
17791783
s.print_pat(&f.node.pat)?;

src/librustc/ich/impls_hir.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -427,12 +427,12 @@ impl<'a> HashStable<StableHashingContext<'a>> for hir::FieldPat {
427427
hasher: &mut StableHasher<W>) {
428428
let hir::FieldPat {
429429
id: _,
430-
name,
430+
ident,
431431
ref pat,
432432
is_shorthand,
433433
} = *self;
434434

435-
name.hash_stable(hcx, hasher);
435+
ident.hash_stable(hcx, hasher);
436436
pat.hash_stable(hcx, hasher);
437437
is_shorthand.hash_stable(hcx, hasher);
438438
}
@@ -525,13 +525,13 @@ impl<'a> HashStable<StableHashingContext<'a>> for hir::Field {
525525
hasher: &mut StableHasher<W>) {
526526
let hir::Field {
527527
id: _,
528-
name,
528+
ident,
529529
ref expr,
530530
span,
531531
is_shorthand,
532532
} = *self;
533533

534-
name.hash_stable(hcx, hasher);
534+
ident.hash_stable(hcx, hasher);
535535
expr.hash_stable(hcx, hasher);
536536
span.hash_stable(hcx, hasher);
537537
is_shorthand.hash_stable(hcx, hasher);
@@ -598,7 +598,7 @@ impl_stable_hash_for!(enum hir::Expr_ {
598598
ExprBlock(blk, label),
599599
ExprAssign(lhs, rhs),
600600
ExprAssignOp(op, lhs, rhs),
601-
ExprField(owner, field_name),
601+
ExprField(owner, ident),
602602
ExprIndex(lhs, rhs),
603603
ExprPath(path),
604604
ExprAddrOf(mutability, sub),
@@ -835,7 +835,7 @@ impl_stable_hash_for!(enum hir::UseKind {
835835

836836
impl_stable_hash_for!(struct hir::StructField {
837837
span,
838-
name,
838+
ident,
839839
vis,
840840
id,
841841
ty,

src/librustc/ich/impls_ty.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,17 @@ impl_stable_hash_for!(enum ty::VariantDiscr {
357357
Relative(distance)
358358
});
359359

360-
impl_stable_hash_for!(struct ty::FieldDef {
361-
did,
362-
name,
363-
vis
364-
});
360+
impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for ty::FieldDef {
361+
fn hash_stable<W: StableHasherResult>(&self,
362+
hcx: &mut StableHashingContext<'a>,
363+
hasher: &mut StableHasher<W>) {
364+
let ty::FieldDef { did, ident, vis } = *self;
365+
366+
did.hash_stable(hcx, hasher);
367+
ident.name.hash_stable(hcx, hasher);
368+
vis.hash_stable(hcx, hasher);
369+
}
370+
}
365371

366372
impl<'a, 'gcx> HashStable<StableHashingContext<'a>>
367373
for ::middle::const_val::ConstVal<'gcx> {

src/librustc/middle/dead.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
588588

589589
fn visit_struct_field(&mut self, field: &'tcx hir::StructField) {
590590
if self.should_warn_about_field(&field) {
591-
self.warn_dead_code(field.id, field.span, field.name, "field", "used");
591+
self.warn_dead_code(field.id, field.span, field.ident.name, "field", "used");
592592
}
593593
intravisit::walk_struct_field(self, field);
594594
}

src/librustc/middle/expr_use_visitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
669669
&*with_expr,
670670
with_cmt.clone(),
671671
f_index,
672-
with_field.name,
672+
with_field.ident,
673673
with_field.ty(self.tcx(), substs)
674674
);
675675
self.delegate_consume(with_expr.id, with_expr.span, &cmt_field);

0 commit comments

Comments
 (0)