From b0e4ca03c4a4cb2f74323a0b4e679171c667a738 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 14 Dec 2022 17:41:51 +0000 Subject: [PATCH] Freeze constraints in a condition check of maximiseType Not doing so, in a context where GADT inferrence is enabled, such as in TypeOps.refineUsingParent, leads to false inferrences. Doing so, and removing the previous fix for #15967, fixes the regression in #16339, and keeps #15967 as well as #16123 (which is somewhat related) fixed. --- compiler/src/dotty/tools/dotc/core/TypeOps.scala | 4 ---- compiler/src/dotty/tools/dotc/typer/Inferencing.scala | 2 +- tests/pos/i16339.scala | 7 +++++++ 3 files changed, 8 insertions(+), 5 deletions(-) create mode 100644 tests/pos/i16339.scala diff --git a/compiler/src/dotty/tools/dotc/core/TypeOps.scala b/compiler/src/dotty/tools/dotc/core/TypeOps.scala index 6d9e20412f4f..ffcdcbe9e869 100644 --- a/compiler/src/dotty/tools/dotc/core/TypeOps.scala +++ b/compiler/src/dotty/tools/dotc/core/TypeOps.scala @@ -881,10 +881,6 @@ object TypeOps: } def instantiate(): Type = { - // if there's a change in variance in type parameters (between subtype tp1 and supertype tp2) - // then we don't want to maximise the type variables in the wrong direction. - // For instance 15967, A[-Z] and B[Y] extends A[Y], we don't want to maximise Y to Any - maximizeType(protoTp1.baseType(tp2.classSymbol), NoSpan) maximizeType(protoTp1, NoSpan) wildApprox(protoTp1) } diff --git a/compiler/src/dotty/tools/dotc/typer/Inferencing.scala b/compiler/src/dotty/tools/dotc/typer/Inferencing.scala index 9d2db773c4d4..656a1266d5ab 100644 --- a/compiler/src/dotty/tools/dotc/typer/Inferencing.scala +++ b/compiler/src/dotty/tools/dotc/typer/Inferencing.scala @@ -417,7 +417,7 @@ object Inferencing { if safeToInstantiate then tvar.instantiate(fromBelow = v == -1) else { val bounds = TypeComparer.fullBounds(tvar.origin) - if bounds.hi <:< bounds.lo || bounds.hi.classSymbol.is(Final) then + if (bounds.hi frozen_<:< bounds.lo) || bounds.hi.classSymbol.is(Final) then tvar.instantiate(fromBelow = false) else { // We do not add the created symbols to GADT constraint immediately, since they may have inter-dependencies. diff --git a/tests/pos/i16339.scala b/tests/pos/i16339.scala new file mode 100644 index 000000000000..72582b778193 --- /dev/null +++ b/tests/pos/i16339.scala @@ -0,0 +1,7 @@ +// scalac: -Werror +sealed trait Get[X, +X2 <: X] +case class Bar[Y, Y2 <: Y](value: Y2) extends Get[Y, Y2] + +class Test: + def t1[Z, Z2 <: Z](get: Get[Z, Z2]) = get match + case Bar(_) =>