Skip to content

Commit c87b727

Browse files
Combine sub and eq
1 parent 3cb3631 commit c87b727

File tree

8 files changed

+356
-492
lines changed

8 files changed

+356
-492
lines changed

compiler/rustc_borrowck/src/type_check/relate_tys.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,15 @@ impl<'bccx, 'tcx> TypeRelation<'tcx> for NllTypeRelating<'_, 'bccx, 'tcx> {
349349

350350
debug!(?self.ambient_variance);
351351
// In a bivariant context this always succeeds.
352-
let r =
353-
if self.ambient_variance == ty::Variance::Bivariant { a } else { self.relate(a, b)? };
352+
let r = if self.ambient_variance == ty::Variance::Bivariant {
353+
Ok(a)
354+
} else {
355+
self.relate(a, b)
356+
};
354357

355358
self.ambient_variance = old_ambient_variance;
356359

357-
Ok(r)
360+
r
358361
}
359362

360363
#[instrument(skip(self), level = "debug")]
@@ -579,10 +582,6 @@ impl<'bccx, 'tcx> ObligationEmittingRelation<'tcx> for NllTypeRelating<'_, 'bccx
579582
);
580583
}
581584

582-
fn alias_relate_direction(&self) -> ty::AliasRelationDirection {
583-
unreachable!("manually overridden to handle ty::Variance::Contravariant ambient variance")
584-
}
585-
586585
fn register_type_relate_obligation(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) {
587586
self.register_predicates([ty::Binder::dummy(match self.ambient_variance {
588587
ty::Variance::Covariant => ty::PredicateKind::AliasRelate(

compiler/rustc_infer/src/infer/relate/combine.rs

+10-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
//! There are four type combiners: [Equate], [Sub], [Lub], and [Glb].
1+
//! There are four type combiners: [TypeRelating], [Lub], and [Glb],
2+
//! and `NllTypeRelating` in rustc_borrowck, which is only used for NLL.
3+
//!
24
//! Each implements the trait [TypeRelation] and contains methods for
35
//! combining two instances of various things and yielding a new instance.
46
//! These combiner methods always yield a `Result<T>`. To relate two
@@ -22,10 +24,9 @@
2224
//! [TypeRelation::a_is_expected], so when dealing with contravariance
2325
//! this should be correctly updated.
2426
25-
use super::equate::Equate;
2627
use super::glb::Glb;
2728
use super::lub::Lub;
28-
use super::sub::Sub;
29+
use super::type_relating::TypeRelating;
2930
use super::StructurallyRelateAliases;
3031
use crate::infer::{DefineOpaqueTypes, InferCtxt, TypeTrace};
3132
use crate::traits::{Obligation, PredicateObligations};
@@ -322,12 +323,12 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
322323
&'a mut self,
323324
structurally_relate_aliases: StructurallyRelateAliases,
324325
a_is_expected: bool,
325-
) -> Equate<'a, 'infcx, 'tcx> {
326-
Equate::new(self, structurally_relate_aliases, a_is_expected)
326+
) -> TypeRelating<'a, 'infcx, 'tcx> {
327+
TypeRelating::new(self, a_is_expected, structurally_relate_aliases, ty::Invariant)
327328
}
328329

329-
pub fn sub<'a>(&'a mut self, a_is_expected: bool) -> Sub<'a, 'infcx, 'tcx> {
330-
Sub::new(self, a_is_expected)
330+
pub fn sub<'a>(&'a mut self, a_is_expected: bool) -> TypeRelating<'a, 'infcx, 'tcx> {
331+
TypeRelating::new(self, a_is_expected, StructurallyRelateAliases::No, ty::Covariant)
331332
}
332333

333334
pub fn lub<'a>(&'a mut self, a_is_expected: bool) -> Lub<'a, 'infcx, 'tcx> {
@@ -367,19 +368,8 @@ pub trait ObligationEmittingRelation<'tcx>: TypeRelation<'tcx> {
367368
/// be used if control over the obligation causes is required.
368369
fn register_predicates(&mut self, obligations: impl IntoIterator<Item: ToPredicate<'tcx>>);
369370

370-
/// Register an obligation that both types must be related to each other according to
371-
/// the [`ty::AliasRelationDirection`] given by [`ObligationEmittingRelation::alias_relate_direction`]
372-
fn register_type_relate_obligation(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) {
373-
self.register_predicates([ty::Binder::dummy(ty::PredicateKind::AliasRelate(
374-
a.into(),
375-
b.into(),
376-
self.alias_relate_direction(),
377-
))]);
378-
}
379-
380-
/// Relation direction emitted for `AliasRelate` predicates, corresponding to the direction
381-
/// of the relation.
382-
fn alias_relate_direction(&self) -> ty::AliasRelationDirection;
371+
/// Register `AliasRelate` obligation(s) that both types must be related to each other.
372+
fn register_type_relate_obligation(&mut self, a: Ty<'tcx>, b: Ty<'tcx>);
383373
}
384374

385375
fn int_unification_error<'tcx>(

compiler/rustc_infer/src/infer/relate/equate.rs

-228
This file was deleted.

compiler/rustc_infer/src/infer/relate/glb.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,12 @@ impl<'tcx> ObligationEmittingRelation<'tcx> for Glb<'_, '_, 'tcx> {
158158
self.fields.register_obligations(obligations);
159159
}
160160

161-
fn alias_relate_direction(&self) -> ty::AliasRelationDirection {
162-
// FIXME(deferred_projection_equality): This isn't right, I think?
163-
ty::AliasRelationDirection::Equate
161+
fn register_type_relate_obligation(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) {
162+
self.register_predicates([ty::Binder::dummy(ty::PredicateKind::AliasRelate(
163+
a.into(),
164+
b.into(),
165+
// FIXME(deferred_projection_equality): This isn't right, I think?
166+
ty::AliasRelationDirection::Equate,
167+
))]);
164168
}
165169
}

compiler/rustc_infer/src/infer/relate/lub.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,12 @@ impl<'tcx> ObligationEmittingRelation<'tcx> for Lub<'_, '_, 'tcx> {
158158
self.fields.register_obligations(obligations)
159159
}
160160

161-
fn alias_relate_direction(&self) -> ty::AliasRelationDirection {
162-
// FIXME(deferred_projection_equality): This isn't right, I think?
163-
ty::AliasRelationDirection::Equate
161+
fn register_type_relate_obligation(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) {
162+
self.register_predicates([ty::Binder::dummy(ty::PredicateKind::AliasRelate(
163+
a.into(),
164+
b.into(),
165+
// FIXME(deferred_projection_equality): This isn't right, I think?
166+
ty::AliasRelationDirection::Equate,
167+
))]);
164168
}
165169
}

compiler/rustc_infer/src/infer/relate/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
//! (except for some relations used for diagnostics and heuristics in the compiler).
33
44
pub(super) mod combine;
5-
mod equate;
65
mod generalize;
76
mod glb;
87
mod higher_ranked;
98
mod lattice;
109
mod lub;
11-
mod sub;
10+
mod type_relating;
1211

1312
/// Whether aliases should be related structurally or not. Used
1413
/// to adjust the behavior of generalization and combine.

0 commit comments

Comments
 (0)