RFR: 8316971: Add Lint warning for restricted method calls

Maurizio Cimadamore mcimadamore at openjdk.org
Thu Sep 28 13:21:51 UTC 2023

On Thu, 28 Sep 2023 13:13:31 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

> This patch adds a new lint warning category, namely `-Xlint:restricted` to enable warnings on restricted method calls.
> The patch is relatively straightforward: javac marks methods that are marked with the `@Restricted` annotation with a corresponding internal flag. This is done both in `Annotate` when compiling JDK from source, and in `ClassReader` when JDK classfiles are read. When calls to methods marked with the special flag are found, a new warning is issued.
> While there are some similarities between this new warning and the preview API warnings, the compiler does *not* emit a mandatory note when a compilation unit is found to have one or more restricted method calls. In other words, this is just a plain lint warning.
> The output from javac looks as follows:
> Foo.java:6: warning: [restricted] MemorySegment.reinterpret(long) is a restricted method.
>       Arena.ofAuto().allocate(10).reinterpret(100);
>                                  ^
>   (Restricted methods are unsafe, and, if used incorrectly, they might crash the JVM or result in memory corruption)

make/modules/java.base/Java.gmk line 26:

> 24: #
> 25: 
> 26: DISABLED_WARNINGS_java += this-escape restricted

I've disabled restricted warnings in java.base as there's a lot of restricted calls in the Linker API implementation :-)

make/test/BuildMicrobenchmark.gmk line 94:

> 92:     SMALL_JAVA := false, \
> 94:     DISABLED_WARNINGS := restricted this-escape processing rawtypes cast serial preview, \

This is needed so that we can compile FFM API benchmarks.

test/langtools/tools/javac/diags/examples.not-yet.txt line 219:

> 217: compiler.err.annotation.unrecognized.attribute.name
> 218: 
> 219: # this one is transitional (waiting for FFM API to exit preview)

Note: in principle I could have added an example for this diagnostic. In practice, the fact that FFM is still a preview API makes this a bit difficult - because we need the sample to also have enable preview flags, and also to catch a bunch of preview diagnostics (some of which I can't add directly as they are also excluded on this file). Since this PR already adds a test, I opted to exclude the diagnostic sample for now, and I will come back later (after FFM is no longer preview) to add one (so as to minimize disruption).


PR Review Comment: https://git.openjdk.org/jdk/pull/15964#discussion_r1340137452
PR Review Comment: https://git.openjdk.org/jdk/pull/15964#discussion_r1340137937
PR Review Comment: https://git.openjdk.org/jdk/pull/15964#discussion_r1340142294

More information about the compiler-dev mailing list