Skip to content

Commit c5454c0

Browse files
committed
Use Idents for lifetimes in HIR
1 parent ba196bd commit c5454c0

File tree

12 files changed

+93
-73
lines changed

12 files changed

+93
-73
lines changed

src/librustc/hir/intravisit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,8 @@ pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
432432
pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
433433
visitor.visit_id(lifetime.id);
434434
match lifetime.name {
435-
LifetimeName::Param(ParamName::Plain(name)) => {
436-
visitor.visit_name(lifetime.span, name);
435+
LifetimeName::Param(ParamName::Plain(ident)) => {
436+
visitor.visit_ident(ident);
437437
}
438438
LifetimeName::Param(ParamName::Fresh(_)) |
439439
LifetimeName::Static |
@@ -741,7 +741,7 @@ pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Generi
741741
visitor.visit_id(param.id);
742742
walk_list!(visitor, visit_attribute, &param.attrs);
743743
match param.name {
744-
ParamName::Plain(name) => visitor.visit_name(param.span, name),
744+
ParamName::Plain(ident) => visitor.visit_ident(ident),
745745
ParamName::Fresh(_) => {}
746746
}
747747
match param.kind {

src/librustc/hir/lowering.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pub struct LoweringContext<'a> {
138138
// When `is_collectin_in_band_lifetimes` is true, each lifetime is checked
139139
// against this list to see if it is already in-scope, or if a definition
140140
// needs to be created for it.
141-
in_scope_lifetimes: Vec<Name>,
141+
in_scope_lifetimes: Vec<Ident>,
142142

143143
type_def_lifetime_params: DefIdMap<usize>,
144144

@@ -678,15 +678,15 @@ impl<'a> LoweringContext<'a> {
678678
// that collisions are ok here and this shouldn't
679679
// really show up for end-user.
680680
let str_name = match hir_name {
681-
ParamName::Plain(name) => name.as_str(),
682-
ParamName::Fresh(_) => keywords::UnderscoreLifetime.name().as_str(),
681+
ParamName::Plain(ident) => name.as_interned_str(),
682+
ParamName::Fresh(_) => keywords::UnderscoreLifetime.name().as_interned_str(),
683683
};
684684

685685
// Add a definition for the in-band lifetime def
686686
self.resolver.definitions().create_def_with_parent(
687687
parent_id.index,
688688
def_node_id,
689-
DefPathData::LifetimeParam(str_name.as_interned_str()),
689+
DefPathData::LifetimeDef(str_name),
690690
DefIndexAddressSpace::High,
691691
Mark::root(),
692692
span,
@@ -712,22 +712,23 @@ impl<'a> LoweringContext<'a> {
712712
/// lifetimes are enabled, then we want to push that lifetime into
713713
/// the vector of names to define later. In that case, it will get
714714
/// added to the appropriate generics.
715-
fn maybe_collect_in_band_lifetime(&mut self, span: Span, name: Name) {
715+
fn maybe_collect_in_band_lifetime(&mut self, ident: Ident) {
716716
if !self.is_collecting_in_band_lifetimes {
717717
return;
718718
}
719719

720-
if self.in_scope_lifetimes.contains(&name) {
720+
if self.in_scope_lifetimes.contains(&ident.modern()) {
721721
return;
722722
}
723723

724724
let hir_name = ParamName::Plain(name);
725725

726-
if self.lifetimes_to_define.iter().any(|(_, lt_name)| *lt_name == hir_name) {
726+
if self.lifetimes_to_define.iter()
727+
.any(|(_, lt_name)| *lt_name.modern() == hir_name.modern()) {
727728
return;
728729
}
729730

730-
self.lifetimes_to_define.push((span, hir_name));
731+
self.lifetimes_to_define.push((ident.span, hir_name));
731732
}
732733

733734
/// When we have either an elided or `'_` lifetime in an impl
@@ -750,7 +751,7 @@ impl<'a> LoweringContext<'a> {
750751
{
751752
let old_len = self.in_scope_lifetimes.len();
752753
let lt_def_names = params.iter().filter_map(|param| match param.kind {
753-
GenericParamKind::Lifetime { .. } => Some(param.ident.name),
754+
GenericParamKind::Lifetime { .. } => Some(param.ident.modern()),
754755
_ => None,
755756
});
756757
self.in_scope_lifetimes.extend(lt_def_names);
@@ -774,7 +775,7 @@ impl<'a> LoweringContext<'a> {
774775
{
775776
let old_len = self.in_scope_lifetimes.len();
776777
let lt_def_names = params.iter().filter_map(|param| match param.kind {
777-
hir::GenericParamKind::Lifetime { .. } => Some(param.name.name()),
778+
hir::GenericParamKind::Lifetime { .. } => Some(param.name.ident().modern()),
778779
_ => None,
779780
});
780781
self.in_scope_lifetimes.extend(lt_def_names);
@@ -1443,7 +1444,7 @@ impl<'a> LoweringContext<'a> {
14431444
self.context.resolver.definitions().create_def_with_parent(
14441445
self.parent,
14451446
def_node_id,
1446-
DefPathData::LifetimeParam(name.name().as_interned_str()),
1447+
DefPathData::LifetimeParam(name.ident().as_interned_str()),
14471448
DefIndexAddressSpace::High,
14481449
Mark::root(),
14491450
lifetime.span,
@@ -2177,20 +2178,22 @@ impl<'a> LoweringContext<'a> {
21772178
fn lower_lifetime(&mut self, l: &Lifetime) -> hir::Lifetime {
21782179
let span = l.ident.span;
21792180
match self.lower_ident(l.ident) {
2180-
x if x == "'static" => self.new_named_lifetime(l.id, span, hir::LifetimeName::Static),
2181-
x if x == "'_" => match self.anonymous_lifetime_mode {
2182-
AnonymousLifetimeMode::CreateParameter => {
2183-
let fresh_name = self.collect_fresh_in_band_lifetime(span);
2184-
self.new_named_lifetime(l.id, span, hir::LifetimeName::Param(fresh_name))
2185-
}
2181+
ident if ident.name == keywords::StaticLifetime.name() =>
2182+
self.new_named_lifetime(l.id, span, hir::LifetimeName::Static),
2183+
ident if ident.name == keywords::UnderscoreLifetime.name() =>
2184+
match self.anonymous_lifetime_mode {
2185+
AnonymousLifetimeMode::CreateParameter => {
2186+
let fresh_name = self.collect_fresh_in_band_lifetime(span);
2187+
self.new_named_lifetime(l.id, span, hir::LifetimeName::Param(fresh_name))
2188+
}
21862189

2187-
AnonymousLifetimeMode::PassThrough => {
2188-
self.new_named_lifetime(l.id, span, hir::LifetimeName::Underscore)
2189-
}
2190-
},
2191-
name => {
2192-
self.maybe_collect_in_band_lifetime(span, name);
2193-
let param_name = ParamName::Plain(name);
2190+
AnonymousLifetimeMode::PassThrough => {
2191+
self.new_named_lifetime(l.id, span, hir::LifetimeName::Underscore)
2192+
}
2193+
},
2194+
ident => {
2195+
self.maybe_collect_in_band_lifetime(ident);
2196+
let param_name = ParamName::Plain(ident);
21942197
self.new_named_lifetime(l.id, span, hir::LifetimeName::Param(param_name))
21952198
}
21962199
}

src/librustc/hir/map/def_collector.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
112112
// information we encapsulate into, the better
113113
let def_data = match i.node {
114114
ItemKind::Impl(..) => DefPathData::Impl,
115-
ItemKind::Trait(..) => DefPathData::Trait(i.ident.name.as_interned_str()),
115+
ItemKind::Trait(..) => DefPathData::Trait(i.ident.as_interned_str()),
116116
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) |
117117
ItemKind::TraitAlias(..) |
118118
ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) | ItemKind::Ty(..) =>
119-
DefPathData::TypeNs(i.ident.name.as_interned_str()),
119+
DefPathData::TypeNs(i.ident.as_interned_str()),
120120
ItemKind::Mod(..) if i.ident == keywords::Invalid.ident() => {
121121
return visit::walk_item(self, i);
122122
}
@@ -129,10 +129,10 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
129129
|this| visit::walk_item(this, i)
130130
)
131131
}
132-
ItemKind::Mod(..) => DefPathData::Module(i.ident.name.as_interned_str()),
132+
ItemKind::Mod(..) => DefPathData::Module(i.ident.as_interned_str()),
133133
ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) =>
134-
DefPathData::ValueNs(i.ident.name.as_interned_str()),
135-
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.name.as_interned_str()),
134+
DefPathData::ValueNs(i.ident.as_interned_str()),
135+
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.as_interned_str()),
136136
ItemKind::Mac(..) => return self.visit_macro_invoc(i.id),
137137
ItemKind::GlobalAsm(..) => DefPathData::Misc,
138138
ItemKind::Use(..) => {
@@ -169,7 +169,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
169169
}
170170

171171
let def = self.create_def(foreign_item.id,
172-
DefPathData::ValueNs(foreign_item.ident.name.as_interned_str()),
172+
DefPathData::ValueNs(foreign_item.ident.as_interned_str()),
173173
REGULAR_SPACE,
174174
foreign_item.span);
175175

@@ -180,8 +180,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
180180

181181
fn visit_variant(&mut self, v: &'a Variant, g: &'a Generics, item_id: NodeId) {
182182
let def = self.create_def(v.node.data.id(),
183-
DefPathData::EnumVariant(v.node.ident
184-
.name.as_interned_str()),
183+
DefPathData::EnumVariant(v.node.ident.as_interned_str()),
185184
REGULAR_SPACE,
186185
v.span);
187186
self.with_parent(def, |this| visit::walk_variant(this, v, g, item_id));
@@ -201,7 +200,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
201200
}
202201

203202
fn visit_generic_param(&mut self, param: &'a GenericParam) {
204-
let name = param.ident.name.as_interned_str();
203+
let name = param.ident.as_interned_str();
205204
let def_path_data = match param.kind {
206205
GenericParamKind::Lifetime { .. } => DefPathData::LifetimeParam(name),
207206
GenericParamKind::Type { .. } => DefPathData::TypeParam(name),
@@ -214,9 +213,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
214213
fn visit_trait_item(&mut self, ti: &'a TraitItem) {
215214
let def_data = match ti.node {
216215
TraitItemKind::Method(..) | TraitItemKind::Const(..) =>
217-
DefPathData::ValueNs(ti.ident.name.as_interned_str()),
216+
DefPathData::ValueNs(ti.ident.as_interned_str()),
218217
TraitItemKind::Type(..) => {
219-
DefPathData::AssocTypeInTrait(ti.ident.name.as_interned_str())
218+
DefPathData::AssocTypeInTrait(ti.ident.as_interned_str())
220219
},
221220
TraitItemKind::Macro(..) => return self.visit_macro_invoc(ti.id),
222221
};
@@ -239,8 +238,8 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
239238
)
240239
}
241240
ImplItemKind::Method(..) | ImplItemKind::Const(..) =>
242-
DefPathData::ValueNs(ii.ident.name.as_interned_str()),
243-
ImplItemKind::Type(..) => DefPathData::AssocTypeInImpl(ii.ident.name.as_interned_str()),
241+
DefPathData::ValueNs(ii.ident.as_interned_str()),
242+
ImplItemKind::Type(..) => DefPathData::AssocTypeInImpl(ii.ident.as_interned_str()),
244243
ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id),
245244
};
246245

src/librustc/hir/map/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -953,8 +953,8 @@ impl<'hir> Map<'hir> {
953953
NodeTraitItem(ti) => ti.name,
954954
NodeVariant(v) => v.node.name,
955955
NodeField(f) => f.ident.name,
956-
NodeLifetime(lt) => lt.name.name(),
957-
NodeGenericParam(param) => param.name.name(),
956+
NodeLifetime(lt) => lt.name.ident().name,
957+
NodeGenericParam(param) => param.name.ident().name,
958958
NodeBinding(&Pat { node: PatKind::Binding(_,_,l,_), .. }) => l.node,
959959
NodeStructCtor(_) => self.name(self.get_parent(id)),
960960
_ => bug!("no name for {}", self.node_to_string(id))

src/librustc/hir/mod.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,13 @@ pub enum LifetimeName {
245245
}
246246

247247
impl LifetimeName {
248-
pub fn name(&self) -> Name {
249-
use self::LifetimeName::*;
248+
pub fn ident(&self) -> Ident {
250249
match *self {
251-
Implicit => keywords::Invalid.name(),
252-
Underscore => keywords::UnderscoreLifetime.name(),
253-
Static => keywords::StaticLifetime.name(),
254-
Param(param_name) => param_name.name(),
250+
LifetimeName::Implicit => keywords::Invalid.ident(),
251+
LifetimeName::Fresh(_) | LifetimeName::Underscore =>
252+
keywords::UnderscoreLifetime.ident(),
253+
LifetimeName::Static => keywords::StaticLifetime.ident(),
254+
LifetimeName::Ident(ident) => ident,
255255
}
256256
}
257257

@@ -272,6 +272,19 @@ impl LifetimeName {
272272
fn is_static(&self) -> bool {
273273
self == &LifetimeName::Static
274274
}
275+
276+
pub fn modern(&self) -> LifetimeName {
277+
match *self {
278+
LifetimeName::Ident(ident) => LifetimeName::Ident(ident.modern()),
279+
lifetime_name => lifetime_name,
280+
}
281+
}
282+
}
283+
284+
impl fmt::Display for Lifetime {
285+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
286+
self.name.ident().fmt(f)
287+
}
275288
}
276289

277290
impl fmt::Debug for Lifetime {

0 commit comments

Comments
 (0)