diff options
author | Manoj Palat | 2021-10-28 08:05:09 +0000 |
---|---|---|
committer | Manoj Palat | 2021-10-29 10:05:52 +0000 |
commit | ed54c185d4c209ffcfd5fccd1e7035d3b73685ff (patch) | |
tree | 64d9868b0d10705995569ea25e77cbaea22f12ae | |
parent | 61c79ebdcd4b621eaf7fdc2b991d65447db7ebe2 (diff) | |
download | eclipse.jdt.core-ed54c185d4c209ffcfd5fccd1e7035d3b73685ff.tar.gz eclipse.jdt.core-ed54c185d4c209ffcfd5fccd1e7035d3b73685ff.tar.xz eclipse.jdt.core-ed54c185d4c209ffcfd5fccd1e7035d3b73685ff.zip |
Bug 576861 - Internal compiler error when switch expression returns aI20211101-1800I20211031-1800I20211030-1800I20211029-1800
functional interface
Change-Id: Ia88420f808bf5a164a4a8af24b0ecfcd68348ae9
Signed-off-by: Manoj Palat <manpalat@in.ibm.com>
Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/187077
3 files changed, 30 insertions, 2 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionsYieldTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionsYieldTest.java index 9bfd9e196b..eb15476e8b 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionsYieldTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionsYieldTest.java @@ -6115,4 +6115,25 @@ public class SwitchExpressionsYieldTest extends AbstractRegressionTest { }, "Success"); } + public void testBug576861_001() { + this.runConformTest( + new String[] { + "X.java", + "import java.util.Comparator;\n"+ + "\n"+ + "public class X {\n"+ + " public static void foo(Comparator<? super Long> comparator) {}\n"+ + "\n"+ + " public static void main(String[] args) {\n"+ + " int someSwitchCondition = 10;\n"+ + " X.foo(switch (someSwitchCondition) {\n"+ + " case 10 -> Comparator.comparingLong(Long::longValue);\n"+ + " default -> throw new IllegalArgumentException(\"Unsupported\");\n"+ + " });\n"+ + " System.out.println(\"hello\");\n"+ + " }\n"+ + "}" + }, + "hello"); + } } diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchExpression.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchExpression.java index 806abac23c..f9801a9fa1 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchExpression.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchExpression.java @@ -53,6 +53,8 @@ public class SwitchExpression extends SwitchStatement implements IPolyExpression private boolean isPolyExpression = false; private TypeBinding[] originalValueResultExpressionTypes; private TypeBinding[] finalValueResultExpressionTypes; + /* package */ Map<Expression, TypeBinding> originalTypeMap; + private int nullStatus = FlowInfo.UNKNOWN; public List<Expression> resultExpressions; @@ -473,6 +475,8 @@ public class SwitchExpression extends SwitchStatement implements IPolyExpression } } + if (this.originalTypeMap == null) + this.originalTypeMap = new HashMap<>(); resolve(upperScope); if (this.statements == null || this.statements.length == 0) { @@ -512,7 +516,8 @@ public class SwitchExpression extends SwitchStatement implements IPolyExpression return this.resolvedType = null; // error flagging would have been done during the earlier phase. for (int i = 0; i < resultExpressionsCount; i++) { Expression resultExpr = this.resultExpressions.get(i); - if (resultExpr.resolvedType == null || resultExpr.resolvedType.kind() == Binding.POLY_TYPE) { + TypeBinding origType = this.originalTypeMap.get(resultExpr); + if (origType == null || origType.kind() == Binding.POLY_TYPE) { this.finalValueResultExpressionTypes[i] = this.originalValueResultExpressionTypes[i] = resultExpr.resolveTypeExpecting(upperScope, this.expectedType); } diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/YieldStatement.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/YieldStatement.java index f626942837..e750fd539a 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/YieldStatement.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/YieldStatement.java @@ -245,7 +245,9 @@ public void resolve(BlockScope skope) { this.scope.problemReporter().switchExpressionsYieldOutsideSwitchExpression(this); } } - this.expression.resolveType(this.scope); + TypeBinding type = this.expression.resolveType(this.scope); + if (this.switchExpression != null && type != null) + this.switchExpression.originalTypeMap.put(this.expression, type); } @Override |