Skip to content

Commit 4e703a2

Browse files
committed
Add associated_items_for_impl_trait_in_trait query
1 parent 833b915 commit 4e703a2

File tree

9 files changed

+68
-4
lines changed

9 files changed

+68
-4
lines changed

compiler/rustc_hir/src/definitions.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ pub enum DefPathData {
280280
AnonConst,
281281
/// An `impl Trait` type node.
282282
ImplTrait,
283+
/// `impl Trait` generated associated type node.
284+
ImplTraitAssocTy,
283285
}
284286

285287
impl Definitions {
@@ -403,7 +405,7 @@ impl DefPathData {
403405
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name),
404406

405407
Impl | ForeignMod | CrateRoot | Use | GlobalAsm | ClosureExpr | Ctor | AnonConst
406-
| ImplTrait => None,
408+
| ImplTrait | ImplTraitAssocTy => None,
407409
}
408410
}
409411

@@ -422,7 +424,7 @@ impl DefPathData {
422424
ClosureExpr => DefPathDataName::Anon { namespace: sym::closure },
423425
Ctor => DefPathDataName::Anon { namespace: sym::constructor },
424426
AnonConst => DefPathDataName::Anon { namespace: sym::constant },
425-
ImplTrait => DefPathDataName::Anon { namespace: sym::opaque },
427+
ImplTrait | ImplTraitAssocTy => DefPathDataName::Anon { namespace: sym::opaque },
426428
}
427429
}
428430
}

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ provide! { tcx, def_id, other, cdata,
248248
.process_decoded(tcx, || panic!("{def_id:?} does not have trait_impl_trait_tys")))
249249
}
250250

251+
associated_items_for_impl_trait_in_trait => { table_defaulted_array }
252+
251253
visibility => { cdata.get_visibility(def_id.index) }
252254
adt_def => { cdata.get_adt_def(def_id.index, tcx) }
253255
adt_destructor => {

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,11 @@ fn should_encode_trait_impl_trait_tys(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
11291129
})
11301130
}
11311131

1132+
// Return `false` to avoid encoding impl trait in trait, while we don't use the query.
1133+
fn should_encode_fn_impl_trait_in_trait<'tcx>(_tcx: TyCtxt<'tcx>, _def_id: DefId) -> bool {
1134+
false
1135+
}
1136+
11321137
impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11331138
fn encode_attrs(&mut self, def_id: LocalDefId) {
11341139
let tcx = self.tcx;
@@ -1211,6 +1216,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12111216
{
12121217
record!(self.tables.trait_impl_trait_tys[def_id] <- table);
12131218
}
1219+
if should_encode_fn_impl_trait_in_trait(tcx, def_id) {
1220+
let table = tcx.associated_items_for_impl_trait_in_trait(def_id);
1221+
record_defaulted_array!(self.tables.associated_items_for_impl_trait_in_trait[def_id] <- table);
1222+
}
12141223
}
12151224

12161225
let inherent_impls = tcx.with_stable_hashing_context(|hcx| {

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ define_tables! {
354354
explicit_item_bounds: Table<DefIndex, LazyArray<(ty::Predicate<'static>, Span)>>,
355355
inferred_outlives_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
356356
inherent_impls: Table<DefIndex, LazyArray<DefIndex>>,
357+
associated_items_for_impl_trait_in_trait: Table<DefIndex, LazyArray<DefId>>,
357358

358359
- optional:
359360
attributes: Table<DefIndex, LazyArray<ast::Attribute>>,

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,13 @@ impl<'hir> Map<'hir> {
849849
}
850850
}
851851

852+
pub fn get_fn_output(self, def_id: LocalDefId) -> Option<&'hir FnRetTy<'hir>> {
853+
match self.tcx.hir_owner(OwnerId { def_id }) {
854+
Some(Owner { node, .. }) => node.fn_decl().map(|fn_decl| &fn_decl.output),
855+
_ => None,
856+
}
857+
}
858+
852859
pub fn expect_variant(self, id: HirId) -> &'hir Variant<'hir> {
853860
match self.find(id) {
854861
Some(Node::Variant(variant)) => variant,

compiler/rustc_middle/src/query/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,13 @@ rustc_queries! {
767767
desc { |tcx| "comparing impl items against trait for `{}`", tcx.def_path_str(impl_id) }
768768
}
769769

770+
/// Given an `fn_def_id`, create and return the associated items for that function.
771+
query associated_items_for_impl_trait_in_trait(fn_def_id: DefId) -> &'tcx [DefId] {
772+
desc { |tcx| "creating associated items for impl trait in trait returned by `{}`", tcx.def_path_str(fn_def_id) }
773+
cache_on_disk_if { fn_def_id.is_local() }
774+
separate_provide_extern
775+
}
776+
770777
/// Given an `impl_id`, return the trait it implements.
771778
/// Return `None` if this is an inherent impl.
772779
query impl_trait_ref(impl_id: DefId) -> Option<ty::EarlyBinder<ty::TraitRef<'tcx>>> {

compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ fn encode_ty_name(tcx: TyCtxt<'_>, def_id: DefId) -> String {
396396
hir::definitions::DefPathData::CrateRoot
397397
| hir::definitions::DefPathData::Use
398398
| hir::definitions::DefPathData::GlobalAsm
399+
| hir::definitions::DefPathData::ImplTraitAssocTy
399400
| hir::definitions::DefPathData::MacroNs(..)
400401
| hir::definitions::DefPathData::LifetimeNs(..) => {
401402
bug!("encode_ty_name: unexpected `{:?}`", disambiguated_data.data);

compiler/rustc_symbol_mangling/src/v0.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
791791
| DefPathData::Use
792792
| DefPathData::GlobalAsm
793793
| DefPathData::Impl
794+
| DefPathData::ImplTraitAssocTy
794795
| DefPathData::MacroNs(_)
795796
| DefPathData::LifetimeNs(_) => {
796797
bug!("symbol_names: unexpected DefPathData: {:?}", disambiguated_data.data)

compiler/rustc_ty_utils/src/assoc.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
use rustc_data_structures::fx::FxHashMap;
22
use rustc_hir as hir;
3-
use rustc_hir::def_id::DefId;
4-
use rustc_middle::ty::{self, TyCtxt};
3+
use rustc_hir::def_id::{DefId, LocalDefId};
4+
use rustc_hir::definitions::DefPathData;
5+
use rustc_hir::intravisit::{self, Visitor};
6+
use rustc_middle::ty::{self, DefIdTree, TyCtxt};
57

68
pub fn provide(providers: &mut ty::query::Providers) {
79
*providers = ty::query::Providers {
810
associated_item,
911
associated_item_def_ids,
1012
associated_items,
13+
associated_items_for_impl_trait_in_trait,
1114
impl_item_implementor_ids,
1215
..*providers
1316
};
@@ -112,3 +115,34 @@ fn associated_item_from_impl_item_ref(impl_item_ref: &hir::ImplItemRef) -> ty::A
112115
fn_has_self_parameter: has_self,
113116
}
114117
}
118+
119+
fn associated_items_for_impl_trait_in_trait(tcx: TyCtxt<'_>, fn_def_id: DefId) -> &'_ [DefId] {
120+
struct RPITVisitor {
121+
rpits: Vec<LocalDefId>,
122+
}
123+
124+
impl<'v> Visitor<'v> for RPITVisitor {
125+
fn visit_ty(&mut self, ty: &'v hir::Ty<'v>) {
126+
if let hir::TyKind::OpaqueDef(item_id, _, _) = ty.kind {
127+
self.rpits.push(item_id.owner_id.def_id)
128+
}
129+
intravisit::walk_ty(self, ty)
130+
}
131+
}
132+
133+
let mut visitor = RPITVisitor { rpits: Vec::new() };
134+
135+
if let Some(output) = tcx.hir().get_fn_output(fn_def_id.expect_local()) {
136+
visitor.visit_fn_ret_ty(output);
137+
138+
let trait_def_id = tcx.parent(fn_def_id).expect_local();
139+
140+
tcx.arena.alloc_from_iter(visitor.rpits.iter().map(|_opaque_ty_def_id| {
141+
let trait_assoc_ty =
142+
tcx.at(output.span()).create_def(trait_def_id, DefPathData::ImplTraitAssocTy);
143+
trait_assoc_ty.def_id().to_def_id()
144+
}))
145+
} else {
146+
&[]
147+
}
148+
}

0 commit comments

Comments
 (0)