[java-psi] Fix resolve for patterns in guards of old-style switch labels (relates to IDEA-326939)

GitOrigin-RevId: 77b81cc6df6c9b600b31261a3e83c1f552f5d06f
This commit is contained in:
Tagir Valeev
2023-09-06 15:09:44 +02:00
committed by intellij-monorepo-bot
parent 28572f3d5a
commit f91bf8e788
5 changed files with 53 additions and 13 deletions

View File

@@ -72,10 +72,6 @@ public abstract class PsiSwitchLabelStatementBaseImpl extends CompositePsiElemen
}
}
}
PsiExpression guardExpression = getGuardExpression();
if (guardExpression != null) {
return guardExpression.processDeclarations(processor, PatternResolveState.WHEN_TRUE.putInto(state), null, place);
}
return true;
}
@@ -84,4 +80,16 @@ public abstract class PsiSwitchLabelStatementBaseImpl extends CompositePsiElemen
public @Nullable PsiCaseLabelElementList getCaseLabelElementList() {
return PsiTreeUtil.getChildOfType(this, PsiCaseLabelElementList.class);
}
protected boolean processPatternVariables(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, @NotNull PsiElement place) {
final PsiCaseLabelElementList patternsInCaseLabel = getCaseLabelElementList();
if (patternsInCaseLabel == null) return true;
if (!patternsInCaseLabel.processDeclarations(processor, state, null, place)) return false;
PsiExpression guardExpression = getGuardExpression();
if (guardExpression != null) {
return guardExpression.processDeclarations(processor, PatternResolveState.WHEN_TRUE.putInto(state), null, place);
}
return true;
}
}

View File

@@ -70,12 +70,7 @@ public class PsiSwitchLabelStatementImpl extends PsiSwitchLabelStatementBaseImpl
@NotNull PsiElement place) {
if (!super.processDeclarations(processor, state, lastParent, place)) return false;
if (!shouldAnalyzePatternVariablesInCaseLabel(place)) return true;
final PsiCaseLabelElementList patternsInCaseLabel = getCaseLabelElementList();
if (patternsInCaseLabel == null) return true;
return patternsInCaseLabel.processDeclarations(processor, state, null, place);
return !shouldAnalyzePatternVariablesInCaseLabel(place) || processPatternVariables(processor, state, place);
}
/**

View File

@@ -51,9 +51,7 @@ public class PsiSwitchLabeledRuleStatementImpl extends PsiSwitchLabelStatementBa
if (!shouldProcess()) return true;
final PsiCaseLabelElementList patternsInCaseLabel = getCaseLabelElementList();
if (patternsInCaseLabel == null) return true;
return patternsInCaseLabel.processDeclarations(processor, state, null, place);
return processPatternVariables(processor, state, place);
}
private boolean shouldProcess() {

View File

@@ -0,0 +1,35 @@
import java.util.function.*;
interface Something {
}
enum E implements Something {A, B,}
record P(String s) implements Something {
}
class Test {
public static void main(String[] args) {
}
public int main1(Something something) {
return switch (something) {
case P(_) when something instanceof Object o: yield o.hashCode();
case E _ when something instanceof Object o: yield o.hashCode();
default: {
yield 2;
}
};
}
public int main2(Something something) {
return switch (something) {
case P(_) when something instanceof Object o: yield o.hashCode();
case E _ when something instanceof Object o1: yield <error descr="Cannot resolve symbol 'o'">o</error>.hashCode();
default: {
yield 2;
}
};
}
}

View File

@@ -22,6 +22,10 @@ public class LightUnnamedVariablesHighlightingTest extends LightJavaCodeInsightF
public void testUnnamedVariables() {
doTest();
}
public void testUnnamedVariablesInGuard() {
doTest();
}
public void testUnnamedVariablesJava9() {
IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_1_9, () -> doTest());