RFR: 8343540: Report preview error for inherited effectively-preview methods

Vicente Romero vromero at openjdk.org
Fri Nov 15 16:04:24 UTC 2024


On Thu, 7 Nov 2024 13:10:34 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

> Consider a case where a new preview interface is introduced to JDK, and an existing non-preview class is retrofitted to use it. I.e. a case like:
> 
> 
> //JDK types:
> @PreviewFeature(...)
> public interface NewAPI {
>     public default void test() {}
> }
> public class ExistingRetrofittedClass implements NewAPI {}
> 
> //user type:
> class Test {
>     void t(ExistingRetrofittedClass c) {
>         c.test();
>     }
> }
> 
> 
> There is currently no error or warning about the invocation of the test method, as the method itself is not marked as preview, and the receiver is not preview either.
> 
> The proposal herein is that invoking a method on a receiver that is not of preview type itself, but the method is declared in a preview class or interface, the method will be considered to be a preview method, and the appropriate error or warning will be printed. Similar behavior is implementing for dereferencing fields.
> 
> 
> Similarly when implementing or overriding such "effectively" preview methods, like:
> 
> 
> //user types:
> class Test1 extends ExistingRetrofittedClass {
>     public void test() {}
> }
> 
> 
> java also does not produce any error or warning. If the method itself would be marked as preview, an error or warning would be printed.
> 
> The proposal herein is to produce an error or warning for a method declared inside a non-preview class or interface, which directly overrides a method declared inside a preview class or interface.
> 
> In particular, for the two example above, javac is currently producing no errors. With the change proposed herein, javac will produce output like (with no `--enable-preview`):
> 
> 
> src/Test1.java:4: error: test() is a preview API and is disabled by default.
>     public void test() {}
>                 ^
>   (use --enable-preview to enable preview APIs)
> src/Test.java:5: error: test() is a preview API and is disabled by default.
>         c.test();
>          ^
>   (use --enable-preview to enable preview APIs)
> 2 errors

looks good

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java line 3852:

> 3850:         } else {
> 3851:             sIsPreview = false;
> 3852:             previewSymbol = null;

nit: this assignment seems to be unnecessary

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

Marked as reviewed by vromero (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/21953#pullrequestreview-2439038038
PR Review Comment: https://git.openjdk.org/jdk/pull/21953#discussion_r1844103204


More information about the compiler-dev mailing list