Skip to content

Commit 3978f54

Browse files
committed
add unused NormalizesTo predicate
1 parent 40aa9f4 commit 3978f54

File tree

18 files changed

+128
-64
lines changed

18 files changed

+128
-64
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -657,19 +657,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
657657
| ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(..))
658658
| ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(..))
659659
| ty::PredicateKind::ObjectSafe(..)
660+
| ty::PredicateKind::NormalizesTo(..)
660661
| ty::PredicateKind::AliasRelate(..)
661662
| ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..))
662663
| ty::PredicateKind::ConstEquate(..)
663-
// N.B., this predicate is created by breaking down a
664-
// `ClosureType: FnFoo()` predicate, where
665-
// `ClosureType` represents some `Closure`. It can't
666-
// possibly be referring to the current closure,
667-
// because we haven't produced the `Closure` for
668-
// this closure yet; this is exactly why the other
669-
// code is looking for a self type of an unresolved
670-
// inference variable.
671-
| ty::PredicateKind::Ambiguous
672-
=> None,
664+
| ty::PredicateKind::Ambiguous => None,
673665
},
674666
)
675667
}

compiler/rustc_infer/src/traits/util.rs

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,14 @@ impl<'tcx, O: Elaboratable<'tcx>> Elaborator<'tcx, O> {
261261
fn elaborate(&mut self, elaboratable: &O) {
262262
let tcx = self.visited.tcx;
263263

264-
let bound_predicate = elaboratable.predicate().kind();
265-
match bound_predicate.skip_binder() {
266-
ty::PredicateKind::Clause(ty::ClauseKind::Trait(data)) => {
264+
// We only elaborate clauses.
265+
let Some(clause) = elaboratable.predicate().as_clause() else {
266+
return;
267+
};
268+
269+
let bound_clause = clause.kind();
270+
match bound_clause.skip_binder() {
271+
ty::ClauseKind::Trait(data) => {
267272
// Negative trait bounds do not imply any supertrait bounds
268273
if data.polarity == ty::ImplPolarity::Negative {
269274
return;
@@ -280,49 +285,16 @@ impl<'tcx, O: Elaboratable<'tcx>> Elaborator<'tcx, O> {
280285
let obligations =
281286
predicates.predicates.iter().enumerate().map(|(index, &(clause, span))| {
282287
elaboratable.child_with_derived_cause(
283-
clause.subst_supertrait(tcx, &bound_predicate.rebind(data.trait_ref)),
288+
clause.subst_supertrait(tcx, &bound_clause.rebind(data.trait_ref)),
284289
span,
285-
bound_predicate.rebind(data),
290+
bound_clause.rebind(data),
286291
index,
287292
)
288293
});
289294
debug!(?data, ?obligations, "super_predicates");
290295
self.extend_deduped(obligations);
291296
}
292-
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(..)) => {
293-
// Currently, we do not elaborate WF predicates,
294-
// although we easily could.
295-
}
296-
ty::PredicateKind::ObjectSafe(..) => {
297-
// Currently, we do not elaborate object-safe
298-
// predicates.
299-
}
300-
ty::PredicateKind::Subtype(..) => {
301-
// Currently, we do not "elaborate" predicates like `X <: Y`,
302-
// though conceivably we might.
303-
}
304-
ty::PredicateKind::Coerce(..) => {
305-
// Currently, we do not "elaborate" predicates like `X -> Y`,
306-
// though conceivably we might.
307-
}
308-
ty::PredicateKind::Clause(ty::ClauseKind::Projection(..)) => {
309-
// Nothing to elaborate in a projection predicate.
310-
}
311-
ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..)) => {
312-
// Currently, we do not elaborate const-evaluatable
313-
// predicates.
314-
}
315-
ty::PredicateKind::ConstEquate(..) => {
316-
// Currently, we do not elaborate const-equate
317-
// predicates.
318-
}
319-
ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(..)) => {
320-
// Nothing to elaborate from `'a: 'b`.
321-
}
322-
ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(
323-
ty_max,
324-
r_min,
325-
))) => {
297+
ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(ty_max, r_min)) => {
326298
// We know that `T: 'a` for some type `T`. We can
327299
// often elaborate this. For example, if we know that
328300
// `[U]: 'a`, that implies that `U: 'a`. Similarly, if
@@ -385,15 +357,25 @@ impl<'tcx, O: Elaboratable<'tcx>> Elaborator<'tcx, O> {
385357
}
386358
})
387359
.map(|clause| {
388-
elaboratable.child(bound_predicate.rebind(clause).to_predicate(tcx))
360+
elaboratable.child(bound_clause.rebind(clause).to_predicate(tcx))
389361
}),
390362
);
391363
}
392-
ty::PredicateKind::Ambiguous => {}
393-
ty::PredicateKind::AliasRelate(..) => {
394-
// No
364+
ty::ClauseKind::RegionOutlives(..) => {
365+
// Nothing to elaborate from `'a: 'b`.
366+
}
367+
ty::ClauseKind::WellFormed(..) => {
368+
// Currently, we do not elaborate WF predicates,
369+
// although we easily could.
370+
}
371+
ty::ClauseKind::Projection(..) => {
372+
// Nothing to elaborate in a projection predicate.
373+
}
374+
ty::ClauseKind::ConstEvaluatable(..) => {
375+
// Currently, we do not elaborate const-evaluatable
376+
// predicates.
395377
}
396-
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(..)) => {
378+
ty::ClauseKind::ConstArgHasType(..) => {
397379
// Nothing to elaborate
398380
}
399381
}

compiler/rustc_middle/src/ty/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
121121
type RegionOutlivesPredicate = ty::RegionOutlivesPredicate<'tcx>;
122122
type TypeOutlivesPredicate = ty::TypeOutlivesPredicate<'tcx>;
123123
type ProjectionPredicate = ty::ProjectionPredicate<'tcx>;
124+
type NormalizesTo = ty::NormalizesTo<'tcx>;
124125
type SubtypePredicate = ty::SubtypePredicate<'tcx>;
125126
type CoercePredicate = ty::CoercePredicate<'tcx>;
126127
type ClosureKind = ty::ClosureKind;

compiler/rustc_middle/src/ty/flags.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@ impl FlagComputation {
272272
self.add_const(found);
273273
}
274274
ty::PredicateKind::Ambiguous => {}
275+
ty::PredicateKind::NormalizesTo(ty::NormalizesTo { alias, term }) => {
276+
self.add_alias_ty(alias);
277+
self.add_term(term);
278+
}
275279
ty::PredicateKind::AliasRelate(t1, t2, _) => {
276280
self.add_term(t1);
277281
self.add_term(t2);

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,10 @@ impl<'tcx> Predicate<'tcx> {
553553
pub fn allow_normalization(self) -> bool {
554554
match self.kind().skip_binder() {
555555
PredicateKind::Clause(ClauseKind::WellFormed(_)) => false,
556+
// `NormalizesTo` is only used in the new solver, so this shouldn't
557+
// matter. Normalizing `term` would be 'wrong' however, as it changes whether
558+
// `normalizes-to(<T as Trait>::Assoc, <T as Trait>::Assoc)` holds.
559+
PredicateKind::NormalizesTo(..) => false,
556560
PredicateKind::Clause(ClauseKind::Trait(_))
557561
| PredicateKind::Clause(ClauseKind::RegionOutlives(_))
558562
| PredicateKind::Clause(ClauseKind::TypeOutlives(_))
@@ -1093,6 +1097,33 @@ impl<'tcx> PolyProjectionPredicate<'tcx> {
10931097
}
10941098
}
10951099

1100+
/// Used by the new solver. Unlike a `ProjectionPredicate` this can only be
1101+
/// proven by actually normalizing `alias`.
1102+
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
1103+
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
1104+
pub struct NormalizesTo<'tcx> {
1105+
pub alias: AliasTy<'tcx>,
1106+
pub term: Term<'tcx>,
1107+
}
1108+
1109+
impl<'tcx> NormalizesTo<'tcx> {
1110+
pub fn self_ty(self) -> Ty<'tcx> {
1111+
self.alias.self_ty()
1112+
}
1113+
1114+
pub fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> NormalizesTo<'tcx> {
1115+
Self { alias: self.alias.with_self_ty(tcx, self_ty), ..self }
1116+
}
1117+
1118+
pub fn trait_def_id(self, tcx: TyCtxt<'tcx>) -> DefId {
1119+
self.alias.trait_def_id(tcx)
1120+
}
1121+
1122+
pub fn def_id(self) -> DefId {
1123+
self.alias.def_id
1124+
}
1125+
}
1126+
10961127
pub trait ToPolyTraitRef<'tcx> {
10971128
fn to_poly_trait_ref(&self) -> PolyTraitRef<'tcx>;
10981129
}
@@ -1274,13 +1305,20 @@ impl<'tcx> ToPredicate<'tcx, Clause<'tcx>> for PolyProjectionPredicate<'tcx> {
12741305
}
12751306
}
12761307

1308+
impl<'tcx> ToPredicate<'tcx> for NormalizesTo<'tcx> {
1309+
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
1310+
PredicateKind::NormalizesTo(self).to_predicate(tcx)
1311+
}
1312+
}
1313+
12771314
impl<'tcx> Predicate<'tcx> {
12781315
pub fn to_opt_poly_trait_pred(self) -> Option<PolyTraitPredicate<'tcx>> {
12791316
let predicate = self.kind();
12801317
match predicate.skip_binder() {
12811318
PredicateKind::Clause(ClauseKind::Trait(t)) => Some(predicate.rebind(t)),
12821319
PredicateKind::Clause(ClauseKind::Projection(..))
12831320
| PredicateKind::Clause(ClauseKind::ConstArgHasType(..))
1321+
| PredicateKind::NormalizesTo(..)
12841322
| PredicateKind::AliasRelate(..)
12851323
| PredicateKind::Subtype(..)
12861324
| PredicateKind::Coerce(..)
@@ -1300,6 +1338,7 @@ impl<'tcx> Predicate<'tcx> {
13001338
PredicateKind::Clause(ClauseKind::Projection(t)) => Some(predicate.rebind(t)),
13011339
PredicateKind::Clause(ClauseKind::Trait(..))
13021340
| PredicateKind::Clause(ClauseKind::ConstArgHasType(..))
1341+
| PredicateKind::NormalizesTo(..)
13031342
| PredicateKind::AliasRelate(..)
13041343
| PredicateKind::Subtype(..)
13051344
| PredicateKind::Coerce(..)

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2814,6 +2814,7 @@ define_print! {
28142814
p!("the constant `", print(c1), "` equals `", print(c2), "`")
28152815
}
28162816
ty::PredicateKind::Ambiguous => p!("ambiguous"),
2817+
ty::PredicateKind::NormalizesTo(data) => p!(print(data.alias), " normalizes-to ", print(data.term)),
28172818
ty::PredicateKind::AliasRelate(t1, t2, dir) => p!(print(t1), write(" {} ", dir), print(t2)),
28182819
}
28192820
}

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ impl<'tcx> fmt::Debug for ty::ProjectionPredicate<'tcx> {
173173
}
174174
}
175175

176+
impl<'tcx> fmt::Debug for ty::NormalizesTo<'tcx> {
177+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
178+
write!(f, "NormalizesTo({:?}, {:?})", self.alias, self.term)
179+
}
180+
}
181+
176182
impl<'tcx> fmt::Debug for ty::Predicate<'tcx> {
177183
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
178184
write!(f, "{:?}", self.kind())

compiler/rustc_smir/src/rustc_smir/convert/ty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ impl<'tcx> Stable<'tcx> for ty::PredicateKind<'tcx> {
601601
stable_mir::ty::PredicateKind::ConstEquate(a.stable(tables), b.stable(tables))
602602
}
603603
PredicateKind::Ambiguous => stable_mir::ty::PredicateKind::Ambiguous,
604+
PredicateKind::NormalizesTo(_pred) => unimplemented!(),
604605
PredicateKind::AliasRelate(a, b, alias_relation_direction) => {
605606
stable_mir::ty::PredicateKind::AliasRelate(
606607
a.unpack().stable(tables),

compiler/rustc_trait_selection/src/solve/eval_ctxt/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
423423
ty::PredicateKind::ConstEquate(_, _) => {
424424
bug!("ConstEquate should not be emitted when `-Ztrait-solver=next` is active")
425425
}
426+
ty::PredicateKind::NormalizesTo(_) => unimplemented!(),
426427
ty::PredicateKind::AliasRelate(lhs, rhs, direction) => self
427428
.compute_alias_relate_goal(Goal {
428429
param_env,

compiler/rustc_trait_selection/src/solve/fulfill.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentCtxt<'tcx> {
108108
MismatchedProjectionTypes { err: TypeError::Mismatch },
109109
)
110110
}
111+
ty::PredicateKind::NormalizesTo(..) => {
112+
FulfillmentErrorCode::CodeProjectionError(
113+
MismatchedProjectionTypes { err: TypeError::Mismatch },
114+
)
115+
}
111116
ty::PredicateKind::AliasRelate(_, _, _) => {
112117
FulfillmentErrorCode::CodeProjectionError(
113118
MismatchedProjectionTypes { err: TypeError::Mismatch },

0 commit comments

Comments
 (0)