Auto merge of #131191 - nnethercote:lattice_op, r=lcnr
Merge `glb` and `lub` modules Tons of code is duplicated across them, and it's easy to factor that out. r? `@lcnr`
This commit is contained in:
commit
7067e4aee4
@ -1,4 +1,4 @@
|
|||||||
//! There are four type combiners: [TypeRelating], [Lub], and [Glb],
|
//! There are four type combiners: [TypeRelating], `Lub`, and `Glb`,
|
||||||
//! and `NllTypeRelating` in rustc_borrowck, which is only used for NLL.
|
//! and `NllTypeRelating` in rustc_borrowck, which is only used for NLL.
|
||||||
//!
|
//!
|
||||||
//! Each implements the trait [TypeRelation] and contains methods for
|
//! Each implements the trait [TypeRelation] and contains methods for
|
||||||
@ -26,8 +26,7 @@
|
|||||||
pub use rustc_next_trait_solver::relate::combine::*;
|
pub use rustc_next_trait_solver::relate::combine::*;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
use super::glb::Glb;
|
use super::lattice::{LatticeOp, LatticeOpKind};
|
||||||
use super::lub::Lub;
|
|
||||||
use super::type_relating::TypeRelating;
|
use super::type_relating::TypeRelating;
|
||||||
use super::{RelateResult, StructurallyRelateAliases};
|
use super::{RelateResult, StructurallyRelateAliases};
|
||||||
use crate::infer::{DefineOpaqueTypes, InferCtxt, TypeTrace, relate};
|
use crate::infer::{DefineOpaqueTypes, InferCtxt, TypeTrace, relate};
|
||||||
@ -303,12 +302,12 @@ pub fn sup<'a>(&'a mut self) -> TypeRelating<'a, 'infcx, 'tcx> {
|
|||||||
TypeRelating::new(self, StructurallyRelateAliases::No, ty::Contravariant)
|
TypeRelating::new(self, StructurallyRelateAliases::No, ty::Contravariant)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lub<'a>(&'a mut self) -> Lub<'a, 'infcx, 'tcx> {
|
pub(crate) fn lub<'a>(&'a mut self) -> LatticeOp<'a, 'infcx, 'tcx> {
|
||||||
Lub::new(self)
|
LatticeOp::new(self, LatticeOpKind::Lub)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn glb<'a>(&'a mut self) -> Glb<'a, 'infcx, 'tcx> {
|
pub(crate) fn glb<'a>(&'a mut self) -> LatticeOp<'a, 'infcx, 'tcx> {
|
||||||
Glb::new(self)
|
LatticeOp::new(self, LatticeOpKind::Glb)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn register_obligations(
|
pub fn register_obligations(
|
||||||
|
@ -1,159 +0,0 @@
|
|||||||
//! Greatest lower bound. See [`lattice`].
|
|
||||||
|
|
||||||
use rustc_middle::traits::solve::Goal;
|
|
||||||
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
|
|
||||||
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
|
|
||||||
use rustc_span::Span;
|
|
||||||
use tracing::{debug, instrument};
|
|
||||||
|
|
||||||
use super::StructurallyRelateAliases;
|
|
||||||
use super::combine::{CombineFields, PredicateEmittingRelation};
|
|
||||||
use super::lattice::{self, LatticeDir};
|
|
||||||
use crate::infer::{DefineOpaqueTypes, InferCtxt, SubregionOrigin};
|
|
||||||
use crate::traits::ObligationCause;
|
|
||||||
|
|
||||||
/// "Greatest lower bound" (common subtype)
|
|
||||||
pub struct Glb<'combine, 'infcx, 'tcx> {
|
|
||||||
fields: &'combine mut CombineFields<'infcx, 'tcx>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'combine, 'infcx, 'tcx> Glb<'combine, 'infcx, 'tcx> {
|
|
||||||
pub fn new(fields: &'combine mut CombineFields<'infcx, 'tcx>) -> Glb<'combine, 'infcx, 'tcx> {
|
|
||||||
Glb { fields }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Glb<'_, '_, 'tcx> {
|
|
||||||
fn cx(&self) -> TyCtxt<'tcx> {
|
|
||||||
self.fields.tcx()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn relate_with_variance<T: Relate<TyCtxt<'tcx>>>(
|
|
||||||
&mut self,
|
|
||||||
variance: ty::Variance,
|
|
||||||
_info: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
|
|
||||||
a: T,
|
|
||||||
b: T,
|
|
||||||
) -> RelateResult<'tcx, T> {
|
|
||||||
match variance {
|
|
||||||
ty::Invariant => self.fields.equate(StructurallyRelateAliases::No).relate(a, b),
|
|
||||||
ty::Covariant => self.relate(a, b),
|
|
||||||
// FIXME(#41044) -- not correct, need test
|
|
||||||
ty::Bivariant => Ok(a),
|
|
||||||
ty::Contravariant => self.fields.lub().relate(a, b),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[instrument(skip(self), level = "trace")]
|
|
||||||
fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
|
|
||||||
lattice::super_lattice_tys(self, a, b)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[instrument(skip(self), level = "trace")]
|
|
||||||
fn regions(
|
|
||||||
&mut self,
|
|
||||||
a: ty::Region<'tcx>,
|
|
||||||
b: ty::Region<'tcx>,
|
|
||||||
) -> RelateResult<'tcx, ty::Region<'tcx>> {
|
|
||||||
let origin = SubregionOrigin::Subtype(Box::new(self.fields.trace.clone()));
|
|
||||||
// GLB(&'static u8, &'a u8) == &RegionLUB('static, 'a) u8 == &'static u8
|
|
||||||
Ok(self.fields.infcx.inner.borrow_mut().unwrap_region_constraints().lub_regions(
|
|
||||||
self.cx(),
|
|
||||||
origin,
|
|
||||||
a,
|
|
||||||
b,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[instrument(skip(self), level = "trace")]
|
|
||||||
fn consts(
|
|
||||||
&mut self,
|
|
||||||
a: ty::Const<'tcx>,
|
|
||||||
b: ty::Const<'tcx>,
|
|
||||||
) -> RelateResult<'tcx, ty::Const<'tcx>> {
|
|
||||||
self.fields.infcx.super_combine_consts(self, a, b)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn binders<T>(
|
|
||||||
&mut self,
|
|
||||||
a: ty::Binder<'tcx, T>,
|
|
||||||
b: ty::Binder<'tcx, T>,
|
|
||||||
) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
|
|
||||||
where
|
|
||||||
T: Relate<TyCtxt<'tcx>>,
|
|
||||||
{
|
|
||||||
// GLB of a binder and itself is just itself
|
|
||||||
if a == b {
|
|
||||||
return Ok(a);
|
|
||||||
}
|
|
||||||
|
|
||||||
debug!("binders(a={:?}, b={:?})", a, b);
|
|
||||||
if a.skip_binder().has_escaping_bound_vars() || b.skip_binder().has_escaping_bound_vars() {
|
|
||||||
// When higher-ranked types are involved, computing the GLB is
|
|
||||||
// very challenging, switch to invariance. This is obviously
|
|
||||||
// overly conservative but works ok in practice.
|
|
||||||
self.relate_with_variance(ty::Invariant, ty::VarianceDiagInfo::default(), a, b)?;
|
|
||||||
Ok(a)
|
|
||||||
} else {
|
|
||||||
Ok(ty::Binder::dummy(self.relate(a.skip_binder(), b.skip_binder())?))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'combine, 'infcx, 'tcx> LatticeDir<'infcx, 'tcx> for Glb<'combine, 'infcx, 'tcx> {
|
|
||||||
fn infcx(&self) -> &'infcx InferCtxt<'tcx> {
|
|
||||||
self.fields.infcx
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cause(&self) -> &ObligationCause<'tcx> {
|
|
||||||
&self.fields.trace.cause
|
|
||||||
}
|
|
||||||
|
|
||||||
fn relate_bound(&mut self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> {
|
|
||||||
let mut sub = self.fields.sub();
|
|
||||||
sub.relate(v, a)?;
|
|
||||||
sub.relate(v, b)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn define_opaque_types(&self) -> DefineOpaqueTypes {
|
|
||||||
self.fields.define_opaque_types
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'tcx> PredicateEmittingRelation<InferCtxt<'tcx>> for Glb<'_, '_, 'tcx> {
|
|
||||||
fn span(&self) -> Span {
|
|
||||||
self.fields.trace.span()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn structurally_relate_aliases(&self) -> StructurallyRelateAliases {
|
|
||||||
StructurallyRelateAliases::No
|
|
||||||
}
|
|
||||||
|
|
||||||
fn param_env(&self) -> ty::ParamEnv<'tcx> {
|
|
||||||
self.fields.param_env
|
|
||||||
}
|
|
||||||
|
|
||||||
fn register_predicates(
|
|
||||||
&mut self,
|
|
||||||
obligations: impl IntoIterator<Item: ty::Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>>,
|
|
||||||
) {
|
|
||||||
self.fields.register_predicates(obligations);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn register_goals(
|
|
||||||
&mut self,
|
|
||||||
obligations: impl IntoIterator<Item = Goal<'tcx, ty::Predicate<'tcx>>>,
|
|
||||||
) {
|
|
||||||
self.fields.register_obligations(obligations);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn register_alias_relate_predicate(&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,
|
|
||||||
))]);
|
|
||||||
}
|
|
||||||
}
|
|
@ -17,50 +17,80 @@
|
|||||||
//!
|
//!
|
||||||
//! [lattices]: https://en.wikipedia.org/wiki/Lattice_(order)
|
//! [lattices]: https://en.wikipedia.org/wiki/Lattice_(order)
|
||||||
|
|
||||||
use rustc_middle::ty::relate::RelateResult;
|
use rustc_middle::traits::solve::Goal;
|
||||||
use rustc_middle::ty::{self, Ty, TyVar};
|
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
|
||||||
use tracing::instrument;
|
use rustc_middle::ty::{self, Ty, TyCtxt, TyVar, TypeVisitableExt};
|
||||||
|
use rustc_span::Span;
|
||||||
|
use tracing::{debug, instrument};
|
||||||
|
|
||||||
use super::combine::PredicateEmittingRelation;
|
use super::StructurallyRelateAliases;
|
||||||
use crate::infer::{DefineOpaqueTypes, InferCtxt};
|
use super::combine::{CombineFields, PredicateEmittingRelation};
|
||||||
use crate::traits::ObligationCause;
|
use crate::infer::{DefineOpaqueTypes, InferCtxt, SubregionOrigin};
|
||||||
|
|
||||||
/// Trait for returning data about a lattice, and for abstracting
|
#[derive(Clone, Copy)]
|
||||||
/// over the "direction" of the lattice operation (LUB/GLB).
|
pub(crate) enum LatticeOpKind {
|
||||||
///
|
Glb,
|
||||||
/// GLB moves "down" the lattice (to smaller values); LUB moves
|
Lub,
|
||||||
/// "up" the lattice (to bigger values).
|
}
|
||||||
pub(crate) trait LatticeDir<'f, 'tcx>: PredicateEmittingRelation<InferCtxt<'tcx>> {
|
|
||||||
fn infcx(&self) -> &'f InferCtxt<'tcx>;
|
|
||||||
|
|
||||||
fn cause(&self) -> &ObligationCause<'tcx>;
|
impl LatticeOpKind {
|
||||||
|
fn invert(self) -> Self {
|
||||||
|
match self {
|
||||||
|
LatticeOpKind::Glb => LatticeOpKind::Lub,
|
||||||
|
LatticeOpKind::Lub => LatticeOpKind::Glb,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn define_opaque_types(&self) -> DefineOpaqueTypes;
|
/// A greatest lower bound" (common subtype) or least upper bound (common supertype).
|
||||||
|
pub(crate) struct LatticeOp<'combine, 'infcx, 'tcx> {
|
||||||
|
fields: &'combine mut CombineFields<'infcx, 'tcx>,
|
||||||
|
kind: LatticeOpKind,
|
||||||
|
}
|
||||||
|
|
||||||
// Relates the type `v` to `a` and `b` such that `v` represents
|
impl<'combine, 'infcx, 'tcx> LatticeOp<'combine, 'infcx, 'tcx> {
|
||||||
// the LUB/GLB of `a` and `b` as appropriate.
|
pub(crate) fn new(
|
||||||
//
|
fields: &'combine mut CombineFields<'infcx, 'tcx>,
|
||||||
// Subtle hack: ordering *may* be significant here. This method
|
kind: LatticeOpKind,
|
||||||
// relates `v` to `a` first, which may help us to avoid unnecessary
|
) -> LatticeOp<'combine, 'infcx, 'tcx> {
|
||||||
// type variable obligations. See caller for details.
|
LatticeOp { fields, kind }
|
||||||
fn relate_bound(&mut self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()>;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'tcx> TypeRelation<TyCtxt<'tcx>> for LatticeOp<'_, '_, 'tcx> {
|
||||||
|
fn cx(&self) -> TyCtxt<'tcx> {
|
||||||
|
self.fields.tcx()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn relate_with_variance<T: Relate<TyCtxt<'tcx>>>(
|
||||||
|
&mut self,
|
||||||
|
variance: ty::Variance,
|
||||||
|
_info: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
|
||||||
|
a: T,
|
||||||
|
b: T,
|
||||||
|
) -> RelateResult<'tcx, T> {
|
||||||
|
match variance {
|
||||||
|
ty::Invariant => self.fields.equate(StructurallyRelateAliases::No).relate(a, b),
|
||||||
|
ty::Covariant => self.relate(a, b),
|
||||||
|
// FIXME(#41044) -- not correct, need test
|
||||||
|
ty::Bivariant => Ok(a),
|
||||||
|
ty::Contravariant => {
|
||||||
|
self.kind = self.kind.invert();
|
||||||
|
let res = self.relate(a, b);
|
||||||
|
self.kind = self.kind.invert();
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Relates two types using a given lattice.
|
/// Relates two types using a given lattice.
|
||||||
#[instrument(skip(this), level = "debug")]
|
#[instrument(skip(self), level = "trace")]
|
||||||
pub fn super_lattice_tys<'a, 'tcx: 'a, L>(
|
fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
|
||||||
this: &mut L,
|
|
||||||
a: Ty<'tcx>,
|
|
||||||
b: Ty<'tcx>,
|
|
||||||
) -> RelateResult<'tcx, Ty<'tcx>>
|
|
||||||
where
|
|
||||||
L: LatticeDir<'a, 'tcx>,
|
|
||||||
{
|
|
||||||
if a == b {
|
if a == b {
|
||||||
return Ok(a);
|
return Ok(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
let infcx = this.infcx();
|
let infcx = self.fields.infcx;
|
||||||
|
|
||||||
let a = infcx.shallow_resolve(a);
|
let a = infcx.shallow_resolve(a);
|
||||||
let b = infcx.shallow_resolve(b);
|
let b = infcx.shallow_resolve(b);
|
||||||
@ -85,31 +115,149 @@ pub fn super_lattice_tys<'a, 'tcx: 'a, L>(
|
|||||||
// iterate on the subtype obligations that are returned, but I
|
// iterate on the subtype obligations that are returned, but I
|
||||||
// think this suffices. -nmatsakis
|
// think this suffices. -nmatsakis
|
||||||
(&ty::Infer(TyVar(..)), _) => {
|
(&ty::Infer(TyVar(..)), _) => {
|
||||||
let v = infcx.next_ty_var(this.cause().span);
|
let v = infcx.next_ty_var(self.fields.trace.cause.span);
|
||||||
this.relate_bound(v, b, a)?;
|
self.relate_bound(v, b, a)?;
|
||||||
Ok(v)
|
Ok(v)
|
||||||
}
|
}
|
||||||
(_, &ty::Infer(TyVar(..))) => {
|
(_, &ty::Infer(TyVar(..))) => {
|
||||||
let v = infcx.next_ty_var(this.cause().span);
|
let v = infcx.next_ty_var(self.fields.trace.cause.span);
|
||||||
this.relate_bound(v, a, b)?;
|
self.relate_bound(v, a, b)?;
|
||||||
Ok(v)
|
Ok(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
(
|
(
|
||||||
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: a_def_id, .. }),
|
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: a_def_id, .. }),
|
||||||
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, .. }),
|
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, .. }),
|
||||||
) if a_def_id == b_def_id => infcx.super_combine_tys(this, a, b),
|
) if a_def_id == b_def_id => infcx.super_combine_tys(self, a, b),
|
||||||
|
|
||||||
(&ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }), _)
|
(&ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }), _)
|
||||||
| (_, &ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }))
|
| (_, &ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }))
|
||||||
if this.define_opaque_types() == DefineOpaqueTypes::Yes
|
if self.fields.define_opaque_types == DefineOpaqueTypes::Yes
|
||||||
&& def_id.is_local()
|
&& def_id.is_local()
|
||||||
&& !this.infcx().next_trait_solver() =>
|
&& !infcx.next_trait_solver() =>
|
||||||
{
|
{
|
||||||
this.register_goals(infcx.handle_opaque_type(a, b, this.span(), this.param_env())?);
|
self.register_goals(infcx.handle_opaque_type(
|
||||||
|
a,
|
||||||
|
b,
|
||||||
|
self.span(),
|
||||||
|
self.param_env(),
|
||||||
|
)?);
|
||||||
Ok(a)
|
Ok(a)
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => infcx.super_combine_tys(this, a, b),
|
_ => infcx.super_combine_tys(self, a, b),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self), level = "trace")]
|
||||||
|
fn regions(
|
||||||
|
&mut self,
|
||||||
|
a: ty::Region<'tcx>,
|
||||||
|
b: ty::Region<'tcx>,
|
||||||
|
) -> RelateResult<'tcx, ty::Region<'tcx>> {
|
||||||
|
let origin = SubregionOrigin::Subtype(Box::new(self.fields.trace.clone()));
|
||||||
|
let mut inner = self.fields.infcx.inner.borrow_mut();
|
||||||
|
let mut constraints = inner.unwrap_region_constraints();
|
||||||
|
Ok(match self.kind {
|
||||||
|
// GLB(&'static u8, &'a u8) == &RegionLUB('static, 'a) u8 == &'static u8
|
||||||
|
LatticeOpKind::Glb => constraints.lub_regions(self.cx(), origin, a, b),
|
||||||
|
|
||||||
|
// LUB(&'static u8, &'a u8) == &RegionGLB('static, 'a) u8 == &'a u8
|
||||||
|
LatticeOpKind::Lub => constraints.glb_regions(self.cx(), origin, a, b),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self), level = "trace")]
|
||||||
|
fn consts(
|
||||||
|
&mut self,
|
||||||
|
a: ty::Const<'tcx>,
|
||||||
|
b: ty::Const<'tcx>,
|
||||||
|
) -> RelateResult<'tcx, ty::Const<'tcx>> {
|
||||||
|
self.fields.infcx.super_combine_consts(self, a, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn binders<T>(
|
||||||
|
&mut self,
|
||||||
|
a: ty::Binder<'tcx, T>,
|
||||||
|
b: ty::Binder<'tcx, T>,
|
||||||
|
) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
|
||||||
|
where
|
||||||
|
T: Relate<TyCtxt<'tcx>>,
|
||||||
|
{
|
||||||
|
// GLB/LUB of a binder and itself is just itself
|
||||||
|
if a == b {
|
||||||
|
return Ok(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
debug!("binders(a={:?}, b={:?})", a, b);
|
||||||
|
if a.skip_binder().has_escaping_bound_vars() || b.skip_binder().has_escaping_bound_vars() {
|
||||||
|
// When higher-ranked types are involved, computing the GLB/LUB is
|
||||||
|
// very challenging, switch to invariance. This is obviously
|
||||||
|
// overly conservative but works ok in practice.
|
||||||
|
self.relate_with_variance(ty::Invariant, ty::VarianceDiagInfo::default(), a, b)?;
|
||||||
|
Ok(a)
|
||||||
|
} else {
|
||||||
|
Ok(ty::Binder::dummy(self.relate(a.skip_binder(), b.skip_binder())?))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'combine, 'infcx, 'tcx> LatticeOp<'combine, 'infcx, 'tcx> {
|
||||||
|
// Relates the type `v` to `a` and `b` such that `v` represents
|
||||||
|
// the LUB/GLB of `a` and `b` as appropriate.
|
||||||
|
//
|
||||||
|
// Subtle hack: ordering *may* be significant here. This method
|
||||||
|
// relates `v` to `a` first, which may help us to avoid unnecessary
|
||||||
|
// type variable obligations. See caller for details.
|
||||||
|
fn relate_bound(&mut self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> {
|
||||||
|
let mut sub = self.fields.sub();
|
||||||
|
match self.kind {
|
||||||
|
LatticeOpKind::Glb => {
|
||||||
|
sub.relate(v, a)?;
|
||||||
|
sub.relate(v, b)?;
|
||||||
|
}
|
||||||
|
LatticeOpKind::Lub => {
|
||||||
|
sub.relate(a, v)?;
|
||||||
|
sub.relate(b, v)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'tcx> PredicateEmittingRelation<InferCtxt<'tcx>> for LatticeOp<'_, '_, 'tcx> {
|
||||||
|
fn span(&self) -> Span {
|
||||||
|
self.fields.trace.span()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn structurally_relate_aliases(&self) -> StructurallyRelateAliases {
|
||||||
|
StructurallyRelateAliases::No
|
||||||
|
}
|
||||||
|
|
||||||
|
fn param_env(&self) -> ty::ParamEnv<'tcx> {
|
||||||
|
self.fields.param_env
|
||||||
|
}
|
||||||
|
|
||||||
|
fn register_predicates(
|
||||||
|
&mut self,
|
||||||
|
obligations: impl IntoIterator<Item: ty::Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>>,
|
||||||
|
) {
|
||||||
|
self.fields.register_predicates(obligations);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn register_goals(
|
||||||
|
&mut self,
|
||||||
|
obligations: impl IntoIterator<Item = Goal<'tcx, ty::Predicate<'tcx>>>,
|
||||||
|
) {
|
||||||
|
self.fields.register_obligations(obligations);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn register_alias_relate_predicate(&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,
|
||||||
|
))]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,158 +0,0 @@
|
|||||||
//! Least upper bound. See [`lattice`].
|
|
||||||
|
|
||||||
use rustc_middle::traits::solve::Goal;
|
|
||||||
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
|
|
||||||
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
|
|
||||||
use rustc_span::Span;
|
|
||||||
use tracing::{debug, instrument};
|
|
||||||
|
|
||||||
use super::StructurallyRelateAliases;
|
|
||||||
use super::combine::{CombineFields, PredicateEmittingRelation};
|
|
||||||
use super::lattice::{self, LatticeDir};
|
|
||||||
use crate::infer::{DefineOpaqueTypes, InferCtxt, SubregionOrigin};
|
|
||||||
use crate::traits::ObligationCause;
|
|
||||||
|
|
||||||
/// "Least upper bound" (common supertype)
|
|
||||||
pub struct Lub<'combine, 'infcx, 'tcx> {
|
|
||||||
fields: &'combine mut CombineFields<'infcx, 'tcx>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'combine, 'infcx, 'tcx> Lub<'combine, 'infcx, 'tcx> {
|
|
||||||
pub fn new(fields: &'combine mut CombineFields<'infcx, 'tcx>) -> Lub<'combine, 'infcx, 'tcx> {
|
|
||||||
Lub { fields }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Lub<'_, '_, 'tcx> {
|
|
||||||
fn cx(&self) -> TyCtxt<'tcx> {
|
|
||||||
self.fields.tcx()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn relate_with_variance<T: Relate<TyCtxt<'tcx>>>(
|
|
||||||
&mut self,
|
|
||||||
variance: ty::Variance,
|
|
||||||
_info: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
|
|
||||||
a: T,
|
|
||||||
b: T,
|
|
||||||
) -> RelateResult<'tcx, T> {
|
|
||||||
match variance {
|
|
||||||
ty::Invariant => self.fields.equate(StructurallyRelateAliases::No).relate(a, b),
|
|
||||||
ty::Covariant => self.relate(a, b),
|
|
||||||
// FIXME(#41044) -- not correct, need test
|
|
||||||
ty::Bivariant => Ok(a),
|
|
||||||
ty::Contravariant => self.fields.glb().relate(a, b),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
|
|
||||||
lattice::super_lattice_tys(self, a, b)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[instrument(skip(self), level = "trace")]
|
|
||||||
fn regions(
|
|
||||||
&mut self,
|
|
||||||
a: ty::Region<'tcx>,
|
|
||||||
b: ty::Region<'tcx>,
|
|
||||||
) -> RelateResult<'tcx, ty::Region<'tcx>> {
|
|
||||||
let origin = SubregionOrigin::Subtype(Box::new(self.fields.trace.clone()));
|
|
||||||
// LUB(&'static u8, &'a u8) == &RegionGLB('static, 'a) u8 == &'a u8
|
|
||||||
Ok(self.fields.infcx.inner.borrow_mut().unwrap_region_constraints().glb_regions(
|
|
||||||
self.cx(),
|
|
||||||
origin,
|
|
||||||
a,
|
|
||||||
b,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[instrument(skip(self), level = "trace")]
|
|
||||||
fn consts(
|
|
||||||
&mut self,
|
|
||||||
a: ty::Const<'tcx>,
|
|
||||||
b: ty::Const<'tcx>,
|
|
||||||
) -> RelateResult<'tcx, ty::Const<'tcx>> {
|
|
||||||
self.fields.infcx.super_combine_consts(self, a, b)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn binders<T>(
|
|
||||||
&mut self,
|
|
||||||
a: ty::Binder<'tcx, T>,
|
|
||||||
b: ty::Binder<'tcx, T>,
|
|
||||||
) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
|
|
||||||
where
|
|
||||||
T: Relate<TyCtxt<'tcx>>,
|
|
||||||
{
|
|
||||||
// LUB of a binder and itself is just itself
|
|
||||||
if a == b {
|
|
||||||
return Ok(a);
|
|
||||||
}
|
|
||||||
|
|
||||||
debug!("binders(a={:?}, b={:?})", a, b);
|
|
||||||
if a.skip_binder().has_escaping_bound_vars() || b.skip_binder().has_escaping_bound_vars() {
|
|
||||||
// When higher-ranked types are involved, computing the LUB is
|
|
||||||
// very challenging, switch to invariance. This is obviously
|
|
||||||
// overly conservative but works ok in practice.
|
|
||||||
self.relate_with_variance(ty::Invariant, ty::VarianceDiagInfo::default(), a, b)?;
|
|
||||||
Ok(a)
|
|
||||||
} else {
|
|
||||||
Ok(ty::Binder::dummy(self.relate(a.skip_binder(), b.skip_binder())?))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'combine, 'infcx, 'tcx> LatticeDir<'infcx, 'tcx> for Lub<'combine, 'infcx, 'tcx> {
|
|
||||||
fn infcx(&self) -> &'infcx InferCtxt<'tcx> {
|
|
||||||
self.fields.infcx
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cause(&self) -> &ObligationCause<'tcx> {
|
|
||||||
&self.fields.trace.cause
|
|
||||||
}
|
|
||||||
|
|
||||||
fn relate_bound(&mut self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> {
|
|
||||||
let mut sub = self.fields.sub();
|
|
||||||
sub.relate(a, v)?;
|
|
||||||
sub.relate(b, v)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn define_opaque_types(&self) -> DefineOpaqueTypes {
|
|
||||||
self.fields.define_opaque_types
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'tcx> PredicateEmittingRelation<InferCtxt<'tcx>> for Lub<'_, '_, 'tcx> {
|
|
||||||
fn span(&self) -> Span {
|
|
||||||
self.fields.trace.span()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn structurally_relate_aliases(&self) -> StructurallyRelateAliases {
|
|
||||||
StructurallyRelateAliases::No
|
|
||||||
}
|
|
||||||
|
|
||||||
fn param_env(&self) -> ty::ParamEnv<'tcx> {
|
|
||||||
self.fields.param_env
|
|
||||||
}
|
|
||||||
|
|
||||||
fn register_predicates(
|
|
||||||
&mut self,
|
|
||||||
obligations: impl IntoIterator<Item: ty::Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>>,
|
|
||||||
) {
|
|
||||||
self.fields.register_predicates(obligations);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn register_goals(
|
|
||||||
&mut self,
|
|
||||||
obligations: impl IntoIterator<Item = Goal<'tcx, ty::Predicate<'tcx>>>,
|
|
||||||
) {
|
|
||||||
self.fields.register_obligations(obligations)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn register_alias_relate_predicate(&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,8 +10,6 @@
|
|||||||
#[allow(hidden_glob_reexports)]
|
#[allow(hidden_glob_reexports)]
|
||||||
pub(super) mod combine;
|
pub(super) mod combine;
|
||||||
mod generalize;
|
mod generalize;
|
||||||
mod glb;
|
|
||||||
mod higher_ranked;
|
mod higher_ranked;
|
||||||
mod lattice;
|
mod lattice;
|
||||||
mod lub;
|
|
||||||
mod type_relating;
|
mod type_relating;
|
||||||
|
Loading…
Reference in New Issue
Block a user