Skip to content

Commit 51343a5

Browse files
committed
Rust: Implement type inference for ArrayListExprs.
1 parent f76b562 commit 51343a5

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

rust/ql/lib/codeql/rust/internal/TypeInference.qll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,15 @@ private Type inferIndexExprType(IndexExpr ie, TypePath path) {
11801180
)
11811181
}
11821182

1183+
pragma[nomagic]
1184+
private Type inferArrayListExprType(/*ArrayExpr*/ArrayListExpr ale, TypePath path) {
1185+
exists(Type type0, TypePath path0 |
1186+
type0 = inferType(ale.getExpr(0), path0) and
1187+
result = type0 and
1188+
path = TypePath::cons(any(ArrayTypeParameter tp), path0)
1189+
)
1190+
}
1191+
11831192
pragma[nomagic]
11841193
private Type inferForLoopExprType(AstNode n, TypePath path) {
11851194
// type of iterable -> type of pattern (loop variable)
@@ -1556,6 +1565,8 @@ private module Cached {
15561565
or
15571566
result = inferIndexExprType(n, path)
15581567
or
1568+
result = inferArrayListExprType(n, path)
1569+
or
15591570
result = inferForLoopExprType(n, path)
15601571
}
15611572
}

rust/ql/test/library-tests/type-inference/main.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,12 +1822,12 @@ mod loops {
18221822
pub fn f() {
18231823
// for loops with arrays
18241824

1825-
for i in [1, 2, 3] { } // $ MISSING: type=i:i32
1825+
for i in [1, 2, 3] { } // $ type=i:i32
18261826
for i in [1, 2, 3].map(|x| x + 1) { } // $ MISSING: type=i:i32
18271827
for i in [1, 2, 3].into_iter() { } // $ MISSING: type=i:i32
18281828

18291829
let vals1 = [1u8, 2, 3]; // $ MISSING: type=vals1:[u8; 3]
1830-
for u in vals1 { } // $ MISSING: type=u:u8
1830+
for u in vals1 { } // $ type=u:u8
18311831

18321832
let vals2 = [1u16; 3]; // $ MISSING: type=vals2:[u16; 3]
18331833
for u in vals2 { } // $ MISSING: type=u:u16
@@ -1841,17 +1841,17 @@ mod loops {
18411841
let mut strings1 = ["foo", "bar", "baz"]; // $ MISSING: type=strings1:[&str; 3]
18421842
for s in &strings1 { } // $ MISSING: type=s:&str
18431843
for s in &mut strings1 { } // $ MISSING: type=s:&str
1844-
for s in strings1 { } // $ MISSING: type=s:str
1844+
for s in strings1 { } // $ type=s:str
18451845

18461846
let strings2 = [String::from("foo"), String::from("bar"), String::from("baz")]; // $ MISSING: type=strings2:[String; 3]
1847-
for s in strings2 { } // $ MISSING: type=s:String
1847+
for s in strings2 { } // $ type=s:String
18481848

18491849
let strings3 = &[String::from("foo"), String::from("bar"), String::from("baz")]; // $ MISSING: type=strings3:&[String; 3]
18501850
for s in strings3 { } // $ MISSING: type=s:String
18511851

18521852
let callables = [MyCallable::new(), MyCallable::new(), MyCallable::new()]; // $ MISSING: type=callables:[MyCallable; 3]
1853-
for c in callables { // $ MISSING: type=c:MyCallable
1854-
let result = c.call(); // $ MISSING: type=result:i64 method=call
1853+
for c in callables { // $ type=c:MyCallable
1854+
let result = c.call(); // $ type=result:i64 method=call
18551855
}
18561856

18571857
// for loops with ranges

rust/ql/test/library-tests/type-inference/type-inference.expected

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2618,31 +2618,43 @@ inferType
26182618
| main.rs:1817:31:1819:9 | { ... } | | {EXTERNAL LOCATION} | i64 |
26192619
| main.rs:1818:13:1818:13 | 1 | | {EXTERNAL LOCATION} | i32 |
26202620
| main.rs:1818:13:1818:13 | 1 | | {EXTERNAL LOCATION} | i64 |
2621+
| main.rs:1825:13:1825:13 | i | | {EXTERNAL LOCATION} | i32 |
2622+
| main.rs:1825:18:1825:26 | [...] | [T;...] | {EXTERNAL LOCATION} | i32 |
26212623
| main.rs:1825:19:1825:19 | 1 | | {EXTERNAL LOCATION} | i32 |
26222624
| main.rs:1825:22:1825:22 | 2 | | {EXTERNAL LOCATION} | i32 |
26232625
| main.rs:1825:25:1825:25 | 3 | | {EXTERNAL LOCATION} | i32 |
2626+
| main.rs:1826:18:1826:26 | [...] | [T;...] | {EXTERNAL LOCATION} | i32 |
26242627
| main.rs:1826:19:1826:19 | 1 | | {EXTERNAL LOCATION} | i32 |
26252628
| main.rs:1826:22:1826:22 | 2 | | {EXTERNAL LOCATION} | i32 |
26262629
| main.rs:1826:25:1826:25 | 3 | | {EXTERNAL LOCATION} | i32 |
26272630
| main.rs:1826:40:1826:40 | 1 | | {EXTERNAL LOCATION} | i32 |
2631+
| main.rs:1827:18:1827:26 | [...] | [T;...] | {EXTERNAL LOCATION} | i32 |
26282632
| main.rs:1827:19:1827:19 | 1 | | {EXTERNAL LOCATION} | i32 |
26292633
| main.rs:1827:22:1827:22 | 2 | | {EXTERNAL LOCATION} | i32 |
26302634
| main.rs:1827:25:1827:25 | 3 | | {EXTERNAL LOCATION} | i32 |
2635+
| main.rs:1829:13:1829:17 | vals1 | [T;...] | {EXTERNAL LOCATION} | u8 |
2636+
| main.rs:1829:21:1829:31 | [...] | [T;...] | {EXTERNAL LOCATION} | u8 |
26312637
| main.rs:1829:22:1829:24 | 1u8 | | {EXTERNAL LOCATION} | u8 |
26322638
| main.rs:1829:27:1829:27 | 2 | | {EXTERNAL LOCATION} | i32 |
26332639
| main.rs:1829:30:1829:30 | 3 | | {EXTERNAL LOCATION} | i32 |
2640+
| main.rs:1830:13:1830:13 | u | | {EXTERNAL LOCATION} | u8 |
2641+
| main.rs:1830:18:1830:22 | vals1 | [T;...] | {EXTERNAL LOCATION} | u8 |
26342642
| main.rs:1832:22:1832:25 | 1u16 | | {EXTERNAL LOCATION} | u16 |
26352643
| main.rs:1832:28:1832:28 | 3 | | {EXTERNAL LOCATION} | i32 |
26362644
| main.rs:1835:13:1835:17 | vals3 | | file://:0:0:0:0 | [] |
2645+
| main.rs:1835:13:1835:17 | vals3 | [T;...] | {EXTERNAL LOCATION} | i32 |
26372646
| main.rs:1835:13:1835:17 | vals3 | [T;...] | {EXTERNAL LOCATION} | u32 |
26382647
| main.rs:1835:26:1835:26 | 3 | | {EXTERNAL LOCATION} | i32 |
26392648
| main.rs:1835:31:1835:39 | [...] | | file://:0:0:0:0 | [] |
2649+
| main.rs:1835:31:1835:39 | [...] | [T;...] | {EXTERNAL LOCATION} | i32 |
26402650
| main.rs:1835:31:1835:39 | [...] | [T;...] | {EXTERNAL LOCATION} | u32 |
26412651
| main.rs:1835:32:1835:32 | 1 | | {EXTERNAL LOCATION} | i32 |
26422652
| main.rs:1835:35:1835:35 | 2 | | {EXTERNAL LOCATION} | i32 |
26432653
| main.rs:1835:38:1835:38 | 3 | | {EXTERNAL LOCATION} | i32 |
2654+
| main.rs:1836:13:1836:13 | u | | {EXTERNAL LOCATION} | i32 |
26442655
| main.rs:1836:13:1836:13 | u | | {EXTERNAL LOCATION} | u32 |
26452656
| main.rs:1836:18:1836:22 | vals3 | | file://:0:0:0:0 | [] |
2657+
| main.rs:1836:18:1836:22 | vals3 | [T;...] | {EXTERNAL LOCATION} | i32 |
26462658
| main.rs:1836:18:1836:22 | vals3 | [T;...] | {EXTERNAL LOCATION} | u32 |
26472659
| main.rs:1838:13:1838:17 | vals4 | | file://:0:0:0:0 | [] |
26482660
| main.rs:1838:13:1838:17 | vals4 | [T;...] | {EXTERNAL LOCATION} | u64 |
@@ -2654,29 +2666,52 @@ inferType
26542666
| main.rs:1839:13:1839:13 | u | | {EXTERNAL LOCATION} | u64 |
26552667
| main.rs:1839:18:1839:22 | vals4 | | file://:0:0:0:0 | [] |
26562668
| main.rs:1839:18:1839:22 | vals4 | [T;...] | {EXTERNAL LOCATION} | u64 |
2669+
| main.rs:1841:13:1841:24 | mut strings1 | [T;...] | {EXTERNAL LOCATION} | str |
2670+
| main.rs:1841:28:1841:48 | [...] | [T;...] | {EXTERNAL LOCATION} | str |
26572671
| main.rs:1841:29:1841:33 | "foo" | | {EXTERNAL LOCATION} | str |
26582672
| main.rs:1841:36:1841:40 | "bar" | | {EXTERNAL LOCATION} | str |
26592673
| main.rs:1841:43:1841:47 | "baz" | | {EXTERNAL LOCATION} | str |
26602674
| main.rs:1842:18:1842:26 | &strings1 | | file://:0:0:0:0 | & |
2675+
| main.rs:1842:18:1842:26 | &strings1 | &T.[T;...] | {EXTERNAL LOCATION} | str |
2676+
| main.rs:1842:19:1842:26 | strings1 | [T;...] | {EXTERNAL LOCATION} | str |
26612677
| main.rs:1843:18:1843:30 | &mut strings1 | | file://:0:0:0:0 | & |
2678+
| main.rs:1843:18:1843:30 | &mut strings1 | &T.[T;...] | {EXTERNAL LOCATION} | str |
2679+
| main.rs:1843:23:1843:30 | strings1 | [T;...] | {EXTERNAL LOCATION} | str |
2680+
| main.rs:1844:13:1844:13 | s | | {EXTERNAL LOCATION} | str |
2681+
| main.rs:1844:18:1844:25 | strings1 | [T;...] | {EXTERNAL LOCATION} | str |
2682+
| main.rs:1846:13:1846:20 | strings2 | [T;...] | {EXTERNAL LOCATION} | String |
2683+
| main.rs:1846:24:1846:86 | [...] | [T;...] | {EXTERNAL LOCATION} | String |
26622684
| main.rs:1846:25:1846:43 | ...::from(...) | | {EXTERNAL LOCATION} | String |
26632685
| main.rs:1846:38:1846:42 | "foo" | | {EXTERNAL LOCATION} | str |
26642686
| main.rs:1846:46:1846:64 | ...::from(...) | | {EXTERNAL LOCATION} | String |
26652687
| main.rs:1846:59:1846:63 | "bar" | | {EXTERNAL LOCATION} | str |
26662688
| main.rs:1846:67:1846:85 | ...::from(...) | | {EXTERNAL LOCATION} | String |
26672689
| main.rs:1846:80:1846:84 | "baz" | | {EXTERNAL LOCATION} | str |
2690+
| main.rs:1847:13:1847:13 | s | | {EXTERNAL LOCATION} | String |
2691+
| main.rs:1847:18:1847:25 | strings2 | [T;...] | {EXTERNAL LOCATION} | String |
26682692
| main.rs:1849:13:1849:20 | strings3 | | file://:0:0:0:0 | & |
2693+
| main.rs:1849:13:1849:20 | strings3 | &T.[T;...] | {EXTERNAL LOCATION} | String |
26692694
| main.rs:1849:24:1849:87 | &... | | file://:0:0:0:0 | & |
2695+
| main.rs:1849:24:1849:87 | &... | &T.[T;...] | {EXTERNAL LOCATION} | String |
2696+
| main.rs:1849:25:1849:87 | [...] | [T;...] | {EXTERNAL LOCATION} | String |
26702697
| main.rs:1849:26:1849:44 | ...::from(...) | | {EXTERNAL LOCATION} | String |
26712698
| main.rs:1849:39:1849:43 | "foo" | | {EXTERNAL LOCATION} | str |
26722699
| main.rs:1849:47:1849:65 | ...::from(...) | | {EXTERNAL LOCATION} | String |
26732700
| main.rs:1849:60:1849:64 | "bar" | | {EXTERNAL LOCATION} | str |
26742701
| main.rs:1849:68:1849:86 | ...::from(...) | | {EXTERNAL LOCATION} | String |
26752702
| main.rs:1849:81:1849:85 | "baz" | | {EXTERNAL LOCATION} | str |
26762703
| main.rs:1850:18:1850:25 | strings3 | | file://:0:0:0:0 | & |
2704+
| main.rs:1850:18:1850:25 | strings3 | &T.[T;...] | {EXTERNAL LOCATION} | String |
2705+
| main.rs:1852:13:1852:21 | callables | [T;...] | main.rs:1809:5:1810:5 | MyCallable |
2706+
| main.rs:1852:25:1852:81 | [...] | [T;...] | main.rs:1809:5:1810:5 | MyCallable |
26772707
| main.rs:1852:26:1852:42 | ...::new(...) | | main.rs:1809:5:1810:5 | MyCallable |
26782708
| main.rs:1852:45:1852:61 | ...::new(...) | | main.rs:1809:5:1810:5 | MyCallable |
26792709
| main.rs:1852:64:1852:80 | ...::new(...) | | main.rs:1809:5:1810:5 | MyCallable |
2710+
| main.rs:1853:13:1853:13 | c | | main.rs:1809:5:1810:5 | MyCallable |
2711+
| main.rs:1853:18:1853:26 | callables | [T;...] | main.rs:1809:5:1810:5 | MyCallable |
2712+
| main.rs:1854:17:1854:22 | result | | {EXTERNAL LOCATION} | i64 |
2713+
| main.rs:1854:26:1854:26 | c | | main.rs:1809:5:1810:5 | MyCallable |
2714+
| main.rs:1854:26:1854:33 | c.call() | | {EXTERNAL LOCATION} | i64 |
26802715
| main.rs:1859:18:1859:18 | 0 | | {EXTERNAL LOCATION} | i32 |
26812716
| main.rs:1859:21:1859:22 | 10 | | {EXTERNAL LOCATION} | i32 |
26822717
| main.rs:1860:19:1860:21 | 0u8 | | {EXTERNAL LOCATION} | u8 |
@@ -2695,6 +2730,7 @@ inferType
26952730
| main.rs:1870:13:1870:17 | vals4 | | {EXTERNAL LOCATION} | Vec |
26962731
| main.rs:1870:13:1870:17 | vals4 | T | file://:0:0:0:0 | & |
26972732
| main.rs:1870:13:1870:17 | vals4 | T.&T | {EXTERNAL LOCATION} | u64 |
2733+
| main.rs:1870:33:1870:44 | [...] | [T;...] | {EXTERNAL LOCATION} | u64 |
26982734
| main.rs:1870:33:1870:61 | ... .collect() | | {EXTERNAL LOCATION} | Vec |
26992735
| main.rs:1870:33:1870:61 | ... .collect() | T | file://:0:0:0:0 | & |
27002736
| main.rs:1870:33:1870:61 | ... .collect() | T.&T | {EXTERNAL LOCATION} | u64 |

0 commit comments

Comments
 (0)