Skip to content

Commit 81dba07

Browse files
Double check the lowered predicates in type_param_predicates
1 parent 697eda5 commit 81dba07

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

+24-7
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
652652
}
653653

654654
// Make sure when elaborating supertraits, probing for associated types, etc.,
655-
// we really truly are elaborating clauses that have `Self` as their self type.
655+
// we really truly are elaborating clauses that have `ty` as their self type.
656656
// This is very important since downstream code relies on this being correct.
657657
pub(super) fn assert_only_contains_predicates_from<'tcx>(
658658
filter: PredicateFilter,
@@ -794,8 +794,6 @@ pub(super) fn type_param_predicates<'tcx>(
794794
None => {}
795795
}
796796

797-
use rustc_hir::*;
798-
799797
// In the HIR, bounds can derive from two places. Either
800798
// written inline like `<T: Foo>` or in a where-clause like
801799
// `where T: Foo`.
@@ -827,7 +825,7 @@ pub(super) fn type_param_predicates<'tcx>(
827825
};
828826

829827
if let Node::Item(item) = hir_node
830-
&& let ItemKind::Trait(..) = item.kind
828+
&& let hir::ItemKind::Trait(..) = item.kind
831829
// Implied `Self: Trait` and supertrait bounds.
832830
&& param_id == item_hir_id
833831
{
@@ -842,9 +840,28 @@ pub(super) fn type_param_predicates<'tcx>(
842840
PredicateFilter::SelfTraitThatDefines(assoc_name),
843841
));
844842

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

850867
impl<'tcx> ItemCtxt<'tcx> {

0 commit comments

Comments
 (0)