From 4670733010fb24dd154466a58067d21673a558e5 Mon Sep 17 00:00:00 2001
From: "Paolo G. Giarrusso"
Date: Sat, 25 Aug 2018 18:44:04 +0200
Subject: [PATCH] Fix #5008: forbid enums extending enums
Check if we inherit from scala.Enum both directly (as a result of the
desugaring) and indirectly.
---
compiler/src/dotty/tools/dotc/typer/Typer.scala | 9 +++++++++
tests/neg/i5008.scala | 5 +++++
tests/pending/neg/i5008 | 2 --
3 files changed, 14 insertions(+), 2 deletions(-)
create mode 100644 tests/neg/i5008.scala
delete mode 100644 tests/pending/neg/i5008
diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala
index ee076403eb35..a473c4881efa 100644
--- a/compiler/src/dotty/tools/dotc/typer/Typer.scala
+++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala
@@ -1575,6 +1575,15 @@ class Typer extends Namer
if (!cls.is(AbstractOrTrait) && !ctx.isAfterTyper)
checkRealizableBounds(cls, cdef.namePos)
if (cls.is(Case) && cls.derivesFrom(defn.EnumClass)) checkEnum(cdef, cls)
+ if (seenParents.contains(defn.EnumClass)) {
+ // Since enums are classes and Namer checks that classes don't extend multiple classes, we only check the class
+ // parent.
+ val firstParent = parents1.head.tpe.dealias.typeSymbol
+ if (firstParent.derivesFrom(defn.EnumClass))
+ //Tricky to phrase; language taken from "case-to-case inheritance is prohibited".
+ ctx.error(s"Enum ${cls.name} has enum ancestor ${firstParent.name}, but enum-to-enum inheritance is prohibited", cdef.pos)
+ }
+
val cdef1 = assignType(cpy.TypeDef(cdef)(name, impl1), cls)
checkVariance(cdef1)
if (ctx.phase.isTyper && cdef1.tpe.derivesFrom(defn.DynamicClass) && !ctx.dynamicsEnabled) {
diff --git a/tests/neg/i5008.scala b/tests/neg/i5008.scala
new file mode 100644
index 000000000000..2c856a07c334
--- /dev/null
+++ b/tests/neg/i5008.scala
@@ -0,0 +1,5 @@
+enum Foo { case A }
+enum Bar { case A }
+enum Baz extends Foo { case Z } // error
+
+enum Quux extends Foo with Bar { case Z } // error
diff --git a/tests/pending/neg/i5008 b/tests/pending/neg/i5008
deleted file mode 100644
index 2dc75e6b199c..000000000000
--- a/tests/pending/neg/i5008
+++ /dev/null
@@ -1,2 +0,0 @@
-enum Foo {}
-enum Bar extends Foo {} // error