Skip to content

Commit 2e3581b

Browse files
committed
Auto merge of #121864 - compiler-errors:type-relating-variances, r=aliemjay
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
2 parents 4cdd205 + 003b920 commit 2e3581b

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use crate::infer::{
55
};
66
use crate::traits::{Obligation, PredicateObligations};
77

8-
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
8+
use rustc_middle::ty::relate::{
9+
relate_args_invariantly, relate_args_with_variances, Relate, RelateResult, TypeRelation,
10+
};
911
use rustc_middle::ty::TyVar;
1012
use rustc_middle::ty::{self, Ty, TyCtxt};
1113
use rustc_span::Span;
@@ -36,6 +38,24 @@ impl<'tcx> TypeRelation<'tcx> for TypeRelating<'_, '_, 'tcx> {
3638
self.fields.infcx.tcx
3739
}
3840

41+
fn relate_item_args(
42+
&mut self,
43+
item_def_id: rustc_hir::def_id::DefId,
44+
a_arg: ty::GenericArgsRef<'tcx>,
45+
b_arg: ty::GenericArgsRef<'tcx>,
46+
) -> RelateResult<'tcx, ty::GenericArgsRef<'tcx>> {
47+
if self.ambient_variance == ty::Variance::Invariant {
48+
// Avoid fetching the variance if we are in an invariant
49+
// context; no need, and it can induce dependency cycles
50+
// (e.g., #41849).
51+
relate_args_invariantly(self, a_arg, b_arg)
52+
} else {
53+
let tcx = self.tcx();
54+
let opt_variances = tcx.variances_of(item_def_id);
55+
relate_args_with_variances(self, item_def_id, opt_variances, a_arg, b_arg, false)
56+
}
57+
}
58+
3959
fn relate_with_variance<T: Relate<'tcx>>(
4060
&mut self,
4161
variance: ty::Variance,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//@ check-pass
2+
3+
#![feature(inherent_associated_types)]
4+
//~^ WARN the feature `inherent_associated_types` is incomplete
5+
6+
struct D<T> {
7+
a: T
8+
}
9+
10+
impl<T: Default> D<T> {
11+
type Item = T;
12+
13+
fn next() -> Self::Item {
14+
Self::Item::default()
15+
}
16+
}
17+
18+
19+
fn main() {
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
warning: the feature `inherent_associated_types` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/variance-computation-requires-equality.rs:3:12
3+
|
4+
LL | #![feature(inherent_associated_types)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #8995 <https://github.com/rust-lang/rust/issues/8995> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
warning: 1 warning emitted
11+

0 commit comments

Comments
 (0)