Skip to content

Commit 639d8e5

Browse files
committed
fix sealedStrictDescendants for Java enum
- Rename JavaEnumTrait flags to JavaEnum (the actual flags set) - test java enum in SealedDescendantsTest
1 parent 476a617 commit 639d8e5

File tree

10 files changed

+41
-22
lines changed

10 files changed

+41
-22
lines changed

compiler/src/dotty/tools/backend/jvm/BCodeHelpers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ trait BCodeHelpers extends BCodeIdiomatic {
292292
}
293293
case Ident(nme.WILDCARD) =>
294294
// An underscore argument indicates that we want to use the default value for this parameter, so do not emit anything
295-
case t: tpd.RefTree if t.symbol.owner.linkedClass.isAllOf(JavaEnumTrait) =>
295+
case t: tpd.RefTree if t.symbol.owner.linkedClass.isAllOf(JavaEnum) =>
296296
val edesc = innerClasesStore.typeDescriptor(t.tpe) // the class descriptor of the enumeration class.
297297
val evalue = t.symbol.javaSimpleName // value the actual enumeration value.
298298
av.visitEnum(name, edesc, evalue)

compiler/src/dotty/tools/backend/jvm/BTypesFromSymbols.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ class BTypesFromSymbols[I <: DottyBackendInterface](val int: I, val frontendAcce
304304
.addFlagIf(sym.is(Bridge), ACC_BRIDGE | ACC_SYNTHETIC)
305305
.addFlagIf(sym.is(Artifact), ACC_SYNTHETIC)
306306
.addFlagIf(sym.isClass && !sym.isInterface, ACC_SUPER)
307-
.addFlagIf(sym.isAllOf(JavaEnumTrait), ACC_ENUM)
307+
.addFlagIf(sym.isAllOf(JavaEnum), ACC_ENUM)
308308
.addFlagIf(sym.is(JavaVarargs), ACC_VARARGS)
309309
.addFlagIf(sym.is(Synchronized), ACC_SYNCHRONIZED)
310310
.addFlagIf(sym.isDeprecated, ACC_DEPRECATED)

compiler/src/dotty/tools/dotc/core/Flags.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ object Flags {
576576
val InlineMethod: FlagSet = Inline | Method
577577
val InlineParam: FlagSet = Inline | Param
578578
val InlineByNameProxy: FlagSet = InlineProxy | Method
579-
val JavaEnumTrait: FlagSet = JavaDefined | Enum // A Java enum trait
579+
val JavaEnum: FlagSet = JavaDefined | Enum // A Java enum trait
580580
val JavaEnumValue: FlagSet = JavaDefined | EnumValue // A Java enum value
581581
val StaticProtected: FlagSet = JavaDefined | JavaStatic | Protected // Java symbol which is `protected` and `static`
582582
val JavaModule: FlagSet = JavaDefined | Module // A Java companion object

compiler/src/dotty/tools/dotc/core/SymDenotations.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1695,7 +1695,7 @@ object SymDenotations {
16951695
c.ensureCompleted()
16961696
end completeChildrenIn
16971697

1698-
if is(Sealed) || isAllOf(JavaEnumTrait) then
1698+
if is(Sealed) || isAllOf(JavaEnum) && isClass then
16991699
if !is(ChildrenQueried) then
17001700
// Make sure all visible children are completed, so that
17011701
// they show up in Child annotations. A possible child is visible if it

compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ object JavaParsers {
992992
Select(New(javaLangDot(tpnme.Enum)), nme.CONSTRUCTOR), List(enumType)), Nil)
993993
val enumclazz = atSpan(start, nameOffset) {
994994
TypeDef(name,
995-
makeTemplate(superclazz :: interfaces, body, List(), true)).withMods(mods | Flags.JavaEnumTrait)
995+
makeTemplate(superclazz :: interfaces, body, List(), true)).withMods(mods | Flags.JavaEnum)
996996
}
997997
addCompanionObject(consts ::: statics ::: predefs, enumclazz)
998998
}
@@ -1011,7 +1011,7 @@ object JavaParsers {
10111011
skipAhead()
10121012
accept(RBRACE)
10131013
}
1014-
ValDef(name.toTermName, enumType, unimplementedExpr).withMods(Modifiers(Flags.JavaEnumTrait | Flags.StableRealizable | Flags.JavaDefined | Flags.JavaStatic))
1014+
ValDef(name.toTermName, enumType, unimplementedExpr).withMods(Modifiers(Flags.JavaEnumValue | Flags.JavaStatic))
10151015
}
10161016
}
10171017

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ object SpaceEngine {
626626
case tp if tp.isRef(defn.UnitClass) => ConstantType(Constant(())) :: Nil
627627
case tp @ NamedType(Parts(parts), _) => parts.map(tp.derivedSelect)
628628
case _: SingletonType => ListOfNoType
629-
case tp if tp.classSymbol.isAllOf(JavaEnumTrait) => tp.classSymbol.children.map(_.termRef)
629+
case tp if tp.classSymbol.isAllOf(JavaEnum) => tp.classSymbol.children.map(_.termRef)
630630
// the class of a java enum value is the enum class, so this must follow SingletonType to not loop infinitely
631631

632632
case tp @ AppliedType(Parts(parts), targs) if tp.classSymbol.children.isEmpty =>
@@ -843,7 +843,7 @@ object SpaceEngine {
843843
isCheckable(and.tp1) || isCheckable(and.tp2)
844844
}) ||
845845
tpw.isRef(defn.BooleanClass) ||
846-
classSym.isAllOf(JavaEnumTrait) ||
846+
classSym.isAllOf(JavaEnum) ||
847847
classSym.is(Case) && {
848848
if seen.add(tpw) then productSelectorTypes(tpw, sel.srcPos).exists(isCheckable(_))
849849
else true // recursive case class: return true and other members can still fail the check

compiler/test/dotty/tools/dotc/core/SealedDescendantsTest.scala

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ class SealedDescendantsTest extends DottyTest {
4747
)
4848
end enumOpt
4949

50+
@Test
51+
def javaEnum: Unit =
52+
expectedDescendents("java.util.concurrent.TimeUnit",
53+
"TimeUnit" ::
54+
"NANOSECONDS.type" ::
55+
"MICROSECONDS.type" ::
56+
"MILLISECONDS.type" ::
57+
"SECONDS.type" ::
58+
"MINUTES.type" ::
59+
"HOURS.type" ::
60+
"DAYS.type" :: Nil
61+
)
62+
5063
@Test
5164
def hierarchicalSharedChildren: Unit =
5265
// Q is a child of both Z and A and should appear once
@@ -91,10 +104,22 @@ class SealedDescendantsTest extends DottyTest {
91104
)
92105
end hierarchicalSharedChildrenB
93106

94-
def expectedDescendents(source: String, root: String, expected: List[String]) =
95-
exploreRoot(source, root) { rootCls =>
96-
val descendents = rootCls.sealedDescendants.map(sym => s"${sym.name}${if (sym.isTerm) ".type" else ""}")
97-
assertEquals(expected.toString, descendents.toString)
107+
def assertMatchingDescenants(rootCls: Symbol, expected: List[String])(using Context): Unit =
108+
val descendents = rootCls.sealedDescendants.map(sym => s"${sym.name}${if (sym.isTerm) ".type" else ""}")
109+
assertEquals(expected.toString, descendents.toString)
110+
111+
def expectedDescendents(root: String, expected: List[String]): Unit =
112+
exploreRootNoSource(root)(assertMatchingDescenants(_, expected))
113+
114+
def expectedDescendents(source: String, root: String, expected: List[String]): Unit =
115+
exploreRoot(source, root)(assertMatchingDescenants(_, expected))
116+
117+
def exploreRootNoSource(root: String)(op: Context ?=> ClassSymbol => Unit) =
118+
val source1 = s"""package testsealeddescendants
119+
|object Foo { def foo: $root = ??? }""".stripMargin
120+
checkCompile("typer", source1) { (_, context) =>
121+
given Context = context
122+
op(requiredClass(root))
98123
}
99124

100125
def exploreRoot(source: String, root: String)(op: Context ?=> ClassSymbol => Unit) =

presentation-compiler/src/main/dotty/tools/pc/completions/MatchCaseCompletions.scala

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,7 @@ object CaseKeywordCompletion:
349349
symTpe <:< tpe
350350

351351
val parents = getParentTypes(tpe, List.empty)
352-
parents.toList.map { parent =>
353-
// There is an issue in Dotty, `sealedStrictDescendants` ends in an exception for java enums. https://github.com/lampepfl/dotty/issues/15908
354-
if parent.isAllOf(JavaEnumTrait) then parent.children
355-
else sealedStrictDescendants(parent)
356-
} match
352+
parents.toList.map(sealedStrictDescendants) match
357353
case Nil => Nil
358354
case subcls :: Nil => subcls
359355
case subcls =>
@@ -409,9 +405,7 @@ class CompletionValueGenerator(
409405
Context
410406
): Option[String] =
411407
val isModuleLike =
412-
sym.is(Flags.Module) || sym.isOneOf(JavaEnumTrait) || sym.isOneOf(
413-
JavaEnumValue
414-
) || sym.isAllOf(EnumCase)
408+
sym.is(Flags.Module) || sym.isOneOf(JavaEnum) || sym.isOneOf(JavaEnumValue) || sym.isAllOf(EnumCase)
415409
if isModuleLike && hasBind then None
416410
else
417411
val pattern =

tests/pos-with-compiler-cc/backend/jvm/BCodeHelpers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ trait BCodeHelpers extends BCodeIdiomatic with BytecodeWriters {
374374
}
375375
case Ident(nme.WILDCARD) =>
376376
// An underscore argument indicates that we want to use the default value for this parameter, so do not emit anything
377-
case t: tpd.RefTree if t.symbol.owner.linkedClass.isAllOf(JavaEnumTrait) =>
377+
case t: tpd.RefTree if t.symbol.owner.linkedClass.isAllOf(JavaEnum) =>
378378
val edesc = innerClasesStore.typeDescriptor(t.tpe) // the class descriptor of the enumeration class.
379379
val evalue = t.symbol.javaSimpleName // value the actual enumeration value.
380380
av.visitEnum(name, edesc, evalue)

tests/pos-with-compiler-cc/backend/jvm/BTypesFromSymbols.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ class BTypesFromSymbols[I <: DottyBackendInterface](val int: I) extends BTypes {
330330
.addFlagIf(sym.is(Bridge), ACC_BRIDGE | ACC_SYNTHETIC)
331331
.addFlagIf(sym.is(Artifact), ACC_SYNTHETIC)
332332
.addFlagIf(sym.isClass && !sym.isInterface, ACC_SUPER)
333-
.addFlagIf(sym.isAllOf(JavaEnumTrait), ACC_ENUM)
333+
.addFlagIf(sym.isAllOf(JavaEnum), ACC_ENUM)
334334
.addFlagIf(sym.is(JavaVarargs), ACC_VARARGS)
335335
.addFlagIf(sym.is(Synchronized), ACC_SYNCHRONIZED)
336336
.addFlagIf(sym.isDeprecated, ACC_DEPRECATED)

0 commit comments

Comments
 (0)