Skip to content

Commit bd4c85a

Browse files
committed
Rust: Add cross-crate path resolution test
1 parent c5106f7 commit bd4c85a

File tree

9 files changed

+66
-49
lines changed

9 files changed

+66
-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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Provides an inline expectations test for path resolution.
3+
*/
4+
5+
private import rust
6+
private import codeql.rust.internal.PathResolution
7+
private import codeql.rust.internal.TypeInference
8+
private import utils.test.InlineExpectationsTest
9+
10+
private module ResolveTest implements TestSig {
11+
string getARelevantTag() { result = "item" }
12+
13+
private predicate itemAt(ItemNode i, string filepath, int line, boolean inMacro) {
14+
i.getLocation().hasLocationInfo(filepath, _, _, line, _) and
15+
if i.(AstNode).isInMacroExpansion() then inMacro = true else inMacro = false
16+
}
17+
18+
private predicate commmentAt(string text, string filepath, int line) {
19+
exists(Comment c |
20+
c.getLocation().hasLocationInfo(filepath, line, _, _, _) and
21+
c.getCommentText() = text
22+
)
23+
}
24+
25+
private predicate item(ItemNode i, string value) {
26+
exists(string filepath, int line, boolean inMacro | itemAt(i, filepath, line, inMacro) |
27+
commmentAt(value, filepath, line) and inMacro = false
28+
or
29+
not (commmentAt(_, filepath, line) and inMacro = false) and
30+
value = i.getName()
31+
)
32+
}
33+
34+
predicate hasActualResult(Location location, string element, string tag, string value) {
35+
exists(AstNode n |
36+
not n = any(Path parent).getQualifier() and
37+
location = n.getLocation() and
38+
element = n.toString() and
39+
tag = "item"
40+
|
41+
item(resolvePath(n), value)
42+
or
43+
item(n.(MethodCallExpr).getStaticTarget(), value)
44+
)
45+
}
46+
}
47+
48+
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)