Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 43c9350

Browse files
committed
Add test for typeflow propagation through instanceof and pattern-case
1 parent fa09be0 commit 43c9350

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

java/ql/test/library-tests/typeflow/A.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,20 @@ public void m9(Object[] xs, int i) {
102102
default -> { }
103103
}
104104
}
105+
106+
public void m10(Object o) {
107+
String s = "Hello world!";
108+
Object o2 = s; // Alas, the type information, it is lost
109+
110+
if (o2 instanceof CharSequence cs) {
111+
// Partially recovered statically, but we should know cs is an alias of o and therefore it's really a string.
112+
Object target = cs;
113+
}
114+
115+
// The same applies to a pattern case
116+
switch (o2) {
117+
case CharSequence cs -> { Object target = cs; }
118+
default -> { }
119+
}
120+
}
105121
}

java/ql/test/library-tests/typeflow/typeflow.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@
1414
| A.java:70:23:70:24 | x2 | Integer | false |
1515
| A.java:92:18:92:18 | n | Integer | false |
1616
| A.java:100:20:100:20 | n | Integer | false |
17+
| A.java:110:9:110:10 | o2 | String | false |
18+
| A.java:112:23:112:24 | cs | String | false |
19+
| A.java:116:13:116:14 | o2 | String | false |
20+
| A.java:117:49:117:50 | cs | String | false |
1721
| UnionTypes.java:45:7:45:7 | x | Inter | false |
1822
| UnionTypes.java:48:23:48:23 | x | Inter | false |

0 commit comments

Comments
 (0)