Skip to content

Consider GADT upper bounds when upcasting the scrutinee type #15351

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 2 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 21 additions & 6 deletions compiler/src/dotty/tools/dotc/core/PatternTypeConstrainer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,28 @@ trait PatternTypeConstrainer { self: TypeComparer =>
val andType = buildAndType(baseClasses)
!andType.exists || constrainPatternType(pat, andType)
case _ =>
val upcasted: Type = scrut match {
case scrut: TypeProxy => scrut.superType
case _ => NoType
def tryGadtBounds = scrut match {
case scrut: TypeRef =>
ctx.gadt.bounds(scrut.symbol) match {
case tb: TypeBounds =>
val hi = tb.hi
constrainPatternType(pat, hi)
case null => false
}
case _ => false
}
if (upcasted.exists)
tryConstrainSimplePatternType(pat, upcasted) || constrainUpcasted(upcasted)
else true

def trySuperType =
val upcasted: Type = scrut match {
case scrut: TypeProxy =>
scrut.superType
case _ => NoType
}
if (upcasted.exists)
tryConstrainSimplePatternType(pat, upcasted) || constrainUpcasted(upcasted)
else true

tryGadtBounds || trySuperType
}
}

Expand Down
21 changes: 21 additions & 0 deletions tests/pos/i15274.orig.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
enum Format[A]:
case Str[Next](next: Format[Next]) extends Format[(String, Next)]
case Num[Next](next: Format[Next]) extends Format[(Int, Next)]
case Constant[Next](value: String, next: Format[Next]) extends Format[Next]
case Done extends Format[Unit]

def printf[A](format: Format[A], params: A): Unit = (format, params) match
case (Format.Done, ()) =>
()

case (Format.Constant(value, next), params) =>
println(value)
printf(next, params)

case (Format.Str(next), (str, rest)) =>
println(str)
printf(next, rest)

case (Format.Num(next), (i, rest)) =>
println(i)
printf(next, rest)
6 changes: 6 additions & 0 deletions tests/pos/i15274.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
enum Format[A]:
case Str[Next](next: Format[Next]) extends Format[(String, Next)]

def printf[A](format: Format[A], params: A): Unit = (format, params) match
case (Format.Str(next), (str, rest)) =>
val s: String = str