Skip to content

Commit 7645000

Browse files
committed
Fix #4323: allow narrowing of class type parameters in pattern match
1 parent 78ab82d commit 7645000

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ class Typer extends Namer
984984
val accu = new TypeAccumulator[Set[Symbol]] {
985985
def apply(tsyms: Set[Symbol], t: Type): Set[Symbol] = {
986986
val tsyms1 = t match {
987-
case tr: TypeRef if (tr.symbol is TypeParam) && tr.symbol.owner.isTerm && variance == 0 =>
987+
case tr: TypeRef if (tr.symbol is TypeParam) && variance == 0 =>
988988
tsyms + tr.symbol
989989
case _ =>
990990
tsyms

tests/pos/i4323.scala

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
enum Expr[T] {
2+
case IExpr(value: Int) extends Expr[Int]
3+
case BExpr(value: Boolean) extends Expr[Boolean]
4+
5+
def join(other: Expr[T]): Expr[T] = (this, other) match {
6+
case (IExpr(i1), IExpr(i2)) => IExpr(i1 + i2)
7+
case (BExpr(b1), BExpr(b2)) => BExpr(b1 & b2)
8+
}
9+
}

tests/pos/i4323b.scala

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
sealed trait Expr[T] {
2+
import Expr._
3+
4+
def join(other: Expr[T]): Expr[T] = (this, other) match {
5+
case (IExpr(i1), IExpr(i2)) => IExpr(i1 + i2)
6+
case (BExpr(b1), BExpr(b2)) => BExpr(b1 & b2)
7+
}
8+
}
9+
10+
object Expr {
11+
case class IExpr(value: Int) extends Expr[Int]
12+
case class BExpr(value: Boolean) extends Expr[Boolean]
13+
}

tests/pos/i4323c.scala

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
sealed trait Expr[T] { outer =>
2+
class Inner {
3+
def join(other: Expr[T]): Expr[T] = (outer, other) match {
4+
case (IExpr(i1), IExpr(i2)) => IExpr(i1 + i2)
5+
case (BExpr(b1), BExpr(b2)) => BExpr(b1 & b2)
6+
}
7+
}
8+
}
9+
10+
case class IExpr(value: Int) extends Expr[Int]
11+
case class BExpr(value: Boolean) extends Expr[Boolean]

0 commit comments

Comments
 (0)