Skip to content

Commit c4295bf

Browse files
committed
Rust: Add cross-crate path resolution test
1 parent 925abd8 commit c4295bf

File tree

9 files changed

+62
-49
lines changed

9 files changed

+62
-49
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use lib::a_module::hello;
1+
use lib::a_module::hello; // $ MISSING: item=HELLO
22

33
mod a_module;
44

55
fn main() {
6-
hello();
6+
hello(); // $ MISSING: item=HELLO
77
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
pub fn hello() {
22
println!("Hello, world!");
3-
}
3+
} // HELLO

rust/ql/integration-tests/hello-workspace/path-resolution.expected

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import utils.test.PathResolutionInlineExpectationsTest

rust/ql/integration-tests/hello-workspace/rust-project.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22
"sysroot_src": "filled by the rust_project fixture",
33
"crates": [
44
{
5+
"display_name": "exe",
6+
"version": "0.1.0",
57
"root_module": "exe/src/main.rs",
68
"edition": "2021",
7-
"deps": [{"crate": 1, "name": "lib"}]
9+
"deps": [
10+
{
11+
"crate": 1,
12+
"name": "lib"
13+
}
14+
]
815
},
916
{
17+
"display_name": "lib",
18+
"version": "0.1.0",
1019
"root_module": "lib/src/lib.rs",
1120
"edition": "2021",
1221
"deps": []
1322
}
1423
]
15-
}
24+
}

rust/ql/integration-tests/hello-workspace/summary.cargo.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
| Elements extracted | 87 |
1+
| Elements extracted | 90 |
22
| Elements unextracted | 0 |
33
| Extraction errors | 0 |
44
| Extraction warnings | 0 |

rust/ql/integration-tests/hello-workspace/summary.rust-project.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
| Elements extracted | 87 |
1+
| Elements extracted | 90 |
22
| Elements unextracted | 0 |
33
| Extraction errors | 0 |
44
| Extraction warnings | 0 |
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
private import rust
2+
private import codeql.rust.internal.PathResolution
3+
private import codeql.rust.internal.TypeInference
4+
private import utils.test.InlineExpectationsTest
5+
6+
private module ResolveTest implements TestSig {
7+
string getARelevantTag() { result = "item" }
8+
9+
private predicate itemAt(ItemNode i, string filepath, int line, boolean inMacro) {
10+
i.getLocation().hasLocationInfo(filepath, _, _, line, _) and
11+
if i.(AstNode).isInMacroExpansion() then inMacro = true else inMacro = false
12+
}
13+
14+
private predicate commmentAt(string text, string filepath, int line) {
15+
exists(Comment c |
16+
c.getLocation().hasLocationInfo(filepath, line, _, _, _) and
17+
c.getCommentText() = text
18+
)
19+
}
20+
21+
private predicate item(ItemNode i, string value) {
22+
exists(string filepath, int line, boolean inMacro | itemAt(i, filepath, line, inMacro) |
23+
commmentAt(value, filepath, line) and inMacro = false
24+
or
25+
not (commmentAt(_, filepath, line) and inMacro = false) and
26+
value = i.getName()
27+
)
28+
}
29+
30+
predicate hasActualResult(Location location, string element, string tag, string value) {
31+
exists(AstNode n |
32+
not n = any(Path parent).getQualifier() and
33+
location = n.getLocation() and
34+
element = n.toString() and
35+
tag = "item"
36+
|
37+
item(resolvePath(n), value)
38+
or
39+
item(n.(MethodCallExpr).getStaticTarget(), value)
40+
)
41+
}
42+
}
43+
44+
import MakeTest<ResolveTest>
Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,8 @@
11
import rust
22
import codeql.rust.internal.PathResolution
3-
import codeql.rust.internal.TypeInference
4-
import utils.test.InlineExpectationsTest
3+
import utils.test.PathResolutionInlineExpectationsTest
54
import TestUtils
65

76
query predicate mod(Module m) { toBeTested(m) }
87

98
query predicate resolvePath(Path p, ItemNode i) { toBeTested(p) and i = resolvePath(p) }
10-
11-
module ResolveTest implements TestSig {
12-
string getARelevantTag() { result = "item" }
13-
14-
private predicate itemAt(ItemNode i, string filepath, int line, boolean inMacro) {
15-
i.getLocation().hasLocationInfo(filepath, _, _, line, _) and
16-
if i.isInMacroExpansion() then inMacro = true else inMacro = false
17-
}
18-
19-
private predicate commmentAt(string text, string filepath, int line) {
20-
exists(Comment c |
21-
c.getLocation().hasLocationInfo(filepath, line, _, _, _) and
22-
c.getCommentText() = text
23-
)
24-
}
25-
26-
private predicate item(ItemNode i, string value) {
27-
exists(string filepath, int line, boolean inMacro | itemAt(i, filepath, line, inMacro) |
28-
commmentAt(value, filepath, line) and inMacro = false
29-
or
30-
not (commmentAt(_, filepath, line) and inMacro = false) and
31-
value = i.getName()
32-
)
33-
}
34-
35-
predicate hasActualResult(Location location, string element, string tag, string value) {
36-
exists(AstNode n |
37-
not n = any(Path parent).getQualifier() and
38-
location = n.getLocation() and
39-
element = n.toString() and
40-
tag = "item"
41-
|
42-
item(resolvePath(n), value)
43-
or
44-
item(n.(MethodCallExpr).getStaticTarget(), value)
45-
)
46-
}
47-
}
48-
49-
import MakeTest<ResolveTest>

0 commit comments

Comments
 (0)