Skip to content

Commit

Permalink
Fix provablyDisjoint handling enum constants with mixins (scala#21876)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjrd authored Nov 2, 2024
2 parents d9d1047 + 19c945b commit 3e9b248
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
7 changes: 4 additions & 3 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3196,9 +3196,10 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
cls.is(Sealed) && !cls.hasAnonymousChild

def decompose(cls: Symbol): List[Symbol] =
cls.children.map { child =>
if child.isTerm then child.info.classSymbol
else child
cls.children.flatMap { child =>
if child.isTerm then
child.info.classSymbols // allow enum vals to be decomposed to their enum class (then filtered out) and any mixins
else child :: Nil
}.filter(child => child.exists && child != cls)

def eitherDerivesFromOther(cls1: Symbol, cls2: Symbol): Boolean =
Expand Down
16 changes: 16 additions & 0 deletions tests/warn/i21860.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
trait Figure
sealed trait Corners { self: Figure => }

enum Shape extends Figure:
case Triangle extends Shape with Corners
case Square extends Shape with Corners
case Circle extends Shape
case Ellipsis extends Shape

def hasCorners(s: Shape): Boolean = s match
case hasCorners: Corners => true // <--- reported as `Unreachable case`
case _ => false

class Test:
def test(): Unit =
println(hasCorners(Shape.Circle))
17 changes: 17 additions & 0 deletions tests/warn/i21860.unenum.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
trait Figure
sealed trait Corners { self: Figure => }

sealed abstract class Shape extends Figure
object Shape:
case object Triange extends Shape with Corners
case object Square extends Shape with Corners
case object Circle extends Shape
case object Ellipsis extends Shape

def hasCorners(s: Shape): Boolean = s match
case hasCorners: Corners => true // <--- reported as `Unreachable case`
case _ => false

class Test:
def test(): Unit =
println(hasCorners(Shape.Circle))

0 comments on commit 3e9b248

Please sign in to comment.