Skip to content

Commit dc2ae75

Browse files
committed
make values array lazy when not static
1 parent 5dfcd6d commit dc2ae75

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

compiler/src/dotty/tools/dotc/ast/DesugarEnums.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ object DesugarEnums {
117117
val rawEnumClassRef = rawRef(enumClass.typeRef)
118118
extension (tpe: NamedType) def ofRawEnum = AppliedTypeTree(ref(tpe), rawEnumClassRef)
119119

120+
val lazyFlagOpt = if enumCompanion.owner.isStatic then EmptyFlags else Lazy
120121
val privateValuesDef = ValDef(nme.DOLLAR_VALUES, TypeTree(), ArrayLiteral(enumValues, rawEnumClassRef))
121-
.withFlags(Private | Synthetic)
122+
.withFlags(Private | Synthetic | lazyFlagOpt)
122123

123124
val valuesDef =
124125
DefDef(nme.values, Nil, Nil, defn.ArrayType.ofRawEnum, valuesDot(nme.clone_))

tests/run/enums-thunk.scala

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Outer {
2+
val thunk = { () =>
3+
enum E { case A1 }
4+
E.A1
5+
}
6+
val thunk2 = { () =>
7+
enum E { case A2 }
8+
E.values
9+
}
10+
}
11+
12+
object Outer2 {
13+
val thunk = { () =>
14+
enum E { case B1 }
15+
E.B1
16+
}
17+
val thunk2 = { () =>
18+
enum E { case B2 }
19+
E.values
20+
}
21+
}
22+
23+
@main def Test =
24+
assert(Outer().thunk().toString == "A1")
25+
assert(Outer().thunk2()(0).toString == "A2")
26+
assert(Outer2.thunk().toString == "B1")
27+
assert(Outer2.thunk2()(0).toString == "B2")

0 commit comments

Comments
 (0)