Skip to content

Commit 697eda5

Browse files
Make SelfTraitThatDefines a tighter filter
1 parent ac67d29 commit 697eda5

File tree

4 files changed

+55
-27
lines changed

4 files changed

+55
-27
lines changed

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn associated_type_bounds<'tcx>(
4343
match filter {
4444
PredicateFilter::All
4545
| PredicateFilter::SelfOnly
46-
| PredicateFilter::SelfThatDefines(_)
46+
| PredicateFilter::SelfTraitThatDefines(_)
4747
| PredicateFilter::SelfAndAssociatedTypeBounds => {
4848
icx.lowerer().add_sized_bound(&mut bounds, item_ty, hir_bounds, None, span);
4949
}
@@ -122,7 +122,7 @@ fn remap_gat_vars_and_recurse_into_nested_projections<'tcx>(
122122
PredicateFilter::SelfOnly => {
123123
return None;
124124
}
125-
PredicateFilter::SelfThatDefines(_)
125+
PredicateFilter::SelfTraitThatDefines(_)
126126
| PredicateFilter::SelfConstIfConst
127127
| PredicateFilter::SelfAndAssociatedTypeBounds
128128
| PredicateFilter::ConstIfConst => {
@@ -329,7 +329,7 @@ fn opaque_type_bounds<'tcx>(
329329
match filter {
330330
PredicateFilter::All
331331
| PredicateFilter::SelfOnly
332-
| PredicateFilter::SelfThatDefines(_)
332+
| PredicateFilter::SelfTraitThatDefines(_)
333333
| PredicateFilter::SelfAndAssociatedTypeBounds => {
334334
// Associated types are implicitly sized unless a `?Sized` bound is found
335335
icx.lowerer().add_sized_bound(&mut bounds, item_ty, hir_bounds, None, span);

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+42-18
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,11 @@ pub(super) fn explicit_supertraits_containing_assoc_item<'tcx>(
557557
tcx: TyCtxt<'tcx>,
558558
(trait_def_id, assoc_name): (DefId, Ident),
559559
) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
560-
implied_predicates_with_filter(tcx, trait_def_id, PredicateFilter::SelfThatDefines(assoc_name))
560+
implied_predicates_with_filter(
561+
tcx,
562+
trait_def_id,
563+
PredicateFilter::SelfTraitThatDefines(assoc_name),
564+
)
561565
}
562566

563567
pub(super) fn explicit_implied_predicates_of<'tcx>(
@@ -586,7 +590,7 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
586590
let Some(trait_def_id) = trait_def_id.as_local() else {
587591
// if `assoc_name` is None, then the query should've been redirected to an
588592
// external provider
589-
assert_matches!(filter, PredicateFilter::SelfThatDefines(_));
593+
assert_matches!(filter, PredicateFilter::SelfTraitThatDefines(_));
590594
return tcx.explicit_super_predicates_of(trait_def_id);
591595
};
592596

@@ -660,7 +664,7 @@ pub(super) fn assert_only_contains_predicates_from<'tcx>(
660664
}
661665

662666
match filter {
663-
PredicateFilter::SelfOnly | PredicateFilter::SelfThatDefines(_) => {
667+
PredicateFilter::SelfOnly => {
664668
for (clause, _) in bounds {
665669
match clause.kind().skip_binder() {
666670
ty::ClauseKind::Trait(trait_predicate) => {
@@ -700,6 +704,33 @@ pub(super) fn assert_only_contains_predicates_from<'tcx>(
700704
}
701705
}
702706
}
707+
PredicateFilter::SelfTraitThatDefines(_) => {
708+
for (clause, _) in bounds {
709+
match clause.kind().skip_binder() {
710+
ty::ClauseKind::Trait(trait_predicate) => {
711+
assert_eq!(
712+
trait_predicate.self_ty(),
713+
ty,
714+
"expected `Self` predicate when computing \
715+
`{filter:?}` implied bounds: {clause:?}"
716+
);
717+
}
718+
719+
ty::ClauseKind::Projection(_)
720+
| ty::ClauseKind::TypeOutlives(_)
721+
| ty::ClauseKind::RegionOutlives(_)
722+
| ty::ClauseKind::ConstArgHasType(_, _)
723+
| ty::ClauseKind::WellFormed(_)
724+
| ty::ClauseKind::ConstEvaluatable(_)
725+
| ty::ClauseKind::HostEffect(..) => {
726+
bug!(
727+
"unexpected non-`Self` predicate when computing \
728+
`{filter:?}` implied bounds: {clause:?}"
729+
);
730+
}
731+
}
732+
}
733+
}
703734
PredicateFilter::ConstIfConst => {
704735
for (clause, _) in bounds {
705736
match clause.kind().skip_binder() {
@@ -771,11 +802,10 @@ pub(super) fn type_param_predicates<'tcx>(
771802

772803
let param_id = tcx.local_def_id_to_hir_id(def_id);
773804
let param_owner = tcx.hir().ty_param_owner(def_id);
774-
let generics = tcx.generics_of(param_owner);
775-
let index = generics.param_def_id_to_index[&def_id.to_def_id()];
776805

777806
// Don't look for bounds where the type parameter isn't in scope.
778807
let parent = if item_def_id == param_owner {
808+
// FIXME: Shouldn't this be unreachable?
779809
None
780810
} else {
781811
tcx.generics_of(item_def_id).parent.map(|def_id| def_id.expect_local())
@@ -795,6 +825,7 @@ pub(super) fn type_param_predicates<'tcx>(
795825
let Some(hir_generics) = hir_node.generics() else {
796826
return result;
797827
};
828+
798829
if let Node::Item(item) = hir_node
799830
&& let ItemKind::Trait(..) = item.kind
800831
// Implied `Self: Trait` and supertrait bounds.
@@ -805,18 +836,11 @@ pub(super) fn type_param_predicates<'tcx>(
805836
}
806837

807838
let icx = ItemCtxt::new(tcx, item_def_id);
808-
let extra_predicates = extend.into_iter().chain(
809-
icx.probe_ty_param_bounds_in_generics(
810-
hir_generics,
811-
def_id,
812-
PredicateFilter::SelfThatDefines(assoc_name),
813-
)
814-
.into_iter()
815-
.filter(|(predicate, _)| match predicate.kind().skip_binder() {
816-
ty::ClauseKind::Trait(data) => data.self_ty().is_param(index),
817-
_ => false,
818-
}),
819-
);
839+
let extra_predicates = extend.into_iter().chain(icx.probe_ty_param_bounds_in_generics(
840+
hir_generics,
841+
def_id,
842+
PredicateFilter::SelfTraitThatDefines(assoc_name),
843+
));
820844

821845
ty::EarlyBinder::bind(
822846
tcx.arena.alloc_from_iter(result.skip_binder().iter().copied().chain(extra_predicates)),
@@ -851,7 +875,7 @@ impl<'tcx> ItemCtxt<'tcx> {
851875
// Ok
852876
}
853877
PredicateFilter::SelfOnly
854-
| PredicateFilter::SelfThatDefines(_)
878+
| PredicateFilter::SelfTraitThatDefines(_)
855879
| PredicateFilter::SelfConstIfConst
856880
| PredicateFilter::SelfAndAssociatedTypeBounds => continue,
857881
PredicateFilter::ConstIfConst => unreachable!(),

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
152152
'tcx: 'hir,
153153
{
154154
for hir_bound in hir_bounds {
155-
// In order to avoid cycles, when we're lowering `SelfThatDefines`,
155+
// In order to avoid cycles, when we're lowering `SelfTraitThatDefines`,
156156
// we skip over any traits that don't define the given associated type.
157-
if let PredicateFilter::SelfThatDefines(assoc_name) = predicate_filter {
157+
if let PredicateFilter::SelfTraitThatDefines(assoc_name) = predicate_filter {
158158
if let Some(trait_ref) = hir_bound.trait_ref()
159159
&& let Some(trait_did) = trait_ref.trait_def_id()
160160
&& self.tcx().trait_may_define_assoc_item(trait_did, assoc_name)
@@ -395,7 +395,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
395395
match predicate_filter {
396396
PredicateFilter::All
397397
| PredicateFilter::SelfOnly
398-
| PredicateFilter::SelfThatDefines(_)
399398
| PredicateFilter::SelfAndAssociatedTypeBounds => {
400399
bounds.push_projection_bound(
401400
tcx,
@@ -406,6 +405,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
406405
constraint.span,
407406
);
408407
}
408+
// SelfTraitThatDefines is only interested in trait predicates.
409+
PredicateFilter::SelfTraitThatDefines(_) => {}
409410
// `ConstIfConst` is only interested in `~const` bounds.
410411
PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {}
411412
}
@@ -432,7 +433,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
432433
);
433434
}
434435
PredicateFilter::SelfOnly
435-
| PredicateFilter::SelfThatDefines(_)
436+
| PredicateFilter::SelfTraitThatDefines(_)
436437
| PredicateFilter::SelfConstIfConst => {}
437438
}
438439
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub enum PredicateFilter {
7575
/// Only traits that reference `Self: ..` and define an associated type
7676
/// with the given ident are implied by the trait. This mode exists to
7777
/// side-step query cycles when lowering associated types.
78-
SelfThatDefines(Ident),
78+
SelfTraitThatDefines(Ident),
7979

8080
/// Only traits that reference `Self: ..` and their associated type bounds.
8181
/// For example, given `Self: Tr<A: B>`, this would expand to `Self: Tr`
@@ -708,9 +708,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
708708
);
709709

710710
match predicate_filter {
711+
// This is only concerned with trait predicates.
712+
PredicateFilter::SelfTraitThatDefines(..) => {
713+
bounds.push_trait_bound(tcx, poly_trait_ref, span, polarity);
714+
}
711715
PredicateFilter::All
712716
| PredicateFilter::SelfOnly
713-
| PredicateFilter::SelfThatDefines(..)
714717
| PredicateFilter::SelfAndAssociatedTypeBounds => {
715718
debug!(?poly_trait_ref);
716719
bounds.push_trait_bound(tcx, poly_trait_ref, span, polarity);

0 commit comments

Comments
 (0)