RFR: Remove redundant null checks [v5]
Vicente Romero
vromero at openjdk.org
Fri Jan 23 19:05:23 UTC 2026
On Fri, 23 Jan 2026 10:11:00 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
>> Vicente Romero has updated the pull request incrementally with one additional commit since the last revision:
>>
>> adding more tests
>
> src/jdk.compiler/share/classes/com/sun/tools/javac/comp/NullChecksWriter.java line 218:
>
>> 216:
>> 217: public void visitTypeCast(JCTypeCast tree) {
>> 218: if (tree.expr instanceof JCTypeCast otherTypeCast && tree.strict && otherTypeCast.strict) {
>
> I think we need to generalize this a bit. E.g. this code will correctly remove double casts. But I think we need to have a global visitor flag saying whether the last thing we visited was already use-site checked (e.g. provablyStrict) -- and then omit any further use site check if that flag is set.
>
> E.g. say we have:
>
>
> (String!)otherClass.m();
>
>
> Where `OtherClass::m` is
>
>
> String! m() { ... }
>
>
> In this case, after the method call, we know we're already strict because:
> * the method is in this compilation unit, so we know the result value will be checked
> * the method is in a different compilation unit, so we know we'll get a use site check there
>
> So, emitting a cast at that point is not very useful. We've already checked that thing.
>
> (In fact, thinking more, if use-site checks are enabled, field access and method calls are _always_ eagerly checked -- which should mean that any inner expression is always at the level of strictness it says it is?)
I have made several changes to the PR to filter out many more cases for which an additional null check should be unnecessary. Currently use side checks are being generated even for methods inside the current compilation unit as long as those methods could be overriden
-------------
PR Review Comment: https://git.openjdk.org/valhalla/pull/1947#discussion_r2722466041
More information about the valhalla-dev
mailing list