Skip to content

Commit fb7d25e

Browse files
committed
Separate lifetime ident from resolution in HIR.
1 parent d121aa3 commit fb7d25e

File tree

28 files changed

+179
-275
lines changed

28 files changed

+179
-275
lines changed

compiler/rustc_ast_lowering/src/index.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
303303
}
304304

305305
fn visit_lifetime(&mut self, lifetime: &'hir Lifetime) {
306-
self.insert(lifetime.span, lifetime.hir_id, Node::Lifetime(lifetime));
306+
self.insert(lifetime.ident.span, lifetime.hir_id, Node::Lifetime(lifetime));
307307
}
308308

309309
fn visit_variant(&mut self, v: &'hir Variant<'hir>) {

compiler/rustc_ast_lowering/src/item.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1479,10 +1479,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
14791479
}))
14801480
}
14811481
GenericParamKind::Lifetime => {
1482-
let ident_span = self.lower_span(ident.span);
14831482
let ident = self.lower_ident(ident);
14841483
let lt_id = self.next_node_id();
1485-
let lifetime = self.new_named_lifetime(id, lt_id, ident_span, ident);
1484+
let lifetime = self.new_named_lifetime(id, lt_id, ident);
14861485
Some(hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
14871486
lifetime,
14881487
span,

compiler/rustc_ast_lowering/src/lib.rs

+17-36
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12551255
} else {
12561256
self.next_node_id()
12571257
};
1258-
let span = self.tcx.sess.source_map().start_point(t.span);
1258+
let span = self.tcx.sess.source_map().start_point(t.span).shrink_to_hi();
12591259
Lifetime { ident: Ident::new(kw::UnderscoreLifetime, span), id }
12601260
});
12611261
let lifetime = self.lower_lifetime(&region);
@@ -1546,15 +1546,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15461546
let lifetimes =
15471547
self.arena.alloc_from_iter(collected_lifetimes.into_iter().map(|(_, lifetime)| {
15481548
let id = self.next_node_id();
1549-
let span = lifetime.ident.span;
1550-
1551-
let ident = if lifetime.ident.name == kw::UnderscoreLifetime {
1552-
Ident::with_dummy_span(kw::UnderscoreLifetime)
1553-
} else {
1554-
lifetime.ident
1555-
};
1556-
1557-
let l = self.new_named_lifetime(lifetime.id, id, span, ident);
1549+
let l = self.new_named_lifetime(lifetime.id, id, lifetime.ident);
15581550
hir::GenericArg::Lifetime(l)
15591551
}));
15601552
debug!(?lifetimes);
@@ -2014,18 +2006,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20142006
let generic_args = self.arena.alloc_from_iter(collected_lifetimes.into_iter().map(
20152007
|(_, lifetime, res)| {
20162008
let id = self.next_node_id();
2017-
let span = lifetime.ident.span;
2018-
2019-
let ident = if lifetime.ident.name == kw::UnderscoreLifetime {
2020-
Ident::with_dummy_span(kw::UnderscoreLifetime)
2021-
} else {
2022-
lifetime.ident
2023-
};
2024-
20252009
let res = res.unwrap_or(
20262010
self.resolver.get_lifetime_res(lifetime.id).unwrap_or(LifetimeRes::Error),
20272011
);
2028-
hir::GenericArg::Lifetime(self.new_named_lifetime_with_res(id, span, ident, res))
2012+
hir::GenericArg::Lifetime(self.new_named_lifetime_with_res(id, lifetime.ident, res))
20292013
},
20302014
));
20312015

@@ -2095,43 +2079,41 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20952079
}
20962080

20972081
fn lower_lifetime(&mut self, l: &Lifetime) -> &'hir hir::Lifetime {
2098-
let span = self.lower_span(l.ident.span);
20992082
let ident = self.lower_ident(l.ident);
2100-
self.new_named_lifetime(l.id, l.id, span, ident)
2083+
self.new_named_lifetime(l.id, l.id, ident)
21012084
}
21022085

21032086
#[instrument(level = "debug", skip(self))]
21042087
fn new_named_lifetime_with_res(
21052088
&mut self,
21062089
id: NodeId,
2107-
span: Span,
21082090
ident: Ident,
21092091
res: LifetimeRes,
21102092
) -> &'hir hir::Lifetime {
2111-
let name = match res {
2093+
let res = match res {
21122094
LifetimeRes::Param { param, .. } => {
2113-
let p_name = ParamName::Plain(ident);
21142095
let param = self.get_remapped_def_id(param);
2115-
2116-
hir::LifetimeName::Param(param, p_name)
2096+
hir::LifetimeName::Param(param)
21172097
}
21182098
LifetimeRes::Fresh { param, .. } => {
21192099
debug_assert_eq!(ident.name, kw::UnderscoreLifetime);
21202100
let param = self.local_def_id(param);
2121-
2122-
hir::LifetimeName::Param(param, ParamName::Fresh)
2101+
hir::LifetimeName::Param(param)
21232102
}
21242103
LifetimeRes::Infer => hir::LifetimeName::Infer,
21252104
LifetimeRes::Static => hir::LifetimeName::Static,
21262105
LifetimeRes::Error => hir::LifetimeName::Error,
2127-
res => panic!("Unexpected lifetime resolution {:?} for {:?} at {:?}", res, ident, span),
2106+
res => panic!(
2107+
"Unexpected lifetime resolution {:?} for {:?} at {:?}",
2108+
res, ident, ident.span
2109+
),
21282110
};
21292111

2130-
debug!(?name);
2112+
debug!(?res);
21312113
self.arena.alloc(hir::Lifetime {
21322114
hir_id: self.lower_node_id(id),
2133-
span: self.lower_span(span),
2134-
name,
2115+
ident: self.lower_ident(ident),
2116+
res,
21352117
})
21362118
}
21372119

@@ -2140,11 +2122,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21402122
&mut self,
21412123
id: NodeId,
21422124
new_id: NodeId,
2143-
span: Span,
21442125
ident: Ident,
21452126
) -> &'hir hir::Lifetime {
21462127
let res = self.resolver.get_lifetime_res(id).unwrap_or(LifetimeRes::Error);
2147-
self.new_named_lifetime_with_res(new_id, span, ident, res)
2128+
self.new_named_lifetime_with_res(new_id, ident, res)
21482129
}
21492130

21502131
fn lower_generic_params_mut<'s>(
@@ -2556,8 +2537,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25562537
fn elided_dyn_bound(&mut self, span: Span) -> &'hir hir::Lifetime {
25572538
let r = hir::Lifetime {
25582539
hir_id: self.next_id(),
2559-
span: self.lower_span(span),
2560-
name: hir::LifetimeName::ImplicitObjectLifetimeDefault,
2540+
ident: Ident::new(kw::Empty, self.lower_span(span)),
2541+
res: hir::LifetimeName::ImplicitObjectLifetimeDefault,
25612542
};
25622543
debug!("elided_dyn_bound: r={:?}", r);
25632544
self.arena.alloc(r)

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2509,7 +2509,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
25092509
if let hir::TyKind::Rptr(lifetime, _) = &fn_decl.inputs[index].kind {
25102510
// With access to the lifetime, we can get
25112511
// the span of it.
2512-
arguments.push((*argument, lifetime.span));
2512+
arguments.push((*argument, lifetime.ident.span));
25132513
} else {
25142514
bug!("ty type is a ref but hir type is not");
25152515
}
@@ -2528,7 +2528,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
25282528
let mut return_span = fn_decl.output.span();
25292529
if let hir::FnRetTy::Return(ty) = &fn_decl.output {
25302530
if let hir::TyKind::Rptr(lifetime, _) = ty.kind {
2531-
return_span = lifetime.span;
2531+
return_span = lifetime.ident.span;
25322532
}
25332533
}
25342534

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ fn get_mut_span_in_struct_field<'tcx>(
12111211
&& let hir::Node::Field(field) = node
12121212
&& let hir::TyKind::Rptr(lt, hir::MutTy { mutbl: hir::Mutability::Not, ty }) = field.ty.kind
12131213
{
1214-
return Some(lt.span.between(ty.span));
1214+
return Some(lt.ident.span.between(ty.span));
12151215
}
12161216

12171217
None

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+4-24
Original file line numberDiff line numberDiff line change
@@ -576,30 +576,10 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
576576
let args = last_segment.args.as_ref()?;
577577
let lifetime =
578578
self.try_match_adt_and_generic_args(substs, needle_fr, args, search_stack)?;
579-
match lifetime.name {
580-
hir::LifetimeName::Param(_, hir::ParamName::Plain(_) | hir::ParamName::Error)
581-
| hir::LifetimeName::Error
582-
| hir::LifetimeName::Static => {
583-
let lifetime_span = lifetime.span;
584-
Some(RegionNameHighlight::MatchedAdtAndSegment(lifetime_span))
585-
}
586-
587-
hir::LifetimeName::Param(_, hir::ParamName::Fresh)
588-
| hir::LifetimeName::ImplicitObjectLifetimeDefault
589-
| hir::LifetimeName::Infer => {
590-
// In this case, the user left off the lifetime; so
591-
// they wrote something like:
592-
//
593-
// ```
594-
// x: Foo<T>
595-
// ```
596-
//
597-
// where the fully elaborated form is `Foo<'_, '1,
598-
// T>`. We don't consider this a match; instead we let
599-
// the "fully elaborated" type fallback above handle
600-
// it.
601-
None
602-
}
579+
if lifetime.is_anonymous() {
580+
None
581+
} else {
582+
Some(RegionNameHighlight::MatchedAdtAndSegment(lifetime.ident.span))
603583
}
604584
}
605585

compiler/rustc_hir/src/hir.rs

+28-46
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ use std::fmt;
2929
#[derive(Debug, Copy, Clone, Encodable, HashStable_Generic)]
3030
pub struct Lifetime {
3131
pub hir_id: HirId,
32-
pub span: Span,
32+
pub ident: Ident,
3333

3434
/// Either "`'a`", referring to a named lifetime definition,
3535
/// or "``" (i.e., `kw::Empty`), for elision placeholders.
3636
///
3737
/// HIR lowering inserts these placeholders in type paths that
3838
/// refer to type definitions needing lifetime parameters,
3939
/// `&T` and `&mut T`, and trait objects without `... + 'a`.
40-
pub name: LifetimeName,
40+
pub res: LifetimeName,
4141
}
4242

4343
#[derive(Debug, Clone, PartialEq, Eq, Encodable, Hash, Copy)]
@@ -88,7 +88,7 @@ impl ParamName {
8888
#[derive(HashStable_Generic)]
8989
pub enum LifetimeName {
9090
/// User-given names or fresh (synthetic) names.
91-
Param(LocalDefId, ParamName),
91+
Param(LocalDefId),
9292

9393
/// Implicit lifetime in a context like `dyn Foo`. This is
9494
/// distinguished from implicit lifetimes elsewhere because the
@@ -116,25 +116,6 @@ pub enum LifetimeName {
116116
}
117117

118118
impl LifetimeName {
119-
pub fn ident(&self) -> Ident {
120-
match *self {
121-
LifetimeName::ImplicitObjectLifetimeDefault | LifetimeName::Error => Ident::empty(),
122-
LifetimeName::Infer => Ident::with_dummy_span(kw::UnderscoreLifetime),
123-
LifetimeName::Static => Ident::with_dummy_span(kw::StaticLifetime),
124-
LifetimeName::Param(_, param_name) => param_name.ident(),
125-
}
126-
}
127-
128-
pub fn is_anonymous(&self) -> bool {
129-
match *self {
130-
LifetimeName::ImplicitObjectLifetimeDefault
131-
| LifetimeName::Infer
132-
| LifetimeName::Param(_, ParamName::Fresh)
133-
| LifetimeName::Error => true,
134-
LifetimeName::Static | LifetimeName::Param(..) => false,
135-
}
136-
}
137-
138119
pub fn is_elided(&self) -> bool {
139120
match self {
140121
LifetimeName::ImplicitObjectLifetimeDefault | LifetimeName::Infer => true,
@@ -146,34 +127,25 @@ impl LifetimeName {
146127
LifetimeName::Error | LifetimeName::Param(..) | LifetimeName::Static => false,
147128
}
148129
}
149-
150-
fn is_static(&self) -> bool {
151-
self == &LifetimeName::Static
152-
}
153-
154-
pub fn normalize_to_macros_2_0(&self) -> LifetimeName {
155-
match *self {
156-
LifetimeName::Param(def_id, param_name) => {
157-
LifetimeName::Param(def_id, param_name.normalize_to_macros_2_0())
158-
}
159-
lifetime_name => lifetime_name,
160-
}
161-
}
162130
}
163131

164132
impl fmt::Display for Lifetime {
165133
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
166-
self.name.ident().fmt(f)
134+
self.ident.fmt(f)
167135
}
168136
}
169137

170138
impl Lifetime {
171139
pub fn is_elided(&self) -> bool {
172-
self.name.is_elided()
140+
self.res.is_elided()
141+
}
142+
143+
pub fn is_anonymous(&self) -> bool {
144+
self.ident.name == kw::Empty || self.ident.name == kw::UnderscoreLifetime
173145
}
174146

175147
pub fn is_static(&self) -> bool {
176-
self.name.is_static()
148+
self.res == LifetimeName::Static
177149
}
178150
}
179151

@@ -267,7 +239,7 @@ pub enum GenericArg<'hir> {
267239
impl GenericArg<'_> {
268240
pub fn span(&self) -> Span {
269241
match self {
270-
GenericArg::Lifetime(l) => l.span,
242+
GenericArg::Lifetime(l) => l.ident.span,
271243
GenericArg::Type(t) => t.span,
272244
GenericArg::Const(c) => c.span,
273245
GenericArg::Infer(i) => i.span,
@@ -284,7 +256,7 @@ impl GenericArg<'_> {
284256
}
285257

286258
pub fn is_synthetic(&self) -> bool {
287-
matches!(self, GenericArg::Lifetime(lifetime) if lifetime.name.ident() == Ident::empty())
259+
matches!(self, GenericArg::Lifetime(lifetime) if lifetime.ident == Ident::empty())
288260
}
289261

290262
pub fn descr(&self) -> &'static str {
@@ -446,7 +418,7 @@ impl GenericBound<'_> {
446418
match self {
447419
GenericBound::Trait(t, ..) => t.span,
448420
GenericBound::LangItemTrait(_, span, ..) => *span,
449-
GenericBound::Outlives(l) => l.span,
421+
GenericBound::Outlives(l) => l.ident.span,
450422
}
451423
}
452424
}
@@ -559,6 +531,19 @@ impl<'hir> Generics<'hir> {
559531
}
560532
}
561533

534+
/// If there are generic parameters, return where to introduce a new one.
535+
pub fn span_for_lifetime_suggestion(&self) -> Option<Span> {
536+
if let Some(first) = self.params.first()
537+
&& self.span.contains(first.span)
538+
{
539+
// `fn foo<A>(t: impl Trait)`
540+
// ^ suggest `'a, ` here
541+
Some(first.span.shrink_to_lo())
542+
} else {
543+
None
544+
}
545+
}
546+
562547
/// If there are generic parameters, return where to introduce a new one.
563548
pub fn span_for_param_suggestion(&self) -> Option<Span> {
564549
if self.params.iter().any(|p| self.span.contains(p.span)) {
@@ -765,10 +750,7 @@ pub struct WhereRegionPredicate<'hir> {
765750
impl<'hir> WhereRegionPredicate<'hir> {
766751
/// Returns `true` if `param_def_id` matches the `lifetime` of this predicate.
767752
pub fn is_param_bound(&self, param_def_id: LocalDefId) -> bool {
768-
match self.lifetime.name {
769-
LifetimeName::Param(id, _) => id == param_def_id,
770-
_ => false,
771-
}
753+
self.lifetime.res == LifetimeName::Param(param_def_id)
772754
}
773755
}
774756

@@ -3453,7 +3435,7 @@ impl<'hir> Node<'hir> {
34533435
| Node::Variant(Variant { ident, .. })
34543436
| Node::Item(Item { ident, .. })
34553437
| Node::PathSegment(PathSegment { ident, .. }) => Some(*ident),
3456-
Node::Lifetime(lt) => Some(lt.name.ident()),
3438+
Node::Lifetime(lt) => Some(lt.ident),
34573439
Node::GenericParam(p) => Some(p.name.ident()),
34583440
Node::TypeBinding(b) => Some(b.ident),
34593441
Node::Param(..)

compiler/rustc_hir/src/intravisit.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -1109,17 +1109,7 @@ pub fn walk_generic_arg<'v, V: Visitor<'v>>(visitor: &mut V, generic_arg: &'v Ge
11091109

11101110
pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
11111111
visitor.visit_id(lifetime.hir_id);
1112-
match lifetime.name {
1113-
LifetimeName::Param(_, ParamName::Plain(ident)) => {
1114-
visitor.visit_ident(ident);
1115-
}
1116-
LifetimeName::Param(_, ParamName::Fresh)
1117-
| LifetimeName::Param(_, ParamName::Error)
1118-
| LifetimeName::Static
1119-
| LifetimeName::Error
1120-
| LifetimeName::ImplicitObjectLifetimeDefault
1121-
| LifetimeName::Infer => {}
1122-
}
1112+
visitor.visit_ident(lifetime.ident);
11231113
}
11241114

11251115
pub fn walk_qpath<'v, V: Visitor<'v>>(visitor: &mut V, qpath: &'v QPath<'v>, id: HirId) {

compiler/rustc_hir/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![feature(associated_type_defaults)]
66
#![feature(closure_track_caller)]
77
#![feature(const_btree_len)]
8+
#![feature(let_chains)]
89
#![feature(min_specialization)]
910
#![feature(never_type)]
1011
#![feature(rustc_attrs)]

0 commit comments

Comments
 (0)