Skip to content

Commit da73046

Browse files
Make SelfTraitThatDefines a tighter filter
1 parent d9834b1 commit da73046

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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,11 @@ pub(super) fn explicit_supertraits_containing_assoc_item<'tcx>(
564564
tcx: TyCtxt<'tcx>,
565565
(trait_def_id, assoc_name): (DefId, Ident),
566566
) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
567-
implied_predicates_with_filter(tcx, trait_def_id, PredicateFilter::SelfThatDefines(assoc_name))
567+
implied_predicates_with_filter(
568+
tcx,
569+
trait_def_id,
570+
PredicateFilter::SelfTraitThatDefines(assoc_name),
571+
)
568572
}
569573

570574
pub(super) fn explicit_implied_predicates_of<'tcx>(
@@ -593,7 +597,7 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
593597
let Some(trait_def_id) = trait_def_id.as_local() else {
594598
// if `assoc_name` is None, then the query should've been redirected to an
595599
// external provider
596-
assert_matches!(filter, PredicateFilter::SelfThatDefines(_));
600+
assert_matches!(filter, PredicateFilter::SelfTraitThatDefines(_));
597601
return tcx.explicit_super_predicates_of(trait_def_id);
598602
};
599603

@@ -667,7 +671,7 @@ pub(super) fn assert_only_contains_predicates_from<'tcx>(
667671
}
668672

669673
match filter {
670-
PredicateFilter::SelfOnly | PredicateFilter::SelfThatDefines(_) => {
674+
PredicateFilter::SelfOnly => {
671675
for (clause, _) in bounds {
672676
match clause.kind().skip_binder() {
673677
ty::ClauseKind::Trait(trait_predicate) => {
@@ -707,6 +711,33 @@ pub(super) fn assert_only_contains_predicates_from<'tcx>(
707711
}
708712
}
709713
}
714+
PredicateFilter::SelfTraitThatDefines(_) => {
715+
for (clause, _) in bounds {
716+
match clause.kind().skip_binder() {
717+
ty::ClauseKind::Trait(trait_predicate) => {
718+
assert_eq!(
719+
trait_predicate.self_ty(),
720+
ty,
721+
"expected `Self` predicate when computing \
722+
`{filter:?}` implied bounds: {clause:?}"
723+
);
724+
}
725+
726+
ty::ClauseKind::Projection(_)
727+
| ty::ClauseKind::TypeOutlives(_)
728+
| ty::ClauseKind::RegionOutlives(_)
729+
| ty::ClauseKind::ConstArgHasType(_, _)
730+
| ty::ClauseKind::WellFormed(_)
731+
| ty::ClauseKind::ConstEvaluatable(_)
732+
| ty::ClauseKind::HostEffect(..) => {
733+
bug!(
734+
"unexpected non-`Self` predicate when computing \
735+
`{filter:?}` implied bounds: {clause:?}"
736+
);
737+
}
738+
}
739+
}
740+
}
710741
PredicateFilter::ConstIfConst => {
711742
for (clause, _) in bounds {
712743
match clause.kind().skip_binder() {
@@ -768,11 +799,10 @@ pub(super) fn type_param_predicates<'tcx>(
768799

769800
let param_id = tcx.local_def_id_to_hir_id(def_id);
770801
let param_owner = tcx.hir().ty_param_owner(def_id);
771-
let generics = tcx.generics_of(param_owner);
772-
let index = generics.param_def_id_to_index[&def_id.to_def_id()];
773802

774803
// Don't look for bounds where the type parameter isn't in scope.
775804
let parent = if item_def_id == param_owner {
805+
// FIXME: Shouldn't this be unreachable?
776806
None
777807
} else {
778808
tcx.generics_of(item_def_id).parent.map(|def_id| def_id.expect_local())
@@ -792,6 +822,7 @@ pub(super) fn type_param_predicates<'tcx>(
792822
let Some(hir_generics) = hir_node.generics() else {
793823
return result;
794824
};
825+
795826
if let Node::Item(item) = hir_node
796827
&& let ItemKind::Trait(..) = item.kind
797828
// Implied `Self: Trait` and supertrait bounds.
@@ -802,18 +833,11 @@ pub(super) fn type_param_predicates<'tcx>(
802833
}
803834

804835
let icx = ItemCtxt::new(tcx, item_def_id);
805-
let extra_predicates = extend.into_iter().chain(
806-
icx.probe_ty_param_bounds_in_generics(
807-
hir_generics,
808-
def_id,
809-
PredicateFilter::SelfThatDefines(assoc_name),
810-
)
811-
.into_iter()
812-
.filter(|(predicate, _)| match predicate.kind().skip_binder() {
813-
ty::ClauseKind::Trait(data) => data.self_ty().is_param(index),
814-
_ => false,
815-
}),
816-
);
836+
let extra_predicates = extend.into_iter().chain(icx.probe_ty_param_bounds_in_generics(
837+
hir_generics,
838+
def_id,
839+
PredicateFilter::SelfTraitThatDefines(assoc_name),
840+
));
817841

818842
ty::EarlyBinder::bind(
819843
tcx.arena.alloc_from_iter(result.skip_binder().iter().copied().chain(extra_predicates)),
@@ -848,7 +872,7 @@ impl<'tcx> ItemCtxt<'tcx> {
848872
// Ok
849873
}
850874
PredicateFilter::SelfOnly
851-
| PredicateFilter::SelfThatDefines(_)
875+
| PredicateFilter::SelfTraitThatDefines(_)
852876
| PredicateFilter::SelfConstIfConst
853877
| PredicateFilter::SelfAndAssociatedTypeBounds => continue,
854878
PredicateFilter::ConstIfConst => unreachable!(),

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 5 additions & 4 deletions
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)
@@ -402,7 +402,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
402402
match predicate_filter {
403403
PredicateFilter::All
404404
| PredicateFilter::SelfOnly
405-
| PredicateFilter::SelfThatDefines(_)
406405
| PredicateFilter::SelfAndAssociatedTypeBounds => {
407406
bounds.push_projection_bound(
408407
tcx,
@@ -413,6 +412,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
413412
constraint.span,
414413
);
415414
}
415+
// SelfTraitThatDefines is only interested in trait predicates.
416+
PredicateFilter::SelfTraitThatDefines(_) => {}
416417
// `ConstIfConst` is only interested in `~const` bounds.
417418
PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {}
418419
}
@@ -439,7 +440,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
439440
);
440441
}
441442
PredicateFilter::SelfOnly
442-
| PredicateFilter::SelfThatDefines(_)
443+
| PredicateFilter::SelfTraitThatDefines(_)
443444
| PredicateFilter::SelfConstIfConst => {}
444445
}
445446
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 5 additions & 2 deletions
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`
@@ -700,9 +700,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
700700
);
701701

702702
match predicate_filter {
703+
// This is only concerned with trait predicates.
704+
PredicateFilter::SelfTraitThatDefines(..) => {
705+
bounds.push_trait_bound(tcx, poly_trait_ref, span, polarity);
706+
}
703707
PredicateFilter::All
704708
| PredicateFilter::SelfOnly
705-
| PredicateFilter::SelfThatDefines(..)
706709
| PredicateFilter::SelfAndAssociatedTypeBounds => {
707710
debug!(?poly_trait_ref);
708711
bounds.push_trait_bound(tcx, poly_trait_ref, span, polarity);

0 commit comments

Comments
 (0)