JDK-8269121 : Type inference bug with method references
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 16,17,18
  • Priority: P3
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2021-06-22
  • Updated: 2022-08-02
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Blocks :  
Blocks :  
Relates :  
Relates :  
Relates :  
Description
Reported at:
https://mail.openjdk.java.net/pipermail/compiler-dev/2021-June/017333.html

Description from the reporter:

I wanted to report a bug in the type inference with references in
OpenJDK 16 and 17-ea.
I have created a sample project on
https://github.com/mmirwaldt/TypeInferenceBugOfMethodReferences/
<https://github.com/mmirwaldt/TypeInferenceBugOfMethodReferences/>
to reproduce the bug. Just run Main and notice the exception.
However, a friend told me that the bug only occurs with compiling
directly by javac but not by maven compile.
We don't know why. Moreover, it seems to be dependent on the operating
system:
I haven't been able to reproduce the bug on linux (CentOS 7, OpenJDK
16.0.1) yet. Windows 10 and Mac show that bug, though.
Can you please have a look on it, try it out and help me creating a bug
ticket if you can confirm it's a bug?
I mean it must be a bug if the type inference somehow infers the wrong
type of a method reference which leads to an exception at runtime,
doesn't it?
Type inference should either infer the right type or cause a compiler
error if it cannot infer the type or isn't sure about it in order to ask
the developer for an explicit declaration. Is that right?
I am sorry I cannot help you finding the bug in the code of the compiler
which infers types. I would if I could.
Comments
probably a duplicate of JDK-8269020
03-09-2021

reduced further and no records in the middle: interface StringLiteral {} interface Variable {} class MyFact { MyFact(StringLiteral stringLiteral) {} } interface OneVariableQuery<VarType extends Variable> { Object query(VarType var1); } class Interpreter { <VarType extends Variable> Object query(OneVariableQuery<VarType> query) { return null; } } public class Main { public static void main(String[] args) { Interpreter interpreter = new Interpreter(); interpreter.query(MyFact::new); // causes exception // interpreter.<StringVariable>query(MyFact::new); // fixes it } }
03-09-2021

this test case reproduces the issue: // if StringLiteral is declared after Variable the issue doesn't occur interface StringLiteral { String value(); } interface Variable {} record MyFact(StringLiteral stringLiteral) { } interface OneVariableQuery<VarType extends Variable> { Object query(VarType var1); } interface TwoVariablesQuery< Var1Type extends Variable, Var2Type extends Variable > { Object eval(Var1Type var1, Var2Type var2); } class Interpreter { <VarType extends Variable> Object query(OneVariableQuery<VarType> query) { return null; } <Var1Type extends Variable, Var2Type extends Variable> Object query(TwoVariablesQuery<Var1Type, Var2Type> query) { return null; } } public class Main { public static void main(String[] args) { Interpreter interpreter = new Interpreter(); interpreter.query(MyFact::new); // causes exception // interpreter.<StringVariable>query(MyFact::new); // fixes it } }
03-09-2021

When I put the `Variable.java` in front of `StringLiteral.java` during compiling, the error is gone and the program runs well. Please see https://mail.openjdk.java.net/pipermail/compiler-dev/2021-June/017349.html for more information. Although I did some research on this bug, currently I may have no time to solve it. Feel free to assign it if its status is not in progress.
23-06-2021

Although the reporter said the bug is not reproduced at linux or by using maven, I reproduce it locally(at linux) by using maven. Please see https://mail.openjdk.java.net/pipermail/compiler-dev/2021-June/017341.html So I think it is an issue about all the generic platforms instead of only windows or OSX.
22-06-2021