Skip to content

Commit

Permalink
Auto merge of #121462 - compiler-errors:eq-and-sub, r=<try>
Browse files Browse the repository at this point in the history
Combine `Sub` and `Equate`

Combine `Sub` and `Equate` into a new relation called `TypeRelating` (that name sounds familiar...)

Tracks the difference between `Sub` and `Equate` via `ambient_variance: ty::Variance` much like the `NllTypeRelating` relation, but implemented slightly jankier because it's a more general purpose relation.

r? lcnr
  • Loading branch information
bors committed Feb 26, 2024
2 parents 0250ef2 + 3034b2b commit c30f81e
Show file tree
Hide file tree
Showing 9 changed files with 350 additions and 457 deletions.
13 changes: 6 additions & 7 deletions compiler/rustc_borrowck/src/type_check/relate_tys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,15 @@ impl<'bccx, 'tcx> TypeRelation<'tcx> for NllTypeRelating<'_, 'bccx, 'tcx> {

debug!(?self.ambient_variance);
// In a bivariant context this always succeeds.
let r =
if self.ambient_variance == ty::Variance::Bivariant { a } else { self.relate(a, b)? };
let r = if self.ambient_variance == ty::Variance::Bivariant {
Ok(a)
} else {
self.relate(a, b)
};

self.ambient_variance = old_ambient_variance;

Ok(r)
r
}

#[instrument(skip(self), level = "debug")]
Expand Down Expand Up @@ -576,10 +579,6 @@ impl<'bccx, 'tcx> ObligationEmittingRelation<'tcx> for NllTypeRelating<'_, 'bccx
);
}

fn alias_relate_direction(&self) -> ty::AliasRelationDirection {
unreachable!("manually overridden to handle ty::Variance::Contravariant ambient variance")
}

fn register_type_relate_obligation(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) {
self.register_predicates([ty::Binder::dummy(match self.ambient_variance {
ty::Variance::Covariant => ty::PredicateKind::AliasRelate(
Expand Down
30 changes: 10 additions & 20 deletions compiler/rustc_infer/src/infer/relate/combine.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! There are four type combiners: [Equate], [Sub], [Lub], and [Glb].
//! There are four type combiners: [TypeRelating], [Lub], and [Glb],
//! (and `NllTypeRelating` in rustc_borrowck, which is only used for NLL).
//!
//! Each implements the trait [TypeRelation] and contains methods for
//! combining two instances of various things and yielding a new instance.
//! These combiner methods always yield a `Result<T>`. To relate two
Expand All @@ -22,10 +24,9 @@
//! [TypeRelation::a_is_expected], so when dealing with contravariance
//! this should be correctly updated.

use super::equate::Equate;
use super::glb::Glb;
use super::lub::Lub;
use super::sub::Sub;
use super::type_relating::TypeRelating;
use crate::infer::{DefineOpaqueTypes, InferCtxt, TypeTrace};
use crate::traits::{Obligation, PredicateObligations};
use rustc_middle::infer::canonical::OriginalQueryValues;
Expand Down Expand Up @@ -303,12 +304,12 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
self.infcx.tcx
}

pub fn equate<'a>(&'a mut self, a_is_expected: bool) -> Equate<'a, 'infcx, 'tcx> {
Equate::new(self, a_is_expected)
pub fn equate<'a>(&'a mut self, a_is_expected: bool) -> TypeRelating<'a, 'infcx, 'tcx> {
TypeRelating::new(self, a_is_expected, ty::Invariant)
}

pub fn sub<'a>(&'a mut self, a_is_expected: bool) -> Sub<'a, 'infcx, 'tcx> {
Sub::new(self, a_is_expected)
pub fn sub<'a>(&'a mut self, a_is_expected: bool) -> TypeRelating<'a, 'infcx, 'tcx> {
TypeRelating::new(self, a_is_expected, ty::Covariant)
}

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

/// Register an obligation that both types must be related to each other according to
/// the [`ty::AliasRelationDirection`] given by [`ObligationEmittingRelation::alias_relate_direction`]
fn register_type_relate_obligation(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) {
self.register_predicates([ty::Binder::dummy(ty::PredicateKind::AliasRelate(
a.into(),
b.into(),
self.alias_relate_direction(),
))]);
}

/// Relation direction emitted for `AliasRelate` predicates, corresponding to the direction
/// of the relation.
fn alias_relate_direction(&self) -> ty::AliasRelationDirection;
/// Register `AliasRelate` obligation(s) that both types must be related to each other.
fn register_type_relate_obligation(&mut self, a: Ty<'tcx>, b: Ty<'tcx>);
}

fn int_unification_error<'tcx>(
Expand Down
198 changes: 0 additions & 198 deletions compiler/rustc_infer/src/infer/relate/equate.rs

This file was deleted.

4 changes: 2 additions & 2 deletions compiler/rustc_infer/src/infer/relate/generalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,9 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
debug!(?self.ambient_variance, "new ambient variance");
// Recursive calls to `relate` can overflow the stack. For example a deeper version of
// `ui/associated-consts/issue-93775.rs`.
let r = ensure_sufficient_stack(|| self.relate(a, b))?;
let r = ensure_sufficient_stack(|| self.relate(a, b));
self.ambient_variance = old_ambient_variance;
Ok(r)
r
}

#[instrument(level = "debug", skip(self, t2), ret)]
Expand Down
10 changes: 7 additions & 3 deletions compiler/rustc_infer/src/infer/relate/glb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,12 @@ impl<'tcx> ObligationEmittingRelation<'tcx> for Glb<'_, '_, 'tcx> {
self.fields.register_obligations(obligations);
}

fn alias_relate_direction(&self) -> ty::AliasRelationDirection {
// FIXME(deferred_projection_equality): This isn't right, I think?
ty::AliasRelationDirection::Equate
fn register_type_relate_obligation(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) {
self.register_predicates([ty::Binder::dummy(ty::PredicateKind::AliasRelate(
a.into(),
b.into(),
// FIXME(deferred_projection_equality): This isn't right, I think?
ty::AliasRelationDirection::Equate,
))]);
}
}
10 changes: 7 additions & 3 deletions compiler/rustc_infer/src/infer/relate/lub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,12 @@ impl<'tcx> ObligationEmittingRelation<'tcx> for Lub<'_, '_, 'tcx> {
self.fields.register_obligations(obligations)
}

fn alias_relate_direction(&self) -> ty::AliasRelationDirection {
// FIXME(deferred_projection_equality): This isn't right, I think?
ty::AliasRelationDirection::Equate
fn register_type_relate_obligation(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) {
self.register_predicates([ty::Binder::dummy(ty::PredicateKind::AliasRelate(
a.into(),
b.into(),
// FIXME(deferred_projection_equality): This isn't right, I think?
ty::AliasRelationDirection::Equate,
))]);
}
}
3 changes: 1 addition & 2 deletions compiler/rustc_infer/src/infer/relate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
//! (except for some relations used for diagnostics and heuristics in the compiler).

pub(super) mod combine;
mod equate;
mod generalize;
mod glb;
mod higher_ranked;
mod lattice;
mod lub;
mod sub;
mod type_relating;
Loading

0 comments on commit c30f81e

Please sign in to comment.