Skip to content

Commit 9cf1847

Browse files
committed
Use Idents in a number of structures in HIR
Namely: labels, type parameters, bindings in patterns, parameter names in functions without body. All of these do not need hygiene after lowering to HIR, only span locations.
1 parent 72fd35a commit 9cf1847

File tree

25 files changed

+146
-179
lines changed

25 files changed

+146
-179
lines changed

src/librustc/hir/intravisit.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) {
421421
}
422422

423423
pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
424-
visitor.visit_name(label.span, label.name);
424+
visitor.visit_ident(label.ident);
425425
}
426426

427427
pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
@@ -680,9 +680,9 @@ pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
680680
PatKind::Ref(ref subpattern, _) => {
681681
visitor.visit_pat(subpattern)
682682
}
683-
PatKind::Binding(_, canonical_id, ref pth1, ref optional_subpattern) => {
683+
PatKind::Binding(_, canonical_id, ident, ref optional_subpattern) => {
684684
visitor.visit_def_mention(Def::Local(canonical_id));
685-
visitor.visit_name(pth1.span, pth1.node);
685+
visitor.visit_ident(ident);
686686
walk_list!(visitor, visit_pat, optional_subpattern);
687687
}
688688
PatKind::Lit(ref expression) => visitor.visit_expr(expression),
@@ -705,11 +705,11 @@ pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v
705705
visitor.visit_name(foreign_item.span, foreign_item.name);
706706

707707
match foreign_item.node {
708-
ForeignItemFn(ref function_declaration, ref names, ref generics) => {
708+
ForeignItemFn(ref function_declaration, ref param_names, ref generics) => {
709709
visitor.visit_generics(generics);
710710
visitor.visit_fn_decl(function_declaration);
711-
for name in names {
712-
visitor.visit_name(name.span, name.node);
711+
for &param_name in param_names {
712+
visitor.visit_ident(param_name);
713713
}
714714
}
715715
ForeignItemStatic(ref typ, _) => visitor.visit_ty(typ),
@@ -747,7 +747,7 @@ pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Generi
747747
}
748748
GenericParam::Type(ref ty_param) => {
749749
visitor.visit_id(ty_param.id);
750-
visitor.visit_name(ty_param.span, ty_param.name);
750+
visitor.visit_ident(ty_param.ident);
751751
walk_list!(visitor, visit_ty_param_bound, &ty_param.bounds);
752752
walk_list!(visitor, visit_ty, &ty_param.default);
753753
walk_list!(visitor, visit_attribute, ty_param.attrs.iter());
@@ -836,11 +836,11 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai
836836
visitor.visit_ty(ty);
837837
walk_list!(visitor, visit_nested_body, default);
838838
}
839-
TraitItemKind::Method(ref sig, TraitMethod::Required(ref names)) => {
839+
TraitItemKind::Method(ref sig, TraitMethod::Required(ref param_names)) => {
840840
visitor.visit_id(trait_item.id);
841841
visitor.visit_fn_decl(&sig.decl);
842-
for name in names {
843-
visitor.visit_name(name.span, name.node);
842+
for &param_name in param_names {
843+
visitor.visit_ident(param_name);
844844
}
845845
}
846846
TraitItemKind::Method(ref sig, TraitMethod::Provided(body_id)) => {

src/librustc/hir/lowering.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -913,8 +913,7 @@ impl<'a> LoweringContext<'a> {
913913

914914
fn lower_label(&mut self, label: Option<Label>) -> Option<hir::Label> {
915915
label.map(|label| hir::Label {
916-
name: label.ident.name,
917-
span: label.ident.span,
916+
ident: label.ident,
918917
})
919918
}
920919

@@ -1145,13 +1144,12 @@ impl<'a> LoweringContext<'a> {
11451144

11461145
let hir_bounds = self.lower_bounds(bounds, itctx);
11471146
// Set the name to `impl Bound1 + Bound2`
1148-
let ident = Ident::from_str(&pprust::ty_to_string(t));
1147+
let ident = Ident::from_str(&pprust::ty_to_string(t)).with_span_pos(span);
11491148
self.in_band_ty_params.push(hir::TyParam {
1150-
name: ident.name,
1149+
ident,
11511150
id: def_node_id,
11521151
bounds: hir_bounds,
11531152
default: None,
1154-
span,
11551153
pure_wrt_drop: false,
11561154
synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
11571155
attrs: P::new(),
@@ -1724,12 +1722,12 @@ impl<'a> LoweringContext<'a> {
17241722
}
17251723
}
17261724

1727-
fn lower_fn_args_to_names(&mut self, decl: &FnDecl) -> hir::HirVec<Spanned<Name>> {
1725+
fn lower_fn_args_to_names(&mut self, decl: &FnDecl) -> hir::HirVec<Ident> {
17281726
decl.inputs
17291727
.iter()
17301728
.map(|arg| match arg.pat.node {
1731-
PatKind::Ident(_, ident, None) => respan(ident.span, ident.name),
1732-
_ => respan(arg.pat.span, keywords::Invalid.name()),
1729+
PatKind::Ident(_, ident, _) => ident,
1730+
_ => Ident::new(keywords::Invalid.name(), arg.pat.span),
17331731
})
17341732
.collect()
17351733
}
@@ -1798,14 +1796,14 @@ impl<'a> LoweringContext<'a> {
17981796
add_bounds: &[TyParamBound],
17991797
itctx: ImplTraitContext,
18001798
) -> hir::TyParam {
1801-
let mut name = self.lower_ident(tp.ident);
1802-
18031799
// Don't expose `Self` (recovered "keyword used as ident" parse error).
18041800
// `rustc::ty` expects `Self` to be only used for a trait's `Self`.
18051801
// Instead, use gensym("Self") to create a distinct name that looks the same.
1806-
if name == keywords::SelfType.name() {
1807-
name = Symbol::gensym("Self");
1808-
}
1802+
let ident = if tp.ident.name == keywords::SelfType.name() {
1803+
tp.ident.gensym()
1804+
} else {
1805+
tp.ident
1806+
};
18091807

18101808
let mut bounds = self.lower_bounds(&tp.bounds, itctx);
18111809
if !add_bounds.is_empty() {
@@ -1817,12 +1815,11 @@ impl<'a> LoweringContext<'a> {
18171815

18181816
hir::TyParam {
18191817
id: self.lower_node_id(tp.id).node_id,
1820-
name,
1818+
ident,
18211819
bounds,
18221820
default: tp.default
18231821
.as_ref()
18241822
.map(|x| self.lower_ty(x, ImplTraitContext::Disallowed)),
1825-
span: tp.ident.span,
18261823
pure_wrt_drop: attr::contains_name(&tp.attrs, "may_dangle"),
18271824
synthetic: tp.attrs
18281825
.iter()
@@ -2830,7 +2827,7 @@ impl<'a> LoweringContext<'a> {
28302827
hir::PatKind::Binding(
28312828
self.lower_binding_mode(binding_mode),
28322829
canonical_id,
2833-
respan(ident.span, ident.name),
2830+
ident,
28342831
sub.as_ref().map(|x| self.lower_pat(x)),
28352832
)
28362833
}
@@ -3987,7 +3984,7 @@ impl<'a> LoweringContext<'a> {
39873984
P(hir::Pat {
39883985
id: node_id,
39893986
hir_id,
3990-
node: hir::PatKind::Binding(bm, node_id, Spanned { span, node: ident.name }, None),
3987+
node: hir::PatKind::Binding(bm, node_id, ident.with_span_pos(span), None),
39913988
span,
39923989
})
39933990
}

src/librustc/hir/map/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ impl<'hir> Map<'hir> {
570570
NodeItem(&Item { node: ItemTrait(..), .. }) => {
571571
keywords::SelfType.name()
572572
}
573-
NodeTyParam(tp) => tp.name,
573+
NodeTyParam(tp) => tp.ident.name,
574574
_ => {
575575
bug!("ty_param_name: {} not a type parameter",
576576
self.node_to_string(id))
@@ -911,8 +911,8 @@ impl<'hir> Map<'hir> {
911911
NodeVariant(v) => v.node.name,
912912
NodeField(f) => f.ident.name,
913913
NodeLifetime(lt) => lt.name.ident().name,
914-
NodeTyParam(tp) => tp.name,
915-
NodeBinding(&Pat { node: PatKind::Binding(_,_,l,_), .. }) => l.node,
914+
NodeTyParam(tp) => tp.ident.name,
915+
NodeBinding(&Pat { node: PatKind::Binding(_,_,i,_), .. }) => i.name,
916916
NodeStructCtor(_) => self.name(self.get_parent(id)),
917917
_ => bug!("no name for {}", self.node_to_string(id))
918918
}
@@ -978,7 +978,7 @@ impl<'hir> Map<'hir> {
978978
Some(EntryBlock(_, _, block)) => block.span,
979979
Some(EntryStructCtor(_, _, _)) => self.expect_item(self.get_parent(id)).span,
980980
Some(EntryLifetime(_, _, lifetime)) => lifetime.span,
981-
Some(EntryTyParam(_, _, ty_param)) => ty_param.span,
981+
Some(EntryTyParam(_, _, ty_param)) => ty_param.ident.span,
982982
Some(EntryVisibility(_, _, &Visibility::Restricted { ref path, .. })) => path.span,
983983
Some(EntryVisibility(_, _, v)) => bug!("unexpected Visibility {:?}", v),
984984
Some(EntryLocal(_, _, local)) => local.span,

src/librustc/hir/mod.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,12 @@ pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId(!0);
178178

179179
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)]
180180
pub struct Label {
181-
pub name: Name,
182-
pub span: Span,
181+
pub ident: Ident,
183182
}
184183

185184
impl fmt::Debug for Label {
186185
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
187-
write!(f, "label({:?})", self.name)
186+
write!(f, "label({:?})", self.ident)
188187
}
189188
}
190189

@@ -457,11 +456,10 @@ pub type TyParamBounds = HirVec<TyParamBound>;
457456

458457
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
459458
pub struct TyParam {
460-
pub name: Name,
459+
pub ident: Ident,
461460
pub id: NodeId,
462461
pub bounds: TyParamBounds,
463462
pub default: Option<P<Ty>>,
464-
pub span: Span,
465463
pub pure_wrt_drop: bool,
466464
pub synthetic: Option<SyntheticTyParamKind>,
467465
pub attrs: HirVec<Attribute>,
@@ -923,7 +921,7 @@ pub enum PatKind {
923921
/// The `NodeId` is the canonical ID for the variable being bound,
924922
/// e.g. in `Ok(x) | Err(x)`, both `x` use the same canonical ID,
925923
/// which is the pattern ID of the first `x`.
926-
Binding(BindingAnnotation, NodeId, Spanned<Name>, Option<P<Pat>>),
924+
Binding(BindingAnnotation, NodeId, Ident, Option<P<Pat>>),
927925

928926
/// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
929927
/// The `bool` is `true` in the presence of a `..`.
@@ -1603,7 +1601,7 @@ pub struct TraitItem {
16031601
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
16041602
pub enum TraitMethod {
16051603
/// No default body in the trait, just a signature.
1606-
Required(HirVec<Spanned<Name>>),
1604+
Required(HirVec<Ident>),
16071605

16081606
/// Both signature and body are provided in the trait.
16091607
Provided(BodyId),
@@ -1698,7 +1696,7 @@ pub struct BareFnTy {
16981696
pub abi: Abi,
16991697
pub generic_params: HirVec<GenericParam>,
17001698
pub decl: P<FnDecl>,
1701-
pub arg_names: HirVec<Spanned<Name>>,
1699+
pub arg_names: HirVec<Ident>,
17021700
}
17031701

17041702
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
@@ -2220,7 +2218,7 @@ pub struct ForeignItem {
22202218
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
22212219
pub enum ForeignItem_ {
22222220
/// A foreign function
2223-
ForeignItemFn(P<FnDecl>, HirVec<Spanned<Name>>, Generics),
2221+
ForeignItemFn(P<FnDecl>, HirVec<Ident>, Generics),
22242222
/// A foreign static item (`static ext: u8`), with optional mutability
22252223
/// (the boolean is true when mutable)
22262224
ForeignItemStatic(P<Ty>, bool),

src/librustc/hir/pat_util.rs

+6-17
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use hir::def::Def;
1212
use hir::def_id::DefId;
1313
use hir::{self, HirId, PatKind};
1414
use syntax::ast;
15-
use syntax::codemap::Spanned;
1615
use syntax_pos::Span;
1716

1817
use std::iter::{Enumerate, ExactSizeIterator};
@@ -91,11 +90,11 @@ impl hir::Pat {
9190
/// Call `f` on every "binding" in a pattern, e.g., on `a` in
9291
/// `match foo() { Some(a) => (), None => () }`
9392
pub fn each_binding<F>(&self, mut f: F)
94-
where F: FnMut(hir::BindingAnnotation, HirId, Span, &Spanned<ast::Name>),
93+
where F: FnMut(hir::BindingAnnotation, HirId, Span, ast::Ident),
9594
{
9695
self.walk(|p| {
97-
if let PatKind::Binding(binding_mode, _, ref pth, _) = p.node {
98-
f(binding_mode, p.hir_id, p.span, pth);
96+
if let PatKind::Binding(binding_mode, _, ident, _) = p.node {
97+
f(binding_mode, p.hir_id, p.span, ident);
9998
}
10099
true
101100
});
@@ -132,20 +131,10 @@ impl hir::Pat {
132131
contains_bindings
133132
}
134133

135-
pub fn simple_name(&self) -> Option<ast::Name> {
134+
pub fn simple_ident(&self) -> Option<ast::Ident> {
136135
match self.node {
137-
PatKind::Binding(hir::BindingAnnotation::Unannotated, _, ref path1, None) |
138-
PatKind::Binding(hir::BindingAnnotation::Mutable, _, ref path1, None) =>
139-
Some(path1.node),
140-
_ => None,
141-
}
142-
}
143-
144-
pub fn simple_span(&self) -> Option<Span> {
145-
match self.node {
146-
PatKind::Binding(hir::BindingAnnotation::Unannotated, _, ref path1, None) |
147-
PatKind::Binding(hir::BindingAnnotation::Mutable, _, ref path1, None) =>
148-
Some(path1.span),
136+
PatKind::Binding(hir::BindingAnnotation::Unannotated, _, ident, None) |
137+
PatKind::Binding(hir::BindingAnnotation::Mutable, _, ident, None) => Some(ident),
149138
_ => None,
150139
}
151140
}

0 commit comments

Comments
 (0)