Skip to content

Commit 4253153

Browse files
committed
Auto merge of #80679 - jackh726:predicate-kind-take2, r=lcnr
Remove PredicateKind and instead only use Binder<PredicateAtom> Originally brought up in #76814 (comment) r? `@lcnr`
2 parents 1f0fc02 + c4376ba commit 4253153

Some content is hidden

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

71 files changed

+655
-759
lines changed

Diff for: compiler/rustc_infer/src/infer/canonical/query_response.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -530,19 +530,18 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
530530

531531
let atom = match k1.unpack() {
532532
GenericArgKind::Lifetime(r1) => {
533-
ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(r1, r2))
533+
ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(r1, r2))
534534
}
535535
GenericArgKind::Type(t1) => {
536-
ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(t1, r2))
536+
ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(t1, r2))
537537
}
538538
GenericArgKind::Const(..) => {
539539
// Consts cannot outlive one another, so we don't expect to
540540
// encounter this branch.
541541
span_bug!(cause.span, "unexpected const outlives {:?}", constraint);
542542
}
543543
};
544-
let predicate =
545-
predicate.rebind(atom).potentially_quantified(self.tcx, ty::PredicateKind::ForAll);
544+
let predicate = predicate.rebind(atom).to_predicate(self.tcx);
546545

547546
Obligation::new(cause.clone(), param_env, predicate)
548547
})
@@ -664,7 +663,7 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for QueryTypeRelatingDelegate<'_, 'tcx> {
664663
self.obligations.push(Obligation {
665664
cause: self.cause.clone(),
666665
param_env: self.param_env,
667-
predicate: ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(sup, sub))
666+
predicate: ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(sup, sub))
668667
.to_predicate(self.infcx.tcx),
669668
recursion_depth: 0,
670669
});

Diff for: compiler/rustc_infer/src/infer/combine.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
358358
self.obligations.push(Obligation::new(
359359
self.trace.cause.clone(),
360360
self.param_env,
361-
ty::PredicateAtom::WellFormed(b_ty.into()).to_predicate(self.infcx.tcx),
361+
ty::PredicateKind::WellFormed(b_ty.into()).to_predicate(self.infcx.tcx),
362362
));
363363
}
364364

@@ -451,9 +451,9 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
451451
b: &'tcx ty::Const<'tcx>,
452452
) {
453453
let predicate = if a_is_expected {
454-
ty::PredicateAtom::ConstEquate(a, b)
454+
ty::PredicateKind::ConstEquate(a, b)
455455
} else {
456-
ty::PredicateAtom::ConstEquate(b, a)
456+
ty::PredicateKind::ConstEquate(b, a)
457457
};
458458
self.obligations.push(Obligation::new(
459459
self.trace.cause.clone(),

Diff for: compiler/rustc_infer/src/infer/error_reporting/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1706,8 +1706,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
17061706

17071707
for (predicate, _) in bounds {
17081708
let predicate = predicate.subst(self.tcx, substs);
1709-
if let ty::PredicateAtom::Projection(projection_predicate) =
1710-
predicate.skip_binders()
1709+
if let ty::PredicateKind::Projection(projection_predicate) =
1710+
predicate.kind().skip_binder()
17111711
{
17121712
if projection_predicate.projection_ty.item_def_id == item_def_id {
17131713
// We don't account for multiple `Future::Output = Ty` contraints.

Diff for: compiler/rustc_infer/src/infer/outlives/mod.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ pub mod verify;
66

77
use rustc_middle::traits::query::OutlivesBound;
88
use rustc_middle::ty;
9-
use rustc_middle::ty::fold::TypeFoldable;
109

1110
pub fn explicit_outlives_bounds<'tcx>(
1211
param_env: ty::ParamEnv<'tcx>,
@@ -15,20 +14,20 @@ pub fn explicit_outlives_bounds<'tcx>(
1514
param_env
1615
.caller_bounds()
1716
.into_iter()
18-
.map(ty::Predicate::skip_binders)
19-
.filter(|atom| !atom.has_escaping_bound_vars())
20-
.filter_map(move |atom| match atom {
21-
ty::PredicateAtom::Projection(..)
22-
| ty::PredicateAtom::Trait(..)
23-
| ty::PredicateAtom::Subtype(..)
24-
| ty::PredicateAtom::WellFormed(..)
25-
| ty::PredicateAtom::ObjectSafe(..)
26-
| ty::PredicateAtom::ClosureKind(..)
27-
| ty::PredicateAtom::TypeOutlives(..)
28-
| ty::PredicateAtom::ConstEvaluatable(..)
29-
| ty::PredicateAtom::ConstEquate(..)
30-
| ty::PredicateAtom::TypeWellFormedFromEnv(..) => None,
31-
ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(r_a, r_b)) => {
17+
.map(ty::Predicate::kind)
18+
.filter_map(ty::Binder::no_bound_vars)
19+
.filter_map(move |kind| match kind {
20+
ty::PredicateKind::Projection(..)
21+
| ty::PredicateKind::Trait(..)
22+
| ty::PredicateKind::Subtype(..)
23+
| ty::PredicateKind::WellFormed(..)
24+
| ty::PredicateKind::ObjectSafe(..)
25+
| ty::PredicateKind::ClosureKind(..)
26+
| ty::PredicateKind::TypeOutlives(..)
27+
| ty::PredicateKind::ConstEvaluatable(..)
28+
| ty::PredicateKind::ConstEquate(..)
29+
| ty::PredicateKind::TypeWellFormedFromEnv(..) => None,
30+
ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(r_a, r_b)) => {
3231
Some(OutlivesBound::RegionSubRegion(r_b, r_a))
3332
}
3433
})

Diff for: compiler/rustc_infer/src/infer/sub.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl TypeRelation<'tcx> for Sub<'combine, 'infcx, 'tcx> {
100100
self.fields.obligations.push(Obligation::new(
101101
self.fields.trace.cause.clone(),
102102
self.fields.param_env,
103-
ty::PredicateAtom::Subtype(ty::SubtypePredicate {
103+
ty::PredicateKind::Subtype(ty::SubtypePredicate {
104104
a_is_expected: self.a_is_expected,
105105
a,
106106
b,

Diff for: compiler/rustc_infer/src/traits/util.rs

+16-21
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@ pub fn anonymize_predicate<'tcx>(
99
tcx: TyCtxt<'tcx>,
1010
pred: ty::Predicate<'tcx>,
1111
) -> ty::Predicate<'tcx> {
12-
match *pred.kind() {
13-
ty::PredicateKind::ForAll(binder) => {
14-
let new = ty::PredicateKind::ForAll(tcx.anonymize_late_bound_regions(binder));
15-
tcx.reuse_or_mk_predicate(pred, new)
16-
}
17-
ty::PredicateKind::Atom(_) => pred,
18-
}
12+
let new = tcx.anonymize_late_bound_regions(pred.kind());
13+
tcx.reuse_or_mk_predicate(pred, new)
1914
}
2015

2116
struct PredicateSet<'tcx> {
@@ -126,9 +121,9 @@ impl Elaborator<'tcx> {
126121
fn elaborate(&mut self, obligation: &PredicateObligation<'tcx>) {
127122
let tcx = self.visited.tcx;
128123

129-
let bound_predicate = obligation.predicate.bound_atom();
124+
let bound_predicate = obligation.predicate.kind();
130125
match bound_predicate.skip_binder() {
131-
ty::PredicateAtom::Trait(data, _) => {
126+
ty::PredicateKind::Trait(data, _) => {
132127
// Get predicates declared on the trait.
133128
let predicates = tcx.super_predicates_of(data.def_id());
134129

@@ -150,36 +145,36 @@ impl Elaborator<'tcx> {
150145

151146
self.stack.extend(obligations);
152147
}
153-
ty::PredicateAtom::WellFormed(..) => {
148+
ty::PredicateKind::WellFormed(..) => {
154149
// Currently, we do not elaborate WF predicates,
155150
// although we easily could.
156151
}
157-
ty::PredicateAtom::ObjectSafe(..) => {
152+
ty::PredicateKind::ObjectSafe(..) => {
158153
// Currently, we do not elaborate object-safe
159154
// predicates.
160155
}
161-
ty::PredicateAtom::Subtype(..) => {
156+
ty::PredicateKind::Subtype(..) => {
162157
// Currently, we do not "elaborate" predicates like `X <: Y`,
163158
// though conceivably we might.
164159
}
165-
ty::PredicateAtom::Projection(..) => {
160+
ty::PredicateKind::Projection(..) => {
166161
// Nothing to elaborate in a projection predicate.
167162
}
168-
ty::PredicateAtom::ClosureKind(..) => {
163+
ty::PredicateKind::ClosureKind(..) => {
169164
// Nothing to elaborate when waiting for a closure's kind to be inferred.
170165
}
171-
ty::PredicateAtom::ConstEvaluatable(..) => {
166+
ty::PredicateKind::ConstEvaluatable(..) => {
172167
// Currently, we do not elaborate const-evaluatable
173168
// predicates.
174169
}
175-
ty::PredicateAtom::ConstEquate(..) => {
170+
ty::PredicateKind::ConstEquate(..) => {
176171
// Currently, we do not elaborate const-equate
177172
// predicates.
178173
}
179-
ty::PredicateAtom::RegionOutlives(..) => {
174+
ty::PredicateKind::RegionOutlives(..) => {
180175
// Nothing to elaborate from `'a: 'b`.
181176
}
182-
ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty_max, r_min)) => {
177+
ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty_max, r_min)) => {
183178
// We know that `T: 'a` for some type `T`. We can
184179
// often elaborate this. For example, if we know that
185180
// `[U]: 'a`, that implies that `U: 'a`. Similarly, if
@@ -209,15 +204,15 @@ impl Elaborator<'tcx> {
209204
if r.is_late_bound() {
210205
None
211206
} else {
212-
Some(ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(
207+
Some(ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(
213208
r, r_min,
214209
)))
215210
}
216211
}
217212

218213
Component::Param(p) => {
219214
let ty = tcx.mk_ty_param(p.index, p.name);
220-
Some(ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(
215+
Some(ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(
221216
ty, r_min,
222217
)))
223218
}
@@ -242,7 +237,7 @@ impl Elaborator<'tcx> {
242237
}),
243238
);
244239
}
245-
ty::PredicateAtom::TypeWellFormedFromEnv(..) => {
240+
ty::PredicateKind::TypeWellFormedFromEnv(..) => {
246241
// Nothing to elaborate
247242
}
248243
}

Diff for: compiler/rustc_lint/src/builtin.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1550,13 +1550,13 @@ declare_lint_pass!(
15501550
impl<'tcx> LateLintPass<'tcx> for TrivialConstraints {
15511551
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
15521552
use rustc_middle::ty::fold::TypeFoldable;
1553-
use rustc_middle::ty::PredicateAtom::*;
1553+
use rustc_middle::ty::PredicateKind::*;
15541554

15551555
if cx.tcx.features().trivial_bounds {
15561556
let def_id = cx.tcx.hir().local_def_id(item.hir_id);
15571557
let predicates = cx.tcx.predicates_of(def_id);
15581558
for &(predicate, span) in predicates.predicates {
1559-
let predicate_kind_name = match predicate.skip_binders() {
1559+
let predicate_kind_name = match predicate.kind().skip_binder() {
15601560
Trait(..) => "Trait",
15611561
TypeOutlives(..) |
15621562
RegionOutlives(..) => "Lifetime",
@@ -1936,8 +1936,8 @@ impl ExplicitOutlivesRequirements {
19361936
) -> Vec<ty::Region<'tcx>> {
19371937
inferred_outlives
19381938
.iter()
1939-
.filter_map(|(pred, _)| match pred.skip_binders() {
1940-
ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(a, b)) => match a {
1939+
.filter_map(|(pred, _)| match pred.kind().skip_binder() {
1940+
ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(a, b)) => match a {
19411941
ty::ReEarlyBound(ebr) if ebr.index == index => Some(b),
19421942
_ => None,
19431943
},
@@ -1952,8 +1952,8 @@ impl ExplicitOutlivesRequirements {
19521952
) -> Vec<ty::Region<'tcx>> {
19531953
inferred_outlives
19541954
.iter()
1955-
.filter_map(|(pred, _)| match pred.skip_binders() {
1956-
ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(a, b)) => {
1955+
.filter_map(|(pred, _)| match pred.kind().skip_binder() {
1956+
ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(a, b)) => {
19571957
a.is_param(index).then_some(b)
19581958
}
19591959
_ => None,

Diff for: compiler/rustc_lint/src/traits.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ declare_lint_pass!(
4545

4646
impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints {
4747
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
48-
use rustc_middle::ty::PredicateAtom::*;
48+
use rustc_middle::ty::PredicateKind::*;
4949

5050
let def_id = cx.tcx.hir().local_def_id(item.hir_id);
5151
let predicates = cx.tcx.explicit_predicates_of(def_id);
5252
for &(predicate, span) in predicates.predicates {
53-
let trait_predicate = match predicate.skip_binders() {
53+
let trait_predicate = match predicate.kind().skip_binder() {
5454
Trait(trait_predicate, _constness) => trait_predicate,
5555
_ => continue,
5656
};

Diff for: compiler/rustc_lint/src/unused.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
202202
let mut has_emitted = false;
203203
for &(predicate, _) in cx.tcx.explicit_item_bounds(def) {
204204
// We only look at the `DefId`, so it is safe to skip the binder here.
205-
if let ty::PredicateAtom::Trait(ref poly_trait_predicate, _) =
206-
predicate.skip_binders()
205+
if let ty::PredicateKind::Trait(ref poly_trait_predicate, _) =
206+
predicate.kind().skip_binder()
207207
{
208208
let def_id = poly_trait_predicate.trait_ref.def_id;
209209
let descr_pre =

Diff for: compiler/rustc_metadata/src/rmeta/encoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub(super) struct EncodeContext<'a, 'tcx> {
4646

4747
lazy_state: LazyState,
4848
type_shorthands: FxHashMap<Ty<'tcx>, usize>,
49-
predicate_shorthands: FxHashMap<ty::Predicate<'tcx>, usize>,
49+
predicate_shorthands: FxHashMap<ty::PredicateKind<'tcx>, usize>,
5050

5151
interpret_allocs: FxIndexSet<interpret::AllocId>,
5252

@@ -328,7 +328,7 @@ impl<'a, 'tcx> TyEncoder<'tcx> for EncodeContext<'a, 'tcx> {
328328
&mut self.type_shorthands
329329
}
330330

331-
fn predicate_shorthands(&mut self) -> &mut FxHashMap<rustc_middle::ty::Predicate<'tcx>, usize> {
331+
fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize> {
332332
&mut self.predicate_shorthands
333333
}
334334

Diff for: compiler/rustc_middle/src/ty/codec.rs

+49-10
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for Ty<'tcx> {
4343
}
4444
}
4545

46-
impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for ty::Predicate<'tcx> {
46+
impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for ty::PredicateKind<'tcx> {
4747
type Variant = ty::PredicateKind<'tcx>;
48+
49+
#[inline]
4850
fn variant(&self) -> &Self::Variant {
49-
self.kind()
51+
self
5052
}
5153
}
5254

@@ -55,7 +57,7 @@ pub trait TyEncoder<'tcx>: Encoder {
5557

5658
fn position(&self) -> usize;
5759
fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize>;
58-
fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::Predicate<'tcx>, usize>;
60+
fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize>;
5961
fn encode_alloc_id(&mut self, alloc_id: &AllocId) -> Result<(), Self::Error>;
6062
}
6163

@@ -118,9 +120,15 @@ impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for Ty<'tcx> {
118120
}
119121
}
120122

123+
impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Binder<ty::PredicateKind<'tcx>> {
124+
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
125+
encode_with_shorthand(e, &self.skip_binder(), TyEncoder::predicate_shorthands)
126+
}
127+
}
128+
121129
impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Predicate<'tcx> {
122130
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
123-
encode_with_shorthand(e, self, TyEncoder::predicate_shorthands)
131+
self.kind().encode(e)
124132
}
125133
}
126134

@@ -218,18 +226,24 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for Ty<'tcx> {
218226
}
219227
}
220228

221-
impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Predicate<'tcx> {
222-
fn decode(decoder: &mut D) -> Result<ty::Predicate<'tcx>, D::Error> {
229+
impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Binder<ty::PredicateKind<'tcx>> {
230+
fn decode(decoder: &mut D) -> Result<ty::Binder<ty::PredicateKind<'tcx>>, D::Error> {
223231
// Handle shorthands first, if we have an usize > 0x80.
224-
let predicate_kind = if decoder.positioned_at_shorthand() {
232+
Ok(ty::Binder::bind(if decoder.positioned_at_shorthand() {
225233
let pos = decoder.read_usize()?;
226234
assert!(pos >= SHORTHAND_OFFSET);
227235
let shorthand = pos - SHORTHAND_OFFSET;
228236

229-
decoder.with_position(shorthand, ty::PredicateKind::decode)
237+
decoder.with_position(shorthand, ty::PredicateKind::decode)?
230238
} else {
231-
ty::PredicateKind::decode(decoder)
232-
}?;
239+
ty::PredicateKind::decode(decoder)?
240+
}))
241+
}
242+
}
243+
244+
impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Predicate<'tcx> {
245+
fn decode(decoder: &mut D) -> Result<ty::Predicate<'tcx>, D::Error> {
246+
let predicate_kind = Decodable::decode(decoder)?;
233247
let predicate = decoder.tcx().mk_predicate(predicate_kind);
234248
Ok(predicate)
235249
}
@@ -457,3 +471,28 @@ macro_rules! implement_ty_decoder {
457471
}
458472
}
459473
}
474+
475+
macro_rules! impl_binder_encode_decode {
476+
($($t:ty),+ $(,)?) => {
477+
$(
478+
impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Binder<$t> {
479+
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
480+
self.as_ref().skip_binder().encode(e)
481+
}
482+
}
483+
impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ty::Binder<$t> {
484+
fn decode(decoder: &mut D) -> Result<Self, D::Error> {
485+
Ok(ty::Binder::bind(Decodable::decode(decoder)?))
486+
}
487+
}
488+
)*
489+
}
490+
}
491+
492+
impl_binder_encode_decode! {
493+
&'tcx ty::List<Ty<'tcx>>,
494+
ty::FnSig<'tcx>,
495+
ty::ExistentialPredicate<'tcx>,
496+
ty::TraitRef<'tcx>,
497+
Vec<ty::GeneratorInteriorTypeCause<'tcx>>,
498+
}

0 commit comments

Comments
 (0)