JDK-8319461 : incompatible types: inference variable has incompatible bounds equality constraint
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 17,21,22
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2023-11-03
  • Updated: 2024-05-20
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 24
24Unresolved
Description
ADDITIONAL SYSTEM INFORMATION :
Java version: 21.0.1
Default locale: en_US, platform encoding: UTF-8
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

A DESCRIPTION OF THE PROBLEM :
I get the errors (see log at "Actual Result") from javac for code using generics in an obviously valid way and had to add casts to raw type as a workaround.
Please note that the Eclipse Compiler perfectly compiles my code.
You can find my code here:
https://github.com/m-m-m/property/blob/1a9b411eb0294187504090a87e17184816757a77/core/src/main/java/io/github/mmm/property/factory/PropertyFactoryManager.java#L88

As you can see the method has this generic declaration:
<V, P extends ReadableProperty<V>>

The called method getRequiredFactory has this generic declaration:
<V, P extends ReadableProperty<V>>

IMHO it should therefore be quite obvious that the method call should work without the cast to "Class" without the generic.
In this place we have the same pattern with even just a single method parameter:
https://github.com/m-m-m/property/blob/1a9b411eb0294187504090a87e17184816757a77/core/src/main/java/io/github/mmm/property/factory/PropertyFactoryManager.java#L159C17-L159C42

I work on Java 17 in this project but just tested with the latest 21 version to confirm the bug is not already fixed.
IMHO there is no version of javac that can compile this code. However, Eclipse Compiler works fine.



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
https://github.com/m-m-m/property/actions/runs/6751066732/job/18354544566
https://github.com/m-m-m/property/commit/a8a75e9e1543859683f22c0328fac2a9f6eb1c8e
https://github.com/m-m-m/property/actions/runs/6751081583
https://github.com/m-m-m/property/actions

You can also clone the repository https://github.com/m-m-m/property.git and build yourself but the project consists of many modules so the setup may take some time. IMHO the description and the source should easily convince there is a bug and with it you should easily be able to isolate the bug pattern with minimum code.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
javac should compile my code
ACTUAL -
[ERROR] D:\projects\mmm\workspaces\main\property\core\src\main\java\io\github\mmm\property\factory\PropertyFactoryManager.java:[87,48] error: incompatible types: inference variable P#1 has incompatible bounds
    equality constraints: P#2
    upper bounds: WritableProperty<V#1>,ReadableProperty<V#1>
  where P#1,V#1,P#2,V#2 are type-variables:
    P#1 extends ReadableProperty<V#1> declared in method <V#1,P#1>getRequiredFactory(Class<P#1>,Class<V#1>)
    V#1 extends Object declared in method <V#1,P#1>getRequiredFactory(Class<P#1>,Class<V#1>)
    P#2 extends ReadableProperty<V#2> declared in method <V#2,P#2>create(Class<P#2>,Class<V#2>,String,PropertyMetadata<V#2>)
    V#2 extends Object declared in method <V#2,P#2>create(Class<P#2>,Class<V#2>,String,PropertyMetadata<V#2>)
[ERROR] D:\projects\mmm\workspaces\main\property\core\src\main\java\io\github\mmm\property\factory\PropertyFactoryManager.java:[116,48] error: incompatible types: inference variable P#1 has incompatible bounds
    equality constraints: P#2
    upper bounds: WritableProperty<V#1>,ReadableProperty<V#1>
  where P#1,V#1,P#2,V#2 are type-variables:
    P#1 extends ReadableProperty<V#1> declared in method <V#1,P#1>getRequiredFactory(Class<P#1>,Class<V#1>)
    V#1 extends Object declared in method <V#1,P#1>getRequiredFactory(Class<P#1>,Class<V#1>)
    P#2 extends ReadableProperty<V#2> declared in method <V#2,P#2>create(Class<P#2>,PropertyTypeInfo<V#2>,String,PropertyMetadata<V#2>)
    V#2 extends Object declared in method <V#2,P#2>create(Class<P#2>,PropertyTypeInfo<V#2>,String,PropertyMetadata<V#2>)
[ERROR] D:\projects\mmm\workspaces\main\property\core\src\main\java\io\github\mmm\property\factory\PropertyFactoryManager.java:[156,41] error: incompatible types: inference variable P#1 has incompatible bounds
    equality constraints: P#2
    upper bounds: WritableProperty<V#1>,ReadableProperty<V#1>
  where P#1,V#1,P#2,V#2 are type-variables:
    P#1 extends ReadableProperty<V#1> declared in method <V#1,P#1>getFactoryForPropertyType(Class<P#1>)
    V#1 extends Object declared in method <V#1,P#1>getFactoryForPropertyType(Class<P#1>)
    P#2 extends ReadableProperty<V#2> declared in method <V#2,P#2>getFactory(Class<P#2>,Class<V#2>)
    V#2 extends Object declared in method <V#2,P#2>getFactory(Class<P#2>,Class<V#2>)

---------- BEGIN SOURCE ----------
https://github.com/m-m-m/property/blob/a8a75e9e1543859683f22c0328fac2a9f6eb1c8e/core/src/main/java/io/github/mmm/property/factory/PropertyFactoryManager.java#L88
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Add casts to remove generic type information losing type-safeness

FREQUENCY : always



Comments
comment from Stephan Herrmann: https://github.com/eclipse-jdt/eclipse.jdt.core/pull/2460#issuecomment-2119209784
20-05-2024

Additional Information from submitter: ============================== > After checking out the branch provided by the submitter, the issue is reproducible Thanks for retesting and reproducing the problem. > I'm seeing this declaration in your code: > PropertyFactory factory = getRequiredFactory(propertyType, valueClass); > this is at PropertyFactoryManager line 88, in the zip file you provided. Now type inference creates this bound set: > > V = V > V <: Object > P = P > P <: WritableProperty<V> > P <: ReadableProperty<V> > > but without going further from the declaration of method PropertyFactoryManager::create, line 84: > default <V, P extends ReadableProperty<V>> P create(Class<P> propertyType, Class<V> valueClass, String name, PropertyMetadata<V> metadata) > > we know that P's upper bound is ReadableProperty<V>, inference will be checking that ReadableProperty<V> is a subtype of WritableProperty<V>, this is false as actually WritableProperty is a subinterface of ReadableProperty, so there is no way inference could be successful here. I think the problem is in your code and that javac is not the one to blame here If your conclusion would be correct, then the Cast would not be correct as then I would get a ClassCastException at runtime but the code works fine and as stated the Eclipse compiler perfectly accepts my code without the Cast workaround. I am using "PropertyFactory<V, ? extends P>" so if P is declared as "P extends ReadableProperty<V>" then why can't "? extends P" be extending "WritableProperty<V>" if "WritableProperty" extends "ReadableProperty". IMHO you have confirmed the bug but do not seem to be interested in fixing it.
13-02-2024

I'm seeing this declaration in your code: PropertyFactory factory = getRequiredFactory(propertyType, valueClass); this is at PropertyFactoryManager line 88, in the zip file you provided. Now type inference creates this bound set: V = V V <: Object P = P P <: WritableProperty<V> P <: ReadableProperty<V> but without going further from the declaration of method PropertyFactoryManager::create, line 84: default <V, P extends ReadableProperty<V>> P create(Class<P> propertyType, Class<V> valueClass, String name, PropertyMetadata<V> metadata) we know that P's upper bound is ReadableProperty<V>, inference will be checking that ReadableProperty<V> is a subtype of WritableProperty<V>, this is false as actually WritableProperty is a subinterface of ReadableProperty, so there is no way inference could be successful here. I think the problem is in your code and that javac is not the one to blame here
15-12-2023

After checking out the branch provided by the submitter, the issue is reproducible: Checked with JDK 17.0.6, JDK 21.0.1 and OpenJDK 22: [ERROR] C:\test\demo\property\core\src\main\java\io\github\mmm\property\factory\PropertyFactoryManager.java:[88,48] error: incompatible types: inference variable P#1 has incompatible bounds [ERROR] equality constraints: P#2 [ERROR] lower bounds: WritableProperty<V#1>,ReadableProperty<V#1> [ERROR] where P#1,V#1,P#2,V#2 are type-variables: [ERROR] P#1 extends ReadableProperty<V#1> declared in method <V#1,P#1>getRequiredFactory(Class<P#1>,Class<V#1>) [ERROR] V#1 extends Object declared in method <V#1,P#1>getRequiredFactory(Class<P#1>,Class<V#1>) [ERROR] P#2 extends ReadableProperty<V#2> declared in method <V#2,P#2>create(Class<P#2>,Class<V#2>,String,PropertyMetadata<V#2>) [ERROR] V#2 extends Object declared in method <V#2,P#2>create(Class<P#2>,Class<V#2>,String,PropertyMetadata<V#2>) [ERROR] C:\test\demo\property\core\src\main\java\io\github\mmm\property\factory\PropertyFactoryManager.java:[118,48] error: incompatible types: inference variable P#1 has incompatible bounds [ERROR] equality constraints: P#2 [ERROR] lower bounds: WritableProperty<V#1>,ReadableProperty<V#1> [ERROR] where P#1,V#1,P#2,V#2 are type-variables: [ERROR] P#1 extends ReadableProperty<V#1> declared in method <V#1,P#1>getRequiredFactory(Class<P#1>,Class<V#1>) [ERROR] V#1 extends Object declared in method <V#1,P#1>getRequiredFactory(Class<P#1>,Class<V#1>) [ERROR] P#2 extends ReadableProperty<V#2> declared in method <V#2,P#2>create(Class<P#2>,PropertyTypeInfo<V#2>,String,PropertyMetadata<V#2>) [ERROR] V#2 extends Object declared in method <V#2,P#2>create(Class<P#2>,PropertyTypeInfo<V#2>,String,PropertyMetadata<V#2>) [ERROR] C:\test\demo\property\core\src\main\java\io\github\mmm\property\factory\PropertyFactoryManager.java:[159,41] error: incompatible types: inference variable P#1 has incompatible bounds [ERROR] equality constraints: P#2 [ERROR] lower bounds: WritableProperty<V#1>,ReadableProperty<V#1> [ERROR] where P#1,V#1,P#2,V#2 are type-variables: [ERROR] P#1 extends ReadableProperty<V#1> declared in method <V#1,P#1>getFactoryForPropertyType(Class<P#1>) [ERROR] V#1 extends Object declared in method <V#1,P#1>getFactoryForPropertyType(Class<P#1>) [ERROR] P#2 extends ReadableProperty<V#2> declared in method <V#2,P#2>getFactory(Class<P#2>,Class<V#2>) [ERROR] V#2 extends Object declared in method <V#2,P#2>getFactory(Class<P#2>,Class<V#2>)
15-11-2023

Additional Information from submitter: =============================== > With JDK 21.0.1, I could not reproduce the issue as well: > With JDK 17.0.6, I could not reproduce the issue: IMHO you have only cloned the latest master branch. Of course I added the casts, I described as a workaround so my project does not get blocked by the bug. I had linked the exact commit proving the error in the build with github action. If you want to reproduce this locally simple do this on the cloned repo of the property repository: git checkout 54a637fbcac3fd06553e586e5c6a597e085cc6da
14-11-2023

The dependencies require JDK 17 and above. Hence it can't be reproduced on JDK 11 and JDK 8: [WARNING] Can't extract module name from mmm-base-0.0-SNAPSHOT.jar: Unsupported major.minor version 61.0 [WARNING] Can't extract module name from mmm-value-converter-0.0-SNAPSHOT.jar: Unsupported major.minor version 61.0 [WARNING] Can't extract module name from mmm-value-observable-0.0-SNAPSHOT.jar: Unsupported major.minor version 61.0 [WARNING] Can't extract module name from mmm-validation-0.0-SNAPSHOT.jar: Unsupported major.minor version 61.0 [WARNING] Can't extract module name from mmm-value-0.0-SNAPSHOT.jar: Unsupported major.minor version 61.0 [WARNING] Can't extract module name from mmm-marshall-0.0-SNAPSHOT.jar: Unsupported major.minor version 61.0 [WARNING] Can't extract module name from mmm-event-0.0-SNAPSHOT.jar: Unsupported major.minor version 61.0 [WARNING] Can't extract module name from mmm-marshall-json-0.0-SNAPSHOT.jar: Unsupported major.minor version 61.0 [WARNING] Can't extract module name from mmm-scanner-0.0-SNAPSHOT.jar: Unsupported major.minor version 61.0
06-11-2023

With JDK 21.0.1, I could not reproduce the issue as well: PS C:\test\property> java -version java version "21.0.1" 2023-10-17 LTS Java(TM) SE Runtime Environment (build 21.0.1+12-LTS-29) Java HotSpot(TM) 64-Bit Server VM (build 21.0.1+12-LTS-29, mixed mode, sharing) PS C:\test\property> mvn clean compile [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Reactor Build Order: [INFO] [INFO] mmm-property-parent [pom] [INFO] mmm-property [jar] [INFO] mmm-property-builder [jar] [INFO] [INFO] ----------------< io.github.m-m-m:mmm-property-parent >----------------- [INFO] Building mmm-property-parent 0.0-SNAPSHOT [1/3] [INFO] --------------------------------[ pom ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:3.3.1:clean (default-clean) @ mmm-property-parent --- [INFO] [INFO] --- flatten-maven-plugin:1.5.0:clean (flatten.clean) @ mmm-property-parent --- [INFO] [INFO] --------------------< io.github.m-m-m:mmm-property >-------------------- [INFO] Building mmm-property 0.0-SNAPSHOT [2/3] [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:3.3.1:clean (default-clean) @ mmm-property --- [INFO] Deleting C:\test\property\core\target [INFO] [INFO] --- flatten-maven-plugin:1.5.0:clean (flatten.clean) @ mmm-property --- [INFO] [INFO] --- maven-resources-plugin:3.3.1:resources (default-resources) @ mmm-property --- [INFO] Copying 1 resource from src\main\resources to target\classes [INFO] [INFO] --- maven-compiler-plugin:3.11.0:compile (default-compile) @ mmm-property --- [INFO] Changes detected - recompiling the module! :source [INFO] Compiling 221 source files with javac [forked debug release 17 module-path] to target\classes [INFO] [INFO] ----------------< io.github.m-m-m:mmm-property-builder >---------------- [INFO] Building mmm-property-builder 0.0-SNAPSHOT [3/3] [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:3.3.1:clean (default-clean) @ mmm-property-builder --- [INFO] Deleting C:\test\property\builder\target [INFO] [INFO] --- flatten-maven-plugin:1.5.0:clean (flatten.clean) @ mmm-property-builder --- [INFO] [INFO] --- maven-resources-plugin:3.3.1:resources (default-resources) @ mmm-property-builder --- [INFO] skip non existing resourceDirectory C:\test\property\builder\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.11.0:compile (default-compile) @ mmm-property-builder --- [INFO] Changes detected - recompiling the module! :dependency [INFO] Compiling 32 source files with javac [forked debug release 17 module-path] to target\classes [INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary for mmm-property-parent 0.0-SNAPSHOT: [INFO] [INFO] mmm-property-parent ................................ SUCCESS [ 0.253 s] [INFO] mmm-property ....................................... SUCCESS [ 19.687 s] [INFO] mmm-property-builder ............................... SUCCESS [ 3.352 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 23.537 s [INFO] Finished at: 2023-11-06T10:54:48+05:30 [INFO] ------------------------------------------------------------------------ PS C:\test\property>
06-11-2023

With JDK 17.0.6, I could not reproduce the issue: PS C:\test\property> java -version java version "17.0.6" 2023-01-17 LTS Java(TM) SE Runtime Environment (build 17.0.6+9-LTS-190) Java HotSpot(TM) 64-Bit Server VM (build 17.0.6+9-LTS-190, mixed mode, sharing) PS C:\test\property> mvn clean compile [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Reactor Build Order: [INFO] [INFO] mmm-property-parent [pom] [INFO] mmm-property [jar] [INFO] mmm-property-builder [jar] [INFO] [INFO] ----------------< io.github.m-m-m:mmm-property-parent >----------------- [INFO] Building mmm-property-parent 0.0-SNAPSHOT [1/3] [INFO] --------------------------------[ pom ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:3.3.1:clean (default-clean) @ mmm-property-parent --- [INFO] [INFO] --- flatten-maven-plugin:1.5.0:clean (flatten.clean) @ mmm-property-parent --- [INFO] [INFO] --------------------< io.github.m-m-m:mmm-property >-------------------- [INFO] Building mmm-property 0.0-SNAPSHOT [2/3] [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:3.3.1:clean (default-clean) @ mmm-property --- [INFO] [INFO] --- flatten-maven-plugin:1.5.0:clean (flatten.clean) @ mmm-property --- [INFO] [INFO] --- maven-resources-plugin:3.3.1:resources (default-resources) @ mmm-property --- [INFO] Copying 1 resource from src\main\resources to target\classes [INFO] [INFO] --- maven-compiler-plugin:3.11.0:compile (default-compile) @ mmm-property --- [INFO] Changes detected - recompiling the module! :source [INFO] Compiling 221 source files with javac [forked debug release 17 module-path] to target\classes [INFO] [INFO] ----------------< io.github.m-m-m:mmm-property-builder >---------------- [INFO] Building mmm-property-builder 0.0-SNAPSHOT [3/3] [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:3.3.1:clean (default-clean) @ mmm-property-builder --- [INFO] [INFO] --- flatten-maven-plugin:1.5.0:clean (flatten.clean) @ mmm-property-builder --- [INFO] [INFO] --- maven-resources-plugin:3.3.1:resources (default-resources) @ mmm-property-builder --- [INFO] skip non existing resourceDirectory C:\test\property\builder\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.11.0:compile (default-compile) @ mmm-property-builder --- [INFO] Changes detected - recompiling the module! :dependency [INFO] Compiling 32 source files with javac [forked debug release 17 module-path] to target\classes [INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary for mmm-property-parent 0.0-SNAPSHOT: [INFO] [INFO] mmm-property-parent ................................ SUCCESS [ 0.181 s] [INFO] mmm-property ....................................... SUCCESS [ 19.113 s] [INFO] mmm-property-builder ............................... SUCCESS [ 3.507 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 22.998 s [INFO] Finished at: 2023-11-06T10:50:31+05:30 [INFO] ------------------------------------------------------------------------
06-11-2023