Skip to content

Avoid inf recursion in provablyDisjointClasses #22489

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
Show file tree
Hide file tree
Changes from 2 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
11 changes: 6 additions & 5 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3211,17 +3211,18 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
cls.is(Sealed) && !cls.hasAnonymousChild

def decompose(cls: Symbol): List[Symbol] =
cls.children.flatMap { child =>
cls.children.map: 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)
child.info.classSymbol.orElse(child)
else child
.filter(child => child.exists && child != cls)

def eitherDerivesFromOther(cls1: Symbol, cls2: Symbol): Boolean =
cls1.derivesFrom(cls2) || cls2.derivesFrom(cls1)

def smallestNonTraitBase(cls: Symbol): Symbol =
cls.asClass.baseClasses.find(!_.is(Trait)).get
val classes = if cls.isClass then cls.asClass.baseClasses else cls.info.classSymbols
classes.find(!_.is(Trait)).get

if (eitherDerivesFromOther(cls1, cls2))
false
Expand Down
22 changes: 22 additions & 0 deletions tests/pos/i22266.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
sealed trait NonPolygon
sealed trait Polygon

sealed trait SymmetryAspect
sealed trait RotationalSymmetry extends SymmetryAspect
sealed trait MaybeRotationalSymmetry extends SymmetryAspect

enum Shape:
case Circle extends Shape with NonPolygon with RotationalSymmetry
case Triangle extends Shape with Polygon with MaybeRotationalSymmetry
case Square extends Shape with Polygon with RotationalSymmetry

object Shape:

def hasPolygon(
rotationalSyms: Vector[Shape & RotationalSymmetry],
maybeSyms: Vector[Shape & MaybeRotationalSymmetry]
): Boolean =
val all = rotationalSyms.concat(maybeSyms)
all.exists:
case _: Polygon => true
case _ => false
22 changes: 22 additions & 0 deletions tests/pos/i22266.unenum.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
sealed trait NonPolygon
sealed trait Polygon

sealed trait SymmetryAspect
sealed trait RotationalSymmetry extends SymmetryAspect
sealed trait MaybeRotationalSymmetry extends SymmetryAspect

sealed abstract class Shape

object Shape:
case object Circle extends Shape with NonPolygon with RotationalSymmetry
case object Triangle extends Shape with Polygon with MaybeRotationalSymmetry
case object Square extends Shape with Polygon with RotationalSymmetry

def hasPolygon(
rotationalSyms: Vector[Shape & RotationalSymmetry],
maybeSyms: Vector[Shape & MaybeRotationalSymmetry]
): Boolean =
val all = rotationalSyms.concat(maybeSyms)
all.exists:
case _: Polygon => true
case _ => false
2 changes: 1 addition & 1 deletion tests/warn/i21860.unenum.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ sealed trait Corners { self: Figure => }

sealed abstract class Shape extends Figure
object Shape:
case object Triange extends Shape with Corners
case object Triangle extends Shape with Corners
case object Square extends Shape with Corners
case object Circle extends Shape
case object Ellipsis extends Shape
Expand Down
Loading