RFR: 8361570: Incorrect 'sealed is not allowed here' compile-time error [v2]

Jan Lahoda jlahoda at openjdk.org
Tue Jul 8 09:28:58 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.

Jan Lahoda has updated the pull request incrementally with one additional commit since the last revision:

  Fixing tests.

-------------

Changes:
  - all: https://git.openjdk.org/jdk/pull/26181/files
  - new: https://git.openjdk.org/jdk/pull/26181/files/efc83ed9..118503f0

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=26181&range=01
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=26181&range=00-01

  Stats: 7 lines in 3 files changed: 5 ins; 0 del; 2 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