Skip to content

Commit b89d6d3

Browse files
committed
Rust: Implement type inference for ArrayRepeatExprs.
1 parent 51343a5 commit b89d6d3

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,9 +1181,17 @@ private Type inferIndexExprType(IndexExpr ie, TypePath path) {
11811181
}
11821182

11831183
pragma[nomagic]
1184-
private Type inferArrayListExprType(/*ArrayExpr*/ArrayListExpr ale, TypePath path) {
1184+
private Type inferArrayExprType(ArrayExpr ae, TypePath path) {
1185+
// an array list expression (`[1, 2, 3]`) has the type of the first (any) element
11851186
exists(Type type0, TypePath path0 |
1186-
type0 = inferType(ale.getExpr(0), path0) and
1187+
type0 = inferType(ae.(ArrayListExpr).getExpr(0), path0) and
1188+
result = type0 and
1189+
path = TypePath::cons(any(ArrayTypeParameter tp), path0)
1190+
)
1191+
or
1192+
// an array repeat expression (`[1; 3]`) has the type of the repeat operand
1193+
exists(Type type0, TypePath path0 |
1194+
type0 = inferType(ae.(ArrayRepeatExpr).getRepeatOperand(), path0) and
11871195
result = type0 and
11881196
path = TypePath::cons(any(ArrayTypeParameter tp), path0)
11891197
)
@@ -1565,7 +1573,7 @@ private module Cached {
15651573
or
15661574
result = inferIndexExprType(n, path)
15671575
or
1568-
result = inferArrayListExprType(n, path)
1576+
result = inferArrayExprType(n, path)
15691577
or
15701578
result = inferForLoopExprType(n, path)
15711579
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1830,7 +1830,7 @@ mod loops {
18301830
for u in vals1 { } // $ type=u:u8
18311831

18321832
let vals2 = [1u16; 3]; // $ MISSING: type=vals2:[u16; 3]
1833-
for u in vals2 { } // $ MISSING: type=u:u16
1833+
for u in vals2 { } // $ type=u:u16
18341834

18351835
let vals3: [u32; 3] = [1, 2, 3]; // $ MISSING: type=vals3:[u32; 3]
18361836
for u in vals3 { } // $ type=u:u32

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2639,8 +2639,12 @@ inferType
26392639
| main.rs:1829:30:1829:30 | 3 | | {EXTERNAL LOCATION} | i32 |
26402640
| main.rs:1830:13:1830:13 | u | | {EXTERNAL LOCATION} | u8 |
26412641
| main.rs:1830:18:1830:22 | vals1 | [T;...] | {EXTERNAL LOCATION} | u8 |
2642+
| main.rs:1832:13:1832:17 | vals2 | [T;...] | {EXTERNAL LOCATION} | u16 |
2643+
| main.rs:1832:21:1832:29 | [1u16; 3] | [T;...] | {EXTERNAL LOCATION} | u16 |
26422644
| main.rs:1832:22:1832:25 | 1u16 | | {EXTERNAL LOCATION} | u16 |
26432645
| main.rs:1832:28:1832:28 | 3 | | {EXTERNAL LOCATION} | i32 |
2646+
| main.rs:1833:13:1833:13 | u | | {EXTERNAL LOCATION} | u16 |
2647+
| main.rs:1833:18:1833:22 | vals2 | [T;...] | {EXTERNAL LOCATION} | u16 |
26442648
| main.rs:1835:13:1835:17 | vals3 | | file://:0:0:0:0 | [] |
26452649
| main.rs:1835:13:1835:17 | vals3 | [T;...] | {EXTERNAL LOCATION} | i32 |
26462650
| main.rs:1835:13:1835:17 | vals3 | [T;...] | {EXTERNAL LOCATION} | u32 |
@@ -2657,14 +2661,18 @@ inferType
26572661
| main.rs:1836:18:1836:22 | vals3 | [T;...] | {EXTERNAL LOCATION} | i32 |
26582662
| main.rs:1836:18:1836:22 | vals3 | [T;...] | {EXTERNAL LOCATION} | u32 |
26592663
| main.rs:1838:13:1838:17 | vals4 | | file://:0:0:0:0 | [] |
2664+
| main.rs:1838:13:1838:17 | vals4 | [T;...] | {EXTERNAL LOCATION} | i32 |
26602665
| main.rs:1838:13:1838:17 | vals4 | [T;...] | {EXTERNAL LOCATION} | u64 |
26612666
| main.rs:1838:26:1838:26 | 3 | | {EXTERNAL LOCATION} | i32 |
26622667
| main.rs:1838:31:1838:36 | [1; 3] | | file://:0:0:0:0 | [] |
2668+
| main.rs:1838:31:1838:36 | [1; 3] | [T;...] | {EXTERNAL LOCATION} | i32 |
26632669
| main.rs:1838:31:1838:36 | [1; 3] | [T;...] | {EXTERNAL LOCATION} | u64 |
26642670
| main.rs:1838:32:1838:32 | 1 | | {EXTERNAL LOCATION} | i32 |
26652671
| main.rs:1838:35:1838:35 | 3 | | {EXTERNAL LOCATION} | i32 |
2672+
| main.rs:1839:13:1839:13 | u | | {EXTERNAL LOCATION} | i32 |
26662673
| main.rs:1839:13:1839:13 | u | | {EXTERNAL LOCATION} | u64 |
26672674
| main.rs:1839:18:1839:22 | vals4 | | file://:0:0:0:0 | [] |
2675+
| main.rs:1839:18:1839:22 | vals4 | [T;...] | {EXTERNAL LOCATION} | i32 |
26682676
| main.rs:1839:18:1839:22 | vals4 | [T;...] | {EXTERNAL LOCATION} | u64 |
26692677
| main.rs:1841:13:1841:24 | mut strings1 | [T;...] | {EXTERNAL LOCATION} | str |
26702678
| main.rs:1841:28:1841:48 | [...] | [T;...] | {EXTERNAL LOCATION} | str |

0 commit comments

Comments
 (0)