RFR: add use site null checks for fields [v5]

Maurizio Cimadamore mcimadamore at openjdk.org
Mon Jan 19 17:53:34 UTC 2026


On Mon, 19 Jan 2026 12:51:08 GMT, Vicente Romero <vromero at openjdk.org> wrote:

>> src/jdk.compiler/share/classes/com/sun/tools/javac/comp/NullChecksWriter.java line 165:
>> 
>>> 163:                     sym.owner.kind == TYP &&
>>> 164:                     sym.kind == VAR &&
>>> 165:                     !isInThisSameCompUnit(sym) &&
>> 
>> I note that, for fields use site checks, we're checking that the field access is pointing to a field in a different compilation unit. But for method calls use-site we don't look for this and always (for now) emit the use site check. I wonder if for now we should err on the side of always generating the checks, and then come back later and (in one shot) relax the translation strategy to omit the checks in cases where they are provably unnecessary.
>
> we can do that or just add the same check to methods. I didn't think about this for the method's case. Seems like a very minimal `optimization` that should be there since the beginning. I could add the same for methods as part of this PR

I'm fine with adding more use-site method optimizations in a follow up PR.

>> src/jdk.compiler/share/classes/com/sun/tools/javac/comp/NullChecksWriter.java line 179:
>> 
>>> 177: 
>>> 178:         private Symbol outermostType(Symbol sym) {
>>> 179:             Symbol prev = null;
>> 
>> Doesn't `sym.outermostClass()` work?
>
> it work but at the end it does a cast to ClassSymbol, unfortunately some fields can have TypeSymbol as its outermost class. This is use in Symtab, an anonymous class of TypeSymbol and the build was failing

Ugh -- for now let's add a comment that `outermostClass` was considered and then discarded for the reasons you mention. 

That said -- the anon class you talk about is (I think) `noSymbol`. I wonder why we're trying to call `outrmostType` on `noSymbol`. That symbol has kind `NIL`. So I wonder what is really going on here? Are we trying to use this visitor on code that has errors?

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

PR Review Comment: https://git.openjdk.org/valhalla/pull/1926#discussion_r2705669144
PR Review Comment: https://git.openjdk.org/valhalla/pull/1926#discussion_r2705667576


More information about the valhalla-dev mailing list