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 },

compiler/rustc_trait_selection/src/traits/auto_trait.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
820820
// the `ParamEnv`.
821821
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(..))
822822
| ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(..))
823+
| ty::PredicateKind::NormalizesTo(..)
823824
| ty::PredicateKind::AliasRelate(..)
824825
| ty::PredicateKind::ObjectSafe(..)
825826
| ty::PredicateKind::Subtype(..)

compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,11 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
854854

855855
ty::PredicateKind::Ambiguous => span_bug!(span, "ambiguous"),
856856

857+
ty::PredicateKind::NormalizesTo(..) => span_bug!(
858+
span,
859+
"NormalizesTo predicate should never be the predicate cause of a SelectionError"
860+
),
861+
857862
ty::PredicateKind::AliasRelate(..) => span_bug!(
858863
span,
859864
"AliasRelate predicate should never be the predicate cause of a SelectionError"

compiler/rustc_trait_selection/src/traits/fulfill.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,11 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
360360
ProcessResult::Changed(mk_pending(vec![obligation.with(infcx.tcx, pred)]))
361361
}
362362
ty::PredicateKind::Ambiguous => ProcessResult::Unchanged,
363+
ty::PredicateKind::NormalizesTo(..) => {
364+
bug!("NormalizesTo is only used by the new solver")
365+
}
363366
ty::PredicateKind::AliasRelate(..) => {
364-
bug!("AliasRelate is only used for new solver")
367+
bug!("AliasRelate is only used by the new solver")
365368
}
366369
},
367370
Some(pred) => match pred {
@@ -412,8 +415,11 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
412415
}
413416

414417
ty::PredicateKind::Ambiguous => ProcessResult::Unchanged,
418+
ty::PredicateKind::NormalizesTo(..) => {
419+
bug!("NormalizesTo is only used by the new solver")
420+
}
415421
ty::PredicateKind::AliasRelate(..) => {
416-
bug!("AliasRelate is only used for new solver")
422+
bug!("AliasRelate is only used by the new solver")
417423
}
418424

419425
// General case overflow check. Allow `process_trait_obligation`

compiler/rustc_trait_selection/src/traits/query/type_op/implied_outlives_bounds.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ pub fn compute_implied_outlives_bounds_inner<'tcx>(
123123
Some(pred) => pred,
124124
};
125125
match pred {
126-
ty::PredicateKind::Clause(ty::ClauseKind::Trait(..))
127126
// FIXME(const_generics): Make sure that `<'a, 'b, const N: &'a &'b u32>` is sound
128127
// if we ever support that
128+
ty::PredicateKind::Clause(ty::ClauseKind::Trait(..))
129129
| ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(..))
130130
| ty::PredicateKind::Subtype(..)
131131
| ty::PredicateKind::Coerce(..)
@@ -134,19 +134,18 @@ pub fn compute_implied_outlives_bounds_inner<'tcx>(
134134
| ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..))
135135
| ty::PredicateKind::ConstEquate(..)
136136
| ty::PredicateKind::Ambiguous
137-
| ty::PredicateKind::AliasRelate(..)
138-
=> {}
137+
| ty::PredicateKind::NormalizesTo(..)
138+
| ty::PredicateKind::AliasRelate(..) => {}
139139

140140
// We need to search through *all* WellFormed predicates
141141
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(arg)) => {
142142
wf_args.push(arg);
143143
}
144144

145145
// We need to register region relationships
146-
ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(
147-
r_a,
148-
r_b,
149-
))) => outlives_bounds.push(ty::OutlivesPredicate(r_a.into(), r_b)),
146+
ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(
147+
ty::OutlivesPredicate(r_a, r_b),
148+
)) => outlives_bounds.push(ty::OutlivesPredicate(r_a.into(), r_b)),
150149

151150
ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(
152151
ty_a,

compiler/rustc_trait_selection/src/traits/select/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,8 +990,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
990990
}
991991
}
992992
}
993+
ty::PredicateKind::NormalizesTo(..) => {
994+
bug!("NormalizesTo is only used by the new solver")
995+
}
993996
ty::PredicateKind::AliasRelate(..) => {
994-
bug!("AliasRelate is only used for new solver")
997+
bug!("AliasRelate is only used by the new solver")
995998
}
996999
ty::PredicateKind::Ambiguous => Ok(EvaluatedToAmbig),
9971000
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => {

compiler/rustc_traits/src/normalize_erasing_regions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ fn not_outlives_predicate(p: ty::Predicate<'_>) -> bool {
5555
ty::PredicateKind::Clause(ty::ClauseKind::Trait(..))
5656
| ty::PredicateKind::Clause(ty::ClauseKind::Projection(..))
5757
| ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(..))
58+
| ty::PredicateKind::NormalizesTo(..)
5859
| ty::PredicateKind::AliasRelate(..)
5960
| ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(..))
6061
| ty::PredicateKind::ObjectSafe(..)

compiler/rustc_type_ir/src/interner.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub trait Interner: Sized {
5757
type RegionOutlivesPredicate: Clone + Debug + Hash + Eq;
5858
type TypeOutlivesPredicate: Clone + Debug + Hash + Eq;
5959
type ProjectionPredicate: Clone + Debug + Hash + Eq;
60+
type NormalizesTo: Clone + Debug + Hash + Eq;
6061
type SubtypePredicate: Clone + Debug + Hash + Eq;
6162
type CoercePredicate: Clone + Debug + Hash + Eq;
6263
type ClosureKind: Clone + Debug + Hash + Eq;

0 commit comments

Comments
 (0)