Skip to content

Commit ba70024

Browse files
committed
refactor condition
1 parent c53140a commit ba70024

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,25 +109,26 @@ object RefChecks {
109109
for (reqd <- cinfo.cls.givenSelfType.classSymbols)
110110
checkSelfConforms(reqd, "missing requirement", "required")
111111

112-
def illegalEnumFlags = !cls.isOneOf(Enum | Trait)
113-
def isJavaEnum = parents.exists(_.classSymbol == defn.JavaEnumClass)
112+
def isClassExtendingJavaEnum =
113+
!cls.isOneOf(Enum | Trait) && parents.exists(_.classSymbol == defn.JavaEnumClass)
114114

115115
// Prevent wrong `extends` of java.lang.Enum
116-
if !migrateTo3 && illegalEnumFlags && isJavaEnum then
117-
report.error(CannotExtendJavaEnum(cls), cls.sourcePos)
118-
else if illegalEnumFlags && isJavaEnum then
119-
val javaEnumCtor = defn.JavaEnumClass.primaryConstructor
120-
parentTrees.exists(parent =>
121-
parent.tpe.typeSymbol == defn.JavaEnumClass
122-
&& (
123-
parent match
124-
case tpd.Apply(tpd.TypeApply(fn, _), _) if fn.tpe.termSymbol eq javaEnumCtor =>
125-
// here we are simulating the error for missing arguments to a constructor.
126-
report.error(JavaEnumParentArgs(parent.tpe), cls.sourcePos)
127-
true
128-
case _ =>
129-
false
130-
))
116+
if isClassExtendingJavaEnum then
117+
if !migrateTo3 then // always error, only traits or enum-syntax is possible under scala 3.x
118+
report.error(CannotExtendJavaEnum(cls), cls.sourcePos)
119+
else
120+
// conditionally error, we allow classes to extend java.lang.Enum in scala 2 migration mode,
121+
// however the no-arg constructor is forbidden, we must look at the parent trees to see
122+
// which overload is called.
123+
val javaEnumCtor = defn.JavaEnumClass.primaryConstructor
124+
parentTrees.exists {
125+
case parent @ tpd.Apply(tpd.TypeApply(fn, _), _) if fn.tpe.termSymbol eq javaEnumCtor =>
126+
// here we are simulating the error for missing arguments to a constructor.
127+
report.error(JavaEnumParentArgs(parent.tpe), cls.sourcePos)
128+
true
129+
case _ =>
130+
false
131+
}
131132

132133
case _ =>
133134
}

tests/neg/extend-java-enum-migration.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@
1010
14 |class Sub extends T // error
1111
| ^
1212
| not enough arguments for constructor Enum: (name: String, ordinal: Int): Enum[T]
13+
-- [E160] Type Error: tests/neg/extend-java-enum-migration.scala:17:10 -------------------------------------------------
14+
17 |val foo = new java.lang.Enum[Color] {} // error
15+
| ^
16+
| not enough arguments for constructor Enum: (name: String, ordinal: Int): Enum[Color]

tests/neg/extend-java-enum-migration.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ object O extends jl.Enum[O.type] // error
1212

1313
trait T extends jl.Enum[T] // ok
1414
class Sub extends T // error
15+
16+
abstract class Color(name: String, ordinal: Int) extends java.lang.Enum[Color](name, ordinal) // ok
17+
val foo = new java.lang.Enum[Color] {} // error

0 commit comments

Comments
 (0)