Skip to content

Commit 69da4e7

Browse files
committed
Rust: Move inferArrayExprType logic into typeEquality predicate.
1 parent 6194676 commit 69da4e7

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

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

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,16 @@ private predicate typeEquality(AstNode n1, TypePath prefix1, AstNode n2, TypePat
287287
prefix2.isEmpty()
288288
)
289289
)
290+
or
291+
// an array list expression (`[1, 2, 3]`) has the type of the first (any) element
292+
n1.(ArrayListExpr).getExpr(0) = n2 and
293+
prefix1 = TypePath::singleton(TArrayTypeParameter()) and
294+
prefix2.isEmpty()
295+
or
296+
// an array repeat expression (`[1; 3]`) has the type of the repeat operand
297+
n1.(ArrayRepeatExpr).getRepeatOperand() = n2 and
298+
prefix1 = TypePath::singleton(TArrayTypeParameter()) and
299+
prefix2.isEmpty()
290300
}
291301

292302
pragma[nomagic]
@@ -1124,23 +1134,6 @@ private Type inferIndexExprType(IndexExpr ie, TypePath path) {
11241134
)
11251135
}
11261136

1127-
pragma[nomagic]
1128-
private Type inferArrayExprType(ArrayExpr ae, TypePath path) {
1129-
// an array list expression (`[1, 2, 3]`) has the type of the first (any) element
1130-
exists(Type type0, TypePath path0 |
1131-
type0 = inferType(ae.(ArrayListExpr).getExpr(0), path0) and
1132-
result = type0 and
1133-
path = TypePath::cons(any(ArrayTypeParameter tp), path0)
1134-
)
1135-
or
1136-
// an array repeat expression (`[1; 3]`) has the type of the repeat operand
1137-
exists(Type type0, TypePath path0 |
1138-
type0 = inferType(ae.(ArrayRepeatExpr).getRepeatOperand(), path0) and
1139-
result = type0 and
1140-
path = TypePath::cons(any(ArrayTypeParameter tp), path0)
1141-
)
1142-
}
1143-
11441137
pragma[nomagic]
11451138
private Type inferForLoopExprType(AstNode n, TypePath path) {
11461139
// type of iterable -> type of pattern (loop variable)
@@ -1470,8 +1463,6 @@ private module Cached {
14701463
or
14711464
result = inferIndexExprType(n, path)
14721465
or
1473-
result = inferArrayExprType(n, path)
1474-
or
14751466
result = inferForLoopExprType(n, path)
14761467
}
14771468
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2685,6 +2685,7 @@ inferType
26852685
| main.rs:1840:31:1840:39 | [...] | [T;...] | {EXTERNAL LOCATION} | i32 |
26862686
| main.rs:1840:31:1840:39 | [...] | [T;...] | {EXTERNAL LOCATION} | u32 |
26872687
| main.rs:1840:32:1840:32 | 1 | | {EXTERNAL LOCATION} | i32 |
2688+
| main.rs:1840:32:1840:32 | 1 | | {EXTERNAL LOCATION} | u32 |
26882689
| main.rs:1840:35:1840:35 | 2 | | {EXTERNAL LOCATION} | i32 |
26892690
| main.rs:1840:38:1840:38 | 3 | | {EXTERNAL LOCATION} | i32 |
26902691
| main.rs:1841:13:1841:13 | u | | {EXTERNAL LOCATION} | i32 |
@@ -2700,6 +2701,7 @@ inferType
27002701
| main.rs:1843:31:1843:36 | [1; 3] | [T;...] | {EXTERNAL LOCATION} | i32 |
27012702
| main.rs:1843:31:1843:36 | [1; 3] | [T;...] | {EXTERNAL LOCATION} | u64 |
27022703
| main.rs:1843:32:1843:32 | 1 | | {EXTERNAL LOCATION} | i32 |
2704+
| main.rs:1843:32:1843:32 | 1 | | {EXTERNAL LOCATION} | u64 |
27032705
| main.rs:1843:35:1843:35 | 3 | | {EXTERNAL LOCATION} | i32 |
27042706
| main.rs:1844:13:1844:13 | u | | {EXTERNAL LOCATION} | i32 |
27052707
| main.rs:1844:13:1844:13 | u | | {EXTERNAL LOCATION} | u64 |

0 commit comments

Comments
 (0)