Skip to content

Commit 180782d

Browse files
authored
Merge pull request #18579 from hvitved/rust/path-resolution
Rust: Implement path resolution in QL
2 parents 384c040 + 1cb524f commit 180782d

File tree

19 files changed

+999
-59
lines changed

19 files changed

+999
-59
lines changed

rust/ql/lib/codeql/rust/AstConsistency.qll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ query predicate multiplePositions(Element parent, int pos1, int pos2, string acc
7373
pos1 != pos2
7474
}
7575

76+
private import codeql.rust.elements.internal.PathResolution
77+
78+
/** Holds if `p` may resolve to multiple items including `i`. */
79+
query predicate multiplePathResolutions(Path p, ItemNode i) {
80+
i = resolvePath(p) and
81+
strictcount(resolvePath(p)) > 1
82+
}
83+
7684
/**
7785
* Gets counts of abstract syntax tree inconsistencies of each type.
7886
*/
@@ -98,4 +106,7 @@ int getAstInconsistencyCounts(string type) {
98106
or
99107
type = "Multiple positions" and
100108
result = count(Element e | multiplePositions(_, _, _, _, e) | e)
109+
or
110+
type = "Multiple path resolutions" and
111+
result = count(Path p | multiplePathResolutions(p, _) | p)
101112
}

rust/ql/lib/codeql/rust/elements/internal/CallExprBaseImpl.qll

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module Impl {
1616
private import codeql.rust.elements.internal.MethodCallExprImpl::Impl
1717
private import codeql.rust.elements.internal.CallExprImpl::Impl
1818
private import codeql.rust.elements.internal.PathExprImpl::Impl
19+
private import codeql.rust.elements.internal.PathResolution
1920

2021
pragma[nomagic]
2122
Resolvable getCallResolvable(CallExprBase call) {
@@ -33,6 +34,10 @@ module Impl {
3334
* Gets the target callable of this call, if a unique such target can
3435
* be statically resolved.
3536
*/
36-
Callable getStaticTarget() { getCallResolvable(this).resolvesAsItem(result) }
37+
Callable getStaticTarget() {
38+
getCallResolvable(this).resolvesAsItem(result)
39+
or
40+
result = resolvePath(this.(CallExpr).getFunction().(PathExpr).getPath())
41+
}
3742
}
3843
}

rust/ql/lib/codeql/rust/elements/internal/PathImpl.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,11 @@ module Impl {
2727
then result = "...::" + this.getPart().toAbbreviatedString()
2828
else result = this.getPart().toAbbreviatedString()
2929
}
30+
31+
/**
32+
* Gets the text of this path, if it exists.
33+
*/
34+
pragma[nomagic]
35+
string getText() { result = this.getPart().getNameRef().getText() }
3036
}
3137
}

0 commit comments

Comments
 (0)