Rename LifetimeName as LifetimeKind.

It's a much better name, more consistent with how we name such things.

Also rename `Lifetime::res` as `Lifetime::kind` to match. I suspect this
field used to have the type `LifetimeRes` and then the type was changed
but the field name remained the same.
This commit is contained in:
Nicholas Nethercote 2025-03-27 15:52:33 +11:00
parent 414da5b63d
commit fe882bf330
13 changed files with 68 additions and 68 deletions

View file

@ -1768,21 +1768,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
debug_assert_ne!(ident.name, kw::Empty);
let res = self.resolver.get_lifetime_res(id).unwrap_or(LifetimeRes::Error);
let res = match res {
LifetimeRes::Param { param, .. } => hir::LifetimeName::Param(param),
LifetimeRes::Param { param, .. } => hir::LifetimeKind::Param(param),
LifetimeRes::Fresh { param, .. } => {
debug_assert_eq!(ident.name, kw::UnderscoreLifetime);
let param = self.local_def_id(param);
hir::LifetimeName::Param(param)
hir::LifetimeKind::Param(param)
}
LifetimeRes::Infer => {
debug_assert_eq!(ident.name, kw::UnderscoreLifetime);
hir::LifetimeName::Infer
hir::LifetimeKind::Infer
}
LifetimeRes::Static { .. } => {
debug_assert!(matches!(ident.name, kw::StaticLifetime | kw::UnderscoreLifetime));
hir::LifetimeName::Static
hir::LifetimeKind::Static
}
LifetimeRes::Error => hir::LifetimeName::Error,
LifetimeRes::Error => hir::LifetimeKind::Error,
LifetimeRes::ElidedAnchor { .. } => {
panic!("Unexpected `ElidedAnchar` {:?} at {:?}", ident, ident.span);
}
@ -2389,7 +2389,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let r = hir::Lifetime::new(
self.next_id(),
Ident::new(kw::UnderscoreLifetime, self.lower_span(span)),
hir::LifetimeName::ImplicitObjectLifetimeDefault,
hir::LifetimeKind::ImplicitObjectLifetimeDefault,
IsAnonInPath::No,
);
debug!("elided_dyn_bound: r={:?}", r);

View file

@ -888,7 +888,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
// Skip `async` desugaring `impl Future`.
}
if let TyKind::TraitObject(_, lt) = alias_ty.kind {
if lt.res == hir::LifetimeName::ImplicitObjectLifetimeDefault {
if lt.kind == hir::LifetimeKind::ImplicitObjectLifetimeDefault {
spans_suggs.push((lt.ident.span.shrink_to_hi(), " + 'a".to_string()));
} else {
spans_suggs.push((lt.ident.span, "'a".to_string()));

View file

@ -42,7 +42,7 @@ pub enum IsAnonInPath {
}
/// A lifetime. The valid field combinations are non-obvious. The following
/// example shows some of them. See also the comments on `LifetimeName`.
/// example shows some of them. See also the comments on `LifetimeKind`.
/// ```
/// #[repr(C)]
/// struct S<'a>(&'a u32); // res=Param, name='a, IsAnonInPath::No
@ -84,7 +84,7 @@ pub struct Lifetime {
pub ident: Ident,
/// Semantics of this lifetime.
pub res: LifetimeName,
pub kind: LifetimeKind,
/// Is the lifetime anonymous and in a path? Used only for error
/// suggestions. See `Lifetime::suggestion` for example use.
@ -130,7 +130,7 @@ impl ParamName {
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable_Generic)]
pub enum LifetimeName {
pub enum LifetimeKind {
/// User-given names or fresh (synthetic) names.
Param(LocalDefId),
@ -160,16 +160,16 @@ pub enum LifetimeName {
Static,
}
impl LifetimeName {
impl LifetimeKind {
fn is_elided(&self) -> bool {
match self {
LifetimeName::ImplicitObjectLifetimeDefault | LifetimeName::Infer => true,
LifetimeKind::ImplicitObjectLifetimeDefault | LifetimeKind::Infer => true,
// It might seem surprising that `Fresh` counts as not *elided*
// -- but this is because, as far as the code in the compiler is
// concerned -- `Fresh` variants act equivalently to "some fresh name".
// They correspond to early-bound regions on an impl, in other words.
LifetimeName::Error | LifetimeName::Param(..) | LifetimeName::Static => false,
LifetimeKind::Error | LifetimeKind::Param(..) | LifetimeKind::Static => false,
}
}
}
@ -184,10 +184,10 @@ impl Lifetime {
pub fn new(
hir_id: HirId,
ident: Ident,
res: LifetimeName,
kind: LifetimeKind,
is_anon_in_path: IsAnonInPath,
) -> Lifetime {
let lifetime = Lifetime { hir_id, ident, res, is_anon_in_path };
let lifetime = Lifetime { hir_id, ident, kind, is_anon_in_path };
// Sanity check: elided lifetimes form a strict subset of anonymous lifetimes.
#[cfg(debug_assertions)]
@ -202,7 +202,7 @@ impl Lifetime {
}
pub fn is_elided(&self) -> bool {
self.res.is_elided()
self.kind.is_elided()
}
pub fn is_anonymous(&self) -> bool {
@ -1014,7 +1014,7 @@ pub struct WhereRegionPredicate<'hir> {
impl<'hir> WhereRegionPredicate<'hir> {
/// Returns `true` if `param_def_id` matches the `lifetime` of this predicate.
fn is_param_bound(&self, param_def_id: LocalDefId) -> bool {
self.lifetime.res == LifetimeName::Param(param_def_id)
self.lifetime.kind == LifetimeKind::Param(param_def_id)
}
}

View file

@ -57,7 +57,7 @@ fn trait_object_roundtrips_impl(syntax: TraitObjectSyntax) {
Lifetime {
hir_id: HirId::INVALID,
ident: Ident::new(sym::name, DUMMY_SP),
res: LifetimeName::Static,
kind: LifetimeKind::Static,
is_anon_in_path: IsAnonInPath::No,
}
},

View file

@ -16,7 +16,7 @@ use rustc_errors::ErrorGuaranteed;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::intravisit::{self, InferKind, Visitor, VisitorExt};
use rustc_hir::{
self as hir, AmbigArg, GenericArg, GenericParam, GenericParamKind, HirId, LifetimeName, Node,
self as hir, AmbigArg, GenericArg, GenericParam, GenericParamKind, HirId, LifetimeKind, Node,
};
use rustc_macros::extension;
use rustc_middle::hir::nested_filter;
@ -646,14 +646,14 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
arg: &'tcx hir::PreciseCapturingArg<'tcx>,
) -> Self::Result {
match *arg {
hir::PreciseCapturingArg::Lifetime(lt) => match lt.res {
LifetimeName::Param(def_id) => {
hir::PreciseCapturingArg::Lifetime(lt) => match lt.kind {
LifetimeKind::Param(def_id) => {
self.resolve_lifetime_ref(def_id, lt);
}
LifetimeName::Error => {}
LifetimeName::ImplicitObjectLifetimeDefault
| LifetimeName::Infer
| LifetimeName::Static => {
LifetimeKind::Error => {}
LifetimeKind::ImplicitObjectLifetimeDefault
| LifetimeKind::Infer
| LifetimeKind::Static => {
self.tcx.dcx().emit_err(errors::BadPreciseCapture {
span: lt.ident.span,
kind: "lifetime",
@ -774,26 +774,26 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
);
}
});
match lifetime.res {
LifetimeName::ImplicitObjectLifetimeDefault => {
match lifetime.kind {
LifetimeKind::ImplicitObjectLifetimeDefault => {
// If the user does not write *anything*, we
// use the object lifetime defaulting
// rules. So e.g., `Box<dyn Debug>` becomes
// `Box<dyn Debug + 'static>`.
self.resolve_object_lifetime_default(&*lifetime)
}
LifetimeName::Infer => {
LifetimeKind::Infer => {
// If the user writes `'_`, we use the *ordinary* elision
// rules. So the `'_` in e.g., `Box<dyn Debug + '_>` will be
// resolved the same as the `'_` in `&'_ Foo`.
//
// cc #48468
}
LifetimeName::Param(..) | LifetimeName::Static => {
LifetimeKind::Param(..) | LifetimeKind::Static => {
// If the user wrote an explicit name, use that.
self.visit_lifetime(&*lifetime);
}
LifetimeName::Error => {}
LifetimeKind::Error => {}
}
}
hir::TyKind::Ref(lifetime_ref, ref mt) => {
@ -873,17 +873,17 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
#[instrument(level = "debug", skip(self))]
fn visit_lifetime(&mut self, lifetime_ref: &'tcx hir::Lifetime) {
match lifetime_ref.res {
hir::LifetimeName::Static => {
match lifetime_ref.kind {
hir::LifetimeKind::Static => {
self.insert_lifetime(lifetime_ref, ResolvedArg::StaticLifetime)
}
hir::LifetimeName::Param(param_def_id) => {
hir::LifetimeKind::Param(param_def_id) => {
self.resolve_lifetime_ref(param_def_id, lifetime_ref)
}
// If we've already reported an error, just ignore `lifetime_ref`.
hir::LifetimeName::Error => {}
hir::LifetimeKind::Error => {}
// Those will be resolved by typechecking.
hir::LifetimeName::ImplicitObjectLifetimeDefault | hir::LifetimeName::Infer => {}
hir::LifetimeKind::ImplicitObjectLifetimeDefault | hir::LifetimeKind::Infer => {}
}
}
@ -1063,15 +1063,15 @@ fn object_lifetime_default(tcx: TyCtxt<'_>, param_def_id: LocalDefId) -> ObjectL
for bound in bound.bounds {
if let hir::GenericBound::Outlives(lifetime) = bound {
set.insert(lifetime.res);
set.insert(lifetime.kind);
}
}
}
match set {
Set1::Empty => ObjectLifetimeDefault::Empty,
Set1::One(hir::LifetimeName::Static) => ObjectLifetimeDefault::Static,
Set1::One(hir::LifetimeName::Param(param_def_id)) => {
Set1::One(hir::LifetimeKind::Static) => ObjectLifetimeDefault::Static,
Set1::One(hir::LifetimeKind::Param(param_def_id)) => {
ObjectLifetimeDefault::Param(param_def_id.to_def_id())
}
_ => ObjectLifetimeDefault::Ambiguous,
@ -1241,7 +1241,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
// Fresh lifetimes in APIT used to be allowed in async fns and forbidden in
// regular fns.
if let Some(hir::PredicateOrigin::ImplTrait) = where_bound_origin
&& let hir::LifetimeName::Param(param_id) = lifetime_ref.res
&& let hir::LifetimeKind::Param(param_id) = lifetime_ref.kind
&& let Some(generics) =
self.tcx.hir_get_generics(self.tcx.local_parent(param_id))
&& let Some(param) = generics.params.iter().find(|p| p.def_id == param_id)
@ -2440,7 +2440,7 @@ fn is_late_bound_map(
}
fn visit_lifetime(&mut self, lifetime_ref: &'v hir::Lifetime) {
if let hir::LifetimeName::Param(def_id) = lifetime_ref.res {
if let hir::LifetimeKind::Param(def_id) = lifetime_ref.kind {
self.regions.insert(def_id);
}
}
@ -2453,7 +2453,7 @@ fn is_late_bound_map(
impl<'tcx> Visitor<'tcx> for AllCollector {
fn visit_lifetime(&mut self, lifetime_ref: &'tcx hir::Lifetime) {
if let hir::LifetimeName::Param(def_id) = lifetime_ref.res {
if let hir::LifetimeKind::Param(def_id) = lifetime_ref.kind {
self.regions.insert(def_id);
}
}

View file

@ -415,7 +415,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
self.lower_lifetime(lifetime, RegionInferReason::ExplicitObjectLifetime)
} else {
let reason =
if let hir::LifetimeName::ImplicitObjectLifetimeDefault = lifetime.res {
if let hir::LifetimeKind::ImplicitObjectLifetimeDefault = lifetime.kind {
if let hir::Node::Ty(hir::Ty {
kind: hir::TyKind::Ref(parent_lifetime, _),
..

View file

@ -578,8 +578,8 @@ impl<'v> hir::intravisit::Visitor<'v> for TraitObjectVisitor<'v> {
match ty.kind {
hir::TyKind::TraitObject(_, tagged_ptr)
if let hir::Lifetime {
res:
hir::LifetimeName::ImplicitObjectLifetimeDefault | hir::LifetimeName::Static,
kind:
hir::LifetimeKind::ImplicitObjectLifetimeDefault | hir::LifetimeKind::Static,
..
} = tagged_ptr.pointer() =>
{

View file

@ -6,7 +6,7 @@ use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::{Visitor, VisitorExt, walk_ty};
use rustc_hir::{
self as hir, AmbigArg, GenericBound, GenericParam, GenericParamKind, Item, ItemKind, Lifetime,
LifetimeName, LifetimeParamKind, MissingLifetimeKind, Node, TyKind,
LifetimeKind, LifetimeParamKind, MissingLifetimeKind, Node, TyKind,
};
use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor};
use rustc_span::def_id::LocalDefId;
@ -165,7 +165,7 @@ pub fn suggest_new_region_bound(
if let Some(span) = opaque.bounds.iter().find_map(|arg| match arg {
GenericBound::Outlives(Lifetime {
res: LifetimeName::Static, ident, ..
kind: LifetimeKind::Static, ident, ..
}) => Some(ident.span),
_ => None,
}) {
@ -253,7 +253,7 @@ pub fn suggest_new_region_bound(
}
}
TyKind::TraitObject(_, lt) => {
if let LifetimeName::ImplicitObjectLifetimeDefault = lt.res {
if let LifetimeKind::ImplicitObjectLifetimeDefault = lt.kind {
err.span_suggestion_verbose(
fn_return.span.shrink_to_hi(),
format!("{declare} the trait object {captures}, {explicit}",),
@ -414,7 +414,7 @@ pub struct HirTraitObjectVisitor<'a>(pub &'a mut Vec<Span>, pub DefId);
impl<'a, 'tcx> Visitor<'tcx> for HirTraitObjectVisitor<'a> {
fn visit_ty(&mut self, t: &'tcx hir::Ty<'tcx, AmbigArg>) {
if let TyKind::TraitObject(poly_trait_refs, lifetime_ptr) = t.kind
&& let Lifetime { res: LifetimeName::ImplicitObjectLifetimeDefault, .. } =
&& let Lifetime { kind: LifetimeKind::ImplicitObjectLifetimeDefault, .. } =
lifetime_ptr.pointer()
{
for ptr in poly_trait_refs {

View file

@ -850,14 +850,14 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
add_lt_suggs: &mut Vec<(Span, String)>,
) -> String {
struct LifetimeReplaceVisitor<'a> {
needle: hir::LifetimeName,
needle: hir::LifetimeKind,
new_lt: &'a str,
add_lt_suggs: &'a mut Vec<(Span, String)>,
}
impl<'hir> hir::intravisit::Visitor<'hir> for LifetimeReplaceVisitor<'_> {
fn visit_lifetime(&mut self, lt: &'hir hir::Lifetime) {
if lt.res == self.needle {
if lt.kind == self.needle {
self.add_lt_suggs.push(lt.suggestion(self.new_lt));
}
}
@ -894,7 +894,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
};
let mut visitor = LifetimeReplaceVisitor {
needle: hir::LifetimeName::Param(lifetime_def_id),
needle: hir::LifetimeKind::Param(lifetime_def_id),
add_lt_suggs,
new_lt: &new_lt,
};

View file

@ -61,11 +61,11 @@ Here is a summary:
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Describe the *syntax* of a type: what the user wrote (with some desugaring). | Describe the *semantics* of a type: the meaning of what the user wrote. |
| Each `rustc_hir::Ty` has its own spans corresponding to the appropriate place in the program. | Doesnt correspond to a single place in the users program. |
| `rustc_hir::Ty` has generics and lifetimes; however, some of those lifetimes are special markers like [`LifetimeName::Implicit`][implicit]. | `ty::Ty` has the full type, including generics and lifetimes, even if the user left them out |
| `rustc_hir::Ty` has generics and lifetimes; however, some of those lifetimes are special markers like [`LifetimeKind::Implicit`][implicit]. | `ty::Ty` has the full type, including generics and lifetimes, even if the user left them out |
| `fn foo(x: u32) → u32 { }` - Two `rustc_hir::Ty` representing each usage of `u32`, each has its own `Span`s, and `rustc_hir::Ty` doesnt tell us that both are the same type | `fn foo(x: u32) → u32 { }` - One `ty::Ty` for all instances of `u32` throughout the program, and `ty::Ty` tells us that both usages of `u32` mean the same type. |
| `fn foo(x: &u32) -> &u32)` - Two `rustc_hir::Ty` again. Lifetimes for the references show up in the `rustc_hir::Ty`s using a special marker, [`LifetimeName::Implicit`][implicit]. | `fn foo(x: &u32) -> &u32)`- A single `ty::Ty`. The `ty::Ty` has the hidden lifetime param. |
| `fn foo(x: &u32) -> &u32)` - Two `rustc_hir::Ty` again. Lifetimes for the references show up in the `rustc_hir::Ty`s using a special marker, [`LifetimeKind::Implicit`][implicit]. | `fn foo(x: &u32) -> &u32)`- A single `ty::Ty`. The `ty::Ty` has the hidden lifetime param. |
[implicit]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.LifetimeName.html#variant.Implicit
[implicit]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.LifetimeKind.html#variant.Implicit
**Order**

View file

@ -14,7 +14,7 @@ use rustc_hir::intravisit::{
};
use rustc_hir::{
AmbigArg, BareFnTy, BodyId, FnDecl, FnSig, GenericArg, GenericArgs, GenericBound, GenericParam, GenericParamKind,
Generics, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind, Lifetime, LifetimeName, LifetimeParamKind, Node,
Generics, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind, Lifetime, LifetimeKind, LifetimeParamKind, Node,
PolyTraitRef, PredicateOrigin, TraitFn, TraitItem, TraitItemKind, Ty, TyKind, WhereBoundPredicate, WherePredicate,
WherePredicateKind, lang_items,
};
@ -218,7 +218,7 @@ fn check_fn_inner<'tcx>(
for bound in pred.bounds {
let mut visitor = RefVisitor::new(cx);
walk_param_bound(&mut visitor, bound);
if visitor.lts.iter().any(|lt| matches!(lt.res, LifetimeName::Param(_))) {
if visitor.lts.iter().any(|lt| matches!(lt.kind, LifetimeKind::Param(_))) {
return;
}
if let GenericBound::Trait(ref trait_ref) = *bound {
@ -235,7 +235,7 @@ fn check_fn_inner<'tcx>(
_ => None,
});
for bound in lifetimes {
if bound.res != LifetimeName::Static && !bound.is_elided() {
if bound.kind != LifetimeKind::Static && !bound.is_elided() {
return;
}
}
@ -421,8 +421,8 @@ fn named_lifetime_occurrences(lts: &[Lifetime]) -> Vec<(LocalDefId, usize)> {
}
fn named_lifetime(lt: &Lifetime) -> Option<LocalDefId> {
match lt.res {
LifetimeName::Param(id) if !lt.is_anonymous() => Some(id),
match lt.kind {
LifetimeKind::Param(id) if !lt.is_anonymous() => Some(id),
_ => None,
}
}
@ -614,7 +614,7 @@ where
// for lifetimes as parameters of generics
fn visit_lifetime(&mut self, lifetime: &'tcx Lifetime) {
if let LifetimeName::Param(def_id) = lifetime.res
if let LifetimeKind::Param(def_id) = lifetime.kind
&& let Some(usages) = self.map.get_mut(&def_id)
{
usages.push(Usage {
@ -826,7 +826,7 @@ fn report_elidable_lifetimes(
.iter()
.map(|&lt| cx.tcx.def_span(lt))
.chain(usages.iter().filter_map(|usage| {
if let LifetimeName::Param(def_id) = usage.res
if let LifetimeKind::Param(def_id) = usage.kind
&& elidable_lts.contains(&def_id)
{
return Some(usage.ident.span);

View file

@ -3,7 +3,7 @@ use clippy_utils::source::SpanRangeExt;
use clippy_utils::sugg::Sugg;
use clippy_utils::visitors::contains_unsafe_block;
use clippy_utils::{get_expr_use_or_unification_node, is_lint_allowed, path_def_id, path_to_local, std_or_core};
use hir::LifetimeName;
use hir::LifetimeKind;
use rustc_abi::ExternAbi;
use rustc_errors::{Applicability, MultiSpan};
use rustc_hir::hir_id::{HirId, HirIdMap};
@ -432,7 +432,7 @@ fn check_fn_args<'cx, 'tcx: 'cx>(
}
None
}) {
if let LifetimeName::Param(param_def_id) = lifetime.res
if let LifetimeKind::Param(param_def_id) = lifetime.kind
&& !lifetime.is_anonymous()
&& fn_sig
.output()

View file

@ -8,7 +8,7 @@ use rustc_hir::MatchSource::TryDesugar;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::{
AssocItemConstraint, BinOpKind, BindingMode, Block, BodyId, Closure, ConstArg, ConstArgKind, Expr, ExprField,
ExprKind, FnRetTy, GenericArg, GenericArgs, HirId, HirIdMap, InlineAsmOperand, LetExpr, Lifetime, LifetimeName,
ExprKind, FnRetTy, GenericArg, GenericArgs, HirId, HirIdMap, InlineAsmOperand, LetExpr, Lifetime, LifetimeKind,
Pat, PatExpr, PatExprKind, PatField, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, StructTailExpr,
TraitBoundModifiers, Ty, TyKind, TyPat, TyPatKind,
};
@ -483,7 +483,7 @@ impl HirEqInterExpr<'_, '_, '_> {
}
fn eq_lifetime(left: &Lifetime, right: &Lifetime) -> bool {
left.res == right.res
left.kind == right.kind
}
fn eq_pat_field(&mut self, left: &PatField<'_>, right: &PatField<'_>) -> bool {
@ -1245,8 +1245,8 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
pub fn hash_lifetime(&mut self, lifetime: &Lifetime) {
lifetime.ident.name.hash(&mut self.s);
std::mem::discriminant(&lifetime.res).hash(&mut self.s);
if let LifetimeName::Param(param_id) = lifetime.res {
std::mem::discriminant(&lifetime.kind).hash(&mut self.s);
if let LifetimeKind::Param(param_id) = lifetime.kind {
param_id.hash(&mut self.s);
}
}