Commit 4bc8cb7
committed
Fix Java enum forwarders to pull from companion on initialization
Fixes #12637
Previously, Java enum forwarders were initialized to null when the companion's static initializer was triggered before the Java enum forwarder class's static initializer.
For example, when `enum Testme extends java.lang.Enum[Testme]` is accessed from Scala (which accesses `Testme$.Hello`):
- `Testme$.<clinit>` is triggered
- The static initializer creates enum values by calling `Testme$.new(...)`
- It constructs `Testme$$anon$1` (which represents `Hello`), a subtype of `Testme`
- Therefore, `Testme.<clinit>` is triggered
- `Testme.<clinit>` tries to initialize its `Testme.Hello` field by pulling from `Testme$.Hello`
- However, it's still null during the companion's static initialization!
See: #12637 (comment)
```scala
// Testme.scala
object TestenumS:
def go() = println("Scala: Testme Hello= " + Testme.Hello)
enum Testme extends java.lang.Enum[Testme]:
case Hello
// TestenumJ.java
public class TestenumJ {
public static void main(String[] args) {
TestenumS.go();
System.out.println("Java: Testme Hello= " + Testme.Hello);
}
}
```
This commit fixes the initialization problem by having the companion object's static initializer push enum values to the forwarders after it finishes initializing the enum value fields.
**When the companion is accessed first:**
- Companion's `<clinit>` runs and creates enum values
- During initialization, the forwarder's `<clinit>` is triggered
- Forwarders pull from the companion (value will be null)
- Companion's `<clinit>` pushes final values to forwarders at the end
**When the forwarder is accessed first:**
- Enum class's `<clinit>` tries to initialize the forwarder via `getstatic` from the companion
- This triggers the companion's `<clinit>` first
- Companion's `<clinit>` pushes values to the forwarders
- The original `putstatic` completes (resulting in double assignment, but with the correct value)
**Drawbacks:**
- We assign the forwarder field twice, making it slightly slower than before
- **We changed the Java enum forwarder fields to be non-final**1 parent 48cad7c commit 4bc8cb7
File tree
1 file changed
+41
-3
lines changed- compiler/src/dotty/tools/dotc/transform
1 file changed
+41
-3
lines changedLines changed: 41 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
106 | 106 | | |
107 | 107 | | |
108 | 108 | | |
109 | | - | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
110 | 112 | | |
111 | 113 | | |
112 | 114 | | |
| |||
119 | 121 | | |
120 | 122 | | |
121 | 123 | | |
122 | | - | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
123 | 127 | | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
124 | 154 | | |
125 | 155 | | |
126 | 156 | | |
| |||
129 | 159 | | |
130 | 160 | | |
131 | 161 | | |
| 162 | + | |
132 | 163 | | |
133 | 164 | | |
134 | 165 | | |
| |||
181 | 212 | | |
182 | 213 | | |
183 | 214 | | |
184 | | - | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
185 | 222 | | |
186 | 223 | | |
187 | 224 | | |
188 | 225 | | |
189 | 226 | | |
| 227 | + | |
190 | 228 | | |
0 commit comments