Skip to content
/ rust Public
forked from rust-lang/rust

Commit b5e51db

Browse files
committed
Auto merge of rust-lang#112938 - compiler-errors:clause-3, r=oli-obk
Migrate `TyCtxt::predicates_of` and `ParamEnv::caller_bounds` to `Clause` The last big change in the series. I will follow-up with additional filed issues once this PR lands: - [ ] Investigate making `TypeFoldable<TyCtxt<'tcx>> for ty::Clause<'tcx>` implementation less weird: https://github.com/rust-lang/rust/blob/2efe09170530fa18e42ff05b8d9dd23f00b5c430/compiler/rustc_middle/src/ty/structural_impls.rs#L672 - [ ] Clean up the elaborator since it should only be emitting child clauses, not predicates - [ ] Rename identifiers like `pred` and `predicates` to `clause` if they're actually clauses around the codebase - [ ] Validate that all of the `ToPredicate` impls are acutally still needed, or prune them if they're not r? `@ghost` until the other branch lands
2 parents b9ad9b7 + 374173c commit b5e51db

File tree

84 files changed

+512
-738
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+512
-738
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -678,8 +678,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
678678
let tcx = self.infcx.tcx;
679679

680680
// Find out if the predicates show that the type is a Fn or FnMut
681-
let find_fn_kind_from_did = |(pred, _): (ty::Predicate<'tcx>, _)| {
682-
if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) = pred.kind().skip_binder()
681+
let find_fn_kind_from_did = |(pred, _): (ty::Clause<'tcx>, _)| {
682+
if let ty::ClauseKind::Trait(pred) = pred.kind().skip_binder()
683683
&& pred.self_ty() == ty
684684
{
685685
if Some(pred.def_id()) == tcx.lang_items().fn_trait() {
@@ -705,7 +705,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
705705
ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) => tcx
706706
.explicit_item_bounds(def_id)
707707
.subst_iter_copied(tcx, substs)
708-
.find_map(|(clause, span)| find_fn_kind_from_did((clause.as_predicate(), span))),
708+
.find_map(|(clause, span)| find_fn_kind_from_did((clause, span))),
709709
ty::Closure(_, substs) => match substs.as_closure().kind() {
710710
ty::ClosureKind::Fn => Some(hir::Mutability::Not),
711711
ty::ClosureKind::FnMut => Some(hir::Mutability::Mut),

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
928928

929929
fn any_param_predicate_mentions(
930930
&self,
931-
predicates: &[ty::Predicate<'tcx>],
931+
clauses: &[ty::Clause<'tcx>],
932932
ty: Ty<'tcx>,
933933
region: ty::EarlyBoundRegion,
934934
) -> bool {
@@ -937,10 +937,10 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
937937
if let ty::GenericArgKind::Type(ty) = arg.unpack()
938938
&& let ty::Param(_) = ty.kind()
939939
{
940-
predicates.iter().any(|pred| {
940+
clauses.iter().any(|pred| {
941941
match pred.kind().skip_binder() {
942-
ty::PredicateKind::Clause(ty::ClauseKind::Trait(data)) if data.self_ty() == ty => {}
943-
ty::PredicateKind::Clause(ty::ClauseKind::Projection(data)) if data.projection_ty.self_ty() == ty => {}
942+
ty::ClauseKind::Trait(data) if data.self_ty() == ty => {}
943+
ty::ClauseKind::Projection(data) if data.projection_ty.self_ty() == ty => {}
944944
_ => return false,
945945
}
946946
tcx.any_free_region_meets(pred, |r| {

compiler/rustc_hir_analysis/src/astconv/mod.rs

+25-35
Original file line numberDiff line numberDiff line change
@@ -945,40 +945,30 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
945945

946946
let mut trait_bounds = vec![];
947947
let mut projection_bounds = vec![];
948-
for (clause, span) in bounds.clauses() {
949-
let pred: ty::Predicate<'tcx> = clause.as_predicate();
948+
for (pred, span) in bounds.clauses() {
950949
let bound_pred = pred.kind();
951950
match bound_pred.skip_binder() {
952-
ty::PredicateKind::Clause(clause) => match clause {
953-
ty::ClauseKind::Trait(trait_pred) => {
954-
assert_eq!(trait_pred.polarity, ty::ImplPolarity::Positive);
955-
trait_bounds.push((
956-
bound_pred.rebind(trait_pred.trait_ref),
957-
span,
958-
trait_pred.constness,
959-
));
960-
}
961-
ty::ClauseKind::Projection(proj) => {
962-
projection_bounds.push((bound_pred.rebind(proj), span));
963-
}
964-
ty::ClauseKind::TypeOutlives(_) => {
965-
// Do nothing, we deal with regions separately
966-
}
967-
ty::ClauseKind::RegionOutlives(_)
968-
| ty::ClauseKind::ConstArgHasType(..)
969-
| ty::ClauseKind::WellFormed(_)
970-
| ty::ClauseKind::ConstEvaluatable(_) => {
971-
bug!()
972-
}
973-
},
974-
ty::PredicateKind::AliasRelate(..)
975-
| ty::PredicateKind::ObjectSafe(_)
976-
| ty::PredicateKind::ClosureKind(_, _, _)
977-
| ty::PredicateKind::Subtype(_)
978-
| ty::PredicateKind::Coerce(_)
979-
| ty::PredicateKind::ConstEquate(_, _)
980-
| ty::PredicateKind::TypeWellFormedFromEnv(_)
981-
| ty::PredicateKind::Ambiguous => bug!(),
951+
ty::ClauseKind::Trait(trait_pred) => {
952+
assert_eq!(trait_pred.polarity, ty::ImplPolarity::Positive);
953+
trait_bounds.push((
954+
bound_pred.rebind(trait_pred.trait_ref),
955+
span,
956+
trait_pred.constness,
957+
));
958+
}
959+
ty::ClauseKind::Projection(proj) => {
960+
projection_bounds.push((bound_pred.rebind(proj), span));
961+
}
962+
ty::ClauseKind::TypeOutlives(_) => {
963+
// Do nothing, we deal with regions separately
964+
}
965+
ty::ClauseKind::RegionOutlives(_)
966+
| ty::ClauseKind::ConstArgHasType(..)
967+
| ty::ClauseKind::WellFormed(_)
968+
| ty::ClauseKind::ConstEvaluatable(_)
969+
| ty::ClauseKind::TypeWellFormedFromEnv(_) => {
970+
bug!()
971+
}
982972
}
983973
}
984974

@@ -1425,9 +1415,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
14251415
|| {
14261416
traits::transitive_bounds_that_define_assoc_item(
14271417
tcx,
1428-
predicates.iter().filter_map(|(p, _)| {
1429-
Some(p.to_opt_poly_trait_pred()?.map_bound(|t| t.trait_ref))
1430-
}),
1418+
predicates
1419+
.iter()
1420+
.filter_map(|(p, _)| Some(p.as_trait_clause()?.map_bound(|t| t.trait_ref))),
14311421
assoc_name,
14321422
)
14331423
},

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ fn compare_method_predicate_entailment<'tcx>(
220220
// the new hybrid bounds we computed.
221221
let normalize_cause = traits::ObligationCause::misc(impl_m_span, impl_m_def_id);
222222
let param_env = ty::ParamEnv::new(
223-
tcx.mk_predicates(&hybrid_preds.predicates),
223+
tcx.mk_clauses(&hybrid_preds.predicates),
224224
Reveal::UserFacing,
225225
hir::Constness::NotConst,
226226
);
@@ -1835,7 +1835,7 @@ fn compare_type_predicate_entailment<'tcx>(
18351835
let impl_ty_span = tcx.def_span(impl_ty_def_id);
18361836
let normalize_cause = traits::ObligationCause::misc(impl_ty_span, impl_ty_def_id);
18371837
let param_env = ty::ParamEnv::new(
1838-
tcx.mk_predicates(&hybrid_preds.predicates),
1838+
tcx.mk_clauses(&hybrid_preds.predicates),
18391839
Reveal::UserFacing,
18401840
hir::Constness::NotConst,
18411841
);
@@ -2011,7 +2011,7 @@ pub(super) fn check_type_bounds<'tcx>(
20112011
.to_predicate(tcx),
20122012
),
20132013
};
2014-
ty::ParamEnv::new(tcx.mk_predicates(&predicates), Reveal::UserFacing, param_env.constness())
2014+
ty::ParamEnv::new(tcx.mk_clauses(&predicates), Reveal::UserFacing, param_env.constness())
20152015
};
20162016
debug!(?normalize_param_env);
20172017

compiler/rustc_hir_analysis/src/check/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,15 @@ fn default_body_is_unstable(
296296
/// Re-sugar `ty::GenericPredicates` in a way suitable to be used in structured suggestions.
297297
fn bounds_from_generic_predicates<'tcx>(
298298
tcx: TyCtxt<'tcx>,
299-
predicates: impl IntoIterator<Item = (ty::Predicate<'tcx>, Span)>,
299+
predicates: impl IntoIterator<Item = (ty::Clause<'tcx>, Span)>,
300300
) -> (String, String) {
301301
let mut types: FxHashMap<Ty<'tcx>, Vec<DefId>> = FxHashMap::default();
302302
let mut projections = vec![];
303303
for (predicate, _) in predicates {
304304
debug!("predicate {:?}", predicate);
305305
let bound_predicate = predicate.kind();
306306
match bound_predicate.skip_binder() {
307-
ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_predicate)) => {
307+
ty::ClauseKind::Trait(trait_predicate) => {
308308
let entry = types.entry(trait_predicate.self_ty()).or_default();
309309
let def_id = trait_predicate.def_id();
310310
if Some(def_id) != tcx.lang_items().sized_trait() {
@@ -313,7 +313,7 @@ fn bounds_from_generic_predicates<'tcx>(
313313
entry.push(trait_predicate.def_id());
314314
}
315315
}
316-
ty::PredicateKind::Clause(ty::ClauseKind::Projection(projection_pred)) => {
316+
ty::ClauseKind::Projection(projection_pred) => {
317317
projections.push(bound_predicate.rebind(projection_pred));
318318
}
319319
_ => {}
@@ -362,7 +362,7 @@ fn fn_sig_suggestion<'tcx>(
362362
tcx: TyCtxt<'tcx>,
363363
sig: ty::FnSig<'tcx>,
364364
ident: Ident,
365-
predicates: impl IntoIterator<Item = (ty::Predicate<'tcx>, Span)>,
365+
predicates: impl IntoIterator<Item = (ty::Clause<'tcx>, Span)>,
366366
assoc: ty::AssocItem,
367367
) -> String {
368368
let args = sig

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+30-34
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_middle::mir::ConstraintCategory;
1515
use rustc_middle::query::Providers;
1616
use rustc_middle::ty::trait_def::TraitSpecializationKind;
1717
use rustc_middle::ty::{
18-
self, AdtKind, GenericParamDefKind, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable,
18+
self, AdtKind, GenericParamDefKind, ToPredicate, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable,
1919
TypeVisitable, TypeVisitableExt, TypeVisitor,
2020
};
2121
use rustc_middle::ty::{GenericArgKind, InternalSubsts};
@@ -322,7 +322,7 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
322322
// Gather the bounds with which all other items inside of this trait constrain the GAT.
323323
// This is calculated by taking the intersection of the bounds that each item
324324
// constrains the GAT with individually.
325-
let mut new_required_bounds: Option<FxHashSet<ty::Predicate<'_>>> = None;
325+
let mut new_required_bounds: Option<FxHashSet<ty::Clause<'_>>> = None;
326326
for item in associated_items {
327327
let item_def_id = item.id.owner_id;
328328
// Skip our own GAT, since it does not constrain itself at all.
@@ -419,28 +419,25 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
419419
let mut unsatisfied_bounds: Vec<_> = required_bounds
420420
.into_iter()
421421
.filter(|clause| match clause.kind().skip_binder() {
422-
ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(
423-
ty::OutlivesPredicate(a, b),
424-
)) => !region_known_to_outlive(
425-
tcx,
426-
gat_def_id.def_id,
427-
param_env,
428-
&FxIndexSet::default(),
429-
a,
430-
b,
431-
),
432-
ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(
433-
a,
434-
b,
435-
))) => !ty_known_to_outlive(
422+
ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(a, b)) => {
423+
!region_known_to_outlive(
424+
tcx,
425+
gat_def_id.def_id,
426+
param_env,
427+
&FxIndexSet::default(),
428+
a,
429+
b,
430+
)
431+
}
432+
ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(a, b)) => !ty_known_to_outlive(
436433
tcx,
437434
gat_def_id.def_id,
438435
param_env,
439436
&FxIndexSet::default(),
440437
a,
441438
b,
442439
),
443-
_ => bug!("Unexpected PredicateKind"),
440+
_ => bug!("Unexpected ClauseKind"),
444441
})
445442
.map(|clause| clause.to_string())
446443
.collect();
@@ -488,7 +485,7 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
488485
fn augment_param_env<'tcx>(
489486
tcx: TyCtxt<'tcx>,
490487
param_env: ty::ParamEnv<'tcx>,
491-
new_predicates: Option<&FxHashSet<ty::Predicate<'tcx>>>,
488+
new_predicates: Option<&FxHashSet<ty::Clause<'tcx>>>,
492489
) -> ty::ParamEnv<'tcx> {
493490
let Some(new_predicates) = new_predicates else {
494491
return param_env;
@@ -498,7 +495,7 @@ fn augment_param_env<'tcx>(
498495
return param_env;
499496
}
500497

501-
let bounds = tcx.mk_predicates_from_iter(
498+
let bounds = tcx.mk_clauses_from_iter(
502499
param_env.caller_bounds().iter().chain(new_predicates.iter().cloned()),
503500
);
504501
// FIXME(compiler-errors): Perhaps there is a case where we need to normalize this
@@ -524,7 +521,7 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(
524521
wf_tys: &FxIndexSet<Ty<'tcx>>,
525522
gat_def_id: LocalDefId,
526523
gat_generics: &'tcx ty::Generics,
527-
) -> Option<FxHashSet<ty::Predicate<'tcx>>> {
524+
) -> Option<FxHashSet<ty::Clause<'tcx>>> {
528525
// The bounds we that we would require from `to_check`
529526
let mut bounds = FxHashSet::default();
530527

@@ -573,11 +570,10 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(
573570
);
574571
// The predicate we expect to see. (In our example,
575572
// `Self: 'me`.)
576-
let clause = ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(
577-
ty::OutlivesPredicate(ty_param, region_param),
578-
));
579-
let clause = tcx.mk_predicate(ty::Binder::dummy(clause));
580-
bounds.insert(clause);
573+
bounds.insert(
574+
ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(ty_param, region_param))
575+
.to_predicate(tcx),
576+
);
581577
}
582578
}
583579

@@ -622,11 +618,13 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(
622618
},
623619
);
624620
// The predicate we expect to see.
625-
let clause = ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(
626-
ty::OutlivesPredicate(region_a_param, region_b_param),
627-
));
628-
let clause = tcx.mk_predicate(ty::Binder::dummy(clause));
629-
bounds.insert(clause);
621+
bounds.insert(
622+
ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(
623+
region_a_param,
624+
region_b_param,
625+
))
626+
.to_predicate(tcx),
627+
);
630628
}
631629
}
632630
}
@@ -1406,7 +1404,7 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
14061404
infcx,
14071405
wfcx.param_env.without_const(),
14081406
wfcx.body_def_id,
1409-
p,
1407+
p.as_predicate(),
14101408
sp,
14111409
)
14121410
});
@@ -1875,9 +1873,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
18751873
// We lower empty bounds like `Vec<dyn Copy>:` as
18761874
// `WellFormed(Vec<dyn Copy>)`, which will later get checked by
18771875
// regular WF checking
1878-
if let ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(..)) =
1879-
pred.kind().skip_binder()
1880-
{
1876+
if let ty::ClauseKind::WellFormed(..) = pred.kind().skip_binder() {
18811877
continue;
18821878
}
18831879
// Match the existing behavior.

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,12 @@ fn associated_type_bounds<'tcx>(
3838
.iter()
3939
.copied()
4040
.filter(|(pred, _)| match pred.kind().skip_binder() {
41-
ty::PredicateKind::Clause(ty::ClauseKind::Trait(tr)) => tr.self_ty() == item_ty,
42-
ty::PredicateKind::Clause(ty::ClauseKind::Projection(proj)) => {
43-
proj.projection_ty.self_ty() == item_ty
44-
}
45-
ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(outlives)) => {
46-
outlives.0 == item_ty
47-
}
41+
ty::ClauseKind::Trait(tr) => tr.self_ty() == item_ty,
42+
ty::ClauseKind::Projection(proj) => proj.projection_ty.self_ty() == item_ty,
43+
ty::ClauseKind::TypeOutlives(outlives) => outlives.0 == item_ty,
4844
_ => false,
4945
})
50-
.map(|(pred, span)| (pred.expect_clause(), span));
46+
.map(|(clause, span)| (clause, span));
5147

5248
let all_bounds = tcx.arena.alloc_from_iter(bounds.clauses().chain(bounds_from_parent));
5349
debug!(

0 commit comments

Comments
 (0)