Skip to content

Commit

Permalink
Set enum as abstract always
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Jun 7, 2023
1 parent 952c36e commit 3a608df
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/compiler/scala/tools/nsc/symtab/classfile/ClassfileParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -593,19 +593,17 @@ abstract class ClassfileParser(reader: ReusableInstance[ReusableDataReader]) {
getScope(jflags) enter sym

// sealed java enums
if (jflags.isEnum) {
val enumClass = sym.owner.linkedClassOfClass
enumClass match {
if (jflags.isEnum)
sym.owner.linkedClassOfClass match {
case NoSymbol =>
devWarning(s"no linked class for java enum $sym in ${sym.owner}. A referencing class file might be missing an InnerClasses entry.")
case linked =>
if (!linked.isSealed)
// Marking the enum class SEALED | ABSTRACT enables exhaustiveness checking. See also JavaParsers.
// This is a bit of a hack and requires excluding the ABSTRACT flag in the backend, see method javaClassfileFlags.
linked setFlag (SEALED | ABSTRACT)
linked addChild sym
case enumClass =>
// Marking the enum class SEALED | ABSTRACT enables exhaustiveness checking. See also JavaParsers.
// This is a bit of a hack and requires excluding the ABSTRACT flag in the backend, see method javaClassfileFlags.
// Java enums may be already sealed by virtue of permittedSubclasses, if an element had a body.
enumClass.setFlag(SEALED | ABSTRACT)
enumClass.addChild(sym)
}
}
}
}

Expand Down Expand Up @@ -1382,9 +1380,11 @@ abstract class ClassfileParser(reader: ReusableInstance[ReusableDataReader]) {
ClassInfoType(superTpe1 :: ifacesTypes, instanceScope, clazz)
}
sym.setInfo(info)
for (k <- permittedSubclasses)
if (k.parentSymbols.contains(sym))
sym.addChild(k)
// enum children are its enum fields, so don't register subclasses (which are listed as permitted)
if (!sym.hasJavaEnumFlag)
for (k <- permittedSubclasses)
if (k.parentSymbols.contains(sym))
sym.addChild(k)
}
}

Expand Down
7 changes: 7 additions & 0 deletions test/files/neg/t12800.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
matcher_1.scala:8: warning: match may not be exhaustive.
It would fail on the following input: ORANGE
jb match {
^
error: No warnings can be incurred under -Werror.
1 warning
1 error
16 changes: 16 additions & 0 deletions test/files/neg/t12800/JetBrains.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

public enum JetBrains {
APPLE {
public enum Leaves {
A,B,C;
}
@Override public String text() {
return "Cupertino tech company";
}
},
ORANGE
;
public String text() {
return "Boring default";
}
}
11 changes: 11 additions & 0 deletions test/files/neg/t12800/matcher_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

// scalac: -Werror -Xsource:3

import JetBrains.*

class C {
def f(jb: JetBrains): Int =
jb match {
case APPLE => 42
}
}
17 changes: 17 additions & 0 deletions test/files/pos/t12800/JetBrains.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

public enum JetBrains {
APPLE {
public enum Leaves {
A,B,C;
}
@Override public String text() {
return "Cupertino tech company";
}
},
ORANGE {
@Override public String text() {
return "SoCal county";
}
};
public abstract String text();
}
12 changes: 12 additions & 0 deletions test/files/pos/t12800/matcher_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

// scalac: -Werror -Xsource:3

import JetBrains.*

class C {
def f(jb: JetBrains): Int =
jb match {
case APPLE => 42
case ORANGE => 27
}
}

0 comments on commit 3a608df

Please sign in to comment.