[java-highlighting] IDEA-353876 Support intersection type for switch selectors

GitOrigin-RevId: 075363ad27e93718b10b79234904eed7ac919436
This commit is contained in:
Mikhail Pyltsin
2024-05-30 17:34:52 +02:00
committed by intellij-monorepo-bot
parent e198cda518
commit 7327bf910a
3 changed files with 33 additions and 0 deletions

View File

@@ -1036,6 +1036,11 @@ final class PatternHighlightingModel {
.filter(t -> t != null)
.forEach(t -> selectorTypes.add(t));
}
if (selectorType instanceof PsiIntersectionType psiIntersectionType) {
for (PsiType conjunct : psiIntersectionType.getConjuncts()) {
selectorTypes.addAll(getAllTypes(conjunct));
}
}
if (selectorTypes.isEmpty()) {
selectorTypes.add(selectorType);
}

View File

@@ -0,0 +1,24 @@
package dfa;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
public final class ExhaustivenessWithIntersectionType {
public static void main(String[] ignoredArgs) {
CompletableFuture
.supplyAsync(() -> new Random().nextBoolean() ? new Result.Success() : new Result.Failure())
.thenApply(res ->
switch (res) {
case Result.Success _ -> 1;
case Result.Failure _ -> 2;
})
.thenAccept(System.out::println);
}
sealed interface Result {
record Success() implements Result { }
record Failure() implements Result { }
}
}

View File

@@ -214,6 +214,10 @@ public class LightPatternsForSwitchHighlightingTest extends LightJavaCodeInsight
IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_21, this::doTest);
}
public void testExhaustivenessWithIntersectionType() {
IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_22, this::doTest);
}
private void doTest() {
myFixture.configureByFile(getTestName(false) + ".java");
myFixture.checkHighlighting();