Skip to content

generalize: track relevant info in cache key #130194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions compiler/rustc_infer/src/infer/relate/generalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,11 @@ impl<'tcx> InferCtxt<'tcx> {
structurally_relate_aliases,
root_vid,
for_universe,
ambient_variance,
root_term: source_term.into(),
ambient_variance,
in_alias: false,
has_unconstrained_ty_var: false,
cache: Default::default(),
has_unconstrained_ty_var: false,
};

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

/// After we generalize this type, we are going to relate it to
/// some other type. What will be the variance at this point?
ambient_variance: ty::Variance,

/// The root term (const or type) we're generalizing. Used for cycle errors.
root_term: Term<'tcx>,

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

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

cache: SsoHashMap<(Ty<'tcx>, ty::Variance, bool), Ty<'tcx>>,

/// See the field `has_unconstrained_ty_var` in `Generalization`.
has_unconstrained_ty_var: bool,
}
Expand Down Expand Up @@ -451,7 +451,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
fn tys(&mut self, t: Ty<'tcx>, t2: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
assert_eq!(t, t2); // we are misusing TypeRelation here; both LHS and RHS ought to be ==

if let Some(&result) = self.cache.get(&t) {
if let Some(&result) = self.cache.get(&(t, self.ambient_variance, self.in_alias)) {
return Ok(result);
}

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

self.cache.insert(t, g);
self.cache.insert((t, self.ambient_variance, self.in_alias), g);
Ok(g)
}

Expand Down
6 changes: 4 additions & 2 deletions tests/ui/const-generics/occurs-check/unused-substs-3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ fn bind<T>() -> (T, [u8; 6 + 1]) {

fn main() {
let (mut t, foo) = bind();
//~^ ERROR mismatched types
//~| NOTE cyclic type

// `t` is `ty::Infer(TyVar(?1t))`
// `foo` contains `ty::Infer(TyVar(?1t))` in its substs
t = foo;
//~^ ERROR mismatched types
//~| NOTE cyclic type

}
8 changes: 3 additions & 5 deletions tests/ui/const-generics/occurs-check/unused-substs-3.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
error[E0308]: mismatched types
--> $DIR/unused-substs-3.rs:16:9
--> $DIR/unused-substs-3.rs:13:24
|
LL | t = foo;
| ^^^- help: try using a conversion method: `.to_vec()`
| |
| cyclic type of infinite size
LL | let (mut t, foo) = bind();
| ^^^^^^ cyclic type of infinite size

error: aborting due to 1 previous error

Expand Down
Loading