diff options
author | Jay Arthanareeswaran | 2020-12-14 07:06:45 +0000 |
---|---|---|
committer | Jay Arthanareeswaran | 2021-01-06 05:20:10 +0000 |
commit | 081731e7bc274206c1e6b549e55d0515faab967b (patch) | |
tree | f2b730c245f2040af50b676d39580c1180b941ac | |
parent | d5c188d78ee37a5a78869313b94fa62ff542cea6 (diff) | |
download | eclipse.jdt.core-081731e7bc274206c1e6b549e55d0515faab967b.tar.gz eclipse.jdt.core-081731e7bc274206c1e6b549e55d0515faab967b.tar.xz eclipse.jdt.core-081731e7bc274206c1e6b549e55d0515faab967b.zip |
Bug 569922 - Pattern matching instanceof regressionI20210117-1940I20210117-1800I20210116-1800I20210116-0340I20210115-1810I20210114-1800I20210114-0510I20210114-0310I20210112-1800I20210112-0850I20210111-1800
Change-Id: I728d0f9c300ac6781e0dbf60edb2fc7f43330809
Signed-off-by: Jay Arthanareeswaran <jarthana@in.ibm.com>
2 files changed, 58 insertions, 4 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PatternMatching15Test.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PatternMatching15Test.java index c07000b839..aa742bc584 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PatternMatching15Test.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PatternMatching15Test.java @@ -3281,10 +3281,9 @@ public class PatternMatching15Test extends AbstractRegressionTest { "public class X {\n" + " static void foo(Object o) {\n" + " if (!(o instanceof X x) || x != null || x!= null) {\n" - + " System.out.println(x);\n // not allowed" + + " System.out.println(x); // not allowed\n" + " }\n" + " }\n" - + " }\n" + " public static void main(String[] args) {\n" + " foo(new X());\n" + " }\n" @@ -3292,7 +3291,7 @@ public class PatternMatching15Test extends AbstractRegressionTest { }, "----------\n" + "1. ERROR in X.java (at line 5)\n" + - " System.out.println(x);\n" + + " System.out.println(x); // not allowed\n" + " ^\n" + "x cannot be resolved to a variable\n" + "----------\n", @@ -3300,4 +3299,50 @@ public class PatternMatching15Test extends AbstractRegressionTest { true, compilerOptions); } + public void test075() { + Map<String, String> compilerOptions = getCompilerOptions(true); + runConformTest( + new String[] { + "X.java", + "@SuppressWarnings(\"preview\")\n"+ + "public class X {\n" + + " public boolean isMyError(Exception e) {\n" + + " return e instanceof MyError my && (my.getMessage().contains(\"something\") || my.getMessage().contains(\"somethingelse\"));\n" + + " }\n" + + " public static void main(String[] args) {\n" + + " System.out.println(\"hello\");\n" + + " }\n" + + "}\n" + + "class MyError extends Exception {}\n", + }, + "hello", + compilerOptions); + } + public void test076() { + Map<String, String> compilerOptions = getCompilerOptions(true); + runNegativeTest( + new String[] { + "X.java", + "@SuppressWarnings(\"preview\")\n" + + "public class X {\n" + + " static void foo(Object o) {\n" + + " if ( (! (o instanceof String a)) || (o instanceof String a) ) {\n" + + " // Nothing\n" + + " }\n" + + " }\n" + + " public static void main(String[] args) {\n" + + " System.out.println(\"hello\");\n" + + " }\n" + + "}\n", + }, + "----------\n" + + "1. ERROR in X.java (at line 4)\n" + + " if ( (! (o instanceof String a)) || (o instanceof String a) ) {\n" + + " ^\n" + + "Duplicate local variable a\n" + + "----------\n", + null, + true, + compilerOptions); + } } diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/OR_OR_Expression.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/OR_OR_Expression.java index aeaab4f884..935ab1b932 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/OR_OR_Expression.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/OR_OR_Expression.java @@ -279,13 +279,22 @@ public class OR_OR_Expression extends BinaryExpression { } @Override public void collectPatternVariablesToScope(LocalVariableBinding[] variables, BlockScope scope) { + LocalVariableBinding[] temp = variables; this.left.collectPatternVariablesToScope(variables, scope); // Just keep the ones in false scope variables = this.left.getPatternVariablesWhenFalse(); this.addPatternVariablesWhenFalse(variables); - this.right.collectPatternVariablesToScope(variables, scope); + int length = (variables == null ? 0 : variables.length) + (temp == null ? 0 : temp.length); + LocalVariableBinding[] newArray = new LocalVariableBinding[length]; + if (variables != null) { + System.arraycopy(variables, 0, newArray, 0, variables.length); + } + if (temp != null) { + System.arraycopy(temp, 0, newArray, (variables == null ? 0 : variables.length), temp.length); + } + this.right.collectPatternVariablesToScope(newArray, scope); variables = this.right.getPatternVariablesWhenFalse(); this.addPatternVariablesWhenFalse(variables); |