@@ -109,25 +109,26 @@ object RefChecks {
109
109
for (reqd <- cinfo.cls.givenSelfType.classSymbols)
110
110
checkSelfConforms(reqd, " missing requirement" , " required" )
111
111
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 )
114
114
115
115
// 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
+ }
131
132
132
133
case _ =>
133
134
}
0 commit comments