Track lifetimes_res_map per owner

This commit is contained in:
Oli Scherer
2026-05-28 10:34:07 +02:00
parent 5b37028e31
commit b7ea5d8ab7
5 changed files with 21 additions and 26 deletions

View File

@@ -293,11 +293,6 @@ impl<'tcx> ResolverAstLowering<'tcx> {
self.import_res_map.get(&id).copied().unwrap_or_default()
}
/// Obtains resolution for a lifetime with the given `NodeId`.
fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes> {
self.lifetimes_res_map.get(&id).copied()
}
/// Obtain the list of lifetimes parameters to add to an item.
///
/// Extra lifetime parameters should only be added in places that can appear
@@ -1603,7 +1598,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
None => {
let id = if let Some(LifetimeRes::ElidedAnchor { start, end }) =
self.resolver.get_lifetime_res(t.id)
self.owner.get_lifetime_res(t.id)
{
assert_eq!(start.plus(1), end);
start
@@ -2012,7 +2007,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
source: LifetimeSource,
syntax: LifetimeSyntax,
) -> &'hir hir::Lifetime {
let res = if let Some(res) = self.resolver.get_lifetime_res(id) {
let res = if let Some(res) = self.owner.get_lifetime_res(id) {
match res {
LifetimeRes::Param { param, .. } => hir::LifetimeKind::Param(param),
LifetimeRes::Fresh { param, .. } => {
@@ -2098,13 +2093,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
// AST resolution emitted an error on those parameters, so we lower them using
// `ParamName::Error`.
let ident = self.lower_ident(param.ident);
let param_name = if let Some(LifetimeRes::Error(..)) =
self.resolver.get_lifetime_res(param.id)
{
ParamName::Error(ident)
} else {
ParamName::Plain(ident)
};
let param_name =
if let Some(LifetimeRes::Error(..)) = self.owner.get_lifetime_res(param.id) {
ParamName::Error(ident)
} else {
ParamName::Plain(ident)
};
let kind =
hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Explicit };

View File

@@ -17,7 +17,7 @@ use super::errors::{
};
use super::{
AllowReturnTypeNotation, GenericArgsCtor, GenericArgsMode, ImplTraitContext, ImplTraitPosition,
LifetimeRes, LoweringContext, ParamMode, ResolverAstLoweringExt,
LifetimeRes, LoweringContext, ParamMode,
};
impl<'hir> LoweringContext<'_, 'hir> {
@@ -422,7 +422,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
segment_ident_span: Span,
generic_args: &mut GenericArgsCtor<'hir>,
) {
let (start, end) = match self.resolver.get_lifetime_res(segment_id) {
let (start, end) = match self.owner.get_lifetime_res(segment_id) {
Some(LifetimeRes::ElidedAnchor { start, end }) => (start, end),
None => return,
Some(res) => {

View File

@@ -211,6 +211,8 @@ pub struct PerOwnerResolverData {
/// Resolutions for labels.
/// Maps from NodeId of the break/continue expression to the NodeId of their corresponding blocks or loops.
pub label_res_map: NodeMap<ast::NodeId> = Default::default(),
/// Resolutions for lifetimes.
pub lifetimes_res_map: NodeMap<LifetimeRes> = Default::default(),
/// The id of the owner
pub id: ast::NodeId,
@@ -227,6 +229,11 @@ impl PerOwnerResolverData {
pub fn get_label_res(&self, id: ast::NodeId) -> Option<ast::NodeId> {
self.label_res_map.get(&id).copied()
}
/// Obtains resolution for a lifetime with the given `NodeId`.
pub fn get_lifetime_res(&self, id: ast::NodeId) -> Option<LifetimeRes> {
self.lifetimes_res_map.get(&id).copied()
}
}
/// Resolutions that should only be used for lowering.
@@ -237,8 +244,6 @@ pub struct ResolverAstLowering<'tcx> {
pub partial_res_map: NodeMap<hir::def::PartialRes>,
/// Resolutions for import nodes, which have multiple resolutions in different namespaces.
pub import_res_map: NodeMap<hir::def::PerNS<Option<Res<ast::NodeId>>>>,
/// Resolutions for lifetimes.
pub lifetimes_res_map: NodeMap<LifetimeRes>,
/// Lifetime parameters that lowering will have to introduce.
pub extra_lifetime_params_map: NodeMap<Vec<(Ident, ast::NodeId, MissingLifetimeKind)>>,

View File

@@ -2406,7 +2406,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
/// Define a new lifetime (e.g. in generics)
#[instrument(level = "debug", skip(self))]
fn record_lifetime_def(&mut self, id: NodeId, res: LifetimeRes) {
if let Some(prev_res) = self.r.lifetimes_res_map.insert(id, res) {
if let Some(prev_res) = self.r.current_owner.lifetimes_res_map.insert(id, res) {
panic!(
"lifetime parameter {id:?} resolved multiple times ({prev_res:?} before, {res:?} now)"
)
@@ -2611,11 +2611,11 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
let lt_id = if let Some(lt) = lt {
lt.id
} else {
let res = self.r.lifetimes_res_map[&ty.id];
let res = self.r.current_owner.lifetimes_res_map[&ty.id];
let LifetimeRes::ElidedAnchor { start, .. } = res else { bug!() };
start
};
let lt_res = self.r.lifetimes_res_map[&lt_id];
let lt_res = self.r.current_owner.lifetimes_res_map[&lt_id];
trace!("FindReferenceVisitor inserting res={:?}", lt_res);
self.lifetime.insert(lt_res);
}

View File

@@ -53,8 +53,7 @@ use rustc_feature::BUILTIN_ATTRIBUTES;
use rustc_hir::attrs::StrippedCfgItem;
use rustc_hir::def::Namespace::{self, *};
use rustc_hir::def::{
self, CtorOf, DefKind, DocLinkResMap, LifetimeRes, MacroKinds, NonMacroAttrKind, PartialRes,
PerNS,
self, CtorOf, DefKind, DocLinkResMap, MacroKinds, NonMacroAttrKind, PartialRes, PerNS,
};
use rustc_hir::def_id::{CRATE_DEF_ID, CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalDefIdMap};
use rustc_hir::definitions::{PerParentDisambiguatorState, PerParentDisambiguatorsMap};
@@ -1369,8 +1368,6 @@ pub struct Resolver<'ra, 'tcx> {
import_res_map: NodeMap<PerNS<Option<Res>>> = Default::default(),
/// An import will be inserted into this map if it has been used.
import_use_map: FxHashMap<Import<'ra>, Used> = default::fx_hash_map(),
/// Resolutions for lifetimes.
lifetimes_res_map: NodeMap<LifetimeRes> = Default::default(),
/// Lifetime parameters that lowering will have to introduce.
extra_lifetime_params_map: NodeMap<Vec<(Ident, NodeId, MissingLifetimeKind)>> = Default::default(),
@@ -2001,7 +1998,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let ast_lowering = ty::ResolverAstLowering {
partial_res_map: self.partial_res_map,
import_res_map: self.import_res_map,
lifetimes_res_map: self.lifetimes_res_map,
extra_lifetime_params_map: self.extra_lifetime_params_map,
next_node_id: self.next_node_id,
owners: self.owners,