Skip to content

Commit cd9743b

Browse files
committed
directly contain PredicateAtom in PredicateKind::ForAll
1 parent d8cf8ba commit cd9743b

File tree

10 files changed

+163
-180
lines changed

10 files changed

+163
-180
lines changed

src/librustc_infer/infer/canonical/query_response.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,11 +531,9 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
531531
let predicate = match k1.unpack() {
532532
GenericArgKind::Lifetime(r1) => {
533533
ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(r1, r2))
534-
.to_predicate(self.tcx)
535534
}
536535
GenericArgKind::Type(t1) => {
537536
ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(t1, r2))
538-
.to_predicate(self.tcx)
539537
}
540538
GenericArgKind::Const(..) => {
541539
// Consts cannot outlive one another, so we don't expect to

src/librustc_middle/ty/flags.rs

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -201,55 +201,54 @@ impl FlagComputation {
201201
}
202202
}
203203

204-
fn add_predicate(&mut self, pred: ty::Predicate<'_>) {
205-
self.add_flags(pred.inner.flags);
206-
self.add_exclusive_binder(pred.inner.outer_exclusive_binder);
207-
}
208-
209204
fn add_predicate_kind(&mut self, kind: &ty::PredicateKind<'_>) {
210205
match kind {
211206
ty::PredicateKind::ForAll(binder) => {
212207
let mut computation = FlagComputation::new();
213208

214-
computation.add_predicate(binder.skip_binder());
209+
computation.add_predicate_atom(binder.skip_binder());
215210

216211
self.add_bound_computation(computation);
217212
}
218-
&ty::PredicateKind::Atom(atom) => match atom {
219-
ty::PredicateAtom::Trait(trait_pred, _constness) => {
220-
self.add_substs(trait_pred.trait_ref.substs);
221-
}
222-
ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(a, b)) => {
223-
self.add_region(a);
224-
self.add_region(b);
225-
}
226-
ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty, region)) => {
227-
self.add_ty(ty);
228-
self.add_region(region);
229-
}
230-
ty::PredicateAtom::Subtype(ty::SubtypePredicate { a_is_expected: _, a, b }) => {
231-
self.add_ty(a);
232-
self.add_ty(b);
233-
}
234-
ty::PredicateAtom::Projection(ty::ProjectionPredicate { projection_ty, ty }) => {
235-
self.add_projection_ty(projection_ty);
236-
self.add_ty(ty);
237-
}
238-
ty::PredicateAtom::WellFormed(arg) => {
239-
self.add_substs(slice::from_ref(&arg));
240-
}
241-
ty::PredicateAtom::ObjectSafe(_def_id) => {}
242-
ty::PredicateAtom::ClosureKind(_def_id, substs, _kind) => {
243-
self.add_substs(substs);
244-
}
245-
ty::PredicateAtom::ConstEvaluatable(_def_id, substs) => {
246-
self.add_substs(substs);
247-
}
248-
ty::PredicateAtom::ConstEquate(expected, found) => {
249-
self.add_const(expected);
250-
self.add_const(found);
251-
}
252-
},
213+
&ty::PredicateKind::Atom(atom) => self.add_predicate_atom(atom),
214+
}
215+
}
216+
217+
fn add_predicate_atom(&mut self, atom: ty::PredicateAtom<'_>) {
218+
match atom {
219+
ty::PredicateAtom::Trait(trait_pred, _constness) => {
220+
self.add_substs(trait_pred.trait_ref.substs);
221+
}
222+
ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(a, b)) => {
223+
self.add_region(a);
224+
self.add_region(b);
225+
}
226+
ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty, region)) => {
227+
self.add_ty(ty);
228+
self.add_region(region);
229+
}
230+
ty::PredicateAtom::Subtype(ty::SubtypePredicate { a_is_expected: _, a, b }) => {
231+
self.add_ty(a);
232+
self.add_ty(b);
233+
}
234+
ty::PredicateAtom::Projection(ty::ProjectionPredicate { projection_ty, ty }) => {
235+
self.add_projection_ty(projection_ty);
236+
self.add_ty(ty);
237+
}
238+
ty::PredicateAtom::WellFormed(arg) => {
239+
self.add_substs(slice::from_ref(&arg));
240+
}
241+
ty::PredicateAtom::ObjectSafe(_def_id) => {}
242+
ty::PredicateAtom::ClosureKind(_def_id, substs, _kind) => {
243+
self.add_substs(substs);
244+
}
245+
ty::PredicateAtom::ConstEvaluatable(_def_id, substs) => {
246+
self.add_substs(substs);
247+
}
248+
ty::PredicateAtom::ConstEquate(expected, found) => {
249+
self.add_const(expected);
250+
self.add_const(found);
251+
}
253252
}
254253
}
255254

src/librustc_middle/ty/mod.rs

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,8 +1053,9 @@ impl<'tcx> Predicate<'tcx> {
10531053
///
10541054
/// Note that this method panics in case this predicate has unbound variables.
10551055
pub fn skip_binders(self) -> PredicateAtom<'tcx> {
1056+
// TODO no_escaping_vars
10561057
match self.kind() {
1057-
&PredicateKind::ForAll(binder) => binder.skip_binder().skip_binders(),
1058+
&PredicateKind::ForAll(binder) => binder.skip_binder(),
10581059
&ty::PredicateKind::Atom(atom) => atom,
10591060
}
10601061
}
@@ -1066,33 +1067,17 @@ impl<'tcx> Predicate<'tcx> {
10661067
/// to end up at the wrong binding level.
10671068
pub fn skip_binders_unchecked(self) -> PredicateAtom<'tcx> {
10681069
match self.kind() {
1069-
&PredicateKind::ForAll(binder) => binder.skip_binder().skip_binders(),
1070+
&PredicateKind::ForAll(binder) => binder.skip_binder(),
10701071
&ty::PredicateKind::Atom(atom) => atom,
10711072
}
10721073
}
10731074

10741075
pub fn bound_atom(self, tcx: TyCtxt<'tcx>) -> Binder<PredicateAtom<'tcx>> {
10751076
match self.kind() {
1076-
&PredicateKind::ForAll(binder) => binder.map_bound(|inner| match inner.kind() {
1077-
ty::PredicateKind::ForAll(_) => bug!("unexpect forall"),
1078-
&ty::PredicateKind::Atom(atom) => atom,
1079-
}),
1077+
&PredicateKind::ForAll(binder) => binder,
10801078
&ty::PredicateKind::Atom(atom) => Binder::wrap_nonbinding(tcx, atom),
10811079
}
10821080
}
1083-
1084-
/// Wraps `self` with the given qualifier if this predicate has any unbound variables.
1085-
pub fn potentially_quantified(
1086-
self,
1087-
tcx: TyCtxt<'tcx>,
1088-
qualifier: impl FnOnce(Binder<Predicate<'tcx>>) -> PredicateKind<'tcx>,
1089-
) -> Predicate<'tcx> {
1090-
if self.has_escaping_bound_vars() {
1091-
qualifier(Binder::bind(self)).to_predicate(tcx)
1092-
} else {
1093-
self
1094-
}
1095-
}
10961081
}
10971082

10981083
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Predicate<'tcx> {
@@ -1114,7 +1099,7 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Predicate<'tcx> {
11141099
#[derive(HashStable, TypeFoldable)]
11151100
pub enum PredicateKind<'tcx> {
11161101
/// `for<'a>: ...`
1117-
ForAll(Binder<Predicate<'tcx>>),
1102+
ForAll(Binder<PredicateAtom<'tcx>>),
11181103

11191104
Atom(PredicateAtom<'tcx>),
11201105
}
@@ -1162,6 +1147,22 @@ pub enum PredicateAtom<'tcx> {
11621147
ConstEquate(&'tcx Const<'tcx>, &'tcx Const<'tcx>),
11631148
}
11641149

1150+
impl<'tcx> PredicateAtom<'tcx> {
1151+
/// Wraps `self` with the given qualifier if this predicate has any unbound variables.
1152+
pub fn potentially_quantified(
1153+
self,
1154+
tcx: TyCtxt<'tcx>,
1155+
qualifier: impl FnOnce(Binder<PredicateAtom<'tcx>>) -> PredicateKind<'tcx>,
1156+
) -> Predicate<'tcx> {
1157+
if self.has_escaping_bound_vars() {
1158+
qualifier(Binder::bind(self))
1159+
} else {
1160+
PredicateKind::Atom(self)
1161+
}
1162+
.to_predicate(tcx)
1163+
}
1164+
}
1165+
11651166
/// The crate outlives map is computed during typeck and contains the
11661167
/// outlives of every item in the local crate. You should not use it
11671168
/// directly, because to do so will make your pass dependent on the
@@ -1249,11 +1250,7 @@ impl<'tcx> Predicate<'tcx> {
12491250
let substs = trait_ref.skip_binder().substs;
12501251
let pred = self.skip_binders();
12511252
let new = pred.subst(tcx, substs);
1252-
if new != pred {
1253-
new.to_predicate(tcx).potentially_quantified(tcx, PredicateKind::ForAll)
1254-
} else {
1255-
self
1256-
}
1253+
if new != pred { new.potentially_quantified(tcx, PredicateKind::ForAll) } else { self }
12571254
}
12581255
}
12591256

@@ -1381,6 +1378,7 @@ impl ToPredicate<'tcx> for PredicateKind<'tcx> {
13811378
impl ToPredicate<'tcx> for PredicateAtom<'tcx> {
13821379
#[inline(always)]
13831380
fn to_predicate(&self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
1381+
debug_assert!(!self.has_escaping_bound_vars(), "excaping bound vars for {:?}", self);
13841382
tcx.mk_predicate(ty::PredicateKind::Atom(*self))
13851383
}
13861384
}
@@ -1408,9 +1406,7 @@ impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<PolyTraitPredicate<'tcx>> {
14081406
ty::PredicateAtom::Trait(pred, self.constness).to_predicate(tcx)
14091407
} else {
14101408
ty::PredicateKind::ForAll(
1411-
self.value.map_bound(|pred| {
1412-
ty::PredicateAtom::Trait(pred, self.constness).to_predicate(tcx)
1413-
}),
1409+
self.value.map_bound(|pred| ty::PredicateAtom::Trait(pred, self.constness)),
14141410
)
14151411
.to_predicate(tcx)
14161412
}
@@ -1423,9 +1419,7 @@ impl<'tcx> ToPredicate<'tcx> for PolyRegionOutlivesPredicate<'tcx> {
14231419
PredicateAtom::RegionOutlives(outlives).to_predicate(tcx)
14241420
} else {
14251421
ty::PredicateKind::ForAll(
1426-
self.map_bound(|outlives| {
1427-
PredicateAtom::RegionOutlives(outlives).to_predicate(tcx)
1428-
}),
1422+
self.map_bound(|outlives| PredicateAtom::RegionOutlives(outlives)),
14291423
)
14301424
.to_predicate(tcx)
14311425
}
@@ -1438,7 +1432,7 @@ impl<'tcx> ToPredicate<'tcx> for PolyTypeOutlivesPredicate<'tcx> {
14381432
PredicateAtom::TypeOutlives(outlives).to_predicate(tcx)
14391433
} else {
14401434
ty::PredicateKind::ForAll(
1441-
self.map_bound(|outlives| PredicateAtom::TypeOutlives(outlives).to_predicate(tcx)),
1435+
self.map_bound(|outlives| PredicateAtom::TypeOutlives(outlives)),
14421436
)
14431437
.to_predicate(tcx)
14441438
}
@@ -1450,10 +1444,8 @@ impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
14501444
if let Some(proj) = self.no_bound_vars() {
14511445
PredicateAtom::Projection(proj).to_predicate(tcx)
14521446
} else {
1453-
ty::PredicateKind::ForAll(
1454-
self.map_bound(|proj| PredicateAtom::Projection(proj).to_predicate(tcx)),
1455-
)
1456-
.to_predicate(tcx)
1447+
ty::PredicateKind::ForAll(self.map_bound(|proj| PredicateAtom::Projection(proj)))
1448+
.to_predicate(tcx)
14571449
}
14581450
}
14591451
}

src/librustc_middle/ty/print/pretty.rs

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,43 +2013,45 @@ define_print_and_forward_display! {
20132013

20142014
ty::Predicate<'tcx> {
20152015
match self.kind() {
2016-
&ty::PredicateKind::Atom(atom) => match atom {
2017-
ty::PredicateAtom::Trait(ref data, constness) => {
2018-
if let hir::Constness::Const = constness {
2019-
p!(write("const "));
2020-
}
2021-
p!(print(data))
2022-
}
2023-
ty::PredicateAtom::Subtype(predicate) => p!(print(predicate)),
2024-
ty::PredicateAtom::RegionOutlives(predicate) => p!(print(predicate)),
2025-
ty::PredicateAtom::TypeOutlives(predicate) => p!(print(predicate)),
2026-
ty::PredicateAtom::Projection(predicate) => p!(print(predicate)),
2027-
ty::PredicateAtom::WellFormed(arg) => p!(print(arg), write(" well-formed")),
2028-
ty::PredicateAtom::ObjectSafe(trait_def_id) => {
2029-
p!(write("the trait `"),
2030-
print_def_path(trait_def_id, &[]),
2031-
write("` is object-safe"))
2032-
}
2033-
ty::PredicateAtom::ClosureKind(closure_def_id, _closure_substs, kind) => {
2034-
p!(write("the closure `"),
2035-
print_value_path(closure_def_id, &[]),
2036-
write("` implements the trait `{}`", kind))
2037-
}
2038-
ty::PredicateAtom::ConstEvaluatable(def, substs) => {
2039-
p!(write("the constant `"),
2040-
print_value_path(def.did, substs),
2041-
write("` can be evaluated"))
2042-
}
2043-
ty::PredicateAtom::ConstEquate(c1, c2) => {
2044-
p!(write("the constant `"),
2045-
print(c1),
2046-
write("` equals `"),
2047-
print(c2),
2048-
write("`"))
2016+
&ty::PredicateKind::Atom(atom) => p!(print(atom)),
2017+
ty::PredicateKind::ForAll(binder) => p!(print(binder)),
2018+
}
2019+
}
2020+
2021+
ty::PredicateAtom<'tcx> {
2022+
match *self {
2023+
ty::PredicateAtom::Trait(ref data, constness) => {
2024+
if let hir::Constness::Const = constness {
2025+
p!(write("const "));
20492026
}
2050-
}
2051-
ty::PredicateKind::ForAll(binder) => {
2052-
p!(print(binder))
2027+
p!(print(data))
2028+
}
2029+
ty::PredicateAtom::Subtype(predicate) => p!(print(predicate)),
2030+
ty::PredicateAtom::RegionOutlives(predicate) => p!(print(predicate)),
2031+
ty::PredicateAtom::TypeOutlives(predicate) => p!(print(predicate)),
2032+
ty::PredicateAtom::Projection(predicate) => p!(print(predicate)),
2033+
ty::PredicateAtom::WellFormed(arg) => p!(print(arg), write(" well-formed")),
2034+
ty::PredicateAtom::ObjectSafe(trait_def_id) => {
2035+
p!(write("the trait `"),
2036+
print_def_path(trait_def_id, &[]),
2037+
write("` is object-safe"))
2038+
}
2039+
ty::PredicateAtom::ClosureKind(closure_def_id, _closure_substs, kind) => {
2040+
p!(write("the closure `"),
2041+
print_value_path(closure_def_id, &[]),
2042+
write("` implements the trait `{}`", kind))
2043+
}
2044+
ty::PredicateAtom::ConstEvaluatable(def, substs) => {
2045+
p!(write("the constant `"),
2046+
print_value_path(def.did, substs),
2047+
write("` can be evaluated"))
2048+
}
2049+
ty::PredicateAtom::ConstEquate(c1, c2) => {
2050+
p!(write("the constant `"),
2051+
print(c1),
2052+
write("` equals `"),
2053+
print(c2),
2054+
write("`"))
20532055
}
20542056
}
20552057
}

src/librustc_trait_selection/traits/fulfill.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_errors::ErrorReported;
66
use rustc_infer::traits::{PolyTraitObligation, TraitEngine, TraitEngineExt as _};
77
use rustc_middle::mir::interpret::ErrorHandled;
88
use rustc_middle::ty::error::ExpectedFound;
9+
use rustc_middle::ty::ToPredicate;
910
use rustc_middle::ty::{self, Binder, Const, Ty, TypeFoldable};
1011
use std::marker::PhantomData;
1112

0 commit comments

Comments
 (0)