Merge pull request #21698 from hvitved/rust/type-inference-index-expr · github/codeql@ee34e33 · GitHub
Skip to content

Commit ee34e33

Browse files
authored
Merge pull request #21698 from hvitved/rust/type-inference-index-expr
Rust: Replace special handling of index expressions in type inference
2 parents f95ee12 + 467933b commit ee34e33

4 files changed

Lines changed: 158 additions & 40 deletions

File tree

‎rust/ql/lib/codeql/rust/frameworks/stdlib/Builtins.qll‎

Lines changed: 12 additions & 0 deletions

‎rust/ql/lib/codeql/rust/internal/typeinference/FunctionOverloading.qll‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77

88
private import rust
9+
private import codeql.rust.frameworks.stdlib.Builtins as Builtins
10+
private import codeql.rust.frameworks.stdlib.Stdlib
911
private import codeql.rust.internal.PathResolution
1012
private import Type
1113
private import TypeAbstraction
@@ -94,6 +96,14 @@ private module MkSiblingImpls<resolveTypeMentionAtSig/2 resolveTypeMentionAt> {
9496
not t1 instanceof TypeParameter or
9597
not t2 instanceof TypeParameter
9698
)
99+
or
100+
// todo: handle blanket/non-blanket siblings in `implSiblings`
101+
trait =
102+
any(IndexTrait it |
103+
implSiblingCandidate(impl, it, _, _) and
104+
impl instanceof Builtins::BuiltinImpl and
105+
path = TypePath::singleton(TAssociatedTypeTypeParameter(trait, it.getOutputType()))
106+
)
97107
}
98108
}
99109

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

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3609,43 +3609,6 @@ private Type inferArrayExprType(ArrayExpr ae) { exists(ae) and result instanceof
36093609
pragma[nomagic]
36103610
private Type inferRangeExprType(RangeExpr re) { result = TDataType(getRangeType(re)) }
36113611

3612-
/**
3613-
* According to [the Rust reference][1]: _"array and slice-typed expressions
3614-
* can be indexed with a `usize` index ... For other types an index expression
3615-
* `a[b]` is equivalent to *std::ops::Index::index(&a, b)"_.
3616-
*
3617-
* The logic below handles array and slice indexing, but for other types it is
3618-
* currently limited to `Vec`.
3619-
*
3620-
* [1]: https://doc.rust-lang.org/reference/expressions/array-expr.html#r-expr.array.index
3621-
*/
3622-
pragma[nomagic]
3623-
private Type inferIndexExprType(IndexExpr ie, TypePath path) {
3624-
// TODO: Method resolution to the `std::ops::Index` trait can handle the
3625-
// `Index` instances for slices and arrays.
3626-
exists(TypePath exprPath, Builtins::BuiltinType t |
3627-
TDataType(t) = inferType(ie.getIndex()) and
3628-
(
3629-
// also allow `i32`, since that is currently the type that we infer for
3630-
// integer literals like `0`
3631-
t instanceof Builtins::I32
3632-
or
3633-
t instanceof Builtins::Usize
3634-
) and
3635-
result = inferType(ie.getBase(), exprPath)
3636-
|
3637-
// todo: remove?
3638-
exprPath.isCons(TTypeParamTypeParameter(any(Vec v).getElementTypeParam()), path)
3639-
or
3640-
exprPath.isCons(getArrayTypeParameter(), path)
3641-
or
3642-
exists(TypePath path0 |
3643-
exprPath.isCons(getRefTypeParameter(_), path0) and
3644-
path0.isCons(getSliceTypeParameter(), path)
3645-
)
3646-
)
3647-
}
3648-
36493612
pragma[nomagic]
36503613
private Type getInferredDerefType(DerefExpr de, TypePath path) { result = inferType(de, path) }
36513614

@@ -3848,7 +3811,8 @@ private module Cached {
38483811
i instanceof ImplItemNode and dispatch = false
38493812
|
38503813
result = call.(AssocFunctionResolution::AssocFunctionCall).resolveCallTarget(i, _, _, _) and
3851-
not call instanceof CallExprImpl::DynamicCallExpr
3814+
not call instanceof CallExprImpl::DynamicCallExpr and
3815+
not i instanceof Builtins::BuiltinImpl
38523816
)
38533817
}
38543818

@@ -3950,8 +3914,6 @@ private module Cached {
39503914
or
39513915
result = inferAwaitExprType(n, path)
39523916
or
3953-
result = inferIndexExprType(n, path)
3954-
or
39553917
result = inferDereferencedExprPtrType(n, path)
39563918
or
39573919
result = inferForLoopExprType(n, path)

‎rust/tools/builtins/impls.rs‎

Lines changed: 134 additions & 0 deletions

0 commit comments

Comments
 (0)