JDK-8313448 : incompatible types error on valid generics
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8,11,17,20,21,22
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2023-07-27
  • Updated: 2023-12-04
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.
JDK 23
23Unresolved
Description
A DESCRIPTION OF THE PROBLEM :
Errors are displayed when compiling a valid code:

java: incompatible types: cannot infer type-variable(s) 

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Use the attached class source

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Code compiles without any problems
ACTUAL -
Compiliation error is displayed:

InvalidGeneric.java:6:66
java: incompatible types: cannot infer type-variable(s) InputType,OutputType
    (argument mismatch; BaseInput<capture#1 of ?,capture#2 of ?> cannot be converted to Input<InputType,OutputType>)
InvalidGeneric.java:7:185
java: incompatible types: cannot infer type-variable(s) InputType,OutputType
    (argument mismatch; BaseInput<capture#3 of ?,capture#4 of ?> cannot be converted to Input<InputType,OutputType>)

---------- BEGIN SOURCE ----------
public class InvalidGeneric {

    public static void main(String[] args) {
        final BaseInput<?, ?> input = null;
        final Service service = new Service() {};
        final BaseInput.BaseOutput<?, ?> output = service.service(input);
        final BaseInput.BaseOutput<? extends BaseInput<?, ? extends BaseInput.BaseOutput<?, ?>>, ? extends BaseInput.BaseOutput<? extends BaseInput<?, ?>, ?>> output2 = service.service(input);
    }
}

interface Service {
    default <InputType extends Input<InputType, OutputType>, OutputType extends Input.Output<InputType, OutputType>> OutputType service(Input<InputType, OutputType> operation) {
        return null;
    }
}

interface Input<InputType extends Input<InputType, OutputType>, OutputType extends Input.Output<InputType, OutputType>> {
    abstract class Output<OutputType extends Input<OutputType, InputType>, InputType extends Output<OutputType, InputType>> {
    }
}

class BaseInput<BaseInputType extends BaseInput<BaseInputType, BaseOutputType>, BaseOutputType extends BaseInput.BaseOutput<BaseInputType, BaseOutputType>>
        implements Input<BaseInputType, BaseOutputType> {
    static class BaseOutput<BaseInputType extends BaseInput<BaseInputType, BaseOutputType>, BaseOutputType extends BaseOutput<BaseInputType, BaseOutputType>> extends Output<BaseInputType, BaseOutputType> {
    }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Declare variable as var or compile using eclipse compiler

FREQUENCY : always



Comments
Tested on Windows 11 x64: The error is produced for all of the below JDKs: JDK 8 JDK 11 JDK 17 JDK 20 Below is the error :- Test.java:6: error: incompatible types: cannot infer type-variable(s) InputType#1,OutputType#1 final BaseInput.BaseOutput<?, ?> output = service.service(input); ^ (argument mismatch; BaseInput<CAP#1,CAP#2> cannot be converted to Input<InputType#2,OutputType#2>) where InputType#1,OutputType#1,InputType#2,OutputType#2 are type-variables: InputType#1 extends Input<InputType#1,OutputType#1> declared in method <InputType#1,OutputType#1>service(Input<InputType#1,OutputType#1>) OutputType#1 extends Output<InputType#1,OutputType#1> declared in method <InputType#1,OutputType#1>service(Input<InputType#1,OutputType#1>) InputType#2 extends Input<InputType#2,OutputType#2> OutputType#2 extends BaseOutput<?,?> where CAP#1,CAP#2 are fresh type-variables: CAP#1 extends BaseInput<CAP#1,CAP#2> from capture of ? CAP#2 extends BaseOutput<CAP#1,CAP#2> from capture of ? Test.java:7: error: incompatible types: cannot infer type-variable(s) InputType#1,OutputType#1 final BaseInput.BaseOutput<? extends BaseInput<?, ? extends BaseInput.BaseOutput<?, ?>>, ? extends BaseInput.BaseOutput<? extends BaseInput<?, ?>, ?>> output2 = service.service(input); ^ (argument mismatch; BaseInput<CAP#1,CAP#2> cannot be converted to Input<InputType#2,OutputType#2>) where InputType#1,OutputType#1,InputType#2,OutputType#2 are type-variables: InputType#1 extends Input<InputType#1,OutputType#1> declared in method <InputType#1,OutputType#1>service(Input<InputType#1,OutputType#1>) OutputType#1 extends Output<InputType#1,OutputType#1> declared in method <InputType#1,OutputType#1>service(Input<InputType#1,OutputType#1>) InputType#2 extends Input<InputType#2,OutputType#2> OutputType#2 extends BaseOutput<? extends BaseInput<?,? extends BaseOutput<?,?>>,? extends BaseOutput<? extends BaseInput<?,?>,?>> where CAP#1,CAP#2 are fresh type-variables: CAP#1 extends BaseInput<CAP#1,CAP#2> from capture of ? CAP#2 extends BaseOutput<CAP#1,CAP#2> from capture of ? 2 errors
31-10-2023