Skip to content

Commit a217c97

Browse files
Compilation error with ECJ but not with javac
+ don't ask left & right of conditional expr for their type if poly Fixes eclipse-jdt#4308
1 parent 947934d commit a217c97

2 files changed

Lines changed: 60 additions & 3 deletions

File tree

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/ConditionalExpression.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -789,12 +789,12 @@ public boolean isPolyExpression() throws UnsupportedOperationException {
789789
if (this.expressionContext != ASSIGNMENT_CONTEXT && this.expressionContext != INVOCATION_CONTEXT)
790790
return false;
791791

792-
if (this.originalValueIfTrueType == null || this.originalValueIfFalseType == null) // resolution error.
793-
return false;
794-
795792
if (this.valueIfTrue.isPolyExpression() || this.valueIfFalse.isPolyExpression())
796793
return true;
797794

795+
if (this.originalValueIfTrueType == null || this.originalValueIfFalseType == null) // resolution error.
796+
return false;
797+
798798
// "... unless both operands produce primitives (or boxed primitives)":
799799
if (this.originalValueIfTrueType.isBaseType() || (this.originalValueIfTrueType.id >= TypeIds.T_JavaLangByte && this.originalValueIfTrueType.id <= TypeIds.T_JavaLangBoolean)) {
800800
if (this.originalValueIfFalseType.isBaseType() || (this.originalValueIfFalseType.id >= TypeIds.T_JavaLangByte && this.originalValueIfFalseType.id <= TypeIds.T_JavaLangBoolean))

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,63 @@ public class LoadExtension extends AbstractLoad<Integer> {
14201420
""";
14211421
runner.runWarningTest();
14221422
}
1423+
public void testGH4308() {
1424+
runConformTest(new String[] {
1425+
"ClassA.java",
1426+
"""
1427+
import java.lang.reflect.InvocationTargetException;
1428+
import java.util.HashSet;
1429+
import java.util.Map;
1430+
import java.util.Set;
1431+
import java.util.function.Consumer;
1432+
import java.util.stream.Collectors;
1433+
1434+
public class ClassA {
1435+
1436+
private static final Map<ClassB, Consumer<ClassC>> S;
1437+
1438+
static {
1439+
S = load(ClassB.class).stream().collect(Collectors.toMap(s -> s, // Compilation error
1440+
s -> s.ping() ? s::sync : ClassD.getInstance()::replay));
1441+
}
1442+
1443+
private static <T> Set<T> load(Class<T> clazz) {
1444+
Set<T> set = new HashSet<>();
1445+
try {
1446+
set.add(clazz.getConstructor().newInstance());
1447+
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
1448+
e.printStackTrace();
1449+
}
1450+
return set;
1451+
}
1452+
1453+
private static class ClassB {
1454+
public ClassB() {}
1455+
boolean ping() {
1456+
return false;
1457+
}
1458+
void sync(ClassC classC) { }
1459+
}
1460+
1461+
private static class ClassD {
1462+
private static ClassD getInstance() {
1463+
return new ClassD();
1464+
}
1465+
void replay(ClassC classC) {
1466+
System.out.println("replay");
1467+
}
1468+
}
1469+
1470+
private static class ClassC { }
1471+
1472+
public static void main(String[] args) {
1473+
S.values().forEach(consumer -> consumer.accept(new ClassC()));
1474+
}
1475+
}
1476+
"""
1477+
},
1478+
"replay");
1479+
}
14231480
public static Class<GenericsRegressionTest_9> testClass() {
14241481
return GenericsRegressionTest_9.class;
14251482
}

0 commit comments

Comments
 (0)