JDK-8238213: javac emits wrong error when calling an non-static overloaded method from static

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Fri Oct 2 20:48:14 UTC 2020


Hi Bernard,
since there's not a lot of rush for getting this one in, I'd suggest it 
maybe better for you to spend time to get acquainted with the new 
process? For instance, I don't think we can "push on your behalf" (like 
we could e.g. on mercurial if you provided a commit patch); the best we 
could do I think would be to list you as a contributor.

Let me know how you want to proceed. My sense is that you'd have to do 
it sooner or later, so this might be as a good bug as any other :-)

Maurizio

On 02/10/2020 18:10, B. Blaser wrote:
> Thanks for the confirmation, Maurizio.
>
> Being still not up-to-date with the new git work-flow, could someone
> help pushing it on my behalf?
>
> Cheers,
> Bernard
>
> On Fri, 2 Oct 2020 at 18:47, Maurizio Cimadamore
> <maurizio.cimadamore at oracle.com> wrote:
>> I agree that seems to be a discrepancy from what the JLS is saying (and,
>> I believe even from what earlier javac versions were doing - e.g. prior
>> to 8).
>>
>> I tried this in Java 7 and this returned:
>>
>> foo.java:3: error: non-static method test(double) cannot be referenced
>> from a static context
>>       test(5.0);
>>       ^
>>
>> as expected.
>>
>> Maurizio
>>
>> On 02/10/2020 14:50, B. Blaser wrote:
>>> Hi,
>>>
>>> Consider the following example from JDK-8238213:
>>>
>>> public class JavacBrokenError {
>>>     public static void main(String[] args) {
>>>       test(5.0);
>>>     }
>>>
>>>     void test(double d) {}
>>>     /* static */ void test(Double d) {}
>>> }
>>>
>>> It seems that javac correctly finds 'test(double)' during the strict
>>> resolution phase (§15.12.2.2) but doesn't stop with a static error as
>>> mandated by §15.12.3 and continues with the loose resolution phase
>>> (§15.12.2.3) which ends up wrongly with the ambiguous error instead.
>>>
>>> The following fix addresses this on jdk14u (langtools:tier1 is OK):
>>>
>>> diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java
>>> b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java
>>> --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java
>>> +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java
>>> @@ -3283,7 +3283,7 @@
>>>             */
>>>            final boolean shouldStop(Symbol sym, MethodResolutionPhase phase) {
>>>                return phase.ordinal() > maxPhase.ordinal() ||
>>> -                !sym.kind.isResolutionError() || sym.kind == AMBIGUOUS;
>>> +                !sym.kind.isResolutionError() || sym.kind ==
>>> AMBIGUOUS || sym.kind == STATICERR;
>>>            }
>>>
>>>            /**
>>>
>>> On jdk16, see:
>>>
>>> https://urldefense.com/v3/__https://github.com/openjdk/jdk/blob/b8966e1f7bfa2408f0e5330ac310526dd5c3cb2d/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java*L3310__;Iw!!GqivPVa7Brio!Kd_zf6Yom6G_TjVtH616SxZrclJ6cb8iAX_1E06X_s5ksEm5wZ_LG59TXuX-BFSqExB0DV0$
>>>
>>> What do you think?
>>>
>>> Thanks,
>>> Bernard


More information about the compiler-dev mailing list