Skip to content

Commit 2a05b8d

Browse files
authored
Merge pull request #8445 from dotty-staging/fix-#6601
Fix #6601: Check that an enum case extends its enum class
2 parents c5a3b5c + 9a157fb commit 2a05b8d

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
253253
cpy.Inlined(tree)(callTrace, transformSub(bindings), transform(expansion)(inlineContext(call)))
254254
case templ: Template =>
255255
withNoCheckNews(templ.parents.flatMap(newPart)) {
256+
Checking.checkEnumParentOK(templ.symbol.owner)
256257
forwardParamAccessors(templ)
257258
synthMbr.addSyntheticMembers(
258259
superAcc.wrapTemplate(templ)(

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,17 @@ object Checking {
592592
stats.foreach(checkValueClassMember)
593593
}
594594
}
595+
596+
/** Check that an enum case extends its enum class */
597+
def checkEnumParentOK(cls: Symbol)(using ctx: Context): Unit =
598+
val enumCase =
599+
if cls.isAllOf(EnumCase) then cls
600+
else if cls.isAnonymousClass && cls.owner.isAllOf(EnumCase) then cls.owner
601+
else NoSymbol
602+
if enumCase.exists then
603+
val enumCls = enumCase.owner.linkedClass
604+
if !cls.info.parents.exists(_.typeSymbol == enumCls) then
605+
ctx.error(i"enum case does not extend its enum $enumCls", enumCase.sourcePos)
595606
}
596607

597608
trait Checking {

docs/docs/internals/syntax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ yield
105105

106106
```
107107
as derives extension inline on opaque open using
108-
~ * | & + -
108+
* + -
109109
```
110110

111111
## Context-free Syntax

docs/docs/reference/enums/desugarEnums.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ Cases such as `case C` expand to a `@static val` as opposed to a `val`. This all
196196

197197
### Other Rules
198198

199-
A normal case class which is not produced from an enum case is not allowed to extend
199+
- A normal case class which is not produced from an enum case is not allowed to extend
200200
`scala.Enum`. This ensures that the only cases of an enum are the ones that are
201201
explicitly declared in it.
202+
203+
- If an enum case has an extends clause, the enum class must be one of the
204+
classes that's extended.

tests/neg/i6601.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
object GADTs2 {
2+
enum Var[G, A] {
3+
case Z[A, G]() extends Expr[(A, G), A] // error
4+
case X extends AnyRef // error
5+
}
6+
enum Expr[G, A] {
7+
case Lit[G](n: Int) extends Expr[G, Int]
8+
// case S[A, G](x:
9+
}
10+
}

0 commit comments

Comments
 (0)