Skip to content

fix #11764: generic sums can have no children #15010

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/SymUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ object SymUtils:
s"it is not a sealed ${self.kindString}"
else if (!self.isOneOf(AbstractOrTrait))
s"it is not an abstract class"
else if self.is(CaseClass) then "it is a sealed abstract case class"
else {
val children = self.children
val companionMirror = self.useCompanionAsSumMirror
Expand All @@ -169,8 +170,7 @@ object SymUtils:
} else i"its child $child is not a generic product because $s"
}
}
if (children.isEmpty) "it does not have subclasses"
else children.map(problem).find(!_.isEmpty).getOrElse("")
children.map(problem).find(!_.isEmpty).getOrElse("")
}

def isGenericSum(declScope: Symbol)(using Context): Boolean = whyNotGenericSum(declScope).isEmpty
Expand Down
18 changes: 11 additions & 7 deletions compiler/src/dotty/tools/dotc/transform/SyntheticMembers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -529,13 +529,17 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
def ordinalBody(cls: Symbol, param: Tree)(using Context): Tree =
if (cls.is(Enum)) param.select(nme.ordinal).ensureApplied
else {
val cases =
for ((child, idx) <- cls.children.zipWithIndex) yield {
val patType = if (child.isTerm) child.reachableTermRef else child.reachableRawTypeRef
val pat = Typed(untpd.Ident(nme.WILDCARD).withType(patType), TypeTree(patType))
CaseDef(pat, EmptyTree, Literal(Constant(idx)))
}
Match(param.annotated(New(defn.UncheckedAnnot.typeRef, Nil)), cases)
val children = cls.children
if children.isEmpty then
Throw(New(defn.MatchErrorClass.typeRef, param :: Nil))
else
val cases =
for (child, idx) <- children.zipWithIndex yield
val patType = if child.isTerm then child.reachableTermRef else child.reachableRawTypeRef
val pat = Typed(untpd.Ident(nme.WILDCARD).withType(patType), TypeTree(patType))
CaseDef(pat, EmptyTree, Literal(Constant(idx)))
end for
Match(param.annotated(New(defn.UncheckedAnnot.typeRef, Nil)), cases)
}

/** - If `impl` is the companion of a generic sum, add `deriving.Mirror.Sum` parent
Expand Down
12 changes: 12 additions & 0 deletions tests/neg/i11764.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Error: tests/neg/i11764.scala:20:38 ---------------------------------------------------------------------------------
20 | val mFoo = summon[Mirror.SumOf[Foo]] // error
| ^
|no given instance of type deriving.Mirror.SumOf[Foo] was found for parameter x of method summon in object Predef
-- Error: tests/neg/i11764.scala:22:38 ---------------------------------------------------------------------------------
22 | val mBar = summon[Mirror.SumOf[Bar]] // error
| ^
|no given instance of type deriving.Mirror.SumOf[Bar] was found for parameter x of method summon in object Predef
-- Error: tests/neg/i11764.scala:23:42 ---------------------------------------------------------------------------------
23 | val mBaz = summon[Mirror.ProductOf[Bar]] // error
| ^
|no given instance of type deriving.Mirror.ProductOf[Bar] was found for parameter x of method summon in object Predef
23 changes: 23 additions & 0 deletions tests/neg/i11764.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import deriving.Mirror

// a sealed trait with inaccessible children should
// still not be a generic sum.
sealed trait Foo
object Foo {
def local =
case class Local() extends Foo
}

// in `3.0-3.1`, sealed abstract case class was neither a
// generic sum type (because it can't have `case` children),
// or a generic product type (because we can't call the ctor),
// but now sum types can have no children,
// so we must restrict against case classes.
sealed abstract case class Bar()

def foo =

val mFoo = summon[Mirror.SumOf[Foo]] // error

val mBar = summon[Mirror.SumOf[Bar]] // error
val mBaz = summon[Mirror.ProductOf[Bar]] // error
15 changes: 15 additions & 0 deletions tests/run/i11764.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import deriving.Mirror

sealed trait Foo
object Foo

sealed trait Bar


@main def Test =
val mFoo = summon[Mirror.SumOf[Foo]]
assert(mFoo eq Foo)
summon[mFoo.MirroredElemTypes =:= EmptyTuple]

val mBar = summon[Mirror.SumOf[Bar]] // also check anonymous case
summon[mBar.MirroredElemTypes =:= EmptyTuple]