Skip to content

Commit b2bb215

Browse files
committed
WIP - order of resolved variables is wrong
Ref pmd#5441 Below the inference trace. What happens here is we have the following inference context. Inference context 7 'a { 'a <: java.lang.String, 'a <: 'e, 'a <: java.lang.Object } 'b { 'b <: java.lang.Object, 'b <: java.lang.Integer } 'e { 'e <: java.lang.Object, 'e >: 'a } 'f { 'f <: java.lang.Object, 'f <: java.lang.Integer } The dependency graph groups 'a and 'e together, as they depend on one another. Here is the problem: - 'a is resolved first. By rule UPPER, it is instantiated to String - 'e is resolved next. By rule LOWER, it is instantiated to 'a, ie to String. If the variables were solved in the reverse order, the following would happen: - 'e is instantiated to Object (by rule UPPER) - 'a is instantiated to Object (by rule UPPER) The latter is the javac behavior, which is why I mention this is a problem. However I do not think our implementation is non-compliant here. The JLS does not specify the resolution order here AFAICT. I also didn't dive deep enough in javac to know why it does what it does. It is possible that it adds a dependency between 'a and 'e to force 'e to be resolved earlier. It has a system of "stuck" dependencies that we don't have. I believe It is also possible that this is just happenstance. To investigate this further, I should look into the dependency graph produced by javac and understand better what it does here. --- Phase STRICT newCacheBuilder(java.lang.String) -> Test$CacheBuilder<K, V> ARGUMENTS Checking arg 0 against java.lang.String At: (unknown):22:59 Expr: \"test\" Success: newCacheBuilder(java.lang.String) -> Test$CacheBuilder<java.lang.String, java.lang.Integer> Skipping instantiation of Test.<K, V> newCacheBuilder(java.lang.String) -> Test$CacheBuilder<java.lang.String, java.lang.Integer>, it's already complete Phase STRICT build(Test#CacheLoader<? super K1, V1>) -> Test$Cache<K1, V1> Changed bound (ctx 3): 'a <: java.lang.String Changed bound (ctx 3): 'b <: java.lang.Integer Context 3 build(Test#CacheLoader<? super 'a, 'b>) -> Test$Cache<'a, 'b> ARGUMENTS Checking arg 0 against Test#CacheLoader<? super 'a, 'b> At: (unknown):23:32 Expr: new CacheLoader<>() Phase STRICT new() -> Test#CacheLoader<K, V> Context 4 new() -> Test#CacheLoader<'c, 'd> ARGUMENTS Solving with context 5 for applicability testing | Ivar instantiated (ctx 5): 'c := java.lang.Object | Ivar instantiated (ctx 5): 'd := java.lang.Object Success: new() -> Test#CacheLoader<'c, 'd> Phase INVOC_STRICT new() -> Test#CacheLoader<K, V> Context 6 new() -> Test#CacheLoader<'e, 'f> RETURN New bound (ctx 6): 'e >: 'a New bound (ctx 3): 'b = 'f ARGUMENTS New bound (ctx 3): 'a <: 'e Ctx 3 adopts ['e, 'f] from ctx 6 Success: new() -> Test#CacheLoader<'e, 'f> New bound (ctx 3): 'f <: java.lang.Integer Ivar merged (ctx 3): 'b <=> 'f Solving with context 7 for applicability testing | Ivar instantiated (ctx 7): 'a := java.lang.String | New bound (ctx 3): 'e >: java.lang.String | Ivar instantiated (ctx 7): 'e := java.lang.String | Ivar instantiated (ctx 7): 'b := java.lang.Integer Success: build(Test#CacheLoader<? super 'a, 'f>) -> Test$Cache<'a, 'f> Phase INVOC_STRICT build(Test#CacheLoader<? super K1, V1>) -> Test$Cache<K1, V1> Changed bound (ctx 8): 'g <: java.lang.String Changed bound (ctx 8): 'h <: java.lang.Integer Context 8 build(Test#CacheLoader<? super 'g, 'h>) -> Test$Cache<'g, 'h> RETURN ARGUMENTS Checking arg 0 against Test#CacheLoader<? super 'g, 'h> At: (unknown):23:32 Expr: new CacheLoader<>() Phase INVOC_STRICT new() -> Test#CacheLoader<K, V> Context 9 new() -> Test#CacheLoader<'i, 'j> RETURN New bound (ctx 9): 'i >: 'g New bound (ctx 8): 'h = 'j ARGUMENTS New bound (ctx 8): 'g <: 'i Ctx 8 adopts ['i, 'j] from ctx 9 Success: new() -> Test#CacheLoader<'i, 'j> New bound (ctx 8): 'j <: java.lang.Integer Ivar merged (ctx 8): 'h <=> 'j Ivar instantiated (ctx 8): 'g := java.lang.String New bound (ctx 8): 'i >: java.lang.String Ivar instantiated (ctx 8): 'i := java.lang.String Ivar instantiated (ctx 8): 'h := java.lang.Integer Success: build(Test#CacheLoader<? super java.lang.String, java.lang.Integer>) -> Test$Cache<java.lang.String, java.lang.Integer>
1 parent 25974fb commit b2bb215

5 files changed

Lines changed: 84 additions & 3 deletions

File tree

‎pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UseDiamondOperatorRule.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private static String produceSuggestedExprImage(ASTConstructorCall ctor) {
144144
produceSameTypeWithDiamond(ctor.getTypeNode(), sb, true);
145145
ASTArgumentList arguments = ctor.getArguments();
146146
String argsString;
147-
if (arguments.size() == 0) {
147+
if (arguments.isEmpty()) {
148148
argsString = "()";
149149
} else {
150150
CharSequence text = arguments.getText();

‎pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/InferenceContext.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ private boolean solve(Supplier<VarWalkStrategy> newWalker) {
526526
*/
527527
private boolean solve(VarWalkStrategy walker) {
528528
graphWasChanged = false;
529-
incorporate();
529+
// incorporate();
530530

531531
while (walker.hasNext()) {
532532

‎pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/types/internal/infer/CtorInferenceTest.kt‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,44 @@ class CtorInferenceTest : ProcessorTestSpec({
353353
ctorCall.methodType.symbol shouldBe ctorSymbol
354354
}
355355
}
356+
357+
parserTest("f:Diamond inference with no constraint") {
358+
val (acu, _) = parser.logTypeInferenceVerbose().parseWithTypeInferenceSpy(
359+
"""
360+
class Test {
361+
362+
interface Cache<K, V> {
363+
}
364+
365+
class CacheLoader<K, V> {
366+
V fetch(K key) {
367+
return null;
368+
}
369+
}
370+
371+
interface CacheBuilder<K, V> {
372+
<K1 extends K, V1 extends V> Cache<K1, V1> build(CacheLoader<? super K1, V1> loader);
373+
}
374+
375+
376+
static <K, V> CacheBuilder<K, V> newCacheBuilder(String id) {
377+
}
378+
379+
void test() {
380+
var cache = Test.<String, Integer>newCacheBuilder("test")
381+
.build(new CacheLoader<>());
382+
}
383+
}
384+
385+
"""
386+
)
387+
388+
val (_, t_Cache, t_CacheLoader) = acu.declaredTypeSignatures()
389+
val ctorCall = acu.firstCtorCall()
390+
391+
ctorCall.withTypeDsl { // for the enclosing method call
392+
ctorCall shouldHaveType t_CacheLoader[ts.OBJECT, int.box()]
393+
acu.varId("cache") shouldHaveType t_Cache[ts.STRING, int.box()]
394+
}
395+
}
356396
})

‎pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryCast.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import java.util.ArrayList;
1313
import java.util.Arrays;
1414
import java.util.List;
1515
import java.util.Map;
16-
import java.uilt.HashMap;
16+
import java.util.HashMap;
1717
1818
public class UnnecessaryCastSample {
1919
private Map<Integer, String> map = new HashMap<>();

‎pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UseDiamondOperator.xml‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,4 +528,45 @@ public class Foo {
528528
}]]>
529529
</code>
530530
</test-code>
531+
<test-code>
532+
<description>[java] UseDiamondOperator reported but Java 21 is unable to infer the type #5441</description>
533+
<expected-problems>0</expected-problems>
534+
<code><![CDATA[import java.util.List;
535+
536+
class Test {
537+
interface CacheLoader<K, V> {
538+
}
539+
540+
interface CacheBuilder<K, V> {
541+
<K1 extends K, V1 extends V> AsyncCache<K1, V1> buildAsync(CacheLoader<? super K1, V1> loader);
542+
}
543+
544+
interface Cache<K, V> {
545+
}
546+
547+
interface AsyncCache<K, V> {
548+
Cache<K, V> synchronous();
549+
}
550+
551+
static class ConcreteCacheLoader<K, V> implements CacheLoader<K, V> {
552+
V fetch(K key) {
553+
return null;
554+
}
555+
}
556+
557+
static <K, V> CacheBuilder<K, V> newCacheBuilder(String id) {
558+
}
559+
560+
Cache<String, List<Integer>> failure() {
561+
return Test.<String, List<Integer>>newCacheBuilder("test")
562+
.buildAsync(new ConcreteCacheLoader<String, List<Integer>>() {
563+
@Override
564+
List<Integer> fetch(String key) {
565+
}
566+
})
567+
.synchronous();
568+
}
569+
}
570+
]]></code>
571+
</test-code>
531572
</test-data>

0 commit comments

Comments
 (0)