Skip to content

Commit

Permalink
Auto merge of #121864 - compiler-errors:type-relating-variances, r=<try>
Browse files Browse the repository at this point in the history
Don't grab variances in `TypeRelating` relation if we're invariant

Since `Invariant.xform(var) = Invariant` always, so just copy what the generalizer relation does.

Fixes #110106
  • Loading branch information
bors committed Mar 1, 2024
2 parents 6db96de + cb7c9cc commit b4af5d3
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion compiler/rustc_infer/src/infer/relate/type_relating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use crate::infer::{
};
use crate::traits::{Obligation, PredicateObligations};

use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
use rustc_middle::ty::relate::{
relate_args_invariantly, relate_args_with_variances, Relate, RelateResult, TypeRelation,
};
use rustc_middle::ty::TyVar;
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::Span;
Expand Down Expand Up @@ -36,6 +38,24 @@ impl<'tcx> TypeRelation<'tcx> for TypeRelating<'_, '_, 'tcx> {
self.fields.infcx.tcx
}

fn relate_item_args(
&mut self,
item_def_id: rustc_hir::def_id::DefId,
a_arg: ty::GenericArgsRef<'tcx>,
b_arg: ty::GenericArgsRef<'tcx>,
) -> RelateResult<'tcx, ty::GenericArgsRef<'tcx>> {
if self.ambient_variance == ty::Variance::Invariant {
// Avoid fetching the variance if we are in an invariant
// context; no need, and it can induce dependency cycles
// (e.g., #41849).
relate_args_invariantly(self, a_arg, b_arg)
} else {
let tcx = self.tcx();
let opt_variances = tcx.variances_of(item_def_id);
relate_args_with_variances(self, item_def_id, opt_variances, a_arg, b_arg, false)
}
}

fn relate_with_variance<T: Relate<'tcx>>(
&mut self,
variance: ty::Variance,
Expand Down

0 comments on commit b4af5d3

Please sign in to comment.