Skip to content

Commit a98c14f

Browse files
authored
Rollup merge of #112772 - compiler-errors:clauses-1, r=lcnr
Add a fully fledged `Clause` type, rename old `Clause` to `ClauseKind` Does two basic things before I put up a more delicate set of PRs (along the lines of #112714, but hopefully much cleaner) that migrate existing usages of `ty::Predicate` to `ty::Clause` (`predicates_of`/`item_bounds`/`ParamEnv::caller_bounds`). 1. Rename `Clause` to `ClauseKind`, so it's parallel with `PredicateKind`. 2. Add a new `Clause` type which is parallel to `Predicate`. * This type exposes `Clause::kind(self) -> Binder<'tcx, ClauseKind<'tcx>>` which is parallel to `Predicate::kind` 😸 The new `Clause` type essentially acts as a newtype wrapper around `Predicate` that asserts that it is specifically a `PredicateKind::Clause`. Turns out from experimentation[^1] that this is not negative performance-wise, which is wonderful, since this a much simpler design than something that requires encoding the discriminant into the alignment bits of a predicate kind, or something else like that... r? ``@lcnr`` or ``@oli-obk`` [^1]: #112714 (comment)
2 parents 34c8e53 + dcee3ab commit a98c14f

File tree

101 files changed

+725
-608
lines changed

Some content is hidden

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

101 files changed

+725
-608
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
679679

680680
// Find out if the predicates show that the type is a Fn or FnMut
681681
let find_fn_kind_from_did = |(pred, _): (ty::Predicate<'tcx>, _)| {
682-
if let ty::PredicateKind::Clause(ty::Clause::Trait(pred)) = pred.kind().skip_binder()
682+
if let ty::PredicateKind::Clause(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() {
@@ -776,7 +776,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
776776
let predicates: Result<Vec<_>, _> = errors
777777
.into_iter()
778778
.map(|err| match err.obligation.predicate.kind().skip_binder() {
779-
PredicateKind::Clause(ty::Clause::Trait(predicate)) => {
779+
PredicateKind::Clause(ty::ClauseKind::Trait(predicate)) => {
780780
match predicate.self_ty().kind() {
781781
ty::Param(param_ty) => Ok((
782782
generics.type_param(param_ty, tcx),

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -939,8 +939,8 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
939939
{
940940
predicates.iter().any(|pred| {
941941
match pred.kind().skip_binder() {
942-
ty::PredicateKind::Clause(ty::Clause::Trait(data)) if data.self_ty() == ty => {}
943-
ty::PredicateKind::Clause(ty::Clause::Projection(data)) if data.projection_ty.self_ty() == ty => {}
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 => {}
944944
_ => return false,
945945
}
946946
tcx.any_free_region_meets(pred, |r| {

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,9 @@ fn check_opaque_type_well_formed<'tcx>(
330330
// Require the hidden type to be well-formed with only the generics of the opaque type.
331331
// Defining use functions may have more bounds than the opaque type, which is ok, as long as the
332332
// hidden type is well formed even without those bounds.
333-
let predicate =
334-
ty::Binder::dummy(ty::PredicateKind::Clause(ty::Clause::WellFormed(definition_ty.into())));
333+
let predicate = ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(
334+
definition_ty.into(),
335+
)));
335336
ocx.register_obligation(Obligation::misc(tcx, definition_span, def_id, param_env, predicate));
336337

337338
// Check that all obligations are satisfied by the implementation's

compiler/rustc_borrowck/src/type_check/canonical.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,13 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
8989
category: ConstraintCategory<'tcx>,
9090
) {
9191
self.prove_predicate(
92-
ty::Binder::dummy(ty::PredicateKind::Clause(ty::Clause::Trait(ty::TraitPredicate {
93-
trait_ref,
94-
constness: ty::BoundConstness::NotConst,
95-
polarity: ty::ImplPolarity::Positive,
96-
}))),
92+
ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::Trait(
93+
ty::TraitPredicate {
94+
trait_ref,
95+
constness: ty::BoundConstness::NotConst,
96+
polarity: ty::ImplPolarity::Positive,
97+
},
98+
))),
9799
locations,
98100
category,
99101
);

compiler/rustc_borrowck/src/type_check/mod.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
14211421
// See #91068 for an example.
14221422
self.prove_predicates(
14231423
sig.inputs_and_output.iter().map(|ty| {
1424-
ty::Binder::dummy(ty::PredicateKind::Clause(ty::Clause::WellFormed(
1424+
ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(
14251425
ty.into(),
14261426
)))
14271427
}),
@@ -1853,7 +1853,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
18531853

18541854
let array_ty = rvalue.ty(body.local_decls(), tcx);
18551855
self.prove_predicate(
1856-
ty::PredicateKind::Clause(ty::Clause::WellFormed(array_ty.into())),
1856+
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(array_ty.into())),
18571857
Locations::Single(location),
18581858
ConstraintCategory::Boring,
18591859
);
@@ -2026,10 +2026,11 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20262026
ConstraintCategory::Cast,
20272027
);
20282028

2029-
let outlives_predicate =
2030-
tcx.mk_predicate(Binder::dummy(ty::PredicateKind::Clause(
2031-
ty::Clause::TypeOutlives(ty::OutlivesPredicate(self_ty, *region)),
2032-
)));
2029+
let outlives_predicate = tcx.mk_predicate(Binder::dummy(
2030+
ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(
2031+
ty::OutlivesPredicate(self_ty, *region),
2032+
)),
2033+
));
20332034
self.prove_predicate(
20342035
outlives_predicate,
20352036
location.to_locations(),

compiler/rustc_hir_analysis/src/astconv/errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
343343
let format_pred = |pred: ty::Predicate<'tcx>| {
344344
let bound_predicate = pred.kind();
345345
match bound_predicate.skip_binder() {
346-
ty::PredicateKind::Clause(ty::Clause::Projection(pred)) => {
346+
ty::PredicateKind::Clause(ty::ClauseKind::Projection(pred)) => {
347347
let pred = bound_predicate.rebind(pred);
348348
// `<Foo as Iterator>::Item = String`.
349349
let projection_ty = pred.skip_binder().projection_ty;
@@ -364,7 +364,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
364364
bound_span_label(projection_ty.self_ty(), &obligation, &quiet);
365365
Some((obligation, projection_ty.self_ty()))
366366
}
367-
ty::PredicateKind::Clause(ty::Clause::Trait(poly_trait_ref)) => {
367+
ty::PredicateKind::Clause(ty::ClauseKind::Trait(poly_trait_ref)) => {
368368
let p = poly_trait_ref.trait_ref;
369369
let self_ty = p.self_ty();
370370
let path = p.print_only_trait_path();

compiler/rustc_hir_analysis/src/astconv/mod.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -945,29 +945,31 @@ 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.predicates() {
949-
let pred: ty::Predicate<'tcx> = clause.to_predicate(tcx);
948+
for (clause, span) in bounds.clauses() {
949+
let pred: ty::Predicate<'tcx> = clause.as_predicate();
950950
let bound_pred = pred.kind();
951951
match bound_pred.skip_binder() {
952952
ty::PredicateKind::Clause(clause) => match clause {
953-
ty::Clause::Trait(trait_pred) => {
953+
ty::ClauseKind::Trait(trait_pred) => {
954954
assert_eq!(trait_pred.polarity, ty::ImplPolarity::Positive);
955955
trait_bounds.push((
956956
bound_pred.rebind(trait_pred.trait_ref),
957957
span,
958958
trait_pred.constness,
959959
));
960960
}
961-
ty::Clause::Projection(proj) => {
961+
ty::ClauseKind::Projection(proj) => {
962962
projection_bounds.push((bound_pred.rebind(proj), span));
963963
}
964-
ty::Clause::TypeOutlives(_) => {
964+
ty::ClauseKind::TypeOutlives(_) => {
965965
// Do nothing, we deal with regions separately
966966
}
967-
ty::Clause::RegionOutlives(_)
968-
| ty::Clause::ConstArgHasType(..)
969-
| ty::Clause::WellFormed(_)
970-
| ty::Clause::ConstEvaluatable(_) => bug!(),
967+
ty::ClauseKind::RegionOutlives(_)
968+
| ty::ClauseKind::ConstArgHasType(..)
969+
| ty::ClauseKind::WellFormed(_)
970+
| ty::ClauseKind::ConstEvaluatable(_) => {
971+
bug!()
972+
}
971973
},
972974
ty::PredicateKind::AliasRelate(..)
973975
| ty::PredicateKind::ObjectSafe(_)
@@ -1064,7 +1066,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10641066

10651067
let bound_predicate = pred.kind();
10661068
match bound_predicate.skip_binder() {
1067-
ty::PredicateKind::Clause(ty::Clause::Trait(pred)) => {
1069+
ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
10681070
let pred = bound_predicate.rebind(pred);
10691071
associated_types.entry(span).or_default().extend(
10701072
tcx.associated_items(pred.def_id())
@@ -1074,7 +1076,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10741076
.map(|item| item.def_id),
10751077
);
10761078
}
1077-
ty::PredicateKind::Clause(ty::Clause::Projection(pred)) => {
1079+
ty::PredicateKind::Clause(ty::ClauseKind::Projection(pred)) => {
10781080
let pred = bound_predicate.rebind(pred);
10791081
// A `Self` within the original bound will be substituted with a
10801082
// `trait_object_dummy_self`, so check for that.

compiler/rustc_hir_analysis/src/bounds.rs

+19-20
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
//! `ty` form from the HIR.
33
44
use rustc_hir::LangItem;
5-
use rustc_middle::ty::Binder;
65
use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt};
76
use rustc_span::Span;
87

@@ -24,58 +23,58 @@ use rustc_span::Span;
2423
/// include the self type (e.g., `trait_bounds`) but in others we do not
2524
#[derive(Default, PartialEq, Eq, Clone, Debug)]
2625
pub struct Bounds<'tcx> {
27-
pub predicates: Vec<(Binder<'tcx, ty::Clause<'tcx>>, Span)>,
26+
pub clauses: Vec<(ty::Clause<'tcx>, Span)>,
2827
}
2928

3029
impl<'tcx> Bounds<'tcx> {
3130
pub fn push_region_bound(
3231
&mut self,
33-
_tcx: TyCtxt<'tcx>,
32+
tcx: TyCtxt<'tcx>,
3433
region: ty::PolyTypeOutlivesPredicate<'tcx>,
3534
span: Span,
3635
) {
37-
self.predicates.push((region.map_bound(|p| ty::Clause::TypeOutlives(p)), span));
36+
self.clauses
37+
.push((region.map_bound(|p| ty::ClauseKind::TypeOutlives(p)).to_predicate(tcx), span));
3838
}
3939

4040
pub fn push_trait_bound(
4141
&mut self,
42-
_tcx: TyCtxt<'tcx>,
42+
tcx: TyCtxt<'tcx>,
4343
trait_ref: ty::PolyTraitRef<'tcx>,
4444
span: Span,
4545
constness: ty::BoundConstness,
4646
polarity: ty::ImplPolarity,
4747
) {
48-
self.predicates.push((
49-
trait_ref.map_bound(|trait_ref| {
50-
ty::Clause::Trait(ty::TraitPredicate { trait_ref, constness, polarity })
51-
}),
48+
self.clauses.push((
49+
trait_ref
50+
.map_bound(|trait_ref| {
51+
ty::ClauseKind::Trait(ty::TraitPredicate { trait_ref, constness, polarity })
52+
})
53+
.to_predicate(tcx),
5254
span,
5355
));
5456
}
5557

5658
pub fn push_projection_bound(
5759
&mut self,
58-
_tcx: TyCtxt<'tcx>,
60+
tcx: TyCtxt<'tcx>,
5961
projection: ty::PolyProjectionPredicate<'tcx>,
6062
span: Span,
6163
) {
62-
self.predicates.push((projection.map_bound(|proj| ty::Clause::Projection(proj)), span));
64+
self.clauses.push((
65+
projection.map_bound(|proj| ty::ClauseKind::Projection(proj)).to_predicate(tcx),
66+
span,
67+
));
6368
}
6469

6570
pub fn push_sized(&mut self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span) {
6671
let sized_def_id = tcx.require_lang_item(LangItem::Sized, Some(span));
6772
let trait_ref = ty::TraitRef::new(tcx, sized_def_id, [ty]);
6873
// Preferable to put this obligation first, since we report better errors for sized ambiguity.
69-
self.predicates.insert(
70-
0,
71-
(
72-
ty::Binder::dummy(ty::Clause::Trait(trait_ref.without_const().to_predicate(tcx))),
73-
span,
74-
),
75-
);
74+
self.clauses.insert(0, (trait_ref.to_predicate(tcx), span));
7675
}
7776

78-
pub fn predicates(&self) -> impl Iterator<Item = (Binder<'tcx, ty::Clause<'tcx>>, Span)> + '_ {
79-
self.predicates.iter().cloned()
77+
pub fn clauses(&self) -> impl Iterator<Item = (ty::Clause<'tcx>, Span)> + '_ {
78+
self.clauses.iter().cloned()
8079
}
8180
}

compiler/rustc_hir_analysis/src/check/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ fn check_opaque_meets_bounds<'tcx>(
440440
// Defining use functions may have more bounds than the opaque type, which is ok, as long as the
441441
// hidden type is well formed even without those bounds.
442442
let predicate =
443-
ty::Binder::dummy(ty::PredicateKind::Clause(ty::Clause::WellFormed(hidden_ty.into())));
443+
ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(hidden_ty.into())));
444444
ocx.register_obligation(Obligation::new(tcx, misc_cause, param_env, predicate));
445445

446446
// Check that all obligations are satisfied by the implementation's

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ fn compare_method_predicate_entailment<'tcx>(
321321
infcx.tcx,
322322
ObligationCause::dummy(),
323323
param_env,
324-
ty::Binder::dummy(ty::PredicateKind::Clause(ty::Clause::WellFormed(
324+
ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(
325325
unnormalized_impl_fty.into(),
326326
))),
327327
));

compiler/rustc_hir_analysis/src/check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ fn bounds_from_generic_predicates<'tcx>(
304304
debug!("predicate {:?}", predicate);
305305
let bound_predicate = predicate.kind();
306306
match bound_predicate.skip_binder() {
307-
ty::PredicateKind::Clause(ty::Clause::Trait(trait_predicate)) => {
307+
ty::PredicateKind::Clause(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::Clause::Projection(projection_pred)) => {
316+
ty::PredicateKind::Clause(ty::ClauseKind::Projection(projection_pred)) => {
317317
projections.push(bound_predicate.rebind(projection_pred));
318318
}
319319
_ => {}

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
8181
self.tcx(),
8282
cause,
8383
param_env,
84-
ty::Binder::dummy(ty::PredicateKind::Clause(ty::Clause::WellFormed(arg))),
84+
ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(arg))),
8585
));
8686
}
8787
}
@@ -419,18 +419,17 @@ 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::Clause::RegionOutlives(ty::OutlivesPredicate(
423-
a,
424-
b,
425-
))) => !region_known_to_outlive(
422+
ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(
423+
ty::OutlivesPredicate(a, b),
424+
)) => !region_known_to_outlive(
426425
tcx,
427426
gat_def_id.def_id,
428427
param_env,
429428
&FxIndexSet::default(),
430429
a,
431430
b,
432431
),
433-
ty::PredicateKind::Clause(ty::Clause::TypeOutlives(ty::OutlivesPredicate(
432+
ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(
434433
a,
435434
b,
436435
))) => !ty_known_to_outlive(
@@ -574,7 +573,7 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(
574573
);
575574
// The predicate we expect to see. (In our example,
576575
// `Self: 'me`.)
577-
let clause = ty::PredicateKind::Clause(ty::Clause::TypeOutlives(
576+
let clause = ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(
578577
ty::OutlivesPredicate(ty_param, region_param),
579578
));
580579
let clause = tcx.mk_predicate(ty::Binder::dummy(clause));
@@ -623,7 +622,7 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(
623622
},
624623
);
625624
// The predicate we expect to see.
626-
let clause = ty::PredicateKind::Clause(ty::Clause::RegionOutlives(
625+
let clause = ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(
627626
ty::OutlivesPredicate(region_a_param, region_b_param),
628627
));
629628
let clause = tcx.mk_predicate(ty::Binder::dummy(clause));
@@ -1032,7 +1031,7 @@ fn check_type_defn<'tcx>(tcx: TyCtxt<'tcx>, item: &hir::Item<'tcx>, all_sized: b
10321031
tcx,
10331032
cause,
10341033
wfcx.param_env,
1035-
ty::Binder::dummy(ty::PredicateKind::Clause(ty::Clause::ConstEvaluatable(
1034+
ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(
10361035
ty::Const::from_anon_const(tcx, discr_def_id.expect_local()),
10371036
))),
10381037
));
@@ -1876,7 +1875,8 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
18761875
// We lower empty bounds like `Vec<dyn Copy>:` as
18771876
// `WellFormed(Vec<dyn Copy>)`, which will later get checked by
18781877
// regular WF checking
1879-
if let ty::PredicateKind::Clause(ty::Clause::WellFormed(..)) = pred.kind().skip_binder()
1878+
if let ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(..)) =
1879+
pred.kind().skip_binder()
18801880
{
18811881
continue;
18821882
}

compiler/rustc_hir_analysis/src/coherence/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ fn infringing_fields_error(
571571
.or_default()
572572
.push(error.obligation.cause.span);
573573
}
574-
if let ty::PredicateKind::Clause(ty::Clause::Trait(ty::TraitPredicate {
574+
if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(ty::TraitPredicate {
575575
trait_ref,
576576
polarity: ty::ImplPolarity::Positive,
577577
..

0 commit comments

Comments
 (0)