Skip to content

Commit

Permalink
ECJ accepts illegal modifiers in classic instanceof expression (#1080)
Browse files Browse the repository at this point in the history
* Fix ECJ's incorrect behavior in accepting illegal modifiers in classic
instanceof expression

* Fix DocumentElementParser's violation of stack discipline.

* Improve error message coordinates

Fixes #1076
  • Loading branch information
srikanth-sankaran committed Jun 8, 2023
1 parent b1cb266 commit 3d72ef7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4605,8 +4605,11 @@ protected void consumeInstanceOfExpression() {
new InstanceOfExpression(
this.expressionStack[this.expressionPtr],
typeRef);
this.intPtr--; // skip modifierSourceStart
this.intPtr--; // lose the fake modifier if any
int anyModifiersourceStart = this.intStack[this.intPtr--];
int anyModifiers = this.intStack[this.intPtr--];
if (anyModifiers != 0) {
problemReporter().illegalModifiers(anyModifiersourceStart, typeRef.sourceEnd);
}
}

if (exp.sourceEnd == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,25 @@ public void test009() {
"s1 cannot be resolved to a variable\n" +
"----------\n");
}
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1076
// ECJ accepts invalid Java code instanceof final Type
public void testGH1076() {
runNegativeTest(
new String[] {
"X.java",
"class Test {\n" +
" void check() {\n" +
" Number n = Integer.valueOf(1);\n" +
" if (n instanceof final Integer) {}\n" +
" if (n instanceof final Integer x) {}\n" +
" }\n" +
"}\n",
},
"----------\n" +
"1. ERROR in X.java (at line 4)\n" +
" if (n instanceof final Integer) {}\n" +
" ^^^^^^^^^^^^^\n" +
"Syntax error, modifiers are not allowed here\n" +
"----------\n");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -806,13 +806,13 @@ protected void consumeFormalParameter(boolean isVarArgs) {
}
@Override
protected void consumeInstanceOfExpression() {
super.consumeInstanceOfExpression();
this.intPtr--; // skip declarationSourceStart of modifiers
super.consumeInstanceOfExpression();
}
@Override
protected void consumeInstanceOfExpressionWithName() {
super.consumeInstanceOfExpressionWithName();
this.intPtr--; // skip declarationSourceStart of modifiers
super.consumeInstanceOfExpressionWithName();
}
/*
*
Expand Down

0 comments on commit 3d72ef7

Please sign in to comment.