diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/Scanner.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/Scanner.java index 2760d441ae4..de41e49c285 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/Scanner.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/Scanner.java @@ -190,7 +190,7 @@ enum ScanContext { private VanguardParser vanguardParser; ConflictedParser activeParser = null; private boolean consumingEllipsisAnnotations = false; - protected boolean multiCaseLabelComma = false; + public boolean multiCaseLabelComma = false; public static final int RoundBracket = 0; public static final int SquareBracket = 1; diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/ResolveTests21.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/ResolveTests21.java index 9d03aa7b126..c9fc9227128 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/ResolveTests21.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/ResolveTests21.java @@ -115,4 +115,36 @@ private record FooBar(T object) implements Foo { elements ); } + +// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3050 +// [code select] Unexpected runtime error while computing a text hover: java.lang.NegativeArraySizeException +public void testIssue3050() throws JavaModelException { + this.wc = getWorkingCopy("/Resolve/src/Hover.java", + """ + interface Function { + R apply(T t); + } + + public class Hover { + + void d() { + Function f = d -> 2; + switch (f) { + case Function s -> {} + } + } + } + """ + ); + String str = this.wc.getSource(); + String selection = "Function"; + int start = str.lastIndexOf(selection); + int length = selection.length(); + IJavaElement[] elements = this.wc.codeSelect(start, length); + assertElementsEqual( + "Unexpected elements", + "Function [in [Working copy] Hover.java [in [in src [in Resolve]]]]", + elements + ); +} } diff --git a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/select/SelectionParser.java b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/select/SelectionParser.java index 643dc8a69cb..5f0453989ac 100644 --- a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/select/SelectionParser.java +++ b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/select/SelectionParser.java @@ -1459,14 +1459,16 @@ protected void consumeToken(int token) { pushOnElementStack(K_BETWEEN_CASE_AND_COLONORARROW, this.expressionPtr); break; case TokenNameCOMMA : - switch (topKnownElementKind(SELECTION_OR_ASSIST_PARSER)) { - // for multi constant case stmt - // case MONDAY, FRIDAY - // if there's a comma, ignore the previous expression (constant) - // Which doesn't matter for the next constant - case K_BETWEEN_CASE_AND_COLONORARROW: - this.expressionPtr--; - this.expressionLengthStack[this.expressionLengthPtr]--; + if (this.scanner.multiCaseLabelComma) { + switch (topKnownElementKind(SELECTION_OR_ASSIST_PARSER)) { + // for multi constant case stmt + // case MONDAY, FRIDAY + // if there's a comma, ignore the previous expression (constant) + // Which doesn't matter for the next constant + case K_BETWEEN_CASE_AND_COLONORARROW: + this.expressionPtr--; + this.expressionLengthStack[this.expressionLengthPtr]--; + } } break; case TokenNameARROW: