Skip to content

Commit 390aa5d

Browse files
committed
Introduce ParamName and use it in place of LifetimeName
1 parent 59feafd commit 390aa5d

File tree

13 files changed

+170
-174
lines changed

13 files changed

+170
-174
lines changed

src/librustc/hir/intravisit.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,10 @@ pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
433433
pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
434434
visitor.visit_id(lifetime.id);
435435
match lifetime.name {
436-
LifetimeName::Name(name) => {
436+
LifetimeName::Param(ParamName::Plain(name)) => {
437437
visitor.visit_name(lifetime.span, name);
438438
}
439-
LifetimeName::Fresh(_) |
439+
LifetimeName::Param(ParamName::Fresh(_)) |
440440
LifetimeName::Static |
441441
LifetimeName::Implicit |
442442
LifetimeName::Underscore => {}
@@ -742,20 +742,13 @@ pub fn walk_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v ParamBou
742742

743743
pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v GenericParam) {
744744
visitor.visit_id(param.id);
745+
match param.name {
746+
ParamName::Plain(name) => visitor.visit_name(param.span, name),
747+
ParamName::Fresh(_) => {}
748+
}
745749
match param.kind {
746-
GenericParamKind::Lifetime { ref lt_name, .. } => {
747-
match lt_name {
748-
LifetimeName::Name(name) => {
749-
visitor.visit_name(param.span, *name);
750-
}
751-
LifetimeName::Fresh(_) |
752-
LifetimeName::Static |
753-
LifetimeName::Implicit |
754-
LifetimeName::Underscore => {}
755-
}
756-
}
750+
GenericParamKind::Lifetime { .. } => {}
757751
GenericParamKind::Type { ref default, ref attrs, .. } => {
758-
visitor.visit_name(param.span, param.name);
759752
walk_list!(visitor, visit_ty, default);
760753
walk_list!(visitor, visit_attribute, attrs.iter());
761754
}

src/librustc/hir/lowering.rs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
//! in the HIR, especially for multiple identifiers.
4242
4343
use dep_graph::DepGraph;
44-
use hir;
44+
use hir::{self, ParamName};
4545
use hir::HirVec;
4646
use hir::map::{DefKey, DefPathData, Definitions};
4747
use hir::def_id::{DefId, DefIndex, DefIndexAddressSpace, CRATE_DEF_INDEX};
@@ -125,7 +125,7 @@ pub struct LoweringContext<'a> {
125125
// (i.e. it doesn't appear in the in_scope_lifetimes list), it is added
126126
// to this list. The results of this list are then added to the list of
127127
// lifetime definitions in the corresponding impl or function generics.
128-
lifetimes_to_define: Vec<(Span, hir::LifetimeName)>,
128+
lifetimes_to_define: Vec<(Span, ParamName)>,
129129

130130
// Whether or not in-band lifetimes are being collected. This is used to
131131
// indicate whether or not we're in a place where new lifetimes will result
@@ -678,13 +678,8 @@ 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-
hir::LifetimeName::Name(n) => n.as_str(),
682-
hir::LifetimeName::Fresh(_) => keywords::UnderscoreLifetime.name().as_str(),
683-
hir::LifetimeName::Implicit
684-
| hir::LifetimeName::Underscore
685-
| hir::LifetimeName::Static => {
686-
span_bug!(span, "unexpected in-band lifetime name: {:?}", hir_name)
687-
}
681+
ParamName::Plain(name) => name.as_str(),
682+
ParamName::Fresh(_) => keywords::UnderscoreLifetime.name().as_str(),
688683
};
689684

690685
// Add a definition for the in-band lifetime def
@@ -699,15 +694,12 @@ impl<'a> LoweringContext<'a> {
699694

700695
hir::GenericParam {
701696
id: def_node_id,
702-
name: hir_name.name(),
697+
name: hir_name,
703698
span,
704699
pure_wrt_drop: false,
705700
bounds: vec![].into(),
706-
kind: hir::GenericParamKind::Lifetime {
707-
lt_name: hir_name,
708-
in_band: true,
709-
}
710-
}
701+
kind: hir::GenericParamKind::Lifetime { in_band: true }
702+
}
711703
})
712704
.chain(in_band_ty_params.into_iter())
713705
.collect();
@@ -728,7 +720,7 @@ impl<'a> LoweringContext<'a> {
728720
return;
729721
}
730722

731-
let hir_name = hir::LifetimeName::Name(name);
723+
let hir_name = ParamName::Plain(name);
732724

733725
if self.lifetimes_to_define.iter().any(|(_, lt_name)| *lt_name == hir_name) {
734726
return;
@@ -739,10 +731,10 @@ impl<'a> LoweringContext<'a> {
739731

740732
/// When we have either an elided or `'_` lifetime in an impl
741733
/// header, we convert it to
742-
fn collect_fresh_in_band_lifetime(&mut self, span: Span) -> hir::LifetimeName {
734+
fn collect_fresh_in_band_lifetime(&mut self, span: Span) -> ParamName {
743735
assert!(self.is_collecting_in_band_lifetimes);
744736
let index = self.lifetimes_to_define.len();
745-
let hir_name = hir::LifetimeName::Fresh(index);
737+
let hir_name = ParamName::Fresh(index);
746738
self.lifetimes_to_define.push((span, hir_name));
747739
hir_name
748740
}
@@ -781,7 +773,7 @@ impl<'a> LoweringContext<'a> {
781773
{
782774
let old_len = self.in_scope_lifetimes.len();
783775
let lt_def_names = params.iter().filter_map(|param| match param.kind {
784-
hir::GenericParamKind::Lifetime { .. } => Some(param.name),
776+
hir::GenericParamKind::Lifetime { .. } => Some(param.name.name()),
785777
_ => None,
786778
});
787779
self.in_scope_lifetimes.extend(lt_def_names);
@@ -1244,7 +1236,7 @@ impl<'a> LoweringContext<'a> {
12441236
let name = Symbol::intern(&pprust::ty_to_string(t));
12451237
self.in_band_ty_params.push(hir::GenericParam {
12461238
id: def_node_id,
1247-
name,
1239+
name: ParamName::Plain(name),
12481240
span,
12491241
pure_wrt_drop: false,
12501242
bounds: hir_bounds,
@@ -1359,9 +1351,10 @@ impl<'a> LoweringContext<'a> {
13591351

13601352
fn visit_generic_param(&mut self, param: &'v hir::GenericParam) {
13611353
// Record the introduction of 'a in `for<'a> ...`
1362-
if let hir::GenericParamKind::Lifetime { lt_name, .. } = param.kind {
1354+
if let hir::GenericParamKind::Lifetime { .. } = param.kind {
13631355
// Introduce lifetimes one at a time so that we can handle
13641356
// cases like `fn foo<'d>() -> impl for<'a, 'b: 'a, 'c: 'b + 'd>`
1357+
let lt_name = hir::LifetimeName::Param(param.name);
13651358
self.currently_bound_lifetimes.push(lt_name);
13661359
}
13671360

@@ -1379,14 +1372,12 @@ impl<'a> LoweringContext<'a> {
13791372
return;
13801373
}
13811374
}
1382-
name @ hir::LifetimeName::Fresh(_) => name,
1383-
name @ hir::LifetimeName::Name(_) => name,
1375+
hir::LifetimeName::Param(_) => lifetime.name,
13841376
hir::LifetimeName::Static => return,
13851377
};
13861378

13871379
if !self.currently_bound_lifetimes.contains(&name)
1388-
&& !self.already_defined_lifetimes.contains(&name)
1389-
{
1380+
&& !self.already_defined_lifetimes.contains(&name) {
13901381
self.already_defined_lifetimes.insert(name);
13911382

13921383
self.output_lifetimes.push(hir::Lifetime {
@@ -1409,16 +1400,23 @@ impl<'a> LoweringContext<'a> {
14091400
lifetime.span,
14101401
);
14111402

1403+
let name = match name {
1404+
hir::LifetimeName::Underscore => {
1405+
hir::ParamName::Plain(keywords::UnderscoreLifetime.name())
1406+
}
1407+
hir::LifetimeName::Param(param_name) => param_name,
1408+
_ => bug!("expected LifetimeName::Param or ParamName::Plain"),
1409+
};
1410+
14121411
self.output_lifetime_params.push(hir::GenericParam {
14131412
id: def_node_id,
1414-
name: name.name(),
1413+
name,
14151414
span: lifetime.span,
14161415
pure_wrt_drop: false,
14171416
bounds: vec![].into(),
14181417
kind: hir::GenericParamKind::Lifetime {
1419-
lt_name: name,
14201418
in_band: false,
1421-
}
1419+
}
14221420
});
14231421
}
14241422
}
@@ -1894,7 +1892,7 @@ impl<'a> LoweringContext<'a> {
18941892
x if x == "'_" => match self.anonymous_lifetime_mode {
18951893
AnonymousLifetimeMode::CreateParameter => {
18961894
let fresh_name = self.collect_fresh_in_band_lifetime(span);
1897-
self.new_named_lifetime(l.id, span, fresh_name)
1895+
self.new_named_lifetime(l.id, span, hir::LifetimeName::Param(fresh_name))
18981896
}
18991897

19001898
AnonymousLifetimeMode::PassThrough => {
@@ -1903,7 +1901,8 @@ impl<'a> LoweringContext<'a> {
19031901
},
19041902
name => {
19051903
self.maybe_collect_in_band_lifetime(span, name);
1906-
self.new_named_lifetime(l.id, span, hir::LifetimeName::Name(name))
1904+
let param_name = ParamName::Plain(name);
1905+
self.new_named_lifetime(l.id, span, hir::LifetimeName::Param(param_name))
19071906
}
19081907
}
19091908
}
@@ -1942,16 +1941,17 @@ impl<'a> LoweringContext<'a> {
19421941
self.is_collecting_in_band_lifetimes = false;
19431942

19441943
let lt = self.lower_lifetime(&Lifetime { id: param.id, ident: param.ident });
1944+
let param_name = match lt.name {
1945+
hir::LifetimeName::Param(param_name) => param_name,
1946+
_ => hir::ParamName::Plain(lt.name.name()),
1947+
};
19451948
let param = hir::GenericParam {
19461949
id: lt.id,
1947-
name: lt.name.name(),
1950+
name: param_name,
19481951
span: lt.span,
19491952
pure_wrt_drop: attr::contains_name(&param.attrs, "may_dangle"),
19501953
bounds,
1951-
kind: hir::GenericParamKind::Lifetime {
1952-
lt_name: lt.name,
1953-
in_band: false,
1954-
}
1954+
kind: hir::GenericParamKind::Lifetime { in_band: false }
19551955
};
19561956

19571957
self.is_collecting_in_band_lifetimes = was_collecting_in_band;
@@ -1977,7 +1977,7 @@ impl<'a> LoweringContext<'a> {
19771977

19781978
hir::GenericParam {
19791979
id: self.lower_node_id(param.id).node_id,
1980-
name,
1980+
name: hir::ParamName::Plain(name),
19811981
span: param.ident.span,
19821982
pure_wrt_drop: attr::contains_name(&param.attrs, "may_dangle"),
19831983
bounds,
@@ -4193,7 +4193,7 @@ impl<'a> LoweringContext<'a> {
41934193
hir::Lifetime {
41944194
id: self.next_id().node_id,
41954195
span,
4196-
name: fresh_name,
4196+
name: hir::LifetimeName::Param(fresh_name),
41974197
}
41984198
}
41994199

src/librustc/hir/map/mod.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ impl<'hir> Map<'hir> {
497497
NodeGenericParam(param) => {
498498
Some(match param.kind {
499499
GenericParamKind::Lifetime { .. } => Def::Local(param.id),
500-
GenericParamKind::Type { .. } => Def::TyParam(self.local_def_id(param.id))
500+
GenericParamKind::Type { .. } => Def::TyParam(self.local_def_id(param.id)),
501501
})
502502
}
503503
}
@@ -616,11 +616,8 @@ impl<'hir> Map<'hir> {
616616
NodeItem(&Item { node: ItemTrait(..), .. }) => {
617617
keywords::SelfType.name()
618618
}
619-
NodeGenericParam(param) => param.name,
620-
_ => {
621-
bug!("ty_param_name: {} not a type parameter",
622-
self.node_to_string(id))
623-
}
619+
NodeGenericParam(param) => param.name.name(),
620+
_ => bug!("ty_param_name: {} not a type parameter", self.node_to_string(id)),
624621
}
625622
}
626623

@@ -957,7 +954,7 @@ impl<'hir> Map<'hir> {
957954
NodeVariant(v) => v.node.name,
958955
NodeField(f) => f.ident.name,
959956
NodeLifetime(lt) => lt.name.name(),
960-
NodeGenericParam(param) => param.name,
957+
NodeGenericParam(param) => param.name.name(),
961958
NodeBinding(&Pat { node: PatKind::Binding(_,_,l,_), .. }) => l.node,
962959
NodeStructCtor(_) => self.name(self.get_parent(id)),
963960
_ => bug!("no name for {}", self.node_to_string(id))

src/librustc/hir/mod.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,9 @@ pub struct Lifetime {
201201
}
202202

203203
#[derive(Debug, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)]
204-
pub enum LifetimeName {
205-
/// User typed nothing. e.g. the lifetime in `&u32`.
206-
Implicit,
207-
208-
/// User typed `'_`.
209-
Underscore,
204+
pub enum ParamName {
205+
/// Some user-given name like `T` or `'x`.
206+
Plain(Name),
210207

211208
/// Synthetic name generated when user elided a lifetime in an impl header,
212209
/// e.g. the lifetimes in cases like these:
@@ -222,22 +219,40 @@ pub enum LifetimeName {
222219
/// where `'f` is something like `Fresh(0)`. The indices are
223220
/// unique per impl, but not necessarily continuous.
224221
Fresh(usize),
222+
}
223+
224+
impl ParamName {
225+
pub fn name(&self) -> Name {
226+
match *self {
227+
ParamName::Plain(name) => name,
228+
ParamName::Fresh(_) => keywords::UnderscoreLifetime.name(),
229+
}
230+
}
231+
}
232+
233+
#[derive(Debug, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)]
234+
pub enum LifetimeName {
235+
/// User-given names or fresh (synthetic) names.
236+
Param(ParamName),
237+
238+
/// User typed nothing. e.g. the lifetime in `&u32`.
239+
Implicit,
240+
241+
/// User typed `'_`.
242+
Underscore,
225243

226244
/// User wrote `'static`
227245
Static,
228-
229-
/// Some user-given name like `'x`
230-
Name(Name),
231246
}
232247

233248
impl LifetimeName {
234249
pub fn name(&self) -> Name {
235250
use self::LifetimeName::*;
236251
match *self {
237252
Implicit => keywords::Invalid.name(),
238-
Fresh(_) | Underscore => keywords::UnderscoreLifetime.name(),
253+
Underscore => keywords::UnderscoreLifetime.name(),
239254
Static => keywords::StaticLifetime.name(),
240-
Name(name) => name,
255+
Param(param_name) => param_name.name(),
241256
}
242257
}
243258

@@ -251,7 +266,7 @@ impl LifetimeName {
251266
// in the compiler is concerned -- `Fresh(_)` variants act
252267
// equivalently to "some fresh name". They correspond to
253268
// early-bound regions on an impl, in other words.
254-
Fresh(_) | Static | Name(_) => false,
269+
Param(_) | Static => false,
255270
}
256271
}
257272

@@ -449,7 +464,6 @@ pub type ParamBounds = HirVec<ParamBound>;
449464
pub enum GenericParamKind {
450465
/// A lifetime definition, eg `'a: 'b + 'c + 'd`.
451466
Lifetime {
452-
lt_name: LifetimeName,
453467
// Indicates that the lifetime definition was synthetically added
454468
// as a result of an in-band lifetime usage like:
455469
// `fn foo(x: &'a u8) -> &'a u8 { x }`
@@ -465,7 +479,7 @@ pub enum GenericParamKind {
465479
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
466480
pub struct GenericParam {
467481
pub id: NodeId,
468-
pub name: Name,
482+
pub name: ParamName,
469483
pub bounds: ParamBounds,
470484
pub span: Span,
471485
pub pure_wrt_drop: bool,

src/librustc/hir/print.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2115,7 +2115,7 @@ impl<'a> State<'a> {
21152115
}
21162116

21172117
pub fn print_generic_param(&mut self, param: &GenericParam) -> io::Result<()> {
2118-
self.print_name(param.name)?;
2118+
self.print_name(param.name.name())?;
21192119
match param.kind {
21202120
GenericParamKind::Lifetime { .. } => {
21212121
let mut sep = ":";

0 commit comments

Comments
 (0)