Skip to content

Commit dd3debc

Browse files
committed
Address review comments
1 parent 537e7a8 commit dd3debc

File tree

9 files changed

+46
-25
lines changed

9 files changed

+46
-25
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use lib::a_module::hello; // $ item=HELLO
22

3+
use lib::my_macro2; // $ item=my_macro2
4+
35
mod a_module;
46

57
fn main() {
8+
my_macro2!(); // $ item=my_macro2
69
hello(); // $ item=HELLO
710
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| exe/src/main.rs:5:1:7:1 | fn main |
1+
| exe/src/main.rs:7:1:10:1 | fn main |
22
| lib/src/a_module/mod.rs:1:1:4:1 | fn hello |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
pub fn hello() {
2-
my_macro!(); // $ item=my_macro
2+
my_macro2!(); // $ item=my_macro2
33
println!("Hello, world!"); // $ item=println
44
} // HELLO
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
#[macro_use]
22
mod macros {
3-
macro_rules! my_macro {
3+
#[macro_export]
4+
macro_rules! my_macro1 {
45
() => {
56
println!("my_macro!");
67
};
78
}
9+
#[macro_export]
10+
macro_rules! my_macro2 {
11+
() => {
12+
$crate::my_macro1!();
13+
};
14+
}
815
}
916

1017
pub mod a_module;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
testFailures
2+
resolveDollarCrate
3+
| exe/src/main.rs:8:5:8:14 | $crate | lib/src/lib.rs:0:0:0:0 | Crate(lib@0.1.0) |
4+
| lib/src/a_module/mod.rs:2:5:2:14 | $crate | lib/src/lib.rs:0:0:0:0 | Crate(lib@0.1.0) |
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1+
import rust
2+
import codeql.rust.internal.PathResolution
13
import utils.test.PathResolutionInlineExpectationsTest
4+
5+
query predicate resolveDollarCrate(RelevantPath p, Crate c) {
6+
c = resolvePath(p) and
7+
p.isDollarCrate() and
8+
p.fromSource() and
9+
c.fromSource()
10+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
| Inconsistencies - Path resolution | 0 |
1010
| Inconsistencies - SSA | 0 |
1111
| Inconsistencies - data flow | 0 |
12-
| Lines of code extracted | 15 |
13-
| Lines of user code extracted | 15 |
14-
| Macro calls - resolved | 5 |
15-
| Macro calls - total | 5 |
12+
| Lines of code extracted | 21 |
13+
| Lines of user code extracted | 21 |
14+
| Macro calls - resolved | 10 |
15+
| Macro calls - total | 10 |
1616
| Macro calls - unresolved | 0 |

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
| Inconsistencies - Path resolution | 0 |
1010
| Inconsistencies - SSA | 0 |
1111
| Inconsistencies - data flow | 0 |
12-
| Lines of code extracted | 15 |
13-
| Lines of user code extracted | 15 |
14-
| Macro calls - resolved | 5 |
15-
| Macro calls - total | 5 |
12+
| Lines of code extracted | 21 |
13+
| Lines of user code extracted | 21 |
14+
| Macro calls - resolved | 10 |
15+
| Macro calls - total | 10 |
1616
| Macro calls - unresolved | 0 |

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,7 +1559,6 @@ private predicate unqualifiedPathLookup(ItemNode ancestor, string name, Namespac
15591559
exists(RelevantPath path |
15601560
path.isUnqualified(name, encl) and
15611561
ancestor = encl and
1562-
exists(ns) and
15631562
not name = ["crate", "$crate", "super", "self"]
15641563
|
15651564
pathUsesNamespace(path, ns)
@@ -1668,7 +1667,7 @@ module TraitIsVisible<relevantTraitVisibleSig/2 relevantTraitVisible> {
16681667
}
16691668

16701669
pragma[nomagic]
1671-
private predicate isMacroExpansion(AstNode expansion, Path macroDefPath) {
1670+
private predicate isDollarCrateSupportedMacroExpansion(Path macroDefPath, AstNode expansion) {
16721671
exists(MacroCall mc |
16731672
expansion = mc.getMacroCallExpansion() and
16741673
macroDefPath = mc.getPath()
@@ -1681,25 +1680,25 @@ private predicate isMacroExpansion(AstNode expansion, Path macroDefPath) {
16811680
}
16821681

16831682
pragma[nomagic]
1684-
predicate isInMacroExpansion(Path macroDefPath, AstNode n) {
1685-
isMacroExpansion(n, macroDefPath)
1683+
predicate isInDollarCrateSupportedMacroExpansion(File macroDefFile, AstNode n) {
1684+
exists(Path macroDefPath |
1685+
isDollarCrateSupportedMacroExpansion(macroDefPath, n) and
1686+
macroDefFile = resolvePathCand(macroDefPath).getFile()
1687+
)
16861688
or
1687-
isInMacroExpansion(macroDefPath, n.getParentNode())
1689+
isInDollarCrateSupportedMacroExpansion(macroDefFile, n.getParentNode())
16881690
}
16891691

16901692
/**
1691-
* Holds if `n` is inside a macro expansion, and the macro _may_ originate from
1692-
* crate `crate`.
1693+
* Holds if `n` is a `$crate` path, and it may resolve to `crate`.
16931694
*
16941695
* The reason why we cannot be sure is that we need to consider all ancestor macro
16951696
* calls.
16961697
*/
16971698
pragma[nomagic]
1698-
predicate isInMacroFromCrateExpansion(CrateItemNode crate, AstNode n) {
1699-
exists(Path macroDefPath |
1700-
isInMacroExpansion(macroDefPath, n) and
1701-
crate.getASourceFile().getFile() = resolvePathCand(macroDefPath).getFile()
1702-
)
1699+
predicate resolveDollarCrate(RelevantPath p, CrateItemNode crate) {
1700+
p.isDollarCrate() and
1701+
isInDollarCrateSupportedMacroExpansion(crate.getASourceFile().getFile(), p)
17031702
}
17041703

17051704
pragma[nomagic]
@@ -1714,8 +1713,7 @@ private ItemNode resolvePathCand0(RelevantPath path, Namespace ns) {
17141713
else result = res
17151714
)
17161715
or
1717-
path.isDollarCrate() and
1718-
isInMacroFromCrateExpansion(result, path) and
1716+
resolveDollarCrate(path, result) and
17191717
ns = result.getNamespace()
17201718
or
17211719
result = resolvePathCandQualified(_, _, path, ns)

0 commit comments

Comments
 (0)