Skip to content

Don't generate Mirror.Sum for simple enum cases #14525

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
merged 4 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Synthesizer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
val cls = mirroredType.classSymbol
val useCompanion = cls.useCompanionAsSumMirror

if cls.isGenericSum(if useCompanion then cls.linkedClass else ctx.owner) then
if (!mirroredType.termSymbol.isEnumCase && (cls.isGenericSum(if useCompanion then cls.linkedClass else ctx.owner))) then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isGenericSum is used in one more place and I wonder if it should be modified as well.

Copy link
Contributor Author

@andrzejressel andrzejressel Feb 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In SyntheticMembers::addMirrorSupport? It's not an issue, because this method will never be called: as far as I understand branch else if (impl.removeAttachment(ExtendsSingletonMirror).isDefined) will be invoked.

Copy link
Member

@bishabosha bishabosha Feb 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's fine to keep isGenericSum implementation unchanged, it should only be called on a class symbol, perhaps it could be more informative to rename to isGenericSumClass, along with isGenericProduct to isGenericProductClass

val elemLabels = cls.children.map(c => ConstantType(Constant(c.name.toString)))

def solve(sym: Symbol): Type = sym match
Expand Down
15 changes: 15 additions & 0 deletions tests/neg/scala_enum_testing.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import scala.util.NotGiven
import scala.compiletime.*
import scala.deriving.Mirror

enum Color:
case Red, Green, Blue
end Color

object Test {

def main(args: Array[String]): Unit = {
summon[Mirror.ProductOf[Color]] // error
summon[Mirror.SumOf[Color.Red.type]] // error
}
}
17 changes: 17 additions & 0 deletions tests/run/scala_enum_testing.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import scala.util.NotGiven
import scala.compiletime.*
import scala.deriving.Mirror

enum Color:
case Red, Green, Blue
end Color

object Test {

def main(args: Array[String]): Unit = {
summon[Mirror.Of[Color.Red.type]]
summon[Mirror.Of[Color]]
summon[Mirror.ProductOf[Color.Red.type]]
summon[Mirror.SumOf[Color]]
}
}