diff options
| author | Camille GILLOT <gillot.camille@gmail.com> | 2025-07-13 13:31:16 +0000 |
|---|---|---|
| committer | Camille GILLOT <gillot.camille@gmail.com> | 2025-07-13 13:50:00 +0000 |
| commit | 3ecd03bdfd8da8464e2e381523adb15d60975254 (patch) | |
| tree | 2ec8fa41e1f7a66279f86d2365bfdfd94545f07a | |
| parent | 50ca0c6ab8eb742c88ed24018ff78973a1f68d37 (diff) | |
| download | rust-3ecd03bdfd8da8464e2e381523adb15d60975254.tar.gz rust-3ecd03bdfd8da8464e2e381523adb15d60975254.zip | |
Move trait_item_def_id from ImplItemRef to ImplItem.
| -rw-r--r-- | compiler/rustc_ast_lowering/src/index.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_ast_lowering/src/item.rs | 10 | ||||
| -rw-r--r-- | compiler/rustc_hir/src/hir.rs | 6 | ||||
| -rw-r--r-- | compiler/rustc_hir/src/intravisit.rs | 3 | ||||
| -rw-r--r-- | compiler/rustc_ty_utils/src/assoc.rs | 113 | ||||
| -rw-r--r-- | tests/ui/stats/input-stats.stderr | 4 |
6 files changed, 64 insertions, 74 deletions
diff --git a/compiler/rustc_ast_lowering/src/index.rs b/compiler/rustc_ast_lowering/src/index.rs index 956cb580d10..db67ce8dc16 100644 --- a/compiler/rustc_ast_lowering/src/index.rs +++ b/compiler/rustc_ast_lowering/src/index.rs @@ -392,7 +392,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { fn visit_impl_item_ref(&mut self, ii: &'hir ImplItemRef) { // Do not visit the duplicate information in ImplItemRef. We want to // map the actual nodes, not the duplicate ones in the *Ref. - let ImplItemRef { id, ident: _, kind: _, span: _, trait_item_def_id: _ } = *ii; + let ImplItemRef { id, ident: _, kind: _, span: _ } = *ii; self.visit_nested_impl_item(id); } diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index bdcb750ba26..b2a69c7c00f 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -1128,6 +1128,11 @@ impl<'hir> LoweringContext<'_, 'hir> { span: self.lower_span(i.span), defaultness, has_delayed_lints: !self.delayed_lints.is_empty(), + trait_item_def_id: self + .resolver + .get_partial_res(i.id) + .map(|r| r.expect_full_res().opt_def_id()) + .unwrap_or(None), }; self.arena.alloc(item) } @@ -1157,11 +1162,6 @@ impl<'hir> LoweringContext<'_, 'hir> { panic!("macros should have been expanded by now") } }, - trait_item_def_id: self - .resolver - .get_partial_res(i.id) - .map(|r| r.expect_full_res().opt_def_id()) - .unwrap_or(None), } } diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 0f6f81d7964..c57a8e648fb 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -3172,6 +3172,8 @@ pub struct ImplItem<'hir> { pub span: Span, pub vis_span: Span, pub has_delayed_lints: bool, + /// When we are in a trait impl, link to the trait-item's id. + pub trait_item_def_id: Option<DefId>, } impl<'hir> ImplItem<'hir> { @@ -4429,8 +4431,6 @@ pub struct ImplItemRef { pub ident: Ident, pub kind: AssocItemKind, pub span: Span, - /// When we are in a trait impl, link to the trait-item's id. - pub trait_item_def_id: Option<DefId>, } #[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)] @@ -4969,7 +4969,7 @@ mod size_asserts { static_assert_size!(GenericBound<'_>, 64); static_assert_size!(Generics<'_>, 56); static_assert_size!(Impl<'_>, 80); - static_assert_size!(ImplItem<'_>, 88); + static_assert_size!(ImplItem<'_>, 96); static_assert_size!(ImplItemKind<'_>, 40); static_assert_size!(Item<'_>, 88); static_assert_size!(ItemKind<'_>, 64); diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index 1bb8f7ad894..c40a6b081ef 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -1271,6 +1271,7 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>( span: _, vis_span: _, has_delayed_lints: _, + trait_item_def_id: _, } = *impl_item; try_visit!(visitor.visit_ident(ident)); @@ -1306,7 +1307,7 @@ pub fn walk_impl_item_ref<'v, V: Visitor<'v>>( visitor: &mut V, impl_item_ref: &'v ImplItemRef, ) -> V::Result { - let ImplItemRef { id, ident, ref kind, span: _, trait_item_def_id: _ } = *impl_item_ref; + let ImplItemRef { id, ident, ref kind, span: _ } = *impl_item_ref; try_visit!(visitor.visit_nested_impl_item(id)); try_visit!(visitor.visit_ident(ident)); visitor.visit_associated_item_kind(kind) diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index d71e8982871..d27f749762f 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -6,6 +6,8 @@ use rustc_hir::{self as hir, ItemKind}; use rustc_middle::query::Providers; use rustc_middle::ty::{self, ImplTraitInTraitData, TyCtxt}; use rustc_middle::{bug, span_bug}; +use rustc_span::Ident; +use rustc_span::symbol::kw; pub(crate) fn provide(providers: &mut Providers) { *providers = Providers { @@ -66,46 +68,33 @@ fn impl_item_implementor_ids(tcx: TyCtxt<'_>, impl_id: DefId) -> DefIdMap<DefId> } fn associated_item(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AssocItem { - let id = tcx.local_def_id_to_hir_id(def_id); - let parent_def_id = tcx.hir_get_parent_item(id); - let parent_item = tcx.hir_expect_item(parent_def_id.def_id); - match parent_item.kind { - hir::ItemKind::Impl(impl_) => { - if let Some(impl_item_ref) = impl_.items.iter().find(|i| i.id.owner_id.def_id == def_id) - { - let assoc_item = associated_item_from_impl_item_ref(impl_item_ref); - debug_assert_eq!(assoc_item.def_id.expect_local(), def_id); - return assoc_item; - } - } - - hir::ItemKind::Trait(.., trait_item_refs) => { - if let Some(trait_item_ref) = - trait_item_refs.iter().find(|i| i.id.owner_id.def_id == def_id) - { - let assoc_item = associated_item_from_trait_item_ref(trait_item_ref); - debug_assert_eq!(assoc_item.def_id.expect_local(), def_id); - return assoc_item; - } - } - - _ => {} - } + let assoc_item = match tcx.hir_node_by_def_id(def_id) { + hir::Node::TraitItem(ti) => associated_item_from_trait_item(tcx, ti), + hir::Node::ImplItem(ii) => associated_item_from_impl_item(tcx, ii), + node => span_bug!(tcx.def_span(def_id), "impl item or item not found: {:?}", node,), + }; + debug_assert_eq!(assoc_item.def_id.expect_local(), def_id); + assoc_item +} - span_bug!( - parent_item.span, - "unexpected parent of trait or impl item or item not found: {:?}", - parent_item.kind - ) +fn fn_has_self_parameter(tcx: TyCtxt<'_>, owner_id: hir::OwnerId) -> bool { + matches!(tcx.fn_arg_idents(owner_id.def_id), [Some(Ident { name: kw::SelfLower, .. }), ..]) } -fn associated_item_from_trait_item_ref(trait_item_ref: &hir::TraitItemRef) -> ty::AssocItem { - let owner_id = trait_item_ref.id.owner_id; - let name = trait_item_ref.ident.name; - let kind = match trait_item_ref.kind { - hir::AssocItemKind::Const => ty::AssocKind::Const { name }, - hir::AssocItemKind::Fn { has_self } => ty::AssocKind::Fn { name, has_self }, - hir::AssocItemKind::Type => ty::AssocKind::Type { data: ty::AssocTypeData::Normal(name) }, +fn associated_item_from_trait_item( + tcx: TyCtxt<'_>, + trait_item: &hir::TraitItem<'_>, +) -> ty::AssocItem { + let owner_id = trait_item.owner_id; + let name = trait_item.ident.name; + let kind = match trait_item.kind { + hir::TraitItemKind::Const { .. } => ty::AssocKind::Const { name }, + hir::TraitItemKind::Fn { .. } => { + ty::AssocKind::Fn { name, has_self: fn_has_self_parameter(tcx, owner_id) } + } + hir::TraitItemKind::Type { .. } => { + ty::AssocKind::Type { data: ty::AssocTypeData::Normal(name) } + } }; ty::AssocItem { @@ -116,19 +105,23 @@ fn associated_item_from_trait_item_ref(trait_item_ref: &hir::TraitItemRef) -> ty } } -fn associated_item_from_impl_item_ref(impl_item_ref: &hir::ImplItemRef) -> ty::AssocItem { - let def_id = impl_item_ref.id.owner_id; - let name = impl_item_ref.ident.name; - let kind = match impl_item_ref.kind { - hir::AssocItemKind::Const => ty::AssocKind::Const { name }, - hir::AssocItemKind::Fn { has_self } => ty::AssocKind::Fn { name, has_self }, - hir::AssocItemKind::Type => ty::AssocKind::Type { data: ty::AssocTypeData::Normal(name) }, +fn associated_item_from_impl_item(tcx: TyCtxt<'_>, impl_item: &hir::ImplItem<'_>) -> ty::AssocItem { + let owner_id = impl_item.owner_id; + let name = impl_item.ident.name; + let kind = match impl_item.kind { + hir::ImplItemKind::Const { .. } => ty::AssocKind::Const { name }, + hir::ImplItemKind::Fn { .. } => { + ty::AssocKind::Fn { name, has_self: fn_has_self_parameter(tcx, owner_id) } + } + hir::ImplItemKind::Type { .. } => { + ty::AssocKind::Type { data: ty::AssocTypeData::Normal(name) } + } }; ty::AssocItem { kind, - def_id: def_id.to_def_id(), - trait_item_def_id: impl_item_ref.trait_item_def_id, + def_id: owner_id.to_def_id(), + trait_item_def_id: impl_item.trait_item_def_id, container: ty::AssocItemContainer::Impl, } } @@ -196,17 +189,13 @@ fn associated_types_for_impl_traits_in_trait_or_impl<'tcx>( return None; } let did = item.id.owner_id.def_id.to_def_id(); + let item = tcx.hir_impl_item(item.id); let Some(trait_item_def_id) = item.trait_item_def_id else { return Some((did, vec![])); }; let iter = in_trait_def[&trait_item_def_id].iter().map(|&id| { - associated_type_for_impl_trait_in_impl( - tcx, - id, - item.id.owner_id.def_id, - disambiguator, - ) - .to_def_id() + associated_type_for_impl_trait_in_impl(tcx, id, item, disambiguator) + .to_def_id() }); Some((did, iter.collect())) }) @@ -285,20 +274,20 @@ fn associated_type_for_impl_trait_in_trait( /// Given an `trait_assoc_def_id` corresponding to an associated item synthesized /// from an `impl Trait` in an associated function from a trait, and an -/// `impl_fn_def_id` that represents an implementation of the associated function +/// `impl_fn` that represents an implementation of the associated function /// that the `impl Trait` comes from, synthesize an associated type for that `impl Trait` /// that inherits properties that we infer from the method and the associated type. fn associated_type_for_impl_trait_in_impl( tcx: TyCtxt<'_>, trait_assoc_def_id: DefId, - impl_fn_def_id: LocalDefId, + impl_fn: &hir::ImplItem<'_>, disambiguator: &mut DisambiguatorState, ) -> LocalDefId { - let impl_local_def_id = tcx.local_parent(impl_fn_def_id); + let impl_local_def_id = tcx.local_parent(impl_fn.owner_id.def_id); - let decl = tcx.hir_node_by_def_id(impl_fn_def_id).fn_decl().expect("expected decl"); - let span = match decl.output { - hir::FnRetTy::DefaultReturn(_) => tcx.def_span(impl_fn_def_id), + let hir::ImplItemKind::Fn(fn_sig, _) = impl_fn.kind else { bug!("expected decl") }; + let span = match fn_sig.decl.output { + hir::FnRetTy::DefaultReturn(_) => tcx.def_span(impl_fn.owner_id), hir::FnRetTy::Return(ty) => ty.span, }; @@ -329,7 +318,7 @@ fn associated_type_for_impl_trait_in_impl( impl_assoc_ty.associated_item(ty::AssocItem { kind: ty::AssocKind::Type { data: ty::AssocTypeData::Rpitit(ImplTraitInTraitData::Impl { - fn_def_id: impl_fn_def_id.to_def_id(), + fn_def_id: impl_fn.owner_id.to_def_id(), }), }, def_id, @@ -338,10 +327,10 @@ fn associated_type_for_impl_trait_in_impl( }); // Copy visility of the containing function. - impl_assoc_ty.visibility(tcx.visibility(impl_fn_def_id)); + impl_assoc_ty.visibility(tcx.visibility(impl_fn.owner_id)); // Copy defaultness of the containing function. - impl_assoc_ty.defaultness(tcx.defaultness(impl_fn_def_id)); + impl_assoc_ty.defaultness(tcx.defaultness(impl_fn.owner_id)); // Copy generics_of the trait's associated item but the impl as the parent. // FIXME: This may be detrimental to diagnostics, as we resolve the early-bound vars diff --git a/tests/ui/stats/input-stats.stderr b/tests/ui/stats/input-stats.stderr index eb038bbcaf1..7c92044ef5f 100644 --- a/tests/ui/stats/input-stats.stderr +++ b/tests/ui/stats/input-stats.stderr @@ -107,17 +107,17 @@ hir-stats WherePredicate 72 (NN.N%) 3 24 hir-stats - BoundPredicate 72 (NN.N%) 3 hir-stats Local 72 (NN.N%) 1 72 hir-stats InlineAsm 72 (NN.N%) 1 72 -hir-stats ImplItemRef 72 (NN.N%) 2 36 hir-stats Body 72 (NN.N%) 3 24 hir-stats Param 64 (NN.N%) 2 32 hir-stats GenericArg 64 (NN.N%) 4 16 hir-stats - Type 16 (NN.N%) 1 hir-stats - Lifetime 48 (NN.N%) 3 hir-stats TraitItemRef 56 (NN.N%) 2 28 +hir-stats ImplItemRef 56 (NN.N%) 2 28 hir-stats ExprField 40 (NN.N%) 1 40 hir-stats Mod 32 (NN.N%) 1 32 hir-stats Lifetime 28 (NN.N%) 1 28 hir-stats ForeignItemRef 24 (NN.N%) 1 24 hir-stats ---------------------------------------------------------------- -hir-stats Total 8_716 173 +hir-stats Total 8_700 173 hir-stats ================================================================ |
