Skip to content

Commit

Permalink
Fix #4323: allow narrowing of class type parameters in pattern match
Browse files Browse the repository at this point in the history
  • Loading branch information
liufengyun committed Apr 20, 2018
1 parent 78ab82d commit 7645000
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ class Typer extends Namer
val accu = new TypeAccumulator[Set[Symbol]] {
def apply(tsyms: Set[Symbol], t: Type): Set[Symbol] = {
val tsyms1 = t match {
case tr: TypeRef if (tr.symbol is TypeParam) && tr.symbol.owner.isTerm && variance == 0 =>
case tr: TypeRef if (tr.symbol is TypeParam) && variance == 0 =>
tsyms + tr.symbol
case _ =>
tsyms
Expand Down
9 changes: 9 additions & 0 deletions tests/pos/i4323.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
enum Expr[T] {
case IExpr(value: Int) extends Expr[Int]
case BExpr(value: Boolean) extends Expr[Boolean]

def join(other: Expr[T]): Expr[T] = (this, other) match {
case (IExpr(i1), IExpr(i2)) => IExpr(i1 + i2)
case (BExpr(b1), BExpr(b2)) => BExpr(b1 & b2)
}
}
13 changes: 13 additions & 0 deletions tests/pos/i4323b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
sealed trait Expr[T] {
import Expr._

def join(other: Expr[T]): Expr[T] = (this, other) match {
case (IExpr(i1), IExpr(i2)) => IExpr(i1 + i2)
case (BExpr(b1), BExpr(b2)) => BExpr(b1 & b2)
}
}

object Expr {
case class IExpr(value: Int) extends Expr[Int]
case class BExpr(value: Boolean) extends Expr[Boolean]
}
11 changes: 11 additions & 0 deletions tests/pos/i4323c.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sealed trait Expr[T] { outer =>
class Inner {
def join(other: Expr[T]): Expr[T] = (outer, other) match {
case (IExpr(i1), IExpr(i2)) => IExpr(i1 + i2)
case (BExpr(b1), BExpr(b2)) => BExpr(b1 & b2)
}
}
}

case class IExpr(value: Int) extends Expr[Int]
case class BExpr(value: Boolean) extends Expr[Boolean]

0 comments on commit 7645000

Please sign in to comment.