Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Idents for fields in HIR #51072

Merged
merged 2 commits into from
May 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/doc/unstable-book/src/language-features/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl LintPass for Pass {

impl EarlyLintPass for Pass {
fn check_item(&mut self, cx: &EarlyContext, it: &ast::Item) {
if it.ident.name.as_str() == "lintme" {
if it.ident.as_str() == "lintme" {
cx.span_lint(TEST_LINT, it.span, "item is named 'lintme'");
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/libproc_macro/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1214,14 +1214,14 @@ impl TokenTree {
SingleQuote => op!('\''),

Ident(ident, false) => {
tt!(self::Ident::new(&ident.name.as_str(), Span(span)))
tt!(self::Ident::new(&ident.as_str(), Span(span)))
}
Ident(ident, true) => {
tt!(self::Ident::new_raw(&ident.name.as_str(), Span(span)))
tt!(self::Ident::new_raw(&ident.as_str(), Span(span)))
}
Lifetime(ident) => {
let ident = ident.without_first_quote();
stack.push(tt!(self::Ident::new(&ident.name.as_str(), Span(span))));
stack.push(tt!(self::Ident::new(&ident.as_str(), Span(span))));
tt!(Punct::new('\'', Spacing::Joint))
}
Literal(lit, suffix) => tt!(self::Literal { lit, suffix, span: Span(span) }),
Expand Down
19 changes: 13 additions & 6 deletions src/librustc/hir/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
//! example generator inference, and possibly also HIR borrowck.

use rustc_target::spec::abi::Abi;
use syntax::ast::{NodeId, CRATE_NODE_ID, Name, Attribute};
use syntax::ast::{NodeId, CRATE_NODE_ID, Ident, Name, Attribute};
use syntax_pos::Span;
use hir::*;
use hir::def::Def;
Expand Down Expand Up @@ -248,6 +248,9 @@ pub trait Visitor<'v> : Sized {
fn visit_name(&mut self, _span: Span, _name: Name) {
// Nothing to do.
}
fn visit_ident(&mut self, ident: Ident) {
walk_ident(self, ident)
}
fn visit_mod(&mut self, m: &'v Mod, _s: Span, n: NodeId) {
walk_mod(self, m, n)
}
Expand Down Expand Up @@ -413,6 +416,10 @@ pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
walk_list!(visitor, visit_ty, &local.ty);
}

pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) {
visitor.visit_name(ident.span, ident.name);
}

pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
visitor.visit_name(label.span, label.name);
}
Expand Down Expand Up @@ -662,7 +669,7 @@ pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
visitor.visit_qpath(qpath, pattern.id, pattern.span);
for field in fields {
visitor.visit_id(field.node.id);
visitor.visit_name(field.span, field.node.name);
visitor.visit_ident(field.node.ident);
visitor.visit_pat(&field.node.pat)
}
}
Expand Down Expand Up @@ -915,7 +922,7 @@ pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V, struct_definition: &
pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V, struct_field: &'v StructField) {
visitor.visit_id(struct_field.id);
visitor.visit_vis(&struct_field.vis);
visitor.visit_name(struct_field.span, struct_field.name);
visitor.visit_ident(struct_field.ident);
visitor.visit_ty(&struct_field.ty);
walk_list!(visitor, visit_attribute, &struct_field.attrs);
}
Expand Down Expand Up @@ -970,7 +977,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
visitor.visit_qpath(qpath, expression.id, expression.span);
for field in fields {
visitor.visit_id(field.id);
visitor.visit_name(field.name.span, field.name.node);
visitor.visit_ident(field.ident);
visitor.visit_expr(&field.expr)
}
walk_list!(visitor, visit_expr, optional_base);
Expand Down Expand Up @@ -1035,9 +1042,9 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
visitor.visit_expr(right_expression);
visitor.visit_expr(left_expression)
}
ExprField(ref subexpression, ref name) => {
ExprField(ref subexpression, ident) => {
visitor.visit_expr(subexpression);
visitor.visit_name(name.span, name.node);
visitor.visit_ident(ident);
}
ExprIndex(ref main_expression, ref index_expression) => {
visitor.visit_expr(main_expression);
Expand Down
20 changes: 9 additions & 11 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2093,11 +2093,11 @@ impl<'a> LoweringContext<'a> {
hir::StructField {
span: f.span,
id: self.lower_node_id(f.id).node_id,
name: self.lower_ident(match f.ident {
ident: match f.ident {
Some(ident) => ident,
// FIXME(jseyfried) positional field hygiene
None => Ident::new(Symbol::intern(&index.to_string()), f.span),
}),
},
vis: self.lower_visibility(&f.vis, None),
ty: self.lower_ty(&f.ty, ImplTraitContext::Disallowed),
attrs: self.lower_attrs(&f.attrs),
Expand All @@ -2107,7 +2107,7 @@ impl<'a> LoweringContext<'a> {
fn lower_field(&mut self, f: &Field) -> hir::Field {
hir::Field {
id: self.next_id().node_id,
name: respan(f.ident.span, self.lower_ident(f.ident)),
ident: f.ident,
expr: P(self.lower_expr(&f.expr)),
span: f.span,
is_shorthand: f.is_shorthand,
Expand Down Expand Up @@ -2877,7 +2877,7 @@ impl<'a> LoweringContext<'a> {
span: f.span,
node: hir::FieldPat {
id: self.next_id().node_id,
name: self.lower_ident(f.node.ident),
ident: f.node.ident,
pat: self.lower_pat(&f.node.pat),
is_shorthand: f.node.is_shorthand,
},
Expand Down Expand Up @@ -3119,10 +3119,7 @@ impl<'a> LoweringContext<'a> {
P(self.lower_expr(el)),
P(self.lower_expr(er)),
),
ExprKind::Field(ref el, ident) => hir::ExprField(
P(self.lower_expr(el)),
respan(ident.span, self.lower_ident(ident)),
),
ExprKind::Field(ref el, ident) => hir::ExprField(P(self.lower_expr(el)), ident),
ExprKind::Index(ref el, ref er) => {
hir::ExprIndex(P(self.lower_expr(el)), P(self.lower_expr(er)))
}
Expand Down Expand Up @@ -3162,7 +3159,8 @@ impl<'a> LoweringContext<'a> {
let expr = P(self.lower_expr(&e));
let unstable_span =
self.allow_internal_unstable(CompilerDesugaringKind::DotFill, e.span);
self.field(Symbol::intern(s), expr, unstable_span)
let ident = Ident::new(Symbol::intern(s), unstable_span);
self.field(ident, expr, unstable_span)
})
.collect::<P<[hir::Field]>>();

Expand Down Expand Up @@ -3777,10 +3775,10 @@ impl<'a> LoweringContext<'a> {
}
}

fn field(&mut self, name: Name, expr: P<hir::Expr>, span: Span) -> hir::Field {
fn field(&mut self, ident: Ident, expr: P<hir::Expr>, span: Span) -> hir::Field {
hir::Field {
id: self.next_id().node_id,
name: Spanned { node: name, span },
ident,
span,
expr,
is_shorthand: false,
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ impl<'hir> Map<'hir> {
NodeImplItem(ii) => ii.name,
NodeTraitItem(ti) => ti.name,
NodeVariant(v) => v.node.name,
NodeField(f) => f.name,
NodeField(f) => f.ident.name,
NodeLifetime(lt) => lt.name.name(),
NodeTyParam(tp) => tp.name,
NodeBinding(&Pat { node: PatKind::Binding(_,_,l,_), .. }) => l.node,
Expand Down Expand Up @@ -1105,7 +1105,7 @@ impl<T:Named> Named for Spanned<T> { fn name(&self) -> Name { self.node.name() }
impl Named for Item { fn name(&self) -> Name { self.name } }
impl Named for ForeignItem { fn name(&self) -> Name { self.name } }
impl Named for Variant_ { fn name(&self) -> Name { self.name } }
impl Named for StructField { fn name(&self) -> Name { self.name } }
impl Named for StructField { fn name(&self) -> Name { self.ident.name } }
impl Named for TraitItem { fn name(&self) -> Name { self.name } }
impl Named for ImplItem { fn name(&self) -> Name { self.name } }

Expand Down Expand Up @@ -1291,7 +1291,7 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
}
Some(NodeField(ref field)) => {
format!("field {} in {}{}",
field.name,
field.ident,
path_str(), id_str)
}
Some(NodeAnonConst(_)) => {
Expand Down
12 changes: 6 additions & 6 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use mir::mono::Linkage;
use syntax_pos::{Span, DUMMY_SP};
use syntax::codemap::{self, Spanned};
use rustc_target::spec::abi::Abi;
use syntax::ast::{self, Name, NodeId, DUMMY_NODE_ID, AsmDialect};
use syntax::ast::{self, Ident, Name, NodeId, DUMMY_NODE_ID, AsmDialect};
use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy, MetaItem};
use syntax::attr::InlineAttr;
use syntax::ext::hygiene::SyntaxContext;
Expand Down Expand Up @@ -866,7 +866,7 @@ impl Pat {
pub struct FieldPat {
pub id: NodeId,
/// The identifier for the field
pub name: Name,
pub ident: Ident,
/// The pattern the field is destructured to
pub pat: P<Pat>,
pub is_shorthand: bool,
Expand Down Expand Up @@ -1211,7 +1211,7 @@ pub struct Arm {
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct Field {
pub id: NodeId,
pub name: Spanned<Name>,
pub ident: Ident,
pub expr: P<Expr>,
pub span: Span,
pub is_shorthand: bool,
Expand Down Expand Up @@ -1414,7 +1414,7 @@ pub enum Expr_ {
/// For example, `a += 1`.
ExprAssignOp(BinOp, P<Expr>, P<Expr>),
/// Access of a named (`obj.foo`) or unnamed (`obj.0`) struct or tuple field
ExprField(P<Expr>, Spanned<Name>),
ExprField(P<Expr>, Ident),
/// An indexing operation (`foo[2]`)
ExprIndex(P<Expr>, P<Expr>),

Expand Down Expand Up @@ -1973,7 +1973,7 @@ impl Visibility {
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct StructField {
pub span: Span,
pub name: Name,
pub ident: Ident,
pub vis: Visibility,
pub id: NodeId,
pub ty: P<Ty>,
Expand All @@ -1983,7 +1983,7 @@ pub struct StructField {
impl StructField {
// Still necessary in couple of places
pub fn is_positional(&self) -> bool {
let first = self.name.as_str().as_bytes()[0];
let first = self.ident.as_str().as_bytes()[0];
first >= b'0' && first <= b'9'
}
}
Expand Down
24 changes: 14 additions & 10 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ impl<'a> State<'a> {
self.maybe_print_comment(field.span.lo())?;
self.print_outer_attributes(&field.attrs)?;
self.print_visibility(&field.vis)?;
self.print_name(field.name)?;
self.print_ident(field.ident)?;
self.word_nbsp(":")?;
self.print_type(&field.ty)?;
self.s.word(",")?;
Expand Down Expand Up @@ -1166,7 +1166,7 @@ impl<'a> State<'a> {
|s, field| {
s.ibox(indent_unit)?;
if !field.is_shorthand {
s.print_name(field.name.node)?;
s.print_ident(field.ident)?;
s.word_space(":")?;
}
s.print_expr(&field.expr)?;
Expand Down Expand Up @@ -1406,10 +1406,10 @@ impl<'a> State<'a> {
self.word_space("=")?;
self.print_expr_maybe_paren(&rhs, prec)?;
}
hir::ExprField(ref expr, name) => {
hir::ExprField(ref expr, ident) => {
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX)?;
self.s.word(".")?;
self.print_name(name.node)?;
self.print_ident(ident)?;
}
hir::ExprIndex(ref expr, ref index) => {
self.print_expr_maybe_paren(&expr, parser::PREC_POSTFIX)?;
Expand Down Expand Up @@ -1561,13 +1561,17 @@ impl<'a> State<'a> {
self.s.word(&i.to_string())
}

pub fn print_name(&mut self, name: ast::Name) -> io::Result<()> {
if name.to_ident().is_raw_guess() {
self.s.word(&format!("r#{}", name))?;
pub fn print_ident(&mut self, ident: ast::Ident) -> io::Result<()> {
if ident.is_raw_guess() {
self.s.word(&format!("r#{}", ident.name))?;
} else {
self.s.word(&name.as_str())?;
self.s.word(&ident.as_str())?;
}
self.ann.post(self, NodeName(&name))
self.ann.post(self, NodeName(&ident.name))
}

pub fn print_name(&mut self, name: ast::Name) -> io::Result<()> {
self.print_ident(name.to_ident())
}

pub fn print_for_decl(&mut self, loc: &hir::Local, coll: &hir::Expr) -> io::Result<()> {
Expand Down Expand Up @@ -1773,7 +1777,7 @@ impl<'a> State<'a> {
|s, f| {
s.cbox(indent_unit)?;
if !f.node.is_shorthand {
s.print_name(f.node.name)?;
s.print_ident(f.node.ident)?;
s.word_nbsp(":")?;
}
s.print_pat(&f.node.pat)?;
Expand Down
12 changes: 6 additions & 6 deletions src/librustc/ich/impls_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,12 @@ impl<'a> HashStable<StableHashingContext<'a>> for hir::FieldPat {
hasher: &mut StableHasher<W>) {
let hir::FieldPat {
id: _,
name,
ident,
ref pat,
is_shorthand,
} = *self;

name.hash_stable(hcx, hasher);
ident.hash_stable(hcx, hasher);
pat.hash_stable(hcx, hasher);
is_shorthand.hash_stable(hcx, hasher);
}
Expand Down Expand Up @@ -525,13 +525,13 @@ impl<'a> HashStable<StableHashingContext<'a>> for hir::Field {
hasher: &mut StableHasher<W>) {
let hir::Field {
id: _,
name,
ident,
ref expr,
span,
is_shorthand,
} = *self;

name.hash_stable(hcx, hasher);
ident.hash_stable(hcx, hasher);
expr.hash_stable(hcx, hasher);
span.hash_stable(hcx, hasher);
is_shorthand.hash_stable(hcx, hasher);
Expand Down Expand Up @@ -598,7 +598,7 @@ impl_stable_hash_for!(enum hir::Expr_ {
ExprBlock(blk, label),
ExprAssign(lhs, rhs),
ExprAssignOp(op, lhs, rhs),
ExprField(owner, field_name),
ExprField(owner, ident),
ExprIndex(lhs, rhs),
ExprPath(path),
ExprAddrOf(mutability, sub),
Expand Down Expand Up @@ -835,7 +835,7 @@ impl_stable_hash_for!(enum hir::UseKind {

impl_stable_hash_for!(struct hir::StructField {
span,
name,
ident,
vis,
id,
ty,
Expand Down
16 changes: 11 additions & 5 deletions src/librustc/ich/impls_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,17 @@ impl_stable_hash_for!(enum ty::VariantDiscr {
Relative(distance)
});

impl_stable_hash_for!(struct ty::FieldDef {
did,
name,
vis
});
impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for ty::FieldDef {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
let ty::FieldDef { did, ident, vis } = *self;

did.hash_stable(hcx, hasher);
ident.name.hash_stable(hcx, hasher);
vis.hash_stable(hcx, hasher);
}
}

impl<'a, 'gcx> HashStable<StableHashingContext<'a>>
for ::middle::const_val::ConstVal<'gcx> {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {

fn visit_struct_field(&mut self, field: &'tcx hir::StructField) {
if self.should_warn_about_field(&field) {
self.warn_dead_code(field.id, field.span, field.name, "field", "used");
self.warn_dead_code(field.id, field.span, field.ident.name, "field", "used");
}
intravisit::walk_struct_field(self, field);
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
&*with_expr,
with_cmt.clone(),
f_index,
with_field.name,
with_field.ident,
with_field.ty(self.tcx(), substs)
);
self.delegate_consume(with_expr.id, with_expr.span, &cmt_field);
Expand Down
Loading