Skip to content

Commit 076e627

Browse files
authored
Rollup merge of #108141 - spastorino:add_rpitit_queries, r=compiler-errors
Add rpitit queries This is part of the changes we are making to lower RPITITs as an associated type. The rest of the stuff will follow under a `-Z` flag. I still need to add comments to the code, explain stuff and also I'd need to avoid encoding in metadata when rpitit queries return `&[]` r? `@compiler-errors`
2 parents e781a6f + 5e763b6 commit 076e627

File tree

11 files changed

+158
-13
lines changed

11 files changed

+158
-13
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
@@ -254,6 +254,8 @@ provide! { tcx, def_id, other, cdata,
254254
.process_decoded(tcx, || panic!("{def_id:?} does not have trait_impl_trait_tys")))
255255
}
256256

257+
associated_items_for_impl_trait_in_trait => { table_defaulted_array }
258+
257259
visibility => { cdata.get_visibility(def_id.index) }
258260
adt_def => { cdata.get_adt_def(def_id.index, tcx) }
259261
adt_destructor => {

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 11 additions & 2 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;
@@ -1137,8 +1142,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11371142
is_doc_hidden: false,
11381143
};
11391144
let attr_iter = tcx
1140-
.hir()
1141-
.attrs(tcx.hir().local_def_id_to_hir_id(def_id))
1145+
.opt_local_def_id_to_hir_id(def_id)
1146+
.map_or(Default::default(), |hir_id| tcx.hir().attrs(hir_id))
11421147
.iter()
11431148
.filter(|attr| analyze_attr(attr, &mut state));
11441149

@@ -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/hir/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,13 @@ pub fn provide(providers: &mut Providers) {
121121
let node = owner.node();
122122
Some(Owner { node, hash_without_bodies: owner.nodes.hash_without_bodies })
123123
};
124-
providers.local_def_id_to_hir_id = |tcx, id| {
124+
providers.opt_local_def_id_to_hir_id = |tcx, id| {
125125
let owner = tcx.hir_crate(()).owners[id].map(|_| ());
126-
match owner {
126+
Some(match owner {
127127
MaybeOwner::Owner(_) => HirId::make_owner(id),
128128
MaybeOwner::Phantom => bug!("No HirId for {:?}", id),
129129
MaybeOwner::NonOwner(hir_id) => hir_id,
130-
}
130+
})
131131
};
132132
providers.hir_owner_nodes = |tcx, id| tcx.hir_crate(()).owners[id.def_id].map(|i| &i.nodes);
133133
providers.hir_owner_parent = |tcx, id| {

compiler/rustc_middle/src/query/mod.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,10 @@ rustc_queries! {
8585
desc { |tcx| "getting HIR owner of `{}`", tcx.def_path_str(key.to_def_id()) }
8686
}
8787

88-
/// Gives access to the HIR ID for the given `LocalDefId` owner `key`.
88+
/// Gives access to the HIR ID for the given `LocalDefId` owner `key` if any.
8989
///
90-
/// This can be conveniently accessed by methods on `tcx.hir()`.
91-
/// Avoid calling this query directly.
92-
query local_def_id_to_hir_id(key: LocalDefId) -> hir::HirId {
90+
/// Definitions that were generated with no HIR, would be feeded to return `None`.
91+
query opt_local_def_id_to_hir_id(key: LocalDefId) -> Option<hir::HirId>{
9392
desc { |tcx| "getting HIR ID of `{}`", tcx.def_path_str(key.to_def_id()) }
9493
}
9594

@@ -767,6 +766,26 @@ rustc_queries! {
767766
desc { |tcx| "comparing impl items against trait for `{}`", tcx.def_path_str(impl_id) }
768767
}
769768

769+
/// Given `fn_def_id` of a trait or of an impl that implements a given trait:
770+
/// if `fn_def_id` is the def id of a function defined inside a trait, then it creates and returns
771+
/// the associated items that correspond to each impl trait in return position for that trait.
772+
/// if `fn_def_id` is the def id of a function defined inside an impl that implements a trait, then it
773+
/// creates and returns the associated items that correspond to each impl trait in return position
774+
/// of the implemented trait.
775+
query associated_items_for_impl_trait_in_trait(fn_def_id: DefId) -> &'tcx [DefId] {
776+
desc { |tcx| "creating associated items for impl trait in trait returned by `{}`", tcx.def_path_str(fn_def_id) }
777+
cache_on_disk_if { fn_def_id.is_local() }
778+
separate_provide_extern
779+
}
780+
781+
/// Given an impl trait in trait `opaque_ty_def_id`, create and return the corresponding
782+
/// associated item.
783+
query associated_item_for_impl_trait_in_trait(opaque_ty_def_id: LocalDefId) -> LocalDefId {
784+
desc { |tcx| "creates the associated item corresponding to the opaque type `{}`", tcx.def_path_str(opaque_ty_def_id.to_def_id()) }
785+
cache_on_disk_if { true }
786+
separate_provide_extern
787+
}
788+
770789
/// Given an `impl_id`, return the trait it implements.
771790
/// Return `None` if this is an inherent impl.
772791
query impl_trait_ref(impl_id: DefId) -> Option<ty::EarlyBinder<ty::TraitRef<'tcx>>> {

compiler/rustc_middle/src/ty/context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2451,6 +2451,10 @@ impl<'tcx> TyCtxt<'tcx> {
24512451
)
24522452
}
24532453

2454+
pub fn local_def_id_to_hir_id(self, local_def_id: LocalDefId) -> HirId {
2455+
self.opt_local_def_id_to_hir_id(local_def_id).unwrap()
2456+
}
2457+
24542458
pub fn trait_solver_next(self) -> bool {
24552459
self.sess.opts.unstable_opts.trait_solver == rustc_session::config::TraitSolver::Next
24562460
}

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)

0 commit comments

Comments
 (0)