Skip to content

Fix: Prevent GADT reasoning in pattern alternatives #22853

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
Mar 25, 2025
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
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2805,8 +2805,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
else
assert(ctx.reporter.errorsReported)
tree.withType(defn.AnyType)
val savedGadt = nestedCtx.gadt
val trees1 = tree.trees.mapconserve(typed(_, pt)(using nestedCtx))
.mapconserve(ensureValueTypeOrWildcard)
nestedCtx.gadtState.restore(savedGadt) // Disable GADT reasoning for pattern alternatives
assignType(cpy.Alternative(tree)(trees1), trees1)
}

Expand Down
9 changes: 9 additions & 0 deletions tests/neg/gadt-alternatives.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
enum Expr[+T]:
case StringVal(x: String) extends Expr[String]
case IntVal(x: Int) extends Expr[Int]
case IntValAlt(x: Int) extends Expr[Int]
import Expr.*
def eval[T](e: Expr[T]): T = e match
case StringVal(_) | IntVal(_) => "42" // error
def eval1[T](e: Expr[T]): T = e match
case IntValAlt(_) | IntVal(_) => 42 // error // limitation
Loading