Integrated: 8361570: Incorrect 'sealed is not allowed here' compile-time error
Jan Lahoda
jlahoda at openjdk.org
Tue Jul 8 14:37:52 UTC 2025
On Tue, 8 Jul 2025 07:58:32 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:
> 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 parameter 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.
This pull request has now been integrated.
Changeset: 85331943
Author: Jan Lahoda <jlahoda at openjdk.org>
URL: https://git.openjdk.org/jdk/commit/853319439e7887ddd54f8c4a3d79aa62ec51fd64
Stats: 118 lines in 4 files changed: 105 ins; 5 del; 8 mod
8361570: Incorrect 'sealed is not allowed here' compile-time error
Reviewed-by: mcimadamore, vromero
-------------
PR: https://git.openjdk.org/jdk/pull/26181
More information about the compiler-dev
mailing list