Skip to content

Commit 5302657

Browse files
ECJ compile error: "Bound mismatch: The generic method ... is not applicable for the arguments" (eclipse-jdt#4041)
always "normalize" `? extends capture#1-of ? extends X` to `? extends X` Fixes eclipse-jdt#4033
1 parent abd056f commit 5302657

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/LookupEnvironment.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,7 @@ public RawTypeBinding createRawType(ReferenceBinding genericType, ReferenceBindi
14731473
}
14741474

14751475
public WildcardBinding createWildcard(ReferenceBinding genericType, int rank, TypeBinding bound, TypeBinding[] otherBounds, int boundKind) {
1476+
bound = normalizeWildcardBound(bound, boundKind);
14761477
if (genericType != null) {
14771478
AnnotationBinding[] annotations = genericType.typeAnnotations;
14781479
if (annotations != Binding.NO_ANNOTATIONS)
@@ -1486,9 +1487,19 @@ public CaptureBinding createCapturedWildcard(WildcardBinding wildcard, Reference
14861487
}
14871488

14881489
public WildcardBinding createWildcard(ReferenceBinding genericType, int rank, TypeBinding bound, TypeBinding[] otherBounds, int boundKind, AnnotationBinding [] annotations) {
1490+
bound = normalizeWildcardBound(bound, boundKind);
14891491
return this.typeSystem.getWildcard(genericType, rank, bound, otherBounds, boundKind, annotations);
14901492
}
14911493

1494+
private TypeBinding normalizeWildcardBound(TypeBinding bound, int boundKind) {
1495+
if (boundKind == Wildcard.EXTENDS && bound.getClass() == CaptureBinding.class) {
1496+
CaptureBinding capture = (CaptureBinding) bound;
1497+
if (capture.firstBound != null && capture.otherUpperBounds() == Binding.NO_TYPES && capture.wildcard.boundKind() == Wildcard.EXTENDS)
1498+
return capture.firstBound;
1499+
}
1500+
return bound;
1501+
}
1502+
14921503
/**
14931504
* Returns the access restriction associated to a given type, or null if none
14941505
*/

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest_9.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,31 @@ public Set<String> getAllLabels(FluentIterable<TaskDefinition> from) {
11861186
"""
11871187
});
11881188
}
1189+
public void testGH4033() {
1190+
runConformTest(new String[] {
1191+
"Snippet.java",
1192+
"""
1193+
import java.util.Collection;
1194+
import java.util.Iterator;
1195+
1196+
public class Snippet {
1197+
interface Apple {}
1198+
interface Banana<T1, T2> {}
1199+
interface Smoothie<T extends Apple, M extends Apple> extends Banana<T, String> {}
1200+
1201+
public static void main(String[] args) {
1202+
Collection<Smoothie<? extends Apple, ? extends Apple>> c = null;
1203+
method(c);
1204+
}
1205+
1206+
static final <S extends Banana<? extends T, ?>, T> Iterator<T> method(
1207+
Collection<S> c) {
1208+
return null;
1209+
}
1210+
}
1211+
"""
1212+
});
1213+
}
11891214
public static Class<GenericsRegressionTest_9> testClass() {
11901215
return GenericsRegressionTest_9.class;
11911216
}

0 commit comments

Comments
 (0)