IDEA-229846 Resolve for pattern matching (except switch handling)

GitOrigin-RevId: b8addebac00f681641d5cef1089fa6f7d2d668a7
This commit is contained in:
Tagir Valeev
2019-12-27 07:33:29 +00:00
committed by intellij-monorepo-bot
parent 14183dd8a9
commit 0df312338d
21 changed files with 685 additions and 23 deletions
@@ -0,0 +1,61 @@
class X {
void expressions(Object obj) {
boolean b1 = obj instanceof String s && s.isEmpty();
boolean b2 = !(obj instanceof String s) && <error descr="Cannot resolve symbol 's'">s</error>.isEmpty();
boolean b3 = obj instanceof String s || <error descr="Cannot resolve symbol 's'">s</error>.isEmpty();
boolean b4 = !(obj instanceof String s) || s.isEmpty();
boolean b5 = obj instanceof String s ? s.isEmpty() : obj == null;
boolean b6 = !(obj instanceof String s) ? obj == null : s.isEmpty();
boolean b7 = obj instanceof String s ? s.isEmpty() : <error descr="Cannot resolve symbol 's'">s</error>.isEmpty();
boolean b8 = !(obj instanceof String s) ? <error descr="Cannot resolve symbol 's'">s</error>.isEmpty() : s.isEmpty();
}
void twoPatterns(Object o1, Object o2) {
if (o1 instanceof String s1 && o2 instanceof String s2 && s1.startsWith(s2)) {}
if ((o1 instanceof String s1 && o2 instanceof String s2) && s1.startsWith(s2)) {}
if (o1 instanceof String s1 && (o2 instanceof String s2 && s1.startsWith(s2))) {}
if (o1 instanceof String s1 && !(o2 instanceof String s2) && s1.startsWith(<error descr="Cannot resolve symbol 's2'">s2</error>)) {}
}
void polyadicInCondition(Object o1, Object o2) {
boolean b1 = o1 instanceof String s1 && o2 instanceof String s2 ? s1.isEmpty() && s2.isEmpty() : false;
boolean b2 = o1 instanceof String s1 && !(o2 instanceof String s2) ? s1.isEmpty() : <error descr="Cannot resolve symbol 's2'">s2</error>.isEmpty();
}
void ifThenSimple(Object o) {
if (o instanceof String s) {
System.out.println(s.trim());
} else {
System.out.println(<error descr="Cannot resolve symbol 's'">s</error>.trim());
}
if (!(o instanceof String s)) {
System.out.println(<error descr="Cannot resolve symbol 's'">s</error>.trim());
} else {
System.out.println(s.trim());
}
}
interface Node {
Object next();
String name();
}
void whileSimple(Object o) {
while (o instanceof Node n) {
o = n.next();
}
}
void whileNot(Object o) {
while (!(o instanceof Node n)) {
o = <error descr="Cannot resolve symbol 'n'">n</error>.next();
}
}
void forSimple(Object o) {
for (; o instanceof Node n; o = n.next()) {
System.out.println(n.name());
}
}
}
@@ -0,0 +1,120 @@
class X {
void simpleIf(Object obj) {
if (!(obj instanceof String s)) return;
System.out.println(s.trim());
}
void ifElse(Object obj) {
if (!(obj instanceof String s)) return;
else {
System.out.println(s.trim());
}
System.out.println(s.trim());
}
static boolean FLAG2 = true;
static final boolean FLAG = true;
void ifElseInfiniteLoop(Object obj) {
if (obj instanceof String s) {}
else {
while (FLAG) {
System.out.println("oops");
}
}
System.out.println(s);
}
void ifElseInfiniteLoopLocal(Object obj) {
final boolean FLAG = true;
if (obj instanceof String s) {}
else {
while (FLAG) {
System.out.println("oops");
}
}
System.out.println(s);
}
void ifElseFiniteLoop(Object obj) {
if (obj instanceof String s) {}
else {
while (FLAG2) {
System.out.println("oops");
}
}
System.out.println(<error descr="Cannot resolve symbol 's'">s</error>);
}
void ifElseInfiniteSelfRef(Object obj) {
if (obj instanceof Boolean b) {
while(b) {}
}
else {
while(<error descr="Cannot resolve symbol 'b'">b</error>) {}
}
while(<error descr="Cannot resolve symbol 'b'">b</error>) {}
}
class Shadow {
String b;
void ifElseInfiniteSelfRef(Object obj) {
if (obj instanceof Boolean b) {
while(b) {}
}
else {
while(b == "") {}
}
while(b == "") {}
}
void inverted(Object obj) {
if (!(obj instanceof Integer b)) {}
System.out.println(b.trim());
}
}
native Object getNextObj();
void testWhile(Object obj) {
while (!(obj instanceof Integer x)) {
obj = getNextObj();
}
System.out.println(x.intValue());
}
void testWhileWithBreak(Object obj) {
while (!(obj instanceof Integer x)) {
obj = getNextObj();
if (obj instanceof String) break;
}
System.out.println(<error descr="Cannot resolve symbol 'x'">x</error>.intValue());
}
void testDoWhile(Object obj) {
do {
obj = getNextObj();
if (obj instanceof String) break;
}
while (!(obj instanceof Integer x));
System.out.println(<error descr="Cannot resolve symbol 'x'">x</error>.intValue());
}
void testDoWhileWithBreak(Object obj) {
do {
obj = getNextObj();
if (obj instanceof String) break;
}
while (!(obj instanceof Integer x));
System.out.println(<error descr="Cannot resolve symbol 'x'">x</error>.intValue());
}
void testFor() {
for (Object obj = getNextObj(); !(obj instanceof String s); obj = getNextObj()) {
System.out.println("going further");
}
System.out.println("Found: "+s.trim());
}
}
@@ -0,0 +1,32 @@
class X {
void polyadic(Object o1, Object o2) {
boolean b1 = o1 instanceof String s && o2 instanceof String <error descr="Variable 's' is already defined in the scope">s</error>;
boolean b2 = !(o1 instanceof String s) && !(o2 instanceof String <error descr="Variable 's' is already defined in the scope">s</error>);
boolean b3 = !(o1 instanceof String s) || !(o2 instanceof String <error descr="Variable 's' is already defined in the scope">s</error>);
boolean b4 = o1 instanceof String s || o2 instanceof String <error descr="Variable 's' is already defined in the scope">s</error>;
// Dubious cases: spec is not very clear about whether this should be accepted
boolean b5 = o1 instanceof String s && !(o2 instanceof String <error descr="Variable 's' is already defined in the scope">s</error>);
boolean b6 = o1 instanceof String s && (!(o2 instanceof String <error descr="Variable 's' is already defined in the scope">s</error>) || s.isEmpty());
boolean b7 = !(o2 instanceof String s) && o1 instanceof String s;
boolean b8 = (!(o2 instanceof String s) || s.isEmpty()) && o1 instanceof String s && s.isEmpty();
}
void ternary(Object o1, Object o2, Object o3) {
// Currently all these samples are accepted by javac
boolean b1 = o1 instanceof String s ? o2 instanceof String <error descr="Variable 's' is already defined in the scope">s</error> : o3 instanceof String s1;
boolean b2 = o1 instanceof String s ? o2 instanceof String s1 : o3 instanceof String <error descr="Variable 's' is already defined in the scope">s</error>;
boolean b3 = o1 instanceof String s1 ? o2 instanceof String s : o3 instanceof String <error descr="Variable 's' is already defined in the scope">s</error>;
boolean b4 = !(o1 instanceof String s) ? o2 instanceof String <error descr="Variable 's' is already defined in the scope">s</error> : o3 instanceof String s1;
boolean b5 = !(o1 instanceof String s) ? o2 instanceof String s1 : o3 instanceof String <error descr="Variable 's' is already defined in the scope">s</error>;
boolean b6 = !(o1 instanceof String s1) ? o2 instanceof String s : o3 instanceof String <error descr="Variable 's' is already defined in the scope">s</error>;
}
void ifElse(Object o1, Object o2) {
if (o1 instanceof String s) {
if (o2 instanceof String <error descr="Variable 's' is already defined in the scope">s</error>) {
}
}
}
}