RFR: 8361570: Incorrect 'sealed is not allowed here' compile-time error
Jan Lahoda
jlahoda at openjdk.org
Tue Jul 8 08:03:24 UTC 2025
Consider code like this:
$ cat /tmp/T.java
import java.lang.ref.*;
public class T {
public static void main(String[] args) {
new WeakReference<>(null) {};
}
}
Compiling this with JDK 25/26 leads to:
$ ./jdk-25/bin/javac /tmp/T.java
/tmp/T.java:4: error: modifier sealed not allowed here
new WeakReference<>(null) {};
^
1 error
Which does not make much sense.
The reason for this is as follows:
- the type parameter for `WeakReference` is marked with `@jdk.internal.RequiresIdentity`, and the `WeakReference`'s constructor has a parameter whose type is this type parameter.
- as a consequence, this parameter has internally in javac flag `REQUIRES_IDENTITY`. Note this flag has currently the same `long` value as `SEALED`, as the value is reused to mean different things for different Symbol kinds.
- when creating the anonymous class, javac creates a constructor, copying the `REQUIRES_IDENTITY` together with the constructor's parameter
- then javac goes on and checks whether the flags on the field are correct. And it sees the value for `SEALED` is set, and reports the error
Ultimately, I don't think we can reuse the value of `SEALED` to mean different things (and the same for all other similar cases). This PR assigns a different value for `SEALED`, and tries to add a test that strives to hopefully prevent similar cases in the future by saying that no `Flags` in `ExtendedStandardFlags` can be reused.
-------------
Commit messages:
- 8361570: Incorrect 'sealed is not allowed here' compile-time error
Changes: https://git.openjdk.org/jdk/pull/26181/files
Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=26181&range=00
Issue: https://bugs.openjdk.org/browse/JDK-8361570
Stats: 112 lines in 3 files changed: 101 ins; 6 del; 5 mod
Patch: https://git.openjdk.org/jdk/pull/26181.diff
Fetch: git fetch https://git.openjdk.org/jdk.git pull/26181/head:pull/26181
PR: https://git.openjdk.org/jdk/pull/26181
More information about the compiler-dev
mailing list