Skip to content

Commit

Permalink
[Switch Expressions] ArrayIndexOutOfBoundsException in Scope.leastCon…
Browse files Browse the repository at this point in the history
…tainingInvocation for sealed class and switch (#3575)

* Fixes #3554
  • Loading branch information
srikanth-sankaran authored Jan 20, 2025
1 parent 121e230 commit 834a03e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,9 @@ private TypeBinding resultType() {
/** Add an expression to known result expressions, gather some aggregate characteristics if in standalone context.
* @return a flag indicating the overall well-formedness of result expression set.
*/
public boolean add(/*@NonNull*/ Expression rxpression) {
public boolean add(/*@NonNull*/ Expression rxpression, TypeBinding rxpressionType) {

this.rExpressions.add(rxpression);

TypeBinding rxpressionType = rxpression.resolvedType;
if (rxpressionType == null) { // tolerate poly-expression resolving to null in the absence of target type.
if (!rxpression.isPolyExpression() || ((IPolyExpression) rxpression).expectedType() != null)
this.allWellFormed = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ public void resolve(BlockScope scope) {
}
}

this.expression.resolveType(scope);
TypeBinding expressionType = this.expression.resolveType(scope);
if (this.switchExpression != null)
this.switchExpression.results.add(this.expression);
this.switchExpression.results.add(this.expression, expressionType);

if (this.isImplicit) {
if (this.switchExpression == null && !this.expression.statementExpression()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8399,4 +8399,44 @@ public static void main(String [] args) {
},
"a = -2 b = -1 c = -3");
}

// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3554
// [Switch Expressions] ArrayIndexOutOfBoundsException in Scope.leastContainingInvocation for sealed class and switch
public void testIssue3554() {
if (this.complianceLevel < ClassFileConstants.JDK21)
return;
this.runNegativeTest(
new String[] {
"X.java",
"""
public class X {
sealed interface Index {
enum TimeIndex implements Index {
SECONDS;
}
}
public class AbstractLine<S extends Index> {}
public class AbstractTimeLine extends AbstractLine<X.Index.TimeIndex> {}
@SuppressWarnings("unchecked")
public static <S extends Index> AbstractLine<S> create(int id, S index, int owner) {
return (AbstractLine<S>) switch (index) {
case X.Index.TimeIndex tindex when tindex == X.Index.TimeIndex.SECONDS -> new AbstractTimeLine();
default -> new AbstractLine<>(state().newId(), index, owner);
};
}
}
"""
},
"----------\n" +
"1. ERROR in X.java (at line 18)\n" +
" default -> new AbstractLine<>(state().newId(), index, owner);\n" +
" ^^^^^\n" +
"The method state() is undefined for the type X\n" +
"----------\n");
}
}

0 comments on commit 834a03e

Please sign in to comment.