From 833b9154ac7d41d7040129468d6794444533c03f Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 15 Dec 2022 13:20:27 -0300 Subject: [PATCH 1/5] Make encode_attrs use opt_local_def_id_to_hir_id so we can feed it with None for definitions that have no HIR --- compiler/rustc_metadata/src/rmeta/encoder.rs | 4 ++-- compiler/rustc_middle/src/hir/mod.rs | 6 +++--- compiler/rustc_middle/src/query/mod.rs | 7 +++---- compiler/rustc_middle/src/ty/context.rs | 4 ++++ 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index e941dd45688..16029822d03 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1137,8 +1137,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { is_doc_hidden: false, }; let attr_iter = tcx - .hir() - .attrs(tcx.hir().local_def_id_to_hir_id(def_id)) + .opt_local_def_id_to_hir_id(def_id) + .map_or(Default::default(), |hir_id| tcx.hir().attrs(hir_id)) .iter() .filter(|attr| analyze_attr(attr, &mut state)); diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index dc89f762b76..2e2ca6a2788 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -121,13 +121,13 @@ pub fn provide(providers: &mut Providers) { let node = owner.node(); Some(Owner { node, hash_without_bodies: owner.nodes.hash_without_bodies }) }; - providers.local_def_id_to_hir_id = |tcx, id| { + providers.opt_local_def_id_to_hir_id = |tcx, id| { let owner = tcx.hir_crate(()).owners[id].map(|_| ()); - match owner { + Some(match owner { MaybeOwner::Owner(_) => HirId::make_owner(id), MaybeOwner::Phantom => bug!("No HirId for {:?}", id), MaybeOwner::NonOwner(hir_id) => hir_id, - } + }) }; providers.hir_owner_nodes = |tcx, id| tcx.hir_crate(()).owners[id.def_id].map(|i| &i.nodes); providers.hir_owner_parent = |tcx, id| { diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 1ccad25fe08..8b846f10603 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -85,11 +85,10 @@ rustc_queries! { desc { |tcx| "getting HIR owner of `{}`", tcx.def_path_str(key.to_def_id()) } } - /// Gives access to the HIR ID for the given `LocalDefId` owner `key`. + /// Gives access to the HIR ID for the given `LocalDefId` owner `key` if any. /// - /// This can be conveniently accessed by methods on `tcx.hir()`. - /// Avoid calling this query directly. - query local_def_id_to_hir_id(key: LocalDefId) -> hir::HirId { + /// Definitions that were generated with no HIR, would be feeded to return `None`. + query opt_local_def_id_to_hir_id(key: LocalDefId) -> Option{ desc { |tcx| "getting HIR ID of `{}`", tcx.def_path_str(key.to_def_id()) } } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 117f3decd1a..dc7e4ac900a 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -2428,6 +2428,10 @@ impl<'tcx> TyCtxt<'tcx> { ) } + pub fn local_def_id_to_hir_id(self, local_def_id: LocalDefId) -> HirId { + self.opt_local_def_id_to_hir_id(local_def_id).unwrap() + } + pub fn trait_solver_next(self) -> bool { self.sess.opts.unstable_opts.trait_solver == rustc_session::config::TraitSolver::Next } From 4e703a277220a3f52b4772f298535df232b6cb7c Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 26 Oct 2022 16:38:32 -0300 Subject: [PATCH 2/5] Add associated_items_for_impl_trait_in_trait query --- compiler/rustc_hir/src/definitions.rs | 6 ++- .../src/rmeta/decoder/cstore_impl.rs | 2 + compiler/rustc_metadata/src/rmeta/encoder.rs | 9 +++++ compiler/rustc_metadata/src/rmeta/mod.rs | 1 + compiler/rustc_middle/src/hir/map/mod.rs | 7 ++++ compiler/rustc_middle/src/query/mod.rs | 7 ++++ .../src/typeid/typeid_itanium_cxx_abi.rs | 1 + compiler/rustc_symbol_mangling/src/v0.rs | 1 + compiler/rustc_ty_utils/src/assoc.rs | 38 ++++++++++++++++++- 9 files changed, 68 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_hir/src/definitions.rs b/compiler/rustc_hir/src/definitions.rs index cd3c620cbb7..8ceb176491b 100644 --- a/compiler/rustc_hir/src/definitions.rs +++ b/compiler/rustc_hir/src/definitions.rs @@ -280,6 +280,8 @@ pub enum DefPathData { AnonConst, /// An `impl Trait` type node. ImplTrait, + /// `impl Trait` generated associated type node. + ImplTraitAssocTy, } impl Definitions { @@ -403,7 +405,7 @@ impl DefPathData { TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name), Impl | ForeignMod | CrateRoot | Use | GlobalAsm | ClosureExpr | Ctor | AnonConst - | ImplTrait => None, + | ImplTrait | ImplTraitAssocTy => None, } } @@ -422,7 +424,7 @@ impl DefPathData { ClosureExpr => DefPathDataName::Anon { namespace: sym::closure }, Ctor => DefPathDataName::Anon { namespace: sym::constructor }, AnonConst => DefPathDataName::Anon { namespace: sym::constant }, - ImplTrait => DefPathDataName::Anon { namespace: sym::opaque }, + ImplTrait | ImplTraitAssocTy => DefPathDataName::Anon { namespace: sym::opaque }, } } } diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 0bacf51e911..9ca8c1abc16 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -248,6 +248,8 @@ provide! { tcx, def_id, other, cdata, .process_decoded(tcx, || panic!("{def_id:?} does not have trait_impl_trait_tys"))) } + associated_items_for_impl_trait_in_trait => { table_defaulted_array } + visibility => { cdata.get_visibility(def_id.index) } adt_def => { cdata.get_adt_def(def_id.index, tcx) } adt_destructor => { diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 16029822d03..f0dafe73c00 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1129,6 +1129,11 @@ fn should_encode_trait_impl_trait_tys(tcx: TyCtxt<'_>, def_id: DefId) -> bool { }) } +// Return `false` to avoid encoding impl trait in trait, while we don't use the query. +fn should_encode_fn_impl_trait_in_trait<'tcx>(_tcx: TyCtxt<'tcx>, _def_id: DefId) -> bool { + false +} + impl<'a, 'tcx> EncodeContext<'a, 'tcx> { fn encode_attrs(&mut self, def_id: LocalDefId) { let tcx = self.tcx; @@ -1211,6 +1216,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { { record!(self.tables.trait_impl_trait_tys[def_id] <- table); } + if should_encode_fn_impl_trait_in_trait(tcx, def_id) { + let table = tcx.associated_items_for_impl_trait_in_trait(def_id); + record_defaulted_array!(self.tables.associated_items_for_impl_trait_in_trait[def_id] <- table); + } } let inherent_impls = tcx.with_stable_hashing_context(|hcx| { diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 01691398ad0..a7ec2d790b7 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -354,6 +354,7 @@ define_tables! { explicit_item_bounds: Table, Span)>>, inferred_outlives_of: Table, Span)>>, inherent_impls: Table>, + associated_items_for_impl_trait_in_trait: Table>, - optional: attributes: Table>, diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 2eafc356dc3..2df851a7857 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -849,6 +849,13 @@ impl<'hir> Map<'hir> { } } + pub fn get_fn_output(self, def_id: LocalDefId) -> Option<&'hir FnRetTy<'hir>> { + match self.tcx.hir_owner(OwnerId { def_id }) { + Some(Owner { node, .. }) => node.fn_decl().map(|fn_decl| &fn_decl.output), + _ => None, + } + } + pub fn expect_variant(self, id: HirId) -> &'hir Variant<'hir> { match self.find(id) { Some(Node::Variant(variant)) => variant, diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 8b846f10603..b7858d9f86d 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -767,6 +767,13 @@ rustc_queries! { desc { |tcx| "comparing impl items against trait for `{}`", tcx.def_path_str(impl_id) } } + /// Given an `fn_def_id`, create and return the associated items for that function. + query associated_items_for_impl_trait_in_trait(fn_def_id: DefId) -> &'tcx [DefId] { + desc { |tcx| "creating associated items for impl trait in trait returned by `{}`", tcx.def_path_str(fn_def_id) } + cache_on_disk_if { fn_def_id.is_local() } + separate_provide_extern + } + /// Given an `impl_id`, return the trait it implements. /// Return `None` if this is an inherent impl. query impl_trait_ref(impl_id: DefId) -> Option>> { diff --git a/compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs b/compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs index 949ef04dd4d..f4004784626 100644 --- a/compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs +++ b/compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs @@ -396,6 +396,7 @@ fn encode_ty_name(tcx: TyCtxt<'_>, def_id: DefId) -> String { hir::definitions::DefPathData::CrateRoot | hir::definitions::DefPathData::Use | hir::definitions::DefPathData::GlobalAsm + | hir::definitions::DefPathData::ImplTraitAssocTy | hir::definitions::DefPathData::MacroNs(..) | hir::definitions::DefPathData::LifetimeNs(..) => { bug!("encode_ty_name: unexpected `{:?}`", disambiguated_data.data); diff --git a/compiler/rustc_symbol_mangling/src/v0.rs b/compiler/rustc_symbol_mangling/src/v0.rs index c58b6a24ab5..d53f0cea6b5 100644 --- a/compiler/rustc_symbol_mangling/src/v0.rs +++ b/compiler/rustc_symbol_mangling/src/v0.rs @@ -791,6 +791,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> { | DefPathData::Use | DefPathData::GlobalAsm | DefPathData::Impl + | DefPathData::ImplTraitAssocTy | DefPathData::MacroNs(_) | DefPathData::LifetimeNs(_) => { bug!("symbol_names: unexpected DefPathData: {:?}", disambiguated_data.data) diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index a6e0f13f698..a6c137d2f3e 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -1,13 +1,16 @@ use rustc_data_structures::fx::FxHashMap; use rustc_hir as hir; -use rustc_hir::def_id::DefId; -use rustc_middle::ty::{self, TyCtxt}; +use rustc_hir::def_id::{DefId, LocalDefId}; +use rustc_hir::definitions::DefPathData; +use rustc_hir::intravisit::{self, Visitor}; +use rustc_middle::ty::{self, DefIdTree, TyCtxt}; pub fn provide(providers: &mut ty::query::Providers) { *providers = ty::query::Providers { associated_item, associated_item_def_ids, associated_items, + associated_items_for_impl_trait_in_trait, impl_item_implementor_ids, ..*providers }; @@ -112,3 +115,34 @@ fn associated_item_from_impl_item_ref(impl_item_ref: &hir::ImplItemRef) -> ty::A fn_has_self_parameter: has_self, } } + +fn associated_items_for_impl_trait_in_trait(tcx: TyCtxt<'_>, fn_def_id: DefId) -> &'_ [DefId] { + struct RPITVisitor { + rpits: Vec, + } + + impl<'v> Visitor<'v> for RPITVisitor { + fn visit_ty(&mut self, ty: &'v hir::Ty<'v>) { + if let hir::TyKind::OpaqueDef(item_id, _, _) = ty.kind { + self.rpits.push(item_id.owner_id.def_id) + } + intravisit::walk_ty(self, ty) + } + } + + let mut visitor = RPITVisitor { rpits: Vec::new() }; + + if let Some(output) = tcx.hir().get_fn_output(fn_def_id.expect_local()) { + visitor.visit_fn_ret_ty(output); + + let trait_def_id = tcx.parent(fn_def_id).expect_local(); + + tcx.arena.alloc_from_iter(visitor.rpits.iter().map(|_opaque_ty_def_id| { + let trait_assoc_ty = + tcx.at(output.span()).create_def(trait_def_id, DefPathData::ImplTraitAssocTy); + trait_assoc_ty.def_id().to_def_id() + })) + } else { + &[] + } +} From b7dd609d9c3a6a336091e502caee8a10948cc2c3 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 16 Dec 2022 17:15:34 -0300 Subject: [PATCH 3/5] Add associated_item_for_impl_trait_in_trait query --- compiler/rustc_middle/src/query/mod.rs | 8 ++++++++ compiler/rustc_ty_utils/src/assoc.rs | 24 ++++++++++++++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index b7858d9f86d..859fed6ed26 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -774,6 +774,14 @@ rustc_queries! { separate_provide_extern } + /// Given an impl trait in trait `opaque_ty_def_id`, create and return the corresponding + /// associated item. + query associated_item_for_impl_trait_in_trait(opaque_ty_def_id: LocalDefId) -> LocalDefId { + desc { |tcx| "creates the associated item corresponding to the opaque type `{}`", tcx.def_path_str(opaque_ty_def_id.to_def_id()) } + cache_on_disk_if { true } + separate_provide_extern + } + /// Given an `impl_id`, return the trait it implements. /// Return `None` if this is an inherent impl. query impl_trait_ref(impl_id: DefId) -> Option>> { diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index a6c137d2f3e..ef725c48da6 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -1,5 +1,6 @@ use rustc_data_structures::fx::FxHashMap; use rustc_hir as hir; +use rustc_hir::def::DefKind; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::definitions::DefPathData; use rustc_hir::intravisit::{self, Visitor}; @@ -11,6 +12,7 @@ pub fn provide(providers: &mut ty::query::Providers) { associated_item_def_ids, associated_items, associated_items_for_impl_trait_in_trait, + associated_item_for_impl_trait_in_trait, impl_item_implementor_ids, ..*providers }; @@ -135,14 +137,24 @@ fn associated_items_for_impl_trait_in_trait(tcx: TyCtxt<'_>, fn_def_id: DefId) - if let Some(output) = tcx.hir().get_fn_output(fn_def_id.expect_local()) { visitor.visit_fn_ret_ty(output); - let trait_def_id = tcx.parent(fn_def_id).expect_local(); - - tcx.arena.alloc_from_iter(visitor.rpits.iter().map(|_opaque_ty_def_id| { - let trait_assoc_ty = - tcx.at(output.span()).create_def(trait_def_id, DefPathData::ImplTraitAssocTy); - trait_assoc_ty.def_id().to_def_id() + tcx.arena.alloc_from_iter(visitor.rpits.iter().map(|opaque_ty_def_id| { + tcx.associated_item_for_impl_trait_in_trait(opaque_ty_def_id).to_def_id() })) } else { &[] } } + +fn associated_item_for_impl_trait_in_trait( + tcx: TyCtxt<'_>, + opaque_ty_def_id: LocalDefId, +) -> LocalDefId { + let fn_def_id = tcx.impl_trait_in_trait_parent(opaque_ty_def_id.to_def_id()); + let trait_def_id = tcx.parent(fn_def_id); + assert_eq!(tcx.def_kind(trait_def_id), DefKind::Trait); + + let span = tcx.def_span(opaque_ty_def_id); + let trait_assoc_ty = + tcx.at(span).create_def(trait_def_id.expect_local(), DefPathData::ImplTraitAssocTy); + trait_assoc_ty.def_id() +} From 45b0d9da6b463c73cf1f816e9cbe9483ebc84a6d Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 16 Feb 2023 18:12:53 -0300 Subject: [PATCH 4/5] Make associated_items_for_impl_trait_in_trait handle impl traits on impls --- compiler/rustc_ty_utils/src/assoc.rs | 76 +++++++++++++++++++++------- 1 file changed, 59 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index ef725c48da6..0c3322d48f0 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -119,29 +119,58 @@ fn associated_item_from_impl_item_ref(impl_item_ref: &hir::ImplItemRef) -> ty::A } fn associated_items_for_impl_trait_in_trait(tcx: TyCtxt<'_>, fn_def_id: DefId) -> &'_ [DefId] { - struct RPITVisitor { - rpits: Vec, - } + let parent_def_id = tcx.parent(fn_def_id); - impl<'v> Visitor<'v> for RPITVisitor { - fn visit_ty(&mut self, ty: &'v hir::Ty<'v>) { - if let hir::TyKind::OpaqueDef(item_id, _, _) = ty.kind { - self.rpits.push(item_id.owner_id.def_id) + match tcx.def_kind(parent_def_id) { + DefKind::Trait => { + struct RPITVisitor { + rpits: Vec, + } + + impl<'v> Visitor<'v> for RPITVisitor { + fn visit_ty(&mut self, ty: &'v hir::Ty<'v>) { + if let hir::TyKind::OpaqueDef(item_id, _, _) = ty.kind { + self.rpits.push(item_id.owner_id.def_id) + } + intravisit::walk_ty(self, ty) + } + } + + let mut visitor = RPITVisitor { rpits: Vec::new() }; + + if let Some(output) = tcx.hir().get_fn_output(fn_def_id.expect_local()) { + visitor.visit_fn_ret_ty(output); + + tcx.arena.alloc_from_iter(visitor.rpits.iter().map(|opaque_ty_def_id| { + tcx.associated_item_for_impl_trait_in_trait(opaque_ty_def_id).to_def_id() + })) + } else { + &[] } - intravisit::walk_ty(self, ty) } - } - let mut visitor = RPITVisitor { rpits: Vec::new() }; + DefKind::Impl { .. } => { + let Some(trait_fn_def_id) = tcx.associated_item(fn_def_id).trait_item_def_id else { return &[] }; - if let Some(output) = tcx.hir().get_fn_output(fn_def_id.expect_local()) { - visitor.visit_fn_ret_ty(output); + tcx.arena.alloc_from_iter( + tcx.associated_items_for_impl_trait_in_trait(trait_fn_def_id).iter().map( + move |trait_assoc_def_id| { + impl_associated_item_for_impl_trait_in_trait( + tcx, + trait_assoc_def_id.expect_local(), + fn_def_id.expect_local(), + ) + .to_def_id() + }, + ), + ) + } - tcx.arena.alloc_from_iter(visitor.rpits.iter().map(|opaque_ty_def_id| { - tcx.associated_item_for_impl_trait_in_trait(opaque_ty_def_id).to_def_id() - })) - } else { - &[] + def_kind => bug!( + "associated_items_for_impl_trait_in_trait: {:?} should be Trait or Impl but is {:?}", + parent_def_id, + def_kind + ), } } @@ -158,3 +187,16 @@ fn associated_item_for_impl_trait_in_trait( tcx.at(span).create_def(trait_def_id.expect_local(), DefPathData::ImplTraitAssocTy); trait_assoc_ty.def_id() } + +fn impl_associated_item_for_impl_trait_in_trait( + tcx: TyCtxt<'_>, + trait_assoc_def_id: LocalDefId, + impl_fn_def_id: LocalDefId, +) -> LocalDefId { + let impl_def_id = tcx.local_parent(impl_fn_def_id); + + let span = tcx.def_span(trait_assoc_def_id); + let impl_assoc_ty = tcx.at(span).create_def(impl_def_id, DefPathData::ImplTraitAssocTy); + + impl_assoc_ty.def_id() +} From 5e763b662470f8f35d537215928c83be9b537c0e Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 17 Feb 2023 16:03:45 -0300 Subject: [PATCH 5/5] Document associated_item methods --- compiler/rustc_middle/src/query/mod.rs | 7 ++++++- compiler/rustc_ty_utils/src/assoc.rs | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 859fed6ed26..19b8568042a 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -767,7 +767,12 @@ rustc_queries! { desc { |tcx| "comparing impl items against trait for `{}`", tcx.def_path_str(impl_id) } } - /// Given an `fn_def_id`, create and return the associated items for that function. + /// Given `fn_def_id` of a trait or of an impl that implements a given trait: + /// if `fn_def_id` is the def id of a function defined inside a trait, then it creates and returns + /// the associated items that correspond to each impl trait in return position for that trait. + /// if `fn_def_id` is the def id of a function defined inside an impl that implements a trait, then it + /// creates and returns the associated items that correspond to each impl trait in return position + /// of the implemented trait. query associated_items_for_impl_trait_in_trait(fn_def_id: DefId) -> &'tcx [DefId] { desc { |tcx| "creating associated items for impl trait in trait returned by `{}`", tcx.def_path_str(fn_def_id) } cache_on_disk_if { fn_def_id.is_local() } diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index 0c3322d48f0..5cf8060d635 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -118,6 +118,12 @@ fn associated_item_from_impl_item_ref(impl_item_ref: &hir::ImplItemRef) -> ty::A } } +/// Given an `fn_def_id` of a trait or of an impl that implements a given trait: +/// if `fn_def_id` is the def id of a function defined inside a trait, then it creates and returns +/// the associated items that correspond to each impl trait in return position for that trait. +/// if `fn_def_id` is the def id of a function defined inside an impl that implements a trait, then it +/// creates and returns the associated items that correspond to each impl trait in return position +/// of the implemented trait. fn associated_items_for_impl_trait_in_trait(tcx: TyCtxt<'_>, fn_def_id: DefId) -> &'_ [DefId] { let parent_def_id = tcx.parent(fn_def_id); @@ -174,6 +180,8 @@ fn associated_items_for_impl_trait_in_trait(tcx: TyCtxt<'_>, fn_def_id: DefId) - } } +/// Given an `opaque_ty_def_id` corresponding to an impl trait in trait, create and return the +/// corresponding associated item. fn associated_item_for_impl_trait_in_trait( tcx: TyCtxt<'_>, opaque_ty_def_id: LocalDefId, @@ -188,6 +196,9 @@ fn associated_item_for_impl_trait_in_trait( trait_assoc_ty.def_id() } +/// Given an `trait_assoc_def_id` that corresponds to a previously synthethized impl trait in trait +/// into an associated type and an `impl_def_id` corresponding to an impl block, create and return +/// the corresponding associated item inside the impl block. fn impl_associated_item_for_impl_trait_in_trait( tcx: TyCtxt<'_>, trait_assoc_def_id: LocalDefId,