From 656af653434cdcd5400d8f2b608cd2b31157fd7a Mon Sep 17 00:00:00 2001 From: Manoj N Palat Date: Wed, 16 Aug 2023 11:18:37 +0530 Subject: [PATCH] Fixes #587 [pattern switch] Parameterized Type check in exhaustiveness (#1292) --- .../lookup/ParameterizedTypeBinding.java | 11 ++++++++- .../regression/SwitchPatternTest.java | 24 ++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ParameterizedTypeBinding.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ParameterizedTypeBinding.java index df833128834..b8a2e4d1b35 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ParameterizedTypeBinding.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ParameterizedTypeBinding.java @@ -1111,7 +1111,16 @@ public ReferenceBinding[] permittedTypes() { ReferenceBinding[] permTypes = this.type.permittedTypes(); List applicablePermTypes = new ArrayList<>(); for (ReferenceBinding pt : permTypes) { - if (pt.isCompatibleWith(this)) + ReferenceBinding permittedTypeAvatar = pt; + if (pt.isRawType()) { + ReferenceBinding ptRef = pt.actualType(); + // don't use TypeSystem.getParameterizedType below as this is just for temporary check. + ParameterizedTypeBinding ptb = new ParameterizedTypeBinding(ptRef, this.arguments, ptRef.enclosingType(), this.environment); + ptb.superclass(); + ptb.superInterfaces(); + permittedTypeAvatar = ptb; + } + if (permittedTypeAvatar.isCompatibleWith(this)) applicablePermTypes.add(pt); } return applicablePermTypes.toArray(new ReferenceBinding[0]); diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchPatternTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchPatternTest.java index b6d4e4379b1..3d0fb5929e3 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchPatternTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchPatternTest.java @@ -33,7 +33,7 @@ public class SwitchPatternTest extends AbstractRegressionTest9 { static { // TESTS_NUMBERS = new int [] { 40 }; // TESTS_RANGE = new int[] { 1, -1 }; -// TESTS_NAMES = new String[] { "testRecPatExhaust"}; +// TESTS_NAMES = new String[] { "testIssue587_001"}; } private static String previewLevel = "21"; @@ -6228,4 +6228,26 @@ public void testIssue1126b() { "1\n" + "0"); } + public void testIssue587_001() { + runConformTest( + new String[] { + "X.java", + "public class X {\n"+ + " sealed interface I permits A, B {}\n"+ + " final static class A implements I {}\n"+ + " final static class B implements I {}\n"+ + "\n"+ + " static int testGenericSealedExhaustive(I i) {\n"+ + " return switch (i) {\n"+ + " // Exhaustive as no A case possible!\n"+ + " case B bi -> 42;\n"+ + " };\n"+ + " }\n"+ + " public static void main(String[] args) {\n"+ + " System.out.println(testGenericSealedExhaustive(new B()));\n"+ + " }\n"+ + "}", + }, + "42"); + } }