Skip to content

Commit 9d132f7

Browse files
authored
Rollup merge of #108568 - spastorino:new-rpitit-flag, r=compiler-errors
Make associated_item_def_ids for traits use an unstable option to also return associated types for RPITITs r? `@compiler-errors`
2 parents 093a53f + 73e2fe0 commit 9d132f7

File tree

7 files changed

+132
-9
lines changed

7 files changed

+132
-9
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -1101,9 +1101,18 @@ fn should_encode_const(def_kind: DefKind) -> bool {
11011101
}
11021102
}
11031103

1104-
// Return `false` to avoid encoding impl trait in trait, while we don't use the query.
1105-
fn should_encode_fn_impl_trait_in_trait<'tcx>(_tcx: TyCtxt<'tcx>, _def_id: DefId) -> bool {
1106-
false
1104+
// We only encode impl trait in trait when using `lower-impl-trait-in-trait-to-assoc-ty` unstable
1105+
// option.
1106+
fn should_encode_fn_impl_trait_in_trait<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
1107+
if tcx.sess.opts.unstable_opts.lower_impl_trait_in_trait_to_assoc_ty
1108+
&& let Some(assoc_item) = tcx.opt_associated_item(def_id)
1109+
&& assoc_item.container == ty::AssocItemContainer::TraitContainer
1110+
&& assoc_item.kind == ty::AssocKind::Fn
1111+
{
1112+
true
1113+
} else {
1114+
false
1115+
}
11071116
}
11081117

11091118
impl<'a, 'tcx> EncodeContext<'a, 'tcx> {

compiler/rustc_middle/src/hir/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ pub fn provide(providers: &mut Providers) {
177177
}
178178
};
179179
providers.opt_def_kind = |tcx, def_id| tcx.hir().opt_def_kind(def_id.expect_local());
180+
providers.opt_rpitit_info = |_, _| None;
180181
providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;
181182
providers.expn_that_defined = |tcx, id| {
182183
let id = id.expect_local();

compiler/rustc_middle/src/query/mod.rs

+19
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ rustc_queries! {
9090
/// Definitions that were generated with no HIR, would be feeded to return `None`.
9191
query opt_local_def_id_to_hir_id(key: LocalDefId) -> Option<hir::HirId>{
9292
desc { |tcx| "getting HIR ID of `{}`", tcx.def_path_str(key.to_def_id()) }
93+
feedable
9394
}
9495

9596
/// Gives access to the HIR node's parent for the HIR owner `key`.
@@ -166,6 +167,7 @@ rustc_queries! {
166167
}
167168
cache_on_disk_if { key.is_local() }
168169
separate_provide_extern
170+
feedable
169171
}
170172

171173
query collect_return_position_impl_trait_in_trait_tys(key: DefId)
@@ -222,6 +224,7 @@ rustc_queries! {
222224
arena_cache
223225
cache_on_disk_if { key.is_local() }
224226
separate_provide_extern
227+
feedable
225228
}
226229

227230
/// Maps from the `DefId` of an item (trait/struct/enum/fn) to the
@@ -264,6 +267,7 @@ rustc_queries! {
264267
desc { |tcx| "finding item bounds for `{}`", tcx.def_path_str(key) }
265268
cache_on_disk_if { key.is_local() }
266269
separate_provide_extern
270+
feedable
267271
}
268272

269273
/// Elaborated version of the predicates from `explicit_item_bounds`.
@@ -588,6 +592,7 @@ rustc_queries! {
588592
desc { |tcx| "computing explicit predicates of `{}`", tcx.def_path_str(key) }
589593
cache_on_disk_if { key.is_local() }
590594
separate_provide_extern
595+
feedable
591596
}
592597

593598
/// Returns the inferred outlives predicates (e.g., for `struct
@@ -596,6 +601,7 @@ rustc_queries! {
596601
desc { |tcx| "computing inferred outlives predicates of `{}`", tcx.def_path_str(key) }
597602
cache_on_disk_if { key.is_local() }
598603
separate_provide_extern
604+
feedable
599605
}
600606

601607
/// Maps from the `DefId` of a trait to the list of
@@ -728,6 +734,7 @@ rustc_queries! {
728734
desc { |tcx| "computing associated item data for `{}`", tcx.def_path_str(key) }
729735
cache_on_disk_if { key.is_local() }
730736
separate_provide_extern
737+
feedable
731738
}
732739

733740
/// Collects the associated items defined on a trait or impl.
@@ -1142,6 +1149,15 @@ rustc_queries! {
11421149
desc { |tcx| "looking up definition kind of `{}`", tcx.def_path_str(def_id) }
11431150
cache_on_disk_if { def_id.is_local() }
11441151
separate_provide_extern
1152+
feedable
1153+
}
1154+
1155+
/// The `opt_rpitit_info` query returns the pair of the def id of the function where the RPIT
1156+
/// is defined and the opaque def id if any.
1157+
query opt_rpitit_info(def_id: DefId) -> Option<ty::ImplTraitInTraitData> {
1158+
desc { |tcx| "opt_rpitit_info `{}`", tcx.def_path_str(def_id) }
1159+
cache_on_disk_if { def_id.is_local() }
1160+
feedable
11451161
}
11461162

11471163
/// Gets the span for the definition.
@@ -1157,6 +1173,7 @@ rustc_queries! {
11571173
desc { |tcx| "looking up span for `{}`'s identifier", tcx.def_path_str(def_id) }
11581174
cache_on_disk_if { def_id.is_local() }
11591175
separate_provide_extern
1176+
feedable
11601177
}
11611178

11621179
query lookup_stability(def_id: DefId) -> Option<attr::Stability> {
@@ -1498,6 +1515,7 @@ rustc_queries! {
14981515
desc { |tcx| "looking up whether `{}` is a default impl", tcx.def_path_str(def_id) }
14991516
cache_on_disk_if { def_id.is_local() }
15001517
separate_provide_extern
1518+
feedable
15011519
}
15021520

15031521
query check_well_formed(key: hir::OwnerId) -> () {
@@ -1695,6 +1713,7 @@ rustc_queries! {
16951713
query visibility(def_id: DefId) -> ty::Visibility<DefId> {
16961714
desc { |tcx| "computing visibility of `{}`", tcx.def_path_str(def_id) }
16971715
separate_provide_extern
1716+
feedable
16981717
}
16991718

17001719
query inhabited_predicate_adt(key: DefId) -> ty::inhabitedness::InhabitedPredicate<'tcx> {

compiler/rustc_middle/src/ty/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -2071,6 +2071,12 @@ pub enum ImplOverlapKind {
20712071
Issue33140,
20722072
}
20732073

2074+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, TyEncodable, TyDecodable, HashStable)]
2075+
pub enum ImplTraitInTraitData {
2076+
Trait { fn_def_id: DefId, opaque_def_id: DefId },
2077+
Impl { fn_def_id: DefId },
2078+
}
2079+
20742080
impl<'tcx> TyCtxt<'tcx> {
20752081
pub fn typeck_body(self, body: hir::BodyId) -> &'tcx TypeckResults<'tcx> {
20762082
self.typeck(self.hir().body_owner_def_id(body))

compiler/rustc_session/src/options.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,9 @@ options! {
15031503
"what location details should be tracked when using caller_location, either \
15041504
`none`, or a comma separated list of location details, for which \
15051505
valid options are `file`, `line`, and `column` (default: `file,line,column`)"),
1506+
lower_impl_trait_in_trait_to_assoc_ty: bool = (false, parse_bool, [TRACKED],
1507+
"modify the lowering strategy for `impl Trait` in traits so that they are lowered to \
1508+
generic associated types"),
15061509
ls: bool = (false, parse_bool, [UNTRACKED],
15071510
"list the symbols defined by a library crate (default: no)"),
15081511
macro_backtrace: bool = (false, parse_bool, [UNTRACKED],

compiler/rustc_ty_utils/src/assoc.rs

+90-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use rustc_hir::def::DefKind;
44
use rustc_hir::def_id::{DefId, LocalDefId};
55
use rustc_hir::definitions::DefPathData;
66
use rustc_hir::intravisit::{self, Visitor};
7-
use rustc_middle::ty::{self, DefIdTree, TyCtxt};
7+
use rustc_middle::ty::{self, DefIdTree, ImplTraitInTraitData, InternalSubsts, TyCtxt};
8+
use rustc_span::symbol::kw;
89

910
pub fn provide(providers: &mut ty::query::Providers) {
1011
*providers = ty::query::Providers {
@@ -21,9 +22,37 @@ pub fn provide(providers: &mut ty::query::Providers) {
2122
fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] {
2223
let item = tcx.hir().expect_item(def_id.expect_local());
2324
match item.kind {
24-
hir::ItemKind::Trait(.., ref trait_item_refs) => tcx.arena.alloc_from_iter(
25-
trait_item_refs.iter().map(|trait_item_ref| trait_item_ref.id.owner_id.to_def_id()),
26-
),
25+
hir::ItemKind::Trait(.., ref trait_item_refs) => {
26+
if tcx.sess.opts.unstable_opts.lower_impl_trait_in_trait_to_assoc_ty {
27+
// We collect RPITITs for each trait method's return type and create a
28+
// corresponding associated item using associated_items_for_impl_trait_in_trait
29+
// query.
30+
tcx.arena.alloc_from_iter(
31+
trait_item_refs
32+
.iter()
33+
.map(|trait_item_ref| trait_item_ref.id.owner_id.to_def_id())
34+
.chain(
35+
trait_item_refs
36+
.iter()
37+
.filter(|trait_item_ref| {
38+
matches!(trait_item_ref.kind, hir::AssocItemKind::Fn { .. })
39+
})
40+
.flat_map(|trait_item_ref| {
41+
let trait_fn_def_id =
42+
trait_item_ref.id.owner_id.def_id.to_def_id();
43+
tcx.associated_items_for_impl_trait_in_trait(trait_fn_def_id)
44+
})
45+
.map(|def_id| *def_id),
46+
),
47+
)
48+
} else {
49+
tcx.arena.alloc_from_iter(
50+
trait_item_refs
51+
.iter()
52+
.map(|trait_item_ref| trait_item_ref.id.owner_id.to_def_id()),
53+
)
54+
}
55+
}
2756
hir::ItemKind::Impl(ref impl_) => tcx.arena.alloc_from_iter(
2857
impl_.items.iter().map(|impl_item_ref| impl_item_ref.id.owner_id.to_def_id()),
2958
),
@@ -193,10 +222,65 @@ fn associated_item_for_impl_trait_in_trait(
193222
let span = tcx.def_span(opaque_ty_def_id);
194223
let trait_assoc_ty =
195224
tcx.at(span).create_def(trait_def_id.expect_local(), DefPathData::ImplTraitAssocTy);
196-
trait_assoc_ty.def_id()
225+
226+
let local_def_id = trait_assoc_ty.def_id();
227+
let def_id = local_def_id.to_def_id();
228+
229+
trait_assoc_ty.opt_def_kind(Some(DefKind::AssocTy));
230+
231+
// There's no HIR associated with this new synthesized `def_id`, so feed
232+
// `opt_local_def_id_to_hir_id` with `None`.
233+
trait_assoc_ty.opt_local_def_id_to_hir_id(None);
234+
235+
// Copy span of the opaque.
236+
trait_assoc_ty.def_ident_span(Some(span));
237+
238+
// Add the def_id of the function and opaque that generated this synthesized associated type.
239+
trait_assoc_ty.opt_rpitit_info(Some(ImplTraitInTraitData::Trait {
240+
fn_def_id,
241+
opaque_def_id: opaque_ty_def_id.to_def_id(),
242+
}));
243+
244+
trait_assoc_ty.associated_item(ty::AssocItem {
245+
name: kw::Empty,
246+
kind: ty::AssocKind::Type,
247+
def_id,
248+
trait_item_def_id: None,
249+
container: ty::TraitContainer,
250+
fn_has_self_parameter: false,
251+
});
252+
253+
// Copy visility of the containing function.
254+
trait_assoc_ty.visibility(tcx.visibility(fn_def_id));
255+
256+
// Copy impl_defaultness of the containing function.
257+
trait_assoc_ty.impl_defaultness(tcx.impl_defaultness(fn_def_id));
258+
259+
// Copy type_of of the opaque.
260+
trait_assoc_ty.type_of(ty::EarlyBinder(tcx.mk_opaque(
261+
opaque_ty_def_id.to_def_id(),
262+
InternalSubsts::identity_for_item(tcx, opaque_ty_def_id.to_def_id()),
263+
)));
264+
265+
// Copy generics_of of the opaque.
266+
trait_assoc_ty.generics_of(tcx.generics_of(opaque_ty_def_id).clone());
267+
268+
// There are no predicates for the synthesized associated type.
269+
trait_assoc_ty.explicit_predicates_of(ty::GenericPredicates {
270+
parent: Some(trait_def_id),
271+
predicates: &[],
272+
});
273+
274+
// There are no inferred outlives for the synthesized associated type.
275+
trait_assoc_ty.inferred_outlives_of(&[]);
276+
277+
// FIXME implement this.
278+
trait_assoc_ty.explicit_item_bounds(&[]);
279+
280+
local_def_id
197281
}
198282

199-
/// Given an `trait_assoc_def_id` that corresponds to a previously synthethized impl trait in trait
283+
/// Given an `trait_assoc_def_id` that corresponds to a previously synthesized impl trait in trait
200284
/// into an associated type and an `impl_def_id` corresponding to an impl block, create and return
201285
/// the corresponding associated item inside the impl block.
202286
fn impl_associated_item_for_impl_trait_in_trait(

tests/rustdoc-ui/z-help.stdout

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
-Z llvm-plugins=val -- a list LLVM plugins to enable (space separated)
8888
-Z llvm-time-trace=val -- generate JSON tracing data file from LLVM data (default: no)
8989
-Z location-detail=val -- what location details should be tracked when using caller_location, either `none`, or a comma separated list of location details, for which valid options are `file`, `line`, and `column` (default: `file,line,column`)
90+
-Z lower-impl-trait-in-trait-to-assoc-ty=val -- modify the lowering strategy for `impl Trait` in traits so that they are lowered to generic associated types
9091
-Z ls=val -- list the symbols defined by a library crate (default: no)
9192
-Z macro-backtrace=val -- show macro backtraces (default: no)
9293
-Z maximal-hir-to-mir-coverage=val -- save as much information as possible about the correspondence between MIR and HIR as source scopes (default: no)

0 commit comments

Comments
 (0)