Skip to content

Commit 897f6a5

Browse files
committed
Use Idents for lifetimes in HIR
1 parent a32e979 commit 897f6a5

File tree

14 files changed

+114
-106
lines changed

14 files changed

+114
-106
lines changed

src/librustc/hir/intravisit.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,8 @@ pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
427427
pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
428428
visitor.visit_id(lifetime.id);
429429
match lifetime.name {
430-
LifetimeName::Name(name) => {
431-
visitor.visit_name(lifetime.span, name);
430+
LifetimeName::Ident(ident) => {
431+
visitor.visit_ident(ident);
432432
}
433433
LifetimeName::Fresh(_) |
434434
LifetimeName::Static |
@@ -735,8 +735,8 @@ pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Generi
735735
GenericParam::Lifetime(ref ld) => {
736736
visitor.visit_id(ld.lifetime.id);
737737
match ld.lifetime.name {
738-
LifetimeName::Name(name) => {
739-
visitor.visit_name(ld.lifetime.span, name);
738+
LifetimeName::Ident(ident) => {
739+
visitor.visit_ident(ident);
740740
}
741741
LifetimeName::Fresh(_) |
742742
LifetimeName::Static |

src/librustc/hir/lowering.rs

+30-27
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub struct LoweringContext<'a> {
136136
// When `is_collectin_in_band_lifetimes` is true, each lifetime is checked
137137
// against this list to see if it is already in-scope, or if a definition
138138
// needs to be created for it.
139-
in_scope_lifetimes: Vec<Name>,
139+
in_scope_lifetimes: Vec<Ident>,
140140

141141
type_def_lifetime_params: DefIdMap<usize>,
142142

@@ -650,8 +650,9 @@ impl<'a> LoweringContext<'a> {
650650
// that collisions are ok here and this shouldn't
651651
// really show up for end-user.
652652
let str_name = match hir_name {
653-
hir::LifetimeName::Name(n) => n.as_str(),
654-
hir::LifetimeName::Fresh(_) => keywords::UnderscoreLifetime.name().as_str(),
653+
hir::LifetimeName::Ident(ident) => ident.as_interned_str(),
654+
hir::LifetimeName::Fresh(_) =>
655+
keywords::UnderscoreLifetime.ident().as_interned_str(),
655656
hir::LifetimeName::Implicit
656657
| hir::LifetimeName::Underscore
657658
| hir::LifetimeName::Static => {
@@ -663,7 +664,7 @@ impl<'a> LoweringContext<'a> {
663664
self.resolver.definitions().create_def_with_parent(
664665
parent_id.index,
665666
def_node_id,
666-
DefPathData::LifetimeDef(str_name.as_interned_str()),
667+
DefPathData::LifetimeDef(str_name),
667668
DefIndexAddressSpace::High,
668669
Mark::root(),
669670
span,
@@ -694,25 +695,25 @@ impl<'a> LoweringContext<'a> {
694695
/// lifetimes are enabled, then we want to push that lifetime into
695696
/// the vector of names to define later. In that case, it will get
696697
/// added to the appropriate generics.
697-
fn maybe_collect_in_band_lifetime(&mut self, span: Span, name: Name) {
698+
fn maybe_collect_in_band_lifetime(&mut self, ident: Ident) {
698699
if !self.is_collecting_in_band_lifetimes {
699700
return;
700701
}
701702

702-
if self.in_scope_lifetimes.contains(&name) {
703+
if self.in_scope_lifetimes.contains(&ident.modern()) {
703704
return;
704705
}
705706

706-
let hir_name = hir::LifetimeName::Name(name);
707+
let hir_name = hir::LifetimeName::Ident(ident);
707708

708709
if self.lifetimes_to_define
709710
.iter()
710-
.any(|(_, lt_name)| *lt_name == hir_name)
711+
.any(|(_, lt_name)| lt_name.modern() == hir_name.modern())
711712
{
712713
return;
713714
}
714715

715-
self.lifetimes_to_define.push((span, hir_name));
716+
self.lifetimes_to_define.push((ident.span, hir_name));
716717
}
717718

718719
/// When we have either an elided or `'_` lifetime in an impl
@@ -738,7 +739,7 @@ impl<'a> LoweringContext<'a> {
738739
F: FnOnce(&mut LoweringContext) -> T,
739740
{
740741
let old_len = self.in_scope_lifetimes.len();
741-
let lt_def_names = lt_defs.map(|lt_def| lt_def.lifetime.ident.name);
742+
let lt_def_names = lt_defs.map(|lt_def| lt_def.lifetime.ident.modern());
742743
self.in_scope_lifetimes.extend(lt_def_names);
743744

744745
let res = f(self);
@@ -757,7 +758,7 @@ impl<'a> LoweringContext<'a> {
757758
F: FnOnce(&mut LoweringContext) -> T,
758759
{
759760
let old_len = self.in_scope_lifetimes.len();
760-
let lt_def_names = lt_defs.iter().map(|lt_def| lt_def.lifetime.name.name());
761+
let lt_def_names = lt_defs.iter().map(|lt_def| lt_def.lifetime.name.ident().modern());
761762
self.in_scope_lifetimes.extend(lt_def_names);
762763

763764
let res = f(self);
@@ -1279,7 +1280,7 @@ impl<'a> LoweringContext<'a> {
12791280
}
12801281
}
12811282
name @ hir::LifetimeName::Fresh(_) => name,
1282-
name @ hir::LifetimeName::Name(_) => name,
1283+
name @ hir::LifetimeName::Ident(_) => name,
12831284
hir::LifetimeName::Static => return,
12841285
};
12851286

@@ -1298,7 +1299,7 @@ impl<'a> LoweringContext<'a> {
12981299
self.context.resolver.definitions().create_def_with_parent(
12991300
self.parent,
13001301
def_node_id,
1301-
DefPathData::LifetimeDef(name.name().as_interned_str()),
1302+
DefPathData::LifetimeDef(name.ident().as_interned_str()),
13021303
DefIndexAddressSpace::High,
13031304
Mark::root(),
13041305
lifetime.span,
@@ -1834,21 +1835,23 @@ impl<'a> LoweringContext<'a> {
18341835

18351836
fn lower_lifetime(&mut self, l: &Lifetime) -> hir::Lifetime {
18361837
let span = l.ident.span;
1837-
match self.lower_ident(l.ident) {
1838-
x if x == "'static" => self.new_named_lifetime(l.id, span, hir::LifetimeName::Static),
1839-
x if x == "'_" => match self.anonymous_lifetime_mode {
1840-
AnonymousLifetimeMode::CreateParameter => {
1841-
let fresh_name = self.collect_fresh_in_band_lifetime(span);
1842-
self.new_named_lifetime(l.id, span, fresh_name)
1843-
}
1838+
match l.ident {
1839+
ident if ident.name == keywords::StaticLifetime.name() =>
1840+
self.new_named_lifetime(l.id, span, hir::LifetimeName::Static),
1841+
ident if ident.name == keywords::UnderscoreLifetime.name() =>
1842+
match self.anonymous_lifetime_mode {
1843+
AnonymousLifetimeMode::CreateParameter => {
1844+
let fresh_name = self.collect_fresh_in_band_lifetime(span);
1845+
self.new_named_lifetime(l.id, span, fresh_name)
1846+
}
18441847

1845-
AnonymousLifetimeMode::PassThrough => {
1846-
self.new_named_lifetime(l.id, span, hir::LifetimeName::Underscore)
1847-
}
1848-
},
1849-
name => {
1850-
self.maybe_collect_in_band_lifetime(span, name);
1851-
self.new_named_lifetime(l.id, span, hir::LifetimeName::Name(name))
1848+
AnonymousLifetimeMode::PassThrough => {
1849+
self.new_named_lifetime(l.id, span, hir::LifetimeName::Underscore)
1850+
}
1851+
},
1852+
ident => {
1853+
self.maybe_collect_in_band_lifetime(ident);
1854+
self.new_named_lifetime(l.id, span, hir::LifetimeName::Ident(ident))
18521855
}
18531856
}
18541857
}

src/librustc/hir/map/def_collector.rs

+13-14
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,18 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
9191
// information we encapsulate into
9292
let def_data = match i.node {
9393
ItemKind::Impl(..) => DefPathData::Impl,
94-
ItemKind::Trait(..) => DefPathData::Trait(i.ident.name.as_interned_str()),
94+
ItemKind::Trait(..) => DefPathData::Trait(i.ident.as_interned_str()),
9595
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) |
9696
ItemKind::TraitAlias(..) |
9797
ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) | ItemKind::Ty(..) =>
98-
DefPathData::TypeNs(i.ident.name.as_interned_str()),
98+
DefPathData::TypeNs(i.ident.as_interned_str()),
9999
ItemKind::Mod(..) if i.ident == keywords::Invalid.ident() => {
100100
return visit::walk_item(self, i);
101101
}
102-
ItemKind::Mod(..) => DefPathData::Module(i.ident.name.as_interned_str()),
102+
ItemKind::Mod(..) => DefPathData::Module(i.ident.as_interned_str()),
103103
ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) =>
104-
DefPathData::ValueNs(i.ident.name.as_interned_str()),
105-
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.name.as_interned_str()),
104+
DefPathData::ValueNs(i.ident.as_interned_str()),
105+
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.as_interned_str()),
106106
ItemKind::Mac(..) => return self.visit_macro_invoc(i.id),
107107
ItemKind::GlobalAsm(..) => DefPathData::Misc,
108108
ItemKind::Use(..) => {
@@ -139,7 +139,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
139139
}
140140

141141
let def = self.create_def(foreign_item.id,
142-
DefPathData::ValueNs(foreign_item.ident.name.as_interned_str()),
142+
DefPathData::ValueNs(foreign_item.ident.as_interned_str()),
143143
REGULAR_SPACE,
144144
foreign_item.span);
145145

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

151151
fn visit_variant(&mut self, v: &'a Variant, g: &'a Generics, item_id: NodeId) {
152152
let def = self.create_def(v.node.data.id(),
153-
DefPathData::EnumVariant(v.node.ident
154-
.name.as_interned_str()),
153+
DefPathData::EnumVariant(v.node.ident.as_interned_str()),
155154
REGULAR_SPACE,
156155
v.span);
157156
self.with_parent(def, |this| visit::walk_variant(this, v, g, item_id));
@@ -175,15 +174,15 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
175174
GenericParam::Lifetime(ref lifetime_def) => {
176175
self.create_def(
177176
lifetime_def.lifetime.id,
178-
DefPathData::LifetimeDef(lifetime_def.lifetime.ident.name.as_interned_str()),
177+
DefPathData::LifetimeDef(lifetime_def.lifetime.ident.as_interned_str()),
179178
REGULAR_SPACE,
180179
lifetime_def.lifetime.ident.span
181180
);
182181
}
183182
GenericParam::Type(ref ty_param) => {
184183
self.create_def(
185184
ty_param.id,
186-
DefPathData::TypeParam(ty_param.ident.name.as_interned_str()),
185+
DefPathData::TypeParam(ty_param.ident.as_interned_str()),
187186
REGULAR_SPACE,
188187
ty_param.ident.span
189188
);
@@ -196,9 +195,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
196195
fn visit_trait_item(&mut self, ti: &'a TraitItem) {
197196
let def_data = match ti.node {
198197
TraitItemKind::Method(..) | TraitItemKind::Const(..) =>
199-
DefPathData::ValueNs(ti.ident.name.as_interned_str()),
198+
DefPathData::ValueNs(ti.ident.as_interned_str()),
200199
TraitItemKind::Type(..) => {
201-
DefPathData::AssocTypeInTrait(ti.ident.name.as_interned_str())
200+
DefPathData::AssocTypeInTrait(ti.ident.as_interned_str())
202201
},
203202
TraitItemKind::Macro(..) => return self.visit_macro_invoc(ti.id),
204203
};
@@ -210,8 +209,8 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
210209
fn visit_impl_item(&mut self, ii: &'a ImplItem) {
211210
let def_data = match ii.node {
212211
ImplItemKind::Method(..) | ImplItemKind::Const(..) =>
213-
DefPathData::ValueNs(ii.ident.name.as_interned_str()),
214-
ImplItemKind::Type(..) => DefPathData::AssocTypeInImpl(ii.ident.name.as_interned_str()),
212+
DefPathData::ValueNs(ii.ident.as_interned_str()),
213+
ImplItemKind::Type(..) => DefPathData::AssocTypeInImpl(ii.ident.as_interned_str()),
215214
ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id),
216215
};
217216

src/librustc/hir/map/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ impl<'hir> Map<'hir> {
910910
NodeTraitItem(ti) => ti.name,
911911
NodeVariant(v) => v.node.name,
912912
NodeField(f) => f.ident.name,
913-
NodeLifetime(lt) => lt.name.name(),
913+
NodeLifetime(lt) => lt.name.ident().name,
914914
NodeTyParam(tp) => tp.name,
915915
NodeBinding(&Pat { node: PatKind::Binding(_,_,l,_), .. }) => l.node,
916916
NodeStructCtor(_) => self.name(self.get_parent(id)),

src/librustc/hir/mod.rs

+22-10
Original file line numberDiff line numberDiff line change
@@ -229,19 +229,32 @@ pub enum LifetimeName {
229229
Static,
230230

231231
/// Some user-given name like `'x`
232-
Name(Name),
232+
Ident(Ident),
233233
}
234234

235235
impl LifetimeName {
236-
pub fn name(&self) -> Name {
237-
use self::LifetimeName::*;
236+
pub fn ident(&self) -> Ident {
238237
match *self {
239-
Implicit => keywords::Invalid.name(),
240-
Fresh(_) | Underscore => keywords::UnderscoreLifetime.name(),
241-
Static => keywords::StaticLifetime.name(),
242-
Name(name) => name,
238+
LifetimeName::Implicit => keywords::Invalid.ident(),
239+
LifetimeName::Fresh(_) | LifetimeName::Underscore =>
240+
keywords::UnderscoreLifetime.ident(),
241+
LifetimeName::Static => keywords::StaticLifetime.ident(),
242+
LifetimeName::Ident(ident) => ident,
243243
}
244244
}
245+
246+
pub fn modern(&self) -> LifetimeName {
247+
match *self {
248+
LifetimeName::Ident(ident) => LifetimeName::Ident(ident.modern()),
249+
lifetime_name => lifetime_name,
250+
}
251+
}
252+
}
253+
254+
impl fmt::Display for Lifetime {
255+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
256+
self.name.ident().fmt(f)
257+
}
245258
}
246259

247260
impl fmt::Debug for Lifetime {
@@ -255,16 +268,15 @@ impl fmt::Debug for Lifetime {
255268

256269
impl Lifetime {
257270
pub fn is_elided(&self) -> bool {
258-
use self::LifetimeName::*;
259271
match self.name {
260-
Implicit | Underscore => true,
272+
LifetimeName::Implicit | LifetimeName::Underscore => true,
261273

262274
// It might seem surprising that `Fresh(_)` counts as
263275
// *not* elided -- but this is because, as far as the code
264276
// in the compiler is concerned -- `Fresh(_)` variants act
265277
// equivalently to "some fresh name". They correspond to
266278
// early-bound regions on an impl, in other words.
267-
Fresh(_) | Static | Name(_) => false,
279+
LifetimeName::Fresh(_) | LifetimeName::Static | LifetimeName::Ident(_) => false,
268280
}
269281
}
270282

src/librustc/hir/print.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2054,7 +2054,7 @@ impl<'a> State<'a> {
20542054
}
20552055

20562056
pub fn print_lifetime(&mut self, lifetime: &hir::Lifetime) -> io::Result<()> {
2057-
self.print_name(lifetime.name.name())
2057+
self.print_ident(lifetime.name.ident())
20582058
}
20592059

20602060
pub fn print_lifetime_def(&mut self, lifetime: &hir::LifetimeDef) -> io::Result<()> {

src/librustc/ich/impls_hir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl_stable_hash_for!(enum hir::LifetimeName {
147147
Underscore,
148148
Fresh(index),
149149
Static,
150-
Name(name)
150+
Ident(ident)
151151
});
152152

153153
impl_stable_hash_for!(struct hir::Label {

0 commit comments

Comments
 (0)