Skip to content

Commit 90a6240

Browse files
Double check the lowered predicates in type_param_predicates
1 parent da73046 commit 90a6240

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
659659
}
660660

661661
// Make sure when elaborating supertraits, probing for associated types, etc.,
662-
// we really truly are elaborating clauses that have `Self` as their self type.
662+
// we really truly are elaborating clauses that have `ty` as their self type.
663663
// This is very important since downstream code relies on this being correct.
664664
pub(super) fn assert_only_contains_predicates_from<'tcx>(
665665
filter: PredicateFilter,
@@ -791,8 +791,6 @@ pub(super) fn type_param_predicates<'tcx>(
791791
tcx: TyCtxt<'tcx>,
792792
(item_def_id, def_id, assoc_name): (LocalDefId, LocalDefId, Ident),
793793
) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
794-
use rustc_hir::*;
795-
796794
// In the HIR, bounds can derive from two places. Either
797795
// written inline like `<T: Foo>` or in a where-clause like
798796
// `where T: Foo`.
@@ -824,7 +822,7 @@ pub(super) fn type_param_predicates<'tcx>(
824822
};
825823

826824
if let Node::Item(item) = hir_node
827-
&& let ItemKind::Trait(..) = item.kind
825+
&& let hir::ItemKind::Trait(..) = item.kind
828826
// Implied `Self: Trait` and supertrait bounds.
829827
&& param_id == item_hir_id
830828
{
@@ -839,9 +837,28 @@ pub(super) fn type_param_predicates<'tcx>(
839837
PredicateFilter::SelfTraitThatDefines(assoc_name),
840838
));
841839

842-
ty::EarlyBinder::bind(
843-
tcx.arena.alloc_from_iter(result.skip_binder().iter().copied().chain(extra_predicates)),
844-
)
840+
let bounds =
841+
&*tcx.arena.alloc_from_iter(result.skip_binder().iter().copied().chain(extra_predicates));
842+
843+
// Double check that the bounds *only* contain `SelfTy: Trait` preds.
844+
let self_ty = match tcx.def_kind(def_id) {
845+
DefKind::TyParam => Ty::new_param(
846+
tcx,
847+
tcx.generics_of(item_def_id)
848+
.param_def_id_to_index(tcx, def_id.to_def_id())
849+
.expect("expected generic param to be owned by item"),
850+
tcx.item_name(def_id.to_def_id()),
851+
),
852+
DefKind::Trait | DefKind::TraitAlias => tcx.types.self_param,
853+
_ => unreachable!(),
854+
};
855+
assert_only_contains_predicates_from(
856+
PredicateFilter::SelfTraitThatDefines(assoc_name),
857+
bounds,
858+
self_ty,
859+
);
860+
861+
ty::EarlyBinder::bind(bounds)
845862
}
846863

847864
impl<'tcx> ItemCtxt<'tcx> {

0 commit comments

Comments
 (0)