1
Fork 0

Rollup merge of #126354 - compiler-errors:variance, r=lcnr

Use `Variance` glob imported variants everywhere

Fully commit to using the globbed variance. Could be convinced the other way, and change this PR to not use the globbed variants anywhere, but I'd rather we do one or the other.

r? lcnr
This commit is contained in:
Matthias Krüger 2024-06-15 10:56:40 +02:00 committed by GitHub
commit 335e320baa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 74 additions and 111 deletions

View file

@ -212,16 +212,16 @@ impl<'a, 'tcx> At<'a, 'tcx> {
T: ToTrace<'tcx>,
{
match variance {
ty::Variance::Covariant => self.sub(define_opaque_types, expected, actual),
ty::Variance::Invariant => self.eq(define_opaque_types, expected, actual),
ty::Variance::Contravariant => self.sup(define_opaque_types, expected, actual),
ty::Covariant => self.sub(define_opaque_types, expected, actual),
ty::Invariant => self.eq(define_opaque_types, expected, actual),
ty::Contravariant => self.sup(define_opaque_types, expected, actual),
// We could make this make sense but it's not readily
// exposed and I don't feel like dealing with it. Note
// that bivariance in general does a bit more than just
// *nothing*, it checks that the types are the same
// "modulo variance" basically.
ty::Variance::Bivariant => panic!("Bivariant given to `relate()`"),
ty::Bivariant => panic!("Bivariant given to `relate()`"),
}
}

View file

@ -345,7 +345,7 @@ impl<'tcx> InferCtxt<'tcx> {
.args
.iter()
.enumerate()
.filter(|(i, _)| variances[*i] == ty::Variance::Invariant)
.filter(|(i, _)| variances[*i] == ty::Invariant)
.filter_map(|(_, arg)| match arg.unpack() {
GenericArgKind::Lifetime(r) => Some(r),
GenericArgKind::Type(_) | GenericArgKind::Const(_) => None,
@ -441,7 +441,7 @@ where
let variances = self.tcx.variances_of(*def_id);
for (v, s) in std::iter::zip(variances, args.iter()) {
if *v != ty::Variance::Bivariant {
if *v != ty::Bivariant {
s.visit_with(self);
}
}

View file

@ -102,9 +102,7 @@ where
};
for (idx, s) in args.iter().enumerate() {
if variances.map(|variances| variances[idx])
!= Some(ty::Variance::Bivariant)
{
if variances.map(|variances| variances[idx]) != Some(ty::Bivariant) {
s.visit_with(self);
}
}

View file

@ -83,16 +83,16 @@ impl<'tcx> InferCtxt<'tcx> {
// mention `?0`.
if self.next_trait_solver() {
let (lhs, rhs, direction) = match instantiation_variance {
ty::Variance::Invariant => {
ty::Invariant => {
(generalized_ty.into(), source_ty.into(), AliasRelationDirection::Equate)
}
ty::Variance::Covariant => {
ty::Covariant => {
(generalized_ty.into(), source_ty.into(), AliasRelationDirection::Subtype)
}
ty::Variance::Contravariant => {
ty::Contravariant => {
(source_ty.into(), generalized_ty.into(), AliasRelationDirection::Subtype)
}
ty::Variance::Bivariant => unreachable!("bivariant generalization"),
ty::Bivariant => unreachable!("bivariant generalization"),
};
relation.register_predicates([ty::PredicateKind::AliasRelate(lhs, rhs, direction)]);
@ -192,7 +192,7 @@ impl<'tcx> InferCtxt<'tcx> {
relation.span(),
relation.structurally_relate_aliases(),
target_vid,
ty::Variance::Invariant,
ty::Invariant,
source_ct,
)?;
@ -210,14 +210,14 @@ impl<'tcx> InferCtxt<'tcx> {
// generalized const and the source.
if target_is_expected {
relation.relate_with_variance(
ty::Variance::Invariant,
ty::Invariant,
ty::VarianceDiagInfo::default(),
generalized_ct,
source_ct,
)?;
} else {
relation.relate_with_variance(
ty::Variance::Invariant,
ty::Invariant,
ty::VarianceDiagInfo::default(),
source_ct,
generalized_ct,
@ -411,7 +411,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
a_arg: ty::GenericArgsRef<'tcx>,
b_arg: ty::GenericArgsRef<'tcx>,
) -> RelateResult<'tcx, ty::GenericArgsRef<'tcx>> {
if self.ambient_variance == ty::Variance::Invariant {
if self.ambient_variance == ty::Invariant {
// Avoid fetching the variance if we are in an invariant
// context; no need, and it can induce dependency cycles
// (e.g., #41849).
@ -667,7 +667,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
// structural.
ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, args }) => {
let args = self.relate_with_variance(
ty::Variance::Invariant,
ty::Invariant,
ty::VarianceDiagInfo::default(),
args,
args,

View file

@ -94,12 +94,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Glb<'_, '_, 'tcx> {
// 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::Variance::Invariant,
ty::VarianceDiagInfo::default(),
a,
b,
)?;
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())?))

View file

@ -94,12 +94,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Lub<'_, '_, 'tcx> {
// 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::Variance::Invariant,
ty::VarianceDiagInfo::default(),
a,
b,
)?;
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())?))

View file

@ -42,7 +42,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for TypeRelating<'_, '_, 'tcx> {
a_arg: ty::GenericArgsRef<'tcx>,
b_arg: ty::GenericArgsRef<'tcx>,
) -> RelateResult<'tcx, ty::GenericArgsRef<'tcx>> {
if self.ambient_variance == ty::Variance::Invariant {
if self.ambient_variance == ty::Invariant {
// Avoid fetching the variance if we are in an invariant
// context; no need, and it can induce dependency cycles
// (e.g., #41849).
@ -325,23 +325,23 @@ impl<'tcx> PredicateEmittingRelation<'tcx> for TypeRelating<'_, '_, 'tcx> {
fn register_alias_relate_predicate(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) {
self.register_predicates([ty::Binder::dummy(match self.ambient_variance {
ty::Variance::Covariant => ty::PredicateKind::AliasRelate(
ty::Covariant => ty::PredicateKind::AliasRelate(
a.into(),
b.into(),
ty::AliasRelationDirection::Subtype,
),
// a :> b is b <: a
ty::Variance::Contravariant => ty::PredicateKind::AliasRelate(
ty::Contravariant => ty::PredicateKind::AliasRelate(
b.into(),
a.into(),
ty::AliasRelationDirection::Subtype,
),
ty::Variance::Invariant => ty::PredicateKind::AliasRelate(
ty::Invariant => ty::PredicateKind::AliasRelate(
a.into(),
b.into(),
ty::AliasRelationDirection::Equate,
),
ty::Variance::Bivariant => {
ty::Bivariant => {
unreachable!("Expected bivariance to be handled in relate_with_variance")
}
})]);