Skip to content

Commit e10034c

Browse files
committed
Properly implement explicit_item_bounds for RPITITs trait assoc ty
1 parent 83c0ff8 commit e10034c

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::astconv::AstConv;
33
use rustc_hir as hir;
44
use rustc_infer::traits::util;
55
use rustc_middle::ty::subst::InternalSubsts;
6-
use rustc_middle::ty::{self, TyCtxt};
6+
use rustc_middle::ty::{self, ImplTraitInTraitData, Ty, TyCtxt};
77
use rustc_span::def_id::DefId;
88
use rustc_span::Span;
99

@@ -58,17 +58,10 @@ fn opaque_type_bounds<'tcx>(
5858
tcx: TyCtxt<'tcx>,
5959
opaque_def_id: DefId,
6060
ast_bounds: &'tcx [hir::GenericBound<'tcx>],
61+
item_ty: Ty<'tcx>,
6162
span: Span,
62-
in_trait: bool,
6363
) -> &'tcx [(ty::Predicate<'tcx>, Span)] {
6464
ty::print::with_no_queries!({
65-
let substs = InternalSubsts::identity_for_item(tcx, opaque_def_id);
66-
let item_ty = if in_trait {
67-
tcx.mk_projection(opaque_def_id, substs)
68-
} else {
69-
tcx.mk_opaque(opaque_def_id, substs)
70-
};
71-
7265
let icx = ItemCtxt::new(tcx, opaque_def_id);
7366
let mut bounds = icx.astconv().compute_bounds(item_ty, ast_bounds);
7467
// Opaque types are implicitly sized unless a `?Sized` bound is found
@@ -83,7 +76,18 @@ pub(super) fn explicit_item_bounds(
8376
tcx: TyCtxt<'_>,
8477
def_id: DefId,
8578
) -> &'_ [(ty::Predicate<'_>, Span)] {
86-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
79+
// If the def_id is about an RPITIT, delegate explicit_item_bounds to the opaque_def_id that
80+
// generated the synthesized associate type.
81+
let rpitit_info = if let Some(ImplTraitInTraitData::Trait { opaque_def_id, .. }) =
82+
tcx.opt_rpitit_info(def_id)
83+
{
84+
Some(opaque_def_id)
85+
} else {
86+
None
87+
};
88+
89+
let bounds_def_id = rpitit_info.unwrap_or(def_id);
90+
let hir_id = tcx.hir().local_def_id_to_hir_id(bounds_def_id.expect_local());
8791
match tcx.hir().get(hir_id) {
8892
hir::Node::TraitItem(hir::TraitItem {
8993
kind: hir::TraitItemKind::Type(bounds, _),
@@ -94,7 +98,15 @@ pub(super) fn explicit_item_bounds(
9498
kind: hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds, in_trait, .. }),
9599
span,
96100
..
97-
}) => opaque_type_bounds(tcx, def_id, bounds, *span, *in_trait),
101+
}) => {
102+
let substs = InternalSubsts::identity_for_item(tcx, def_id);
103+
let item_ty = if *in_trait || rpitit_info.is_some() {
104+
tcx.mk_projection(def_id, substs)
105+
} else {
106+
tcx.mk_opaque(def_id, substs)
107+
};
108+
opaque_type_bounds(tcx, bounds_def_id, bounds, item_ty, *span)
109+
}
98110
_ => bug!("item_bounds called on {:?}", def_id),
99111
}
100112
}

compiler/rustc_ty_utils/src/assoc.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,6 @@ fn associated_item_for_impl_trait_in_trait(
302302
// There are no inferred outlives for the synthesized associated type.
303303
trait_assoc_ty.inferred_outlives_of(&[]);
304304

305-
// FIXME implement this.
306-
trait_assoc_ty.explicit_item_bounds(&[]);
307-
308305
local_def_id
309306
}
310307

0 commit comments

Comments
 (0)