RFR: [bworld] Emit lint warnings if overriding method does not have same nullability [v2]
Maurizio Cimadamore
mcimadamore at openjdk.org
Mon Feb 9 21:51:47 UTC 2026
On Mon, 9 Feb 2026 21:13:04 GMT, Vicente Romero <vromero at openjdk.org> wrote:
>> emit a lint warning for cases like:
>>
>> class Test {
>> String! m(String s) { return ""; }
>> }
>> class Sub extends Test {
>> @Override
>> String m(String s) { return null; }
>> }
>>
>> or:
>>
>> class Test {
>> String m(String! s) { return ""; }
>> }
>> class Sub extends Test {
>> @Override
>> String m(String s) { return null; }
>> }
>
> Vicente Romero has updated the pull request incrementally with 20 additional commits since the last revision:
>
> - minor change
> - minor change
> - removing commented code
> - addressing review comments
> - minor code change
> - adding separate compilation test
> - updating tests
> - another update
> - reducing common code
> - adding support for explicit lambdas
> - ... and 10 more: https://git.openjdk.org/valhalla/compare/4c1b385b...fd10017d
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java line 1883:
> 1881: while (mtArgs.nonEmpty() && otArgs.nonEmpty()) {
> 1882: if (types.hasNarrowerNullability(otArgs.head, mtArgs.head)) {
> 1883: warnNullableTypes(TreeInfo.diagnosticPositionFor(m, tree), LintWarnings.ArgumentTypeIsNullRestricted);
Not sure if we should report all parameter issues or just the first -- but it's ok for now
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java line 1975:
> 1973: }
> 1974:
> 1975: public void checkArgsNullability(List<Type> overridingArgs,
One possibility to simplify this w/o passing a lot lambdas could be to just have a method that returns a Pair of incompatible types -- then the client will do what it want with it; or `null` if there's no incompatibility
src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties line 4395:
> 4393: # lint: null
> 4394: compiler.warn.return.type.is.null.restricted=\
> 4395: Overriden method''s return type is null restricted
These message seems confusing. The problem is not that the overridden method is null-restricted, but rather, that there's a mismatch AND the overridden method is null-restricted while the overriding method is not.
I suggest something like: "incompatible null restrictions in overriding method" -- maybe followed by a details message (e.g. "return type Foo in the overriding does not match the return type Foo! in the overridden method")
src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties line 4401:
> 4399: # 0: type, 1: type
> 4400: compiler.misc.return.type.nullability.mismatch=\
> 4401: return type {0} in the overriding does not match the return type {1}! in the overridden method
You shouldn't need the `!` here -- if the type has restrictions, they should be displayed
-------------
PR Review Comment: https://git.openjdk.org/valhalla/pull/2058#discussion_r2775132136
PR Review Comment: https://git.openjdk.org/valhalla/pull/2058#discussion_r2783447623
PR Review Comment: https://git.openjdk.org/valhalla/pull/2058#discussion_r2775163272
PR Review Comment: https://git.openjdk.org/valhalla/pull/2058#discussion_r2783473145
More information about the valhalla-dev
mailing list