Skip to content

Commit 5a2dd7d

Browse files
committed
Auto merge of #130194 - lcnr:generalize-cache, r=compiler-errors
generalize: track relevant info in cache key This was previously theoretically incomplete as we could incorrectly generalize as if the type was in an invariant context even though we're in a covariant one. Similar with the `in_alias` flag. r? `@compiler-errors`
2 parents 4c5fc2c + 7a57a74 commit 5a2dd7d

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,11 @@ impl<'tcx> InferCtxt<'tcx> {
259259
structurally_relate_aliases,
260260
root_vid,
261261
for_universe,
262-
ambient_variance,
263262
root_term: source_term.into(),
263+
ambient_variance,
264264
in_alias: false,
265-
has_unconstrained_ty_var: false,
266265
cache: Default::default(),
266+
has_unconstrained_ty_var: false,
267267
};
268268

269269
let value_may_be_infer = generalizer.relate(source_term, source_term)?;
@@ -304,14 +304,12 @@ struct Generalizer<'me, 'tcx> {
304304
/// we reject the relation.
305305
for_universe: ty::UniverseIndex,
306306

307-
/// After we generalize this type, we are going to relate it to
308-
/// some other type. What will be the variance at this point?
309-
ambient_variance: ty::Variance,
310-
311307
/// The root term (const or type) we're generalizing. Used for cycle errors.
312308
root_term: Term<'tcx>,
313309

314-
cache: SsoHashMap<Ty<'tcx>, Ty<'tcx>>,
310+
/// After we generalize this type, we are going to relate it to
311+
/// some other type. What will be the variance at this point?
312+
ambient_variance: ty::Variance,
315313

316314
/// This is set once we're generalizing the arguments of an alias.
317315
///
@@ -320,6 +318,8 @@ struct Generalizer<'me, 'tcx> {
320318
/// hold by either normalizing the outer or the inner associated type.
321319
in_alias: bool,
322320

321+
cache: SsoHashMap<(Ty<'tcx>, ty::Variance, bool), Ty<'tcx>>,
322+
323323
/// See the field `has_unconstrained_ty_var` in `Generalization`.
324324
has_unconstrained_ty_var: bool,
325325
}
@@ -451,7 +451,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
451451
fn tys(&mut self, t: Ty<'tcx>, t2: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
452452
assert_eq!(t, t2); // we are misusing TypeRelation here; both LHS and RHS ought to be ==
453453

454-
if let Some(&result) = self.cache.get(&t) {
454+
if let Some(&result) = self.cache.get(&(t, self.ambient_variance, self.in_alias)) {
455455
return Ok(result);
456456
}
457457

@@ -557,7 +557,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
557557
_ => relate::structurally_relate_tys(self, t, t),
558558
}?;
559559

560-
self.cache.insert(t, g);
560+
self.cache.insert((t, self.ambient_variance, self.in_alias), g);
561561
Ok(g)
562562
}
563563

tests/ui/const-generics/occurs-check/unused-substs-3.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ fn bind<T>() -> (T, [u8; 6 + 1]) {
1111

1212
fn main() {
1313
let (mut t, foo) = bind();
14+
//~^ ERROR mismatched types
15+
//~| NOTE cyclic type
16+
1417
// `t` is `ty::Infer(TyVar(?1t))`
1518
// `foo` contains `ty::Infer(TyVar(?1t))` in its substs
1619
t = foo;
17-
//~^ ERROR mismatched types
18-
//~| NOTE cyclic type
20+
1921
}

tests/ui/const-generics/occurs-check/unused-substs-3.stderr

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
error[E0308]: mismatched types
2-
--> $DIR/unused-substs-3.rs:16:9
2+
--> $DIR/unused-substs-3.rs:13:24
33
|
4-
LL | t = foo;
5-
| ^^^- help: try using a conversion method: `.to_vec()`
6-
| |
7-
| cyclic type of infinite size
4+
LL | let (mut t, foo) = bind();
5+
| ^^^^^^ cyclic type of infinite size
86

97
error: aborting due to 1 previous error
108

0 commit comments

Comments
 (0)