From 5ccf44c02c1d4107cd451a2b6e397a00e4123bf1 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 27 May 2026 13:10:26 +0200 Subject: [PATCH] Track extra_lifetime_params_map per-owner --- compiler/rustc_ast_lowering/src/item.rs | 4 ++-- compiler/rustc_ast_lowering/src/lib.rs | 13 +------------ compiler/rustc_middle/src/ty/mod.rs | 17 ++++++++++++++--- compiler/rustc_resolve/src/late.rs | 1 + compiler/rustc_resolve/src/lib.rs | 5 +---- 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 56abe72e0319f..7e9827c4e494c 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -24,7 +24,7 @@ use super::diagnostics::{ use super::stability::{enabled_names, gate_unstable_abi}; use super::{ FnDeclKind, GenericArgsMode, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode, - RelaxedBoundForbiddenReason, RelaxedBoundPolicy, ResolverAstLoweringExt, + RelaxedBoundForbiddenReason, RelaxedBoundPolicy, }; use crate::diagnostics::ConstComptimeFn; @@ -1878,7 +1878,7 @@ impl<'hir> LoweringContext<'_, 'hir> { .collect(); // Introduce extra lifetimes if late resolution tells us to. - let extra_lifetimes = self.resolver.extra_lifetime_params(self.owner.id); + let extra_lifetimes = self.owner.extra_lifetime_params(self.owner.id); params.extend(extra_lifetimes.into_iter().map(|&(ident, node_id, kind)| { self.lifetime_res_to_generic_param( ident, diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index c6cd3fe5f5922..b30774318dee7 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -351,17 +351,6 @@ impl<'tcx> ResolverAstLowering<'tcx> { ) .map(|fn_indexes| fn_indexes.iter().map(|(num, _)| *num).collect()) } - - /// Obtain the list of lifetimes parameters to add to an item. - /// - /// Extra lifetime parameters should only be added in places that can appear - /// as a `binder` in `LifetimeRes`. - /// - /// The extra lifetimes that appear from the parenthesized `Fn`-trait desugaring - /// should appear at the enclosing `PolyTraitRef`. - fn extra_lifetime_params(&self, id: NodeId) -> &[(Ident, NodeId, MissingLifetimeKind)] { - self.extra_lifetime_params_map.get(&id).map_or(&[], |v| &v[..]) - } } /// How relaxed bounds `?Trait` should be treated. @@ -1129,7 +1118,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ) -> &'hir [hir::GenericParam<'hir>] { // Start by creating params for extra lifetimes params, as this creates the definitions // that may be referred to by the AST inside `generic_params`. - let extra_lifetimes = self.resolver.extra_lifetime_params(binder); + let extra_lifetimes = self.owner.extra_lifetime_params(binder); debug!(?extra_lifetimes); let extra_lifetimes: Vec<_> = extra_lifetimes .iter() diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index d90a709ffcd41..f1133cc52671c 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -28,7 +28,7 @@ use rustc_abi::{ Align, FieldIdx, Integer, IntegerType, ReprFlags, ReprOptions, ScalableElt, VariantIdx, }; use rustc_ast::node_id::NodeMap; -use rustc_ast::{self as ast}; +use rustc_ast::{self as ast, NodeId}; pub use rustc_ast_ir::{Movability, Mutability, try_visit}; use rustc_data_structures::fx::{FxIndexMap, FxIndexSet}; use rustc_data_structures::intern::Interned; @@ -224,6 +224,8 @@ pub struct PerOwnerResolverData<'tcx> { /// Resolution for import nodes, which have multiple resolutions in different namespaces. pub import_res: hir::def::PerNS>> = Default::default(), + /// Lifetime parameters that lowering will have to introduce. + pub extra_lifetime_params_map: NodeMap> = Default::default(), /// The id of the owner pub id: ast::NodeId, @@ -245,6 +247,17 @@ impl<'tcx> PerOwnerResolverData<'tcx> { pub fn get_lifetime_res(&self, id: ast::NodeId) -> Option { 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 + /// as a `binder` in `LifetimeRes`. + /// + /// The extra lifetimes that appear from the parenthesized `Fn`-trait desugaring + /// should appear at the enclosing `PolyTraitRef`. + pub fn extra_lifetime_params(&self, id: NodeId) -> &[(Ident, NodeId, MissingLifetimeKind)] { + self.extra_lifetime_params_map.get(&id).map_or(&[], |v| &v[..]) + } } /// Resolutions that should only be used for lowering. @@ -253,8 +266,6 @@ impl<'tcx> PerOwnerResolverData<'tcx> { pub struct ResolverAstLowering<'tcx> { /// Resolutions for nodes that have a single resolution. pub partial_res_map: NodeMap, - /// Lifetime parameters that lowering will have to introduce. - pub extra_lifetime_params_map: NodeMap>, pub next_node_id: ast::NodeId, diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 5cc8d5af4655a..ea55d1cb9a471 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -2170,6 +2170,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { // Record the created lifetime parameter so lowering can pick it up and add it to HIR. self.r + .current_owner .extra_lifetime_params_map .entry(binder) .or_insert_with(Vec::new) diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 40cad4241e5a3..35d6984d76ade 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -58,7 +58,7 @@ use rustc_hir::def::{ }; use rustc_hir::def_id::{CRATE_DEF_ID, CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalDefIdMap}; use rustc_hir::definitions::{PerParentDisambiguatorState, PerParentDisambiguatorsMap}; -use rustc_hir::{MissingLifetimeKind, PrimTy, TraitCandidate, find_attr}; +use rustc_hir::{PrimTy, TraitCandidate, find_attr}; use rustc_index::bit_set::DenseBitSet; use rustc_metadata::creader::CStore; use rustc_middle::metadata::{AmbigModChild, ModChild, Reexport}; @@ -1336,8 +1336,6 @@ pub struct Resolver<'ra, 'tcx> { partial_res_map: NodeMap = Default::default(), /// An import will be inserted into this map if it has been used. import_use_map: FxHashMap, Used> = default::fx_hash_map(), - /// Lifetime parameters that lowering will have to introduce. - extra_lifetime_params_map: NodeMap> = Default::default(), /// `CrateNum` resolutions of `extern crate` items. extern_crate_map: UnordMap = Default::default(), @@ -1975,7 +1973,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { }; let ast_lowering = ty::ResolverAstLowering { partial_res_map: self.partial_res_map, - extra_lifetime_params_map: self.extra_lifetime_params_map, next_node_id: self.next_node_id, owners: self.owners, lint_buffer: Steal::new(self.lint_buffer),