Skip to content

Commit

Permalink
Rollup merge of rust-lang#126022 - lcnr:generalize-alias-bivariant, r…
Browse files Browse the repository at this point in the history
…=compiler-errors

set `has_unconstrained_ty_var` when generalizing aliases in bivariant contexts

this previously prevented the `regression-31157` benchmark from building

r? `@compiler-errors`
  • Loading branch information
matthiaskrgr committed Jun 5, 2024
2 parents 808ad60 + a8e091d commit dd2e1a3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
12 changes: 10 additions & 2 deletions compiler/rustc_infer/src/infer/relate/generalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,14 @@ impl<'tcx> Generalizer<'_, 'tcx> {
}
}

/// Create a new type variable in the universe of the target when
/// generalizing an alias. This has to set `has_unconstrained_ty_var`
/// if we're currently in a bivariant context.
fn next_ty_var_for_alias(&mut self) -> Ty<'tcx> {
self.has_unconstrained_ty_var |= self.ambient_variance == ty::Bivariant;
self.infcx.next_ty_var_in_universe(self.span, self.for_universe)
}

/// An occurs check failure inside of an alias does not mean
/// that the types definitely don't unify. We may be able
/// to normalize the alias after all.
Expand Down Expand Up @@ -358,7 +366,7 @@ impl<'tcx> Generalizer<'_, 'tcx> {
//
// cc trait-system-refactor-initiative#110
if self.infcx.next_trait_solver() && !alias.has_escaping_bound_vars() && !self.in_alias {
return Ok(self.infcx.next_ty_var_in_universe(self.span, self.for_universe));
return Ok(self.next_ty_var_for_alias());
}

let is_nested_alias = mem::replace(&mut self.in_alias, true);
Expand All @@ -378,7 +386,7 @@ impl<'tcx> Generalizer<'_, 'tcx> {
}

debug!("generalization failure in alias");
Ok(self.infcx.next_ty_var_in_universe(self.span, self.for_universe))
Ok(self.next_ty_var_for_alias())
}
}
};
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/traits/next-solver/generalize/bivariant-alias.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//@ revisions: old next
//@[next] compile-flags: -Znext-solver
//@ ignore-compare-mode-next-solver (explicit revisions)
//@ check-pass

// When generalizing an alias in a bivariant context, we have to set
// `has_unconstrained_ty_var` as we may otherwise never check for
// well-formedness of the generalized type, causing us to error due
// to ambiguity.
trait Trait {
type Assoc;
}

struct BivariantArg<I, T: Trait<Assoc = I>>(T);

fn generalize<T: Trait>(input: BivariantArg<T::Assoc, T>) {
let _generalized = input;
}

pub fn main() {}

0 comments on commit dd2e1a3

Please sign in to comment.