Skip to content

Commit aefab5f

Browse files
committed
Rust: Simple type inference for index expressions
1 parent ae4a425 commit aefab5f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,24 @@ private StructType inferLiteralType(LiteralExpr le) {
10101010
)
10111011
}
10121012

1013+
private class Vec extends Struct {
1014+
Vec() { this.getCanonicalPath() = "alloc::vec::Vec" }
1015+
1016+
TypeParamTypeParameter getElementTypeParameter() {
1017+
result.getTypeParam() = this.getGenericParamList().getTypeParam(0)
1018+
}
1019+
}
1020+
1021+
pragma[nomagic]
1022+
private Type inferIndexExprType(IndexExpr ie, TypePath path) {
1023+
// TODO: Should be implemented as method resolution, using the special
1024+
// `std::ops::Index` trait.
1025+
exists(TypePath exprPath |
1026+
result = inferType(ie.getBase(), exprPath) and
1027+
exprPath.isCons(any(Vec v).getElementTypeParameter(), path)
1028+
)
1029+
}
1030+
10131031
private module MethodCall {
10141032
/** An expression that calls a method. */
10151033
abstract private class MethodCallImpl extends Expr {
@@ -1347,6 +1365,8 @@ private module Cached {
13471365
or
13481366
result = inferLiteralType(n) and
13491367
path.isEmpty()
1368+
or
1369+
result = inferIndexExprType(n, path)
13501370
}
13511371
}
13521372

0 commit comments

Comments
 (0)