Skip to content

Commit 8aa899e

Browse files
authored
Unrolled build for rust-lang#131939
Rollup merge of rust-lang#131939 - compiler-errors:predicate-filter, r=fmease Get rid of `OnlySelfBounds` We turn `PredicateFilter` into a newtyped bool called `OnlySelfBounds`. There's no reason to lose the information of the `PredicateFilter`, so let's just pass it all the way through.
2 parents da93539 + 9989b1b commit 8aa899e

File tree

5 files changed

+61
-95
lines changed

5 files changed

+61
-95
lines changed

compiler/rustc_hir_analysis/src/bounds.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::ty::{self, Ty, TyCtxt, Upcast};
99
use rustc_span::Span;
1010
use rustc_span::def_id::DefId;
1111

12-
use crate::hir_ty_lowering::OnlySelfBounds;
12+
use crate::hir_ty_lowering::PredicateFilter;
1313

1414
/// Collects together a list of type bounds. These lists of bounds occur in many places
1515
/// in Rust's syntax:
@@ -52,7 +52,7 @@ impl<'tcx> Bounds<'tcx> {
5252
span: Span,
5353
polarity: ty::PredicatePolarity,
5454
constness: ty::BoundConstness,
55-
only_self_bounds: OnlySelfBounds,
55+
predicate_filter: PredicateFilter,
5656
) {
5757
let clause = (
5858
bound_trait_ref
@@ -72,9 +72,18 @@ impl<'tcx> Bounds<'tcx> {
7272
// FIXME(effects): Lift this out of `push_trait_bound`, and move it somewhere else.
7373
// Perhaps moving this into `lower_poly_trait_ref`, just like we lower associated
7474
// type bounds.
75-
if !tcx.features().effects || only_self_bounds.0 {
75+
if !tcx.features().effects {
7676
return;
7777
}
78+
match predicate_filter {
79+
PredicateFilter::SelfOnly | PredicateFilter::SelfThatDefines(_) => {
80+
return;
81+
}
82+
PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {
83+
// Ok.
84+
}
85+
}
86+
7887
// For `T: ~const Tr` or `T: const Tr`, we need to add an additional bound on the
7988
// associated type of `<T as Tr>` and make sure that the effect is compatible.
8089
let compat_val = match (tcx.def_kind(defining_def_id), constness) {

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+4-36
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::bounds::Bounds;
1616
use crate::collect::ItemCtxt;
1717
use crate::constrained_generic_params as cgp;
1818
use crate::delegation::inherit_predicates_for_delegation_item;
19-
use crate::hir_ty_lowering::{HirTyLowerer, OnlySelfBounds, PredicateFilter, RegionInferReason};
19+
use crate::hir_ty_lowering::{HirTyLowerer, PredicateFilter, RegionInferReason};
2020

2121
/// Returns a list of all type predicates (explicit and implicit) for the definition with
2222
/// ID `def_id`. This includes all predicates returned by `explicit_predicates_of`, plus
@@ -270,7 +270,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
270270
bound_pred.bounds.iter(),
271271
&mut bounds,
272272
bound_vars,
273-
OnlySelfBounds(false),
273+
PredicateFilter::All,
274274
);
275275
predicates.extend(bounds.clauses(tcx));
276276
effects_min_tys.extend(bounds.effects_min_tys());
@@ -825,20 +825,6 @@ impl<'tcx> ItemCtxt<'tcx> {
825825
continue;
826826
};
827827

828-
// Subtle: If we're collecting `SelfAndAssociatedTypeBounds`, then we
829-
// want to only consider predicates with `Self: ...`, but we don't want
830-
// `OnlySelfBounds(true)` since we want to collect the nested associated
831-
// type bound as well.
832-
let (only_self_bounds, assoc_name) = match filter {
833-
PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {
834-
(OnlySelfBounds(false), None)
835-
}
836-
PredicateFilter::SelfOnly => (OnlySelfBounds(true), None),
837-
PredicateFilter::SelfThatDefines(assoc_name) => {
838-
(OnlySelfBounds(true), Some(assoc_name))
839-
}
840-
};
841-
842828
let bound_ty = if predicate.is_param_bound(param_def_id.to_def_id()) {
843829
ty
844830
} else if matches!(filter, PredicateFilter::All) {
@@ -850,31 +836,13 @@ impl<'tcx> ItemCtxt<'tcx> {
850836
let bound_vars = self.tcx.late_bound_vars(predicate.hir_id);
851837
self.lowerer().lower_poly_bounds(
852838
bound_ty,
853-
predicate.bounds.iter().filter(|bound| {
854-
assoc_name
855-
.map_or(true, |assoc_name| self.bound_defines_assoc_item(bound, assoc_name))
856-
}),
839+
predicate.bounds.iter(),
857840
&mut bounds,
858841
bound_vars,
859-
only_self_bounds,
842+
filter,
860843
);
861844
}
862845

863846
bounds.clauses(self.tcx).collect()
864847
}
865-
866-
#[instrument(level = "trace", skip(self))]
867-
fn bound_defines_assoc_item(&self, b: &hir::GenericBound<'_>, assoc_name: Ident) -> bool {
868-
match b {
869-
hir::GenericBound::Trait(poly_trait_ref) => {
870-
let trait_ref = &poly_trait_ref.trait_ref;
871-
if let Some(trait_did) = trait_ref.trait_def_id() {
872-
self.tcx.trait_may_define_assoc_item(trait_did, assoc_name)
873-
} else {
874-
false
875-
}
876-
}
877-
_ => false,
878-
}
879-
}
880848
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

+38-45
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ use tracing::{debug, instrument};
1919
use super::errors::GenericsArgsErrExtend;
2020
use crate::bounds::Bounds;
2121
use crate::errors;
22-
use crate::hir_ty_lowering::{
23-
AssocItemQSelf, HirTyLowerer, OnlySelfBounds, PredicateFilter, RegionInferReason,
24-
};
22+
use crate::hir_ty_lowering::{AssocItemQSelf, HirTyLowerer, PredicateFilter, RegionInferReason};
2523

2624
impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2725
/// Add a `Sized` bound to the `bounds` if appropriate.
@@ -150,11 +148,25 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
150148
hir_bounds: I,
151149
bounds: &mut Bounds<'tcx>,
152150
bound_vars: &'tcx ty::List<ty::BoundVariableKind>,
153-
only_self_bounds: OnlySelfBounds,
151+
predicate_filter: PredicateFilter,
154152
) where
155153
'tcx: 'hir,
156154
{
157155
for hir_bound in hir_bounds {
156+
// In order to avoid cycles, when we're lowering `SelfThatDefines`,
157+
// we skip over any traits that don't define the given associated type.
158+
159+
if let PredicateFilter::SelfThatDefines(assoc_name) = predicate_filter {
160+
if let Some(trait_ref) = hir_bound.trait_ref()
161+
&& let Some(trait_did) = trait_ref.trait_def_id()
162+
&& self.tcx().trait_may_define_assoc_item(trait_did, assoc_name)
163+
{
164+
// Okay
165+
} else {
166+
continue;
167+
}
168+
}
169+
158170
match hir_bound {
159171
hir::GenericBound::Trait(poly_trait_ref) => {
160172
let (constness, polarity) = match poly_trait_ref.modifiers {
@@ -179,7 +191,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
179191
polarity,
180192
param_ty,
181193
bounds,
182-
only_self_bounds,
194+
predicate_filter,
183195
);
184196
}
185197
hir::GenericBound::Outlives(lifetime) => {
@@ -213,37 +225,16 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
213225
&self,
214226
param_ty: Ty<'tcx>,
215227
hir_bounds: &[hir::GenericBound<'tcx>],
216-
filter: PredicateFilter,
228+
predicate_filter: PredicateFilter,
217229
) -> Bounds<'tcx> {
218230
let mut bounds = Bounds::default();
219231

220-
let only_self_bounds = match filter {
221-
PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {
222-
OnlySelfBounds(false)
223-
}
224-
PredicateFilter::SelfOnly | PredicateFilter::SelfThatDefines(_) => OnlySelfBounds(true),
225-
};
226-
227232
self.lower_poly_bounds(
228233
param_ty,
229-
hir_bounds.iter().filter(|bound| match filter {
230-
PredicateFilter::All
231-
| PredicateFilter::SelfOnly
232-
| PredicateFilter::SelfAndAssociatedTypeBounds => true,
233-
PredicateFilter::SelfThatDefines(assoc_name) => {
234-
if let Some(trait_ref) = bound.trait_ref()
235-
&& let Some(trait_did) = trait_ref.trait_def_id()
236-
&& self.tcx().trait_may_define_assoc_item(trait_did, assoc_name)
237-
{
238-
true
239-
} else {
240-
false
241-
}
242-
}
243-
}),
234+
hir_bounds.iter(),
244235
&mut bounds,
245236
ty::List::empty(),
246-
only_self_bounds,
237+
predicate_filter,
247238
);
248239
debug!(?bounds);
249240

@@ -267,7 +258,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
267258
bounds: &mut Bounds<'tcx>,
268259
duplicates: &mut FxIndexMap<DefId, Span>,
269260
path_span: Span,
270-
only_self_bounds: OnlySelfBounds,
261+
predicate_filter: PredicateFilter,
271262
) -> Result<(), ErrorGuaranteed> {
272263
let tcx = self.tcx();
273264

@@ -444,21 +435,23 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
444435
// Lower a constraint like `Item: Debug` as found in HIR bound `T: Iterator<Item: Debug>`
445436
// to a bound involving a projection: `<T as Iterator>::Item: Debug`.
446437
hir::AssocItemConstraintKind::Bound { bounds: hir_bounds } => {
447-
// NOTE: If `only_self_bounds` is true, do NOT expand this associated type bound into
448-
// a trait predicate, since we only want to add predicates for the `Self` type.
449-
if !only_self_bounds.0 {
450-
let projection_ty = projection_term
451-
.map_bound(|projection_term| projection_term.expect_ty(self.tcx()));
452-
// Calling `skip_binder` is okay, because `lower_bounds` expects the `param_ty`
453-
// parameter to have a skipped binder.
454-
let param_ty = Ty::new_alias(tcx, ty::Projection, projection_ty.skip_binder());
455-
self.lower_poly_bounds(
456-
param_ty,
457-
hir_bounds.iter(),
458-
bounds,
459-
projection_ty.bound_vars(),
460-
only_self_bounds,
461-
);
438+
match predicate_filter {
439+
PredicateFilter::SelfOnly | PredicateFilter::SelfThatDefines(_) => {}
440+
PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {
441+
let projection_ty = projection_term
442+
.map_bound(|projection_term| projection_term.expect_ty(self.tcx()));
443+
// Calling `skip_binder` is okay, because `lower_bounds` expects the `param_ty`
444+
// parameter to have a skipped binder.
445+
let param_ty =
446+
Ty::new_alias(tcx, ty::Projection, projection_ty.skip_binder());
447+
self.lower_poly_bounds(
448+
param_ty,
449+
hir_bounds.iter(),
450+
bounds,
451+
projection_ty.bound_vars(),
452+
predicate_filter,
453+
);
454+
}
462455
}
463456
}
464457
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use tracing::{debug, instrument};
2020
use super::HirTyLowerer;
2121
use crate::bounds::Bounds;
2222
use crate::hir_ty_lowering::{
23-
GenericArgCountMismatch, GenericArgCountResult, OnlySelfBounds, RegionInferReason,
23+
GenericArgCountMismatch, GenericArgCountResult, PredicateFilter, RegionInferReason,
2424
};
2525

2626
impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
@@ -55,9 +55,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
5555
ty::PredicatePolarity::Positive,
5656
dummy_self,
5757
&mut bounds,
58-
// True so we don't populate `bounds` with associated type bounds, even
59-
// though they're disallowed from object types.
60-
OnlySelfBounds(true),
58+
PredicateFilter::SelfOnly,
6159
) {
6260
potential_assoc_types.extend(cur_potential_assoc_types);
6361
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ use crate::require_c_abi_if_c_variadic;
6464
#[derive(Debug)]
6565
pub struct GenericPathSegment(pub DefId, pub usize);
6666

67-
#[derive(Copy, Clone, Debug)]
68-
pub struct OnlySelfBounds(pub bool);
69-
7067
#[derive(Copy, Clone, Debug)]
7168
pub enum PredicateFilter {
7269
/// All predicates may be implied by the trait.
@@ -76,7 +73,8 @@ pub enum PredicateFilter {
7673
SelfOnly,
7774

7875
/// Only traits that reference `Self: ..` and define an associated type
79-
/// with the given ident are implied by the trait.
76+
/// with the given ident are implied by the trait. This mode exists to
77+
/// side-step query cycles when lowering associated types.
8078
SelfThatDefines(Ident),
8179

8280
/// Only traits that reference `Self: ..` and their associated type bounds.
@@ -683,7 +681,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
683681
polarity: ty::PredicatePolarity,
684682
self_ty: Ty<'tcx>,
685683
bounds: &mut Bounds<'tcx>,
686-
only_self_bounds: OnlySelfBounds,
684+
predicate_filter: PredicateFilter,
687685
) -> GenericArgCountResult {
688686
let trait_def_id = trait_ref.trait_def_id().unwrap_or_else(|| FatalError.raise());
689687
let trait_segment = trait_ref.path.segments.last().unwrap();
@@ -720,7 +718,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
720718
span,
721719
polarity,
722720
constness,
723-
only_self_bounds,
721+
predicate_filter,
724722
);
725723

726724
let mut dup_constraints = FxIndexMap::default();
@@ -744,7 +742,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
744742
bounds,
745743
&mut dup_constraints,
746744
constraint.span,
747-
only_self_bounds,
745+
predicate_filter,
748746
);
749747
// Okay to ignore `Err` because of `ErrorGuaranteed` (see above).
750748
}

0 commit comments

Comments
 (0)