Skip to content

Commit b9151b2

Browse files
authored
Rollup merge of #109405 - compiler-errors:rpitit-as-opaques, r=spastorino
RPITITs are `DefKind::Opaque` with new lowering strategy r? `@spastorino` Kinda cherry-picked #109400
2 parents 8ce52b7 + c3e6f68 commit b9151b2

File tree

5 files changed

+45
-16
lines changed

5 files changed

+45
-16
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ pub(super) fn check_opaque_for_inheriting_lifetimes(
305305
}) = item.kind
306306
{
307307
let substs = InternalSubsts::identity_for_item(tcx, def_id);
308-
let opaque_identity_ty = if in_trait {
308+
let opaque_identity_ty = if in_trait && !tcx.lower_impl_trait_in_trait_to_assoc_ty() {
309309
tcx.mk_projection(def_id.to_def_id(), substs)
310310
} else {
311311
tcx.mk_opaque(def_id.to_def_id(), substs)
@@ -554,7 +554,15 @@ fn check_item_type(tcx: TyCtxt<'_>, id: hir::ItemId) {
554554
check_union(tcx, id.owner_id.def_id);
555555
}
556556
DefKind::OpaqueTy => {
557-
check_opaque(tcx, id);
557+
let opaque = tcx.hir().expect_item(id.owner_id.def_id).expect_opaque_ty();
558+
if let hir::OpaqueTyOrigin::FnReturn(fn_def_id) | hir::OpaqueTyOrigin::AsyncFn(fn_def_id) = opaque.origin
559+
&& let hir::Node::TraitItem(trait_item) = tcx.hir().get_by_def_id(fn_def_id)
560+
&& let (_, hir::TraitFn::Required(..)) = trait_item.expect_fn()
561+
{
562+
// Skip opaques from RPIT in traits with no default body.
563+
} else {
564+
check_opaque(tcx, id);
565+
}
558566
}
559567
DefKind::ImplTraitPlaceholder => {
560568
let parent = tcx.impl_trait_in_trait_parent_fn(id.owner_id.to_def_id());

compiler/rustc_metadata/src/rmeta/encoder.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,6 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) ->
10161016
| DefKind::Const
10171017
| DefKind::Static(..)
10181018
| DefKind::TyAlias
1019-
| DefKind::OpaqueTy
10201019
| DefKind::ForeignTy
10211020
| DefKind::Impl { .. }
10221021
| DefKind::AssocFn
@@ -1027,6 +1026,18 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) ->
10271026
| DefKind::AnonConst
10281027
| DefKind::InlineConst => true,
10291028

1029+
DefKind::OpaqueTy => {
1030+
let opaque = tcx.hir().expect_item(def_id).expect_opaque_ty();
1031+
if let hir::OpaqueTyOrigin::FnReturn(fn_def_id) | hir::OpaqueTyOrigin::AsyncFn(fn_def_id) = opaque.origin
1032+
&& let hir::Node::TraitItem(trait_item) = tcx.hir().get_by_def_id(fn_def_id)
1033+
&& let (_, hir::TraitFn::Required(..)) = trait_item.expect_fn()
1034+
{
1035+
false
1036+
} else {
1037+
true
1038+
}
1039+
}
1040+
10301041
DefKind::ImplTraitPlaceholder => {
10311042
let parent_def_id = tcx.impl_trait_in_trait_parent_fn(def_id.to_def_id());
10321043
let assoc_item = tcx.associated_item(parent_def_id);
@@ -1044,7 +1055,13 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) ->
10441055
let assoc_item = tcx.associated_item(def_id);
10451056
match assoc_item.container {
10461057
ty::AssocItemContainer::ImplContainer => true,
1047-
ty::AssocItemContainer::TraitContainer => assoc_item.defaultness(tcx).has_value(),
1058+
// FIXME(-Zlower-impl-trait-in-trait-to-assoc-ty) always encode RPITITs,
1059+
// since we need to be able to "project" from an RPITIT associated item
1060+
// to an opaque when installing the default projection predicates in
1061+
// default trait methods with RPITITs.
1062+
ty::AssocItemContainer::TraitContainer => {
1063+
assoc_item.defaultness(tcx).has_value() || assoc_item.opt_rpitit_info.is_some()
1064+
}
10481065
}
10491066
}
10501067
DefKind::TyParam => {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl<'hir> Map<'hir> {
188188
ItemKind::Macro(_, macro_kind) => DefKind::Macro(macro_kind),
189189
ItemKind::Mod(..) => DefKind::Mod,
190190
ItemKind::OpaqueTy(ref opaque) => {
191-
if opaque.in_trait {
191+
if opaque.in_trait && !self.tcx.lower_impl_trait_in_trait_to_assoc_ty() {
192192
DefKind::ImplTraitPlaceholder
193193
} else {
194194
DefKind::OpaqueTy

compiler/rustc_ty_utils/src/assoc.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,16 @@ fn associated_type_for_impl_trait_in_trait(
254254
tcx: TyCtxt<'_>,
255255
opaque_ty_def_id: LocalDefId,
256256
) -> LocalDefId {
257-
let fn_def_id = tcx.impl_trait_in_trait_parent_fn(opaque_ty_def_id.to_def_id());
258-
let trait_def_id = tcx.parent(fn_def_id);
257+
let (hir::OpaqueTyOrigin::FnReturn(fn_def_id) | hir::OpaqueTyOrigin::AsyncFn(fn_def_id)) =
258+
tcx.hir().expect_item(opaque_ty_def_id).expect_opaque_ty().origin
259+
else {
260+
bug!("expected opaque for {opaque_ty_def_id:?}");
261+
};
262+
let trait_def_id = tcx.local_parent(fn_def_id);
259263
assert_eq!(tcx.def_kind(trait_def_id), DefKind::Trait);
260264

261265
let span = tcx.def_span(opaque_ty_def_id);
262-
let trait_assoc_ty =
263-
tcx.at(span).create_def(trait_def_id.expect_local(), DefPathData::ImplTraitAssocTy);
266+
let trait_assoc_ty = tcx.at(span).create_def(trait_def_id, DefPathData::ImplTraitAssocTy);
264267

265268
let local_def_id = trait_assoc_ty.def_id();
266269
let def_id = local_def_id.to_def_id();
@@ -282,7 +285,7 @@ fn associated_type_for_impl_trait_in_trait(
282285
container: ty::TraitContainer,
283286
fn_has_self_parameter: false,
284287
opt_rpitit_info: Some(ImplTraitInTraitData::Trait {
285-
fn_def_id,
288+
fn_def_id: fn_def_id.to_def_id(),
286289
opaque_def_id: opaque_ty_def_id.to_def_id(),
287290
}),
288291
});
@@ -324,7 +327,7 @@ fn associated_type_for_impl_trait_in_trait(
324327
params.iter().map(|param| (param.def_id, param.index)).collect();
325328

326329
ty::Generics {
327-
parent: Some(trait_def_id),
330+
parent: Some(trait_def_id.to_def_id()),
328331
parent_count,
329332
params,
330333
param_def_id_to_index,
@@ -335,7 +338,7 @@ fn associated_type_for_impl_trait_in_trait(
335338

336339
// There are no predicates for the synthesized associated type.
337340
trait_assoc_ty.explicit_predicates_of(ty::GenericPredicates {
338-
parent: Some(trait_def_id),
341+
parent: Some(trait_def_id.to_def_id()),
339342
predicates: &[],
340343
});
341344

@@ -356,7 +359,6 @@ fn associated_type_for_impl_trait_in_impl(
356359
impl_fn_def_id: LocalDefId,
357360
) -> LocalDefId {
358361
let impl_local_def_id = tcx.local_parent(impl_fn_def_id);
359-
let impl_def_id = impl_local_def_id.to_def_id();
360362

361363
// FIXME fix the span, we probably want the def_id of the return type of the function
362364
let span = tcx.def_span(impl_fn_def_id);
@@ -402,7 +404,7 @@ fn associated_type_for_impl_trait_in_impl(
402404
let trait_assoc_parent_count = trait_assoc_generics.parent_count;
403405
let mut params = trait_assoc_generics.params.clone();
404406

405-
let parent_generics = tcx.generics_of(impl_def_id);
407+
let parent_generics = tcx.generics_of(impl_local_def_id.to_def_id());
406408
let parent_count = parent_generics.parent_count + parent_generics.params.len();
407409

408410
for param in &mut params {
@@ -413,7 +415,7 @@ fn associated_type_for_impl_trait_in_impl(
413415
params.iter().map(|param| (param.def_id, param.index)).collect();
414416

415417
ty::Generics {
416-
parent: Some(impl_def_id),
418+
parent: Some(impl_local_def_id.to_def_id()),
417419
parent_count,
418420
params,
419421
param_def_id_to_index,
@@ -424,7 +426,7 @@ fn associated_type_for_impl_trait_in_impl(
424426

425427
// There are no predicates for the synthesized associated type.
426428
impl_assoc_ty.explicit_predicates_of(ty::GenericPredicates {
427-
parent: Some(impl_def_id),
429+
parent: Some(impl_local_def_id.to_def_id()),
428430
predicates: &[],
429431
});
430432

tests/ui/impl-trait/in-trait/auxiliary/rpitit.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// [next] compile-flags: -Zlower-impl-trait-in-trait-to-assoc-ty
2+
13
#![feature(return_position_impl_trait_in_trait)]
24

35
pub trait Foo {

0 commit comments

Comments
 (0)