mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-06 03:21:12 +07:00
[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:
committed by
intellij-monorepo-bot
parent
28572f3d5a
commit
f91bf8e788
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user