Skip to content

Commit 5ca0e9f

Browse files
committed
Auto merge of rust-lang#132196 - compiler-errors:probe_ty_param_bounds, r=petrochenkov
Some where clause lowering simplifications Rename `PredicateFilter::SelfThatDefines` to `PredicateFilter::SelfTraitThatDefines` to make it clear that it's only concerned with converting *traits*, and make it do a bit less work when converting bounds. Also, make the predicate filter matching in `probe_ty_param_bounds_in_generics` explicit, and simply the args it receives a bit.
2 parents 145f9cf + 81dba07 commit 5ca0e9f

File tree

4 files changed

+94
-49
lines changed

4 files changed

+94
-49
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

+81-40
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

@@ -606,12 +610,8 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
606610
let mut bounds = Bounds::default();
607611
icx.lowerer().lower_bounds(self_param_ty, superbounds, &mut bounds, ty::List::empty(), filter);
608612

609-
let where_bounds_that_match = icx.probe_ty_param_bounds_in_generics(
610-
generics,
611-
item.owner_id.def_id,
612-
self_param_ty,
613-
filter,
614-
);
613+
let where_bounds_that_match =
614+
icx.probe_ty_param_bounds_in_generics(generics, item.owner_id.def_id, filter);
615615

616616
// Combine the two lists to form the complete set of superbounds:
617617
let implied_bounds =
@@ -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,
@@ -664,7 +664,7 @@ pub(super) fn assert_only_contains_predicates_from<'tcx>(
664664
}
665665

666666
match filter {
667-
PredicateFilter::SelfOnly | PredicateFilter::SelfThatDefines(_) => {
667+
PredicateFilter::SelfOnly => {
668668
for (clause, _) in bounds {
669669
match clause.kind().skip_binder() {
670670
ty::ClauseKind::Trait(trait_predicate) => {
@@ -704,6 +704,33 @@ pub(super) fn assert_only_contains_predicates_from<'tcx>(
704704
}
705705
}
706706
}
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+
}
707734
PredicateFilter::ConstIfConst => {
708735
for (clause, _) in bounds {
709736
match clause.kind().skip_binder() {
@@ -767,21 +794,16 @@ pub(super) fn type_param_predicates<'tcx>(
767794
None => {}
768795
}
769796

770-
use rustc_hir::*;
771-
use rustc_middle::ty::Ty;
772-
773797
// In the HIR, bounds can derive from two places. Either
774798
// written inline like `<T: Foo>` or in a where-clause like
775799
// `where T: Foo`.
776800

777801
let param_id = tcx.local_def_id_to_hir_id(def_id);
778802
let param_owner = tcx.hir().ty_param_owner(def_id);
779-
let generics = tcx.generics_of(param_owner);
780-
let index = generics.param_def_id_to_index[&def_id.to_def_id()];
781-
let ty = Ty::new_param(tcx, index, tcx.hir().ty_param_name(def_id));
782803

783804
// Don't look for bounds where the type parameter isn't in scope.
784805
let parent = if item_def_id == param_owner {
806+
// FIXME: Shouldn't this be unreachable?
785807
None
786808
} else {
787809
tcx.generics_of(item_def_id).parent.map(|def_id| def_id.expect_local())
@@ -801,8 +823,9 @@ pub(super) fn type_param_predicates<'tcx>(
801823
let Some(hir_generics) = hir_node.generics() else {
802824
return result;
803825
};
826+
804827
if let Node::Item(item) = hir_node
805-
&& let ItemKind::Trait(..) = item.kind
828+
&& let hir::ItemKind::Trait(..) = item.kind
806829
// Implied `Self: Trait` and supertrait bounds.
807830
&& param_id == item_hir_id
808831
{
@@ -811,23 +834,34 @@ pub(super) fn type_param_predicates<'tcx>(
811834
}
812835

813836
let icx = ItemCtxt::new(tcx, item_def_id);
814-
let extra_predicates = extend.into_iter().chain(
815-
icx.probe_ty_param_bounds_in_generics(
816-
hir_generics,
817-
def_id,
818-
ty,
819-
PredicateFilter::SelfThatDefines(assoc_name),
820-
)
821-
.into_iter()
822-
.filter(|(predicate, _)| match predicate.kind().skip_binder() {
823-
ty::ClauseKind::Trait(data) => data.self_ty().is_param(index),
824-
_ => false,
825-
}),
837+
let extra_predicates = extend.into_iter().chain(icx.probe_ty_param_bounds_in_generics(
838+
hir_generics,
839+
def_id,
840+
PredicateFilter::SelfTraitThatDefines(assoc_name),
841+
));
842+
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,
826862
);
827863

828-
ty::EarlyBinder::bind(
829-
tcx.arena.alloc_from_iter(result.skip_binder().iter().copied().chain(extra_predicates)),
830-
)
864+
ty::EarlyBinder::bind(bounds)
831865
}
832866

833867
impl<'tcx> ItemCtxt<'tcx> {
@@ -841,7 +875,6 @@ impl<'tcx> ItemCtxt<'tcx> {
841875
&self,
842876
hir_generics: &'tcx hir::Generics<'tcx>,
843877
param_def_id: LocalDefId,
844-
ty: Ty<'tcx>,
845878
filter: PredicateFilter,
846879
) -> Vec<(ty::Clause<'tcx>, Span)> {
847880
let mut bounds = Bounds::default();
@@ -851,13 +884,21 @@ impl<'tcx> ItemCtxt<'tcx> {
851884
continue;
852885
};
853886

854-
let bound_ty = if predicate.is_param_bound(param_def_id.to_def_id()) {
855-
ty
856-
} else if matches!(filter, PredicateFilter::All) {
857-
self.lowerer().lower_ty_maybe_return_type_notation(predicate.bounded_ty)
858-
} else {
859-
continue;
860-
};
887+
match filter {
888+
_ if predicate.is_param_bound(param_def_id.to_def_id()) => {
889+
// Ok
890+
}
891+
PredicateFilter::All => {
892+
// Ok
893+
}
894+
PredicateFilter::SelfOnly
895+
| PredicateFilter::SelfTraitThatDefines(_)
896+
| PredicateFilter::SelfConstIfConst
897+
| PredicateFilter::SelfAndAssociatedTypeBounds => continue,
898+
PredicateFilter::ConstIfConst => unreachable!(),
899+
}
900+
901+
let bound_ty = self.lowerer().lower_ty_maybe_return_type_notation(predicate.bounded_ty);
861902

862903
let bound_vars = self.tcx.late_bound_vars(predicate.hir_id);
863904
self.lowerer().lower_bounds(

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)
@@ -389,7 +389,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
389389
match predicate_filter {
390390
PredicateFilter::All
391391
| PredicateFilter::SelfOnly
392-
| PredicateFilter::SelfThatDefines(_)
393392
| PredicateFilter::SelfAndAssociatedTypeBounds => {
394393
bounds.push_projection_bound(
395394
tcx,
@@ -400,6 +399,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
400399
constraint.span,
401400
);
402401
}
402+
// SelfTraitThatDefines is only interested in trait predicates.
403+
PredicateFilter::SelfTraitThatDefines(_) => {}
403404
// `ConstIfConst` is only interested in `~const` bounds.
404405
PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {}
405406
}
@@ -426,7 +427,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
426427
);
427428
}
428429
PredicateFilter::SelfOnly
429-
| PredicateFilter::SelfThatDefines(_)
430+
| PredicateFilter::SelfTraitThatDefines(_)
430431
| PredicateFilter::SelfConstIfConst => {}
431432
}
432433
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

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

8181
/// Only traits that reference `Self: ..` and their associated type bounds.
8282
/// For example, given `Self: Tr<A: B>`, this would expand to `Self: Tr`
@@ -730,9 +730,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
730730
}
731731

732732
match predicate_filter {
733+
// This is only concerned with trait predicates.
734+
PredicateFilter::SelfTraitThatDefines(..) => {
735+
bounds.push_trait_bound(tcx, poly_trait_ref, span, polarity);
736+
}
733737
PredicateFilter::All
734738
| PredicateFilter::SelfOnly
735-
| PredicateFilter::SelfThatDefines(..)
736739
| PredicateFilter::SelfAndAssociatedTypeBounds => {
737740
debug!(?poly_trait_ref);
738741
bounds.push_trait_bound(tcx, poly_trait_ref, span, polarity);

0 commit comments

Comments
 (0)