Consider source files like this:

$ cat /tmp/ValueClass.java 
value class ValueClass {}
$ cat /tmp/TestValue.java 
public class TestValue {
    ValueClass c;
}


When compiled like this:

$ javac --enable-preview --release 28 -Xlint:preview /tmp/ValueClass.java 
/tmp/ValueClass.java:1: warning: [preview] value classes are a preview feature 
and may be removed in a future release.
value class ValueClass {}
^
1 warning
$ javac --enable-preview --release 28 -Xlint:preview -classpath /tmp 
/tmp/TestValue.java 
warning: [preview] class file for /tmp/ValueClass.class uses preview features 
of Java SE 28.
1 warning


Note that while the `TestValue.class` is marked as preview, there's no warning 
on the use of `ValueClass`. This is not aligned with the JEP 12 wording:

> When compiling with preview features enabled, any source code reference to 
> (i) a class or interface declared using a preview language feature, ..., 
> causes a preview warning.

Currently javac marks the classfile preview using an explicit test in the 
`ClassWriter`, but that does not seem quite right. This PR proposes to mark 
value classes as `Preview.declaredUsingPreviewFeature == true`, which should 
automatically produce a warning, and mark the appropriate classfile as preview.

For the example above:

$ javac --enable-preview --release 28 -Xlint:preview -classpath /tmp 
/tmp/TestValue.java 
warning: [preview] class file for /tmp/ValueClass.class uses preview features 
of Java SE 28.
/tmp/TestValue.java:2: warning: [preview] class ValueClass is declared using a 
preview feature, which may be removed in a future release.
    ValueClass c;
    ^
2 warnings


Note this will also report the warning for `Integer`, when preview is enabled:

$ cat /tmp/Test.java 
public class Test {
    Integer i;
}
$ javac /tmp/Test.java
$ javac --enable-preview --release 28 -Xlint:preview /tmp/Test.java
warning: [preview] class file for /@/java.base/java/lang/Integer.sig uses 
preview features of Java SE 28.
/tmp/Test.java:2: warning: [preview] class Integer is declared using a preview 
feature, which may be removed in a future release.
    Integer i;
    ^
2 warnings


---------
- [x] I confirm that I make this contribution in accordance with the [OpenJDK 
Interim AI Policy](https://openjdk.org/legal/ai).

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

Commit messages:
 - 8387484: [lworld] javac should warn on use of value classes

Changes: https://git.openjdk.org/valhalla/pull/2601/files
  Webrev: https://webrevs.openjdk.org/?repo=valhalla&pr=2601&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8387484
  Stats: 115 lines in 3 files changed: 87 ins; 9 del; 19 mod
  Patch: https://git.openjdk.org/valhalla/pull/2601.diff
  Fetch: git fetch https://git.openjdk.org/valhalla.git pull/2601/head:pull/2601

PR: https://git.openjdk.org/valhalla/pull/2601

Reply via email to