Skip to content
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

Allow refineUsingParent to infer GADT bounds #15706

Merged
merged 3 commits into from
Jul 27, 2022
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
7 changes: 5 additions & 2 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1945,8 +1945,11 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
val tparam = tr.symbol
gadts.println(i"narrow gadt bound of $tparam: ${tparam.info} from ${if (isUpper) "above" else "below"} to $bound ${bound.toString} ${bound.isRef(tparam)}")
if (bound.isRef(tparam)) false
else if (isUpper) gadtAddUpperBound(tparam, bound)
else gadtAddLowerBound(tparam, bound)
else
val savedGadt = ctx.gadt.fresh
val success = if isUpper then gadtAddUpperBound(tparam, bound) else gadtAddLowerBound(tparam, bound)
if !success then ctx.gadt.restore(savedGadt)
success
}
}

Expand Down
9 changes: 8 additions & 1 deletion compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ object TypeOps:

val childTp = if (child.isTerm) child.termRef else child.typeRef

inContext(ctx.fresh.setExploreTyperState().setFreshGADTBounds) {
inContext(ctx.fresh.setExploreTyperState().setFreshGADTBounds.addMode(Mode.GadtConstraintInference)) {
instantiateToSubType(childTp, parent).dealias
}
}
Expand Down Expand Up @@ -829,6 +829,13 @@ object TypeOps:
val tvars = tp1.typeParams.map { tparam => newTypeVar(tparam.paramInfo.bounds) }
val protoTp1 = inferThisMap.apply(tp1).appliedTo(tvars)

val getAbstractSymbols = new TypeAccumulator[List[Symbol]]:
def apply(xs: List[Symbol], tp: Type) = tp.dealias match
case tp: TypeRef if !tp.symbol.isClass => foldOver(tp.symbol :: xs, tp)
case tp => foldOver(xs, tp)
val syms2 = getAbstractSymbols(Nil, tp2).reverse
if syms2.nonEmpty then ctx.gadt.addToConstraint(syms2)

// If parent contains a reference to an abstract type, then we should
// refine subtype checking to eliminate abstract types according to
// variance. As this logic is only needed in exhaustivity check,
Expand Down
2 changes: 1 addition & 1 deletion compiler/test/dotty/tools/vulpix/ParallelTesting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ trait ParallelTesting extends RunnerOrchestration { self =>
) extends TestSource {
def sourceFiles: Array[JFile] = files.filter(isSourceFile)

override def toString() = outDir.toString
override def toString() = sourceFiles match { case Array(f) => f.getPath case _ => outDir.getPath }
}

/** A test source whose files will be compiled separately according to their
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ object Test {
def err2[A, B](value: Foo[A, B], a: A => Int): B = value match {
case b: Bar[B] => // spurious // error
b.x
case _ => ??? // avoid fatal inexhaustivity warnings suppressing the uncheckable warning
}

def fail[A, B](value: Foo[A, B], a: A => Int): B = value match {
case b: Bar[Int] => // error
b.x
case _ => ??? // avoid fatal inexhaustivity warnings suppressing the uncheckable warning
}
}
6 changes: 6 additions & 0 deletions tests/pos/i13548.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// scalac: -Werror
sealed abstract class Foo[N, A]
final case class Bar[B](foo: Foo[B, B]) extends Foo[B, B]
class Test:
def pmat[P, C](scr: Foo[P, C]): C = scr match
case Bar(foo) => pmat(foo)
6 changes: 6 additions & 0 deletions tests/pos/i15289.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// scalac: -Werror
sealed abstract class Foo[A, B]
final case class Bar[C](baz: C) extends Foo[C, C]

class Test:
def m1[X](f1: Foo[X, String]): String = f1 match { case Bar(_) => "" }