Skip to content

Commit 99843d6

Browse files
committed
Don't use kw::Empty in hir::Lifetime::ident.
`hir::Lifetime::ident` currently sometimes uses `kw::Empty` for elided lifetimes and sometimes uses `kw::UnderscoreLifetime`, and the distinction is used when creating some error suggestions, e.g. in `Lifetime::suggestion` and `ImplicitLifetimeFinder::visit_ty`. I found this *really* confusing, and it took me a while to understand what was going on. This commit replaces all uses of `kw::Empty` in `hir::Lifetime::ident` with `kw::UnderscoreLifetime`. It adds a new field `hir::Lifetime::is_path_anon` that mostly replaces the old empty/underscore distinction and makes things much clearer. Some other notable changes: - Adds some assertions in `new_named_lifetime` about what ident values are permissible for the different `LifetimeRes` values. - Adds a `Lifetime::new` constructor that does some checking to make sure the `is_elided` and `is_anonymous` states are valid. - `add_static_impl_trait_suggestion` now looks at `Lifetime::res` instead of the ident when creating the suggestion. This is the one case where `is_path_anon` doesn't replace the old empty/underscore distinction.
1 parent b3db6fa commit 99843d6

File tree

8 files changed

+104
-46
lines changed

8 files changed

+104
-46
lines changed

compiler/rustc_ast_lowering/src/item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_ast::*;
55
use rustc_errors::ErrorGuaranteed;
66
use rustc_hir::def::{DefKind, Res};
77
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
8-
use rustc_hir::{self as hir, HirId, PredicateOrigin};
8+
use rustc_hir::{self as hir, HirId, IsAnonInPath, PredicateOrigin};
99
use rustc_index::{IndexSlice, IndexVec};
1010
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
1111
use rustc_span::edit_distance::find_best_match_for_name;
@@ -1787,7 +1787,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
17871787
}
17881788
GenericParamKind::Lifetime => {
17891789
let lt_id = self.next_node_id();
1790-
let lifetime = self.new_named_lifetime(id, lt_id, ident);
1790+
let lifetime = self.new_named_lifetime(id, lt_id, ident, IsAnonInPath::No);
17911791
hir::WherePredicateKind::RegionPredicate(hir::WhereRegionPredicate {
17921792
lifetime,
17931793
bounds,

compiler/rustc_ast_lowering/src/lib.rs

+41-13
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle, StashKey};
5555
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5656
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
5757
use rustc_hir::{
58-
self as hir, ConstArg, GenericArg, HirId, ItemLocalMap, LangItem, ParamName, TraitCandidate,
58+
self as hir, ConstArg, GenericArg, HirId, IsAnonInPath, ItemLocalMap, LangItem, ParamName,
59+
TraitCandidate,
5960
};
6061
use rustc_index::{Idx, IndexSlice, IndexVec};
6162
use rustc_macros::extension;
@@ -1777,7 +1778,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17771778
}
17781779

17791780
fn lower_lifetime(&mut self, l: &Lifetime) -> &'hir hir::Lifetime {
1780-
self.new_named_lifetime(l.id, l.id, l.ident)
1781+
self.new_named_lifetime(l.id, l.id, l.ident, IsAnonInPath::No)
1782+
}
1783+
1784+
fn lower_lifetime_anon_in_path(&mut self, id: NodeId, span: Span) -> &'hir hir::Lifetime {
1785+
self.new_named_lifetime(id, id, Ident::new(kw::UnderscoreLifetime, span), IsAnonInPath::Yes)
17811786
}
17821787

17831788
#[instrument(level = "debug", skip(self))]
@@ -1786,28 +1791,50 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17861791
id: NodeId,
17871792
new_id: NodeId,
17881793
ident: Ident,
1794+
is_anon_in_path: IsAnonInPath,
17891795
) -> &'hir hir::Lifetime {
1796+
debug_assert_ne!(ident.name, kw::Empty);
17901797
let res = self.resolver.get_lifetime_res(id).unwrap_or(LifetimeRes::Error);
17911798
let res = match res {
17921799
LifetimeRes::Param { param, .. } => hir::LifetimeName::Param(param),
17931800
LifetimeRes::Fresh { param, .. } => {
1801+
debug_assert_eq!(ident.name, kw::UnderscoreLifetime);
17941802
let param = self.local_def_id(param);
17951803
hir::LifetimeName::Param(param)
17961804
}
1797-
LifetimeRes::Infer => hir::LifetimeName::Infer,
1798-
LifetimeRes::Static { .. } => hir::LifetimeName::Static,
1805+
LifetimeRes::Infer => {
1806+
debug_assert_eq!(ident.name, kw::UnderscoreLifetime);
1807+
hir::LifetimeName::Infer
1808+
}
1809+
LifetimeRes::Static { .. } => {
1810+
// `kw::UnderscoreLifetime` occurs when `'static` is elided,
1811+
// e.g. in consts and statics such as `A` and `B` here:
1812+
// ```
1813+
// struct Name<'a>(&'a str);
1814+
// const A: Name = Name("name"); // njn: IsAnonInPath::Yes
1815+
// static B: &str = ""; // njn: IsAnonInPath::No
1816+
// ```
1817+
debug_assert!(matches!(ident.name, kw::StaticLifetime | kw::UnderscoreLifetime));
1818+
hir::LifetimeName::Static
1819+
}
17991820
LifetimeRes::Error => hir::LifetimeName::Error,
18001821
LifetimeRes::ElidedAnchor { .. } => {
18011822
panic!("Unexpected `ElidedAnchar` {:?} at {:?}", ident, ident.span);
18021823
}
18031824
};
18041825

1826+
#[cfg(debug_assertions)]
1827+
if is_anon_in_path == IsAnonInPath::Yes {
1828+
debug_assert_eq!(ident.name, kw::UnderscoreLifetime);
1829+
}
1830+
18051831
debug!(?res);
1806-
self.arena.alloc(hir::Lifetime {
1807-
hir_id: self.lower_node_id(new_id),
1808-
ident: self.lower_ident(ident),
1832+
self.arena.alloc(hir::Lifetime::new(
1833+
self.lower_node_id(new_id),
1834+
self.lower_ident(ident),
18091835
res,
1810-
})
1836+
is_anon_in_path,
1837+
))
18111838
}
18121839

18131840
fn lower_generic_params_mut(
@@ -2391,11 +2418,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23912418
/// when the bound is written, even if it is written with `'_` like in
23922419
/// `Box<dyn Debug + '_>`. In those cases, `lower_lifetime` is invoked.
23932420
fn elided_dyn_bound(&mut self, span: Span) -> &'hir hir::Lifetime {
2394-
let r = hir::Lifetime {
2395-
hir_id: self.next_id(),
2396-
ident: Ident::new(kw::Empty, self.lower_span(span)),
2397-
res: hir::LifetimeName::ImplicitObjectLifetimeDefault,
2398-
};
2421+
let r = hir::Lifetime::new(
2422+
self.next_id(),
2423+
Ident::new(kw::UnderscoreLifetime, self.lower_span(span)),
2424+
hir::LifetimeName::ImplicitObjectLifetimeDefault,
2425+
IsAnonInPath::No,
2426+
);
23992427
debug!("elided_dyn_bound: r={:?}", r);
24002428
self.arena.alloc(r)
24012429
}

compiler/rustc_ast_lowering/src/path.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir::def::{DefKind, PartialRes, Res};
77
use rustc_hir::def_id::DefId;
88
use rustc_middle::span_bug;
99
use rustc_session::parse::add_feature_diagnostics;
10-
use rustc_span::{BytePos, DUMMY_SP, DesugaringKind, Ident, Span, Symbol, kw, sym};
10+
use rustc_span::{BytePos, DUMMY_SP, DesugaringKind, Ident, Span, Symbol, sym};
1111
use smallvec::{SmallVec, smallvec};
1212
use tracing::{debug, instrument};
1313

@@ -450,10 +450,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
450450
0,
451451
(start.as_u32()..end.as_u32()).map(|i| {
452452
let id = NodeId::from_u32(i);
453-
let l = self.lower_lifetime(&Lifetime {
454-
id,
455-
ident: Ident::new(kw::Empty, elided_lifetime_span),
456-
});
453+
let l = self.lower_lifetime_anon_in_path(id, elided_lifetime_span);
457454
GenericArg::Lifetime(l)
458455
}),
459456
);

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -513,14 +513,14 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
513513
ty::VarianceDiagInfo::Invariant { ty, param_index } => {
514514
let (desc, note) = match ty.kind() {
515515
ty::RawPtr(ty, mutbl) => {
516-
assert_eq!(*mutbl, rustc_hir::Mutability::Mut);
516+
assert_eq!(*mutbl, hir::Mutability::Mut);
517517
(
518518
format!("a mutable pointer to `{}`", ty),
519519
"mutable pointers are invariant over their type parameter".to_string(),
520520
)
521521
}
522522
ty::Ref(_, inner_ty, mutbl) => {
523-
assert_eq!(*mutbl, rustc_hir::Mutability::Mut);
523+
assert_eq!(*mutbl, hir::Mutability::Mut);
524524
(
525525
format!("a mutable reference to `{inner_ty}`"),
526526
"mutable references are invariant over their type parameter"
@@ -887,7 +887,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
887887
// Skip `async` desugaring `impl Future`.
888888
}
889889
if let TyKind::TraitObject(_, lt) = alias_ty.kind {
890-
if lt.ident.name == kw::Empty {
890+
if lt.res == hir::LifetimeName::ImplicitObjectLifetimeDefault {
891891
spans_suggs.push((lt.ident.span.shrink_to_hi(), " + 'a".to_string()));
892892
} else {
893893
spans_suggs.push((lt.ident.span, "'a".to_string()));

compiler/rustc_hir/src/hir.rs

+41-12
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,27 @@ use crate::def_id::{DefId, LocalDefIdMap};
3535
pub(crate) use crate::hir_id::{HirId, ItemLocalId, ItemLocalMap, OwnerId};
3636
use crate::intravisit::{FnKind, VisitorExt};
3737

38+
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable_Generic)]
39+
pub enum IsAnonInPath {
40+
No,
41+
Yes,
42+
}
43+
3844
#[derive(Debug, Copy, Clone, HashStable_Generic)]
3945
pub struct Lifetime {
4046
#[stable_hasher(ignore)]
4147
pub hir_id: HirId,
4248

43-
/// Either "`'a`", referring to a named lifetime definition,
44-
/// `'_` referring to an anonymous lifetime (either explicitly `'_` or `&type`),
45-
/// or "``" (i.e., `kw::Empty`) when appearing in path.
46-
///
47-
/// See `Lifetime::suggestion_position` for practical use.
49+
/// Either a named lifetime definition (e.g. `'a`) or an anonymous lifetime
50+
/// (`'_`, either explicitly written, or inserted for things like `&type`).
4851
pub ident: Ident,
4952

5053
/// Semantics of this lifetime.
5154
pub res: LifetimeName,
55+
56+
/// Is the lifetime anonymous and in a path? Used only for error
57+
/// suggestions. See `Lifetime::suggestion` for example use.
58+
pub is_anon_in_path: IsAnonInPath,
5259
}
5360

5461
#[derive(Debug, Copy, Clone, HashStable_Generic)]
@@ -135,34 +142,56 @@ impl LifetimeName {
135142

136143
impl fmt::Display for Lifetime {
137144
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138-
if self.ident.name != kw::Empty { self.ident.name.fmt(f) } else { "'_".fmt(f) }
145+
self.ident.name.fmt(f)
139146
}
140147
}
141148

142149
impl Lifetime {
150+
pub fn new(
151+
hir_id: HirId,
152+
ident: Ident,
153+
res: LifetimeName,
154+
is_anon_in_path: IsAnonInPath,
155+
) -> Lifetime {
156+
let lifetime = Lifetime { hir_id, ident, res, is_anon_in_path };
157+
158+
// Sanity check: elided lifetimes form a strict subset of anonymous lifetimes.
159+
#[cfg(debug_assertions)]
160+
match (lifetime.is_elided(), lifetime.is_anonymous()) {
161+
(false, false) => {} // e.g. `'a`
162+
(false, true) => {} // e.g. explicit `'_`
163+
(true, true) => {} // e.g. `&x`
164+
(true, false) => panic!("bad Lifetime"),
165+
}
166+
167+
lifetime
168+
}
169+
143170
pub fn is_elided(&self) -> bool {
144171
self.res.is_elided()
145172
}
146173

147174
pub fn is_anonymous(&self) -> bool {
148-
self.ident.name == kw::Empty || self.ident.name == kw::UnderscoreLifetime
175+
self.ident.name == kw::UnderscoreLifetime
149176
}
150177

151178
pub fn suggestion(&self, new_lifetime: &str) -> (Span, String) {
152179
debug_assert!(new_lifetime.starts_with('\''));
153180

154-
match (self.ident.name.is_empty(), self.ident.span.is_empty()) {
181+
match (self.is_anon_in_path, self.ident.span.is_empty()) {
155182
// The user wrote `Path<T>`, and omitted the `'_,`.
156-
(true, true) => (self.ident.span, format!("{new_lifetime}, ")),
183+
(IsAnonInPath::Yes, true) => (self.ident.span, format!("{new_lifetime}, ")),
157184

158185
// The user wrote `Path` and omitted the `<'_>`.
159-
(true, false) => (self.ident.span.shrink_to_hi(), format!("<{new_lifetime}>")),
186+
(IsAnonInPath::Yes, false) => {
187+
(self.ident.span.shrink_to_hi(), format!("<{new_lifetime}>"))
188+
}
160189

161190
// The user wrote `&type` or `&mut type`.
162-
(false, true) => (self.ident.span, format!("{new_lifetime} ")),
191+
(IsAnonInPath::No, true) => (self.ident.span, format!("{new_lifetime} ")),
163192

164193
// The user wrote `'a` or `'_`.
165-
(false, false) => (self.ident.span, format!("{new_lifetime}")),
194+
(IsAnonInPath::No, false) => (self.ident.span, format!("{new_lifetime}")),
166195
}
167196
}
168197
}

compiler/rustc_hir/src/hir/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ fn trait_object_roundtrips_impl(syntax: TraitObjectSyntax) {
5858
hir_id: HirId::INVALID,
5959
ident: Ident::new(sym::name, DUMMY_SP),
6060
res: LifetimeName::Static,
61+
is_anon_in_path: IsAnonInPath::No,
6162
}
6263
},
6364
syntax,

compiler/rustc_trait_selection/src/errors.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_errors::{
99
use rustc_hir::def::DefKind;
1010
use rustc_hir::def_id::{DefId, LocalDefId};
1111
use rustc_hir::intravisit::{Visitor, VisitorExt, walk_ty};
12-
use rustc_hir::{self as hir, AmbigArg, FnRetTy, GenericParamKind, Node};
12+
use rustc_hir::{self as hir, AmbigArg, FnRetTy, GenericParamKind, IsAnonInPath, Node};
1313
use rustc_macros::{Diagnostic, Subdiagnostic};
1414
use rustc_middle::ty::print::{PrintTraitRefExt as _, TraitRefPrintOnlyTraitPath};
1515
use rustc_middle::ty::{self, Binder, ClosureKind, FnSig, Region, Ty, TyCtxt};
@@ -567,10 +567,14 @@ impl Subdiagnostic for AddLifetimeParamsSuggestion<'_> {
567567

568568
impl<'v> Visitor<'v> for ImplicitLifetimeFinder {
569569
fn visit_ty(&mut self, ty: &'v hir::Ty<'v, AmbigArg>) {
570-
let make_suggestion = |ident: Ident| {
571-
if ident.name == kw::Empty && ident.span.is_empty() {
570+
let make_suggestion = |lifetime: &hir::Lifetime| {
571+
if lifetime.is_anon_in_path == IsAnonInPath::Yes
572+
&& lifetime.ident.span.is_empty()
573+
{
572574
format!("{}, ", self.suggestion_param_name)
573-
} else if ident.name == kw::UnderscoreLifetime && ident.span.is_empty() {
575+
} else if lifetime.ident.name == kw::UnderscoreLifetime
576+
&& lifetime.ident.span.is_empty()
577+
{
574578
format!("{} ", self.suggestion_param_name)
575579
} else {
576580
self.suggestion_param_name.clone()
@@ -584,7 +588,7 @@ impl Subdiagnostic for AddLifetimeParamsSuggestion<'_> {
584588
matches!(
585589
arg,
586590
hir::GenericArg::Lifetime(lifetime)
587-
if lifetime.ident.name == kw::Empty
591+
if lifetime.is_anon_in_path == IsAnonInPath::Yes
588592
)
589593
}) {
590594
self.suggestions.push((
@@ -605,7 +609,7 @@ impl Subdiagnostic for AddLifetimeParamsSuggestion<'_> {
605609
{
606610
self.suggestions.push((
607611
lifetime.ident.span,
608-
make_suggestion(lifetime.ident),
612+
make_suggestion(lifetime),
609613
));
610614
}
611615
}
@@ -614,8 +618,7 @@ impl Subdiagnostic for AddLifetimeParamsSuggestion<'_> {
614618
}
615619
}
616620
hir::TyKind::Ref(lifetime, ..) if lifetime.is_anonymous() => {
617-
self.suggestions
618-
.push((lifetime.ident.span, make_suggestion(lifetime.ident)));
621+
self.suggestions.push((lifetime.ident.span, make_suggestion(lifetime)));
619622
}
620623
_ => {}
621624
}

tests/ui/stats/input-stats.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ hir-stats HIR STATS
119119
hir-stats Name Accumulated Size Count Item Size
120120
hir-stats ----------------------------------------------------------------
121121
hir-stats ForeignItemRef 24 ( 0.3%) 1 24
122-
hir-stats Lifetime 24 ( 0.3%) 1 24
122+
hir-stats Lifetime 28 ( 0.3%) 1 28
123123
hir-stats Mod 32 ( 0.4%) 1 32
124124
hir-stats ExprField 40 ( 0.4%) 1 40
125125
hir-stats TraitItemRef 56 ( 0.6%) 2 28
@@ -155,7 +155,7 @@ hir-stats Generics 560 ( 6.2%) 10 56
155155
hir-stats Ty 720 ( 8.0%) 15 48
156156
hir-stats - Ptr 48 ( 0.5%) 1
157157
hir-stats - Ref 48 ( 0.5%) 1
158-
hir-stats - Path 624 ( 7.0%) 13
158+
hir-stats - Path 624 ( 6.9%) 13
159159
hir-stats Expr 768 ( 8.6%) 12 64
160160
hir-stats - InlineAsm 64 ( 0.7%) 1
161161
hir-stats - Match 64 ( 0.7%) 1
@@ -174,5 +174,5 @@ hir-stats - Use 352 ( 3.9%) 4
174174
hir-stats Path 1_240 (13.8%) 31 40
175175
hir-stats PathSegment 1_920 (21.4%) 40 48
176176
hir-stats ----------------------------------------------------------------
177-
hir-stats Total 8_976 180
177+
hir-stats Total 8_980 180
178178
hir-stats

0 commit comments

Comments
 (0)