Bug 435462: [1.8] NPE in codegen with nested conditional and allocation expressions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest_1_8.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest_1_8.java
index 0060ea8..77ec587 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest_1_8.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest_1_8.java
@@ -3168,16 +3168,33 @@
" new Bar(b ? 0 : new ArrayList<>());\n" +
" ^\n" +
"Type mismatch: cannot convert from int to Collection<String>\n" +
- "----------\n" +
- "3. ERROR in X.java (at line 9)\n" +
- " new Bar(b ? 0 : new ArrayList<>());\n" +
- " ^^^^^^^^^^^^^^^^^\n" +
- "Type mismatch: cannot convert from ArrayList<Object> to Collection<String>\n" +
- "----------\n" +
- "4. WARNING in X.java (at line 12)\n" +
+ "----------\n" +
+ "3. WARNING in X.java (at line 12)\n" +
" public Bar(Collection<String> col) { }\n" +
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
"The constructor X.Bar(Collection<String>) is never used locally\n" +
"----------\n");
}
+// https://bugs.eclipse.org/bugs/show_bug.cgi?id=435462 [1.8] NPE in codegen with nested conditional and allocation expressions
+public void testBug435462() {
+ this.runConformTest(
+ new String[] {
+ "X.java",
+ "import java.util.ArrayList;\n" +
+ "import java.util.Collection;\n" +
+ "import java.util.List;\n" +
+ "public class X {\n" +
+ " public static void main(String[] args) {\n" +
+ " }\n" +
+ " public void bla() {\n" +
+ " boolean b = Boolean.TRUE.booleanValue();\n" +
+ " List<String> c1 = new ArrayList<>();\n" +
+ " new Bar(b ? new ArrayList<>(b ? new ArrayList<>() : c1) : c1);\n" +
+ " }\n" +
+ " private static class Bar {\n" +
+ " public Bar(Collection<?> col) { }\n" +
+ " }\n" +
+ "}"
+ });
+}
}
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AllocationExpression.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AllocationExpression.java
index 019ad81..7722a54 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AllocationExpression.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AllocationExpression.java
@@ -725,7 +725,10 @@
return this.binding;
}
public TypeBinding checkAgainstFinalTargetType(TypeBinding targetType, Scope scope) {
- if (this.binding == null && this.suspendedResolutionState != null && !this.suspendedResolutionState.hasReportedError) {
+ this.typeExpected = targetType;
+ boolean needsUpdate = this.binding == null || // not yet resolved
+ (this.resolvedType != null && targetType != null && !this.resolvedType.isCompatibleWith(targetType)); // previous attempt was wrong
+ if (needsUpdate && this.suspendedResolutionState != null && !this.suspendedResolutionState.hasReportedError) {
// Attempt to resolve half resolved diamond
resolvePart2(this.suspendedResolutionState);
}