Skip to content

Fix #7991: don't set JavaDefined for Dotty Enum module class #8008

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 13 commits into from
Jan 17, 2020
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class Compiler {
new CheckReentrant, // Internal use only: Check that compiled program has no data races involving global vars
new ElimPackagePrefixes, // Eliminate references to package prefixes in Select nodes
new CookComments, // Cook the comments: expand variables, doc, etc.
new CompleteJavaEnums) :: // Fill in constructors for Java enums
List(new CheckStatic, // Check restrictions that apply to @static members
new CheckStatic) :: // Check restrictions that apply to @static members
List(new CompleteJavaEnums, // Fill in constructors for Java enums
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this swap needed? Since we are adding the @static annotation in CompleteJavaEnums, should this phase not go before CheckStatic so that the latter can check the newly added static members?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

CheckStatic will reject our code, because it requires that a @static member is located in a companion object, not the class.

Copy link
Contributor

@anatoliykmetyuk anatoliykmetyuk Jan 17, 2020

Choose a reason for hiding this comment

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

Would it be a better idea to modify CheckStatic to accept the code? E.g. (from CheckStatic doc), "1. Only objects and those classes which extend java.lang.Enum can have members annotated with @static"?

The meaning of CheckStatic is to provide guarantees on @static. If there exists code that doesn't follow the existing guarantees, this complicates the codebase maintenance and usage. We have the exact same scenario with setting flags on completion: there are rules about which flags are immutable wrt completion but they are not enforced. And hence there are lots of places in the codebase that don't follow these rules.

Copy link
Contributor Author

@liufengyun liufengyun Jan 17, 2020

Choose a reason for hiding this comment

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

I have a commit message in f6f3b28: the @static infrastructure needs fundamental refactoring, but it's out of the scope of this PR.

new ElimRepeated, // Rewrite vararg parameters and arguments
new ExpandSAMs, // Expand single abstract method closures to anonymous classes
new ProtectedAccessors, // Add accessors for protected members
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ object ClassfileConstants {
case JAVA_ACC_FINAL => Final
case JAVA_ACC_SYNTHETIC => Synthetic
case JAVA_ACC_STATIC => JavaStatic
case JAVA_ACC_ENUM => Enum
case JAVA_ACC_ABSTRACT => if (isClass) Abstract else Deferred
case JAVA_ACC_INTERFACE => PureInterfaceCreationFlags | JavaDefined
case _ => EmptyFlags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,10 @@ class ClassfileParser(
sym.markAbsent()
}

// eager load java enum definitions for exhaustivity check of pattern match
// eager load enum definitions for exhaustivity check of pattern match
if (isEnum) {
instanceScope.toList.map(_.ensureCompleted())
staticScope.toList.map(_.ensureCompleted())
classRoot.setFlag(Flags.JavaEnumTrait)
moduleRoot.setFlag(Flags.JavaEnumTrait)
}

result
Expand Down
22 changes: 16 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/CompleteJavaEnums.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,20 @@ class CompleteJavaEnums extends MiniPhase with InfoTransformer { thisPhase =>
else tree
}

override def transformValDef(tree: ValDef)(implicit ctx: Context): ValDef = {
val sym = tree.symbol
if ((sym.isAllOf(EnumValue) || sym.name == nme.DOLLAR_VALUES) && sym.owner.linkedClass.derivesFromJavaEnum)
sym.addAnnotation(Annotations.Annotation(defn.ScalaStaticAnnot))
tree
/** Return a list of forwarders for enum values defined in the companion object
* for java interop.
*/
private def addedEnumForwarders(clazz: Symbol)(implicit ctx: Context): List[ValDef] = {
val moduleCls = clazz.companionClass
val moduleRef = ref(clazz.companionModule)

val enums = moduleCls.info.decls.filter(member => member.isAllOf(EnumValue))
for { enumValue <- enums }
yield {
val fieldSym = ctx.newSymbol(clazz, enumValue.name.asTermName, EnumValue | JavaStatic, enumValue.info)
fieldSym.addAnnotation(Annotations.Annotation(defn.ScalaStaticAnnot))
ValDef(fieldSym, moduleRef.select(enumValue))
}
}

/** 1. If this is an enum class, add $name and $ordinal parameters to its
Expand Down Expand Up @@ -143,9 +152,10 @@ class CompleteJavaEnums extends MiniPhase with InfoTransformer { thisPhase =>
val (params, rest) = decomposeTemplateBody(templ.body)
val addedDefs = addedParams(cls, ParamAccessor)
val addedSyms = addedDefs.map(_.symbol.entered)
val addedForwarders = addedEnumForwarders(cls)
cpy.Template(templ)(
parents = addEnumConstrArgs(defn.JavaEnumClass, templ.parents, addedSyms.map(ref)),
body = params ++ addedDefs ++ rest)
body = params ++ addedDefs ++ addedForwarders ++ rest)
}
else if (cls.isAnonymousClass && cls.owner.isAllOf(EnumCase) && cls.owner.owner.linkedClass.derivesFromJavaEnum) {
def rhsOf(name: TermName) =
Expand Down
4 changes: 4 additions & 0 deletions compiler/test-resources/repl/i7410
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
scala> enum E { case A, B, C }
// defined class E
scala> E.A
val res0: E = A
4 changes: 4 additions & 0 deletions tests/run/i6664/Bomb_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
enum Bomb
{
case Kaboom
}
5 changes: 5 additions & 0 deletions tests/run/i6664/Detonator_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object Test
{
def boom: Unit = Bomb.valueOf("Kaboom")
def main(args: Array[String]): Unit = println("ok!")
}
4 changes: 4 additions & 0 deletions tests/run/i6664b/Bomb_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
enum Bomb extends java.lang.Enum[Bomb]
{
case Kaboom
}
4 changes: 4 additions & 0 deletions tests/run/i6664b/Detonator_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object Test
{
def main(args: Array[String]): Unit = println(Bomb.Kaboom)
}
3 changes: 3 additions & 0 deletions tests/run/i6677/Enum_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
enum Foo[A] {
case Bar extends Foo[Int]
}
5 changes: 5 additions & 0 deletions tests/run/i6677/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object Test {
def main(args: Array[String]): Unit = {
println(Foo.Bar)
}
}
5 changes: 5 additions & 0 deletions tests/run/i7287/Enum_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enum Color derives Eql {
case Unknown
case Blue(v: Int)
case Red(v: Int)
}
6 changes: 6 additions & 0 deletions tests/run/i7287/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
object Test {
def main(args: Array[String]): Unit = {
val testData = Seq(("blah", Color.Unknown), ("red", Color.Red(10)))
println(testData)
}
}
3 changes: 3 additions & 0 deletions tests/run/i7410/Enum_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
enum E {
case A, B, C
}
6 changes: 6 additions & 0 deletions tests/run/i7410/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
object Test {
def main(args: Array[String]): Unit = {
val a = E.A
println(a)
}
}
3 changes: 3 additions & 0 deletions tests/run/i7424/Enum_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
enum A {
case A1
}
5 changes: 5 additions & 0 deletions tests/run/i7424/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object Test {
def main(args: Array[String]): Unit = {
println(A.values)
}
}
5 changes: 5 additions & 0 deletions tests/run/i7991.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enum Num { case One }

object Test extends App {
Num.One
}
1 change: 1 addition & 0 deletions tests/run/i7991/Num_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enum Num { case One }
3 changes: 3 additions & 0 deletions tests/run/i7991/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object Test extends App {
Num.One
}