Skip to content

Commit f8b104d

Browse files
committed
Rust: Use doubleBoundedFastTC for resolving $crate paths
1 parent dd3debc commit f8b104d

File tree

1 file changed

+44
-32
lines changed

1 file changed

+44
-32
lines changed

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

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,39 +1666,51 @@ module TraitIsVisible<relevantTraitVisibleSig/2 relevantTraitVisible> {
16661666
}
16671667
}
16681668

1669-
pragma[nomagic]
1670-
private predicate isDollarCrateSupportedMacroExpansion(Path macroDefPath, AstNode expansion) {
1671-
exists(MacroCall mc |
1672-
expansion = mc.getMacroCallExpansion() and
1673-
macroDefPath = mc.getPath()
1674-
)
1675-
or
1676-
exists(ItemNode adt |
1677-
expansion = adt.(Adt).getDeriveMacroExpansion(_) and
1678-
macroDefPath = adt.getAttr("derive").getMeta().getPath()
1679-
)
1680-
}
1669+
private module DollarCrateResolution {
1670+
pragma[nomagic]
1671+
private predicate isDollarCrateSupportedMacroExpansion(Path macroDefPath, AstNode expansion) {
1672+
exists(MacroCall mc |
1673+
expansion = mc.getMacroCallExpansion() and
1674+
macroDefPath = mc.getPath()
1675+
)
1676+
or
1677+
exists(ItemNode adt |
1678+
expansion = adt.(Adt).getDeriveMacroExpansion(_) and
1679+
macroDefPath = adt.getAttr("derive").getMeta().getPath()
1680+
)
1681+
}
16811682

1682-
pragma[nomagic]
1683-
predicate isInDollarCrateSupportedMacroExpansion(File macroDefFile, AstNode n) {
1684-
exists(Path macroDefPath |
1685-
isDollarCrateSupportedMacroExpansion(macroDefPath, n) and
1686-
macroDefFile = resolvePathCand(macroDefPath).getFile()
1687-
)
1688-
or
1689-
isInDollarCrateSupportedMacroExpansion(macroDefFile, n.getParentNode())
1690-
}
1683+
private predicate hasParent(AstNode child, AstNode parent) { parent = child.getParentNode() }
16911684

1692-
/**
1693-
* Holds if `n` is a `$crate` path, and it may resolve to `crate`.
1694-
*
1695-
* The reason why we cannot be sure is that we need to consider all ancestor macro
1696-
* calls.
1697-
*/
1698-
pragma[nomagic]
1699-
predicate resolveDollarCrate(RelevantPath p, CrateItemNode crate) {
1700-
p.isDollarCrate() and
1701-
isInDollarCrateSupportedMacroExpansion(crate.getASourceFile().getFile(), p)
1685+
private predicate isDollarCrateSupportedMacroExpansion(AstNode expansion) {
1686+
isDollarCrateSupportedMacroExpansion(_, expansion)
1687+
}
1688+
1689+
private predicate isDollarCratePath(RelevantPath p) { p.isDollarCrate() }
1690+
1691+
private predicate isInDollarCrateMacroExpansion(RelevantPath p, AstNode expansion) =
1692+
doublyBoundedFastTC(hasParent/2, isDollarCratePath/1, isDollarCrateSupportedMacroExpansion/1)(p,
1693+
expansion)
1694+
1695+
pragma[nomagic]
1696+
private predicate isInDollarCrateMacroExpansionFromFile(File macroDefFile, RelevantPath p) {
1697+
exists(Path macroDefPath, AstNode expansion |
1698+
isDollarCrateSupportedMacroExpansion(macroDefPath, expansion) and
1699+
isInDollarCrateMacroExpansion(p, expansion) and
1700+
macroDefFile = resolvePathCand(macroDefPath).getFile()
1701+
)
1702+
}
1703+
1704+
/**
1705+
* Holds if `p` is a `$crate` path, and it may resolve to `crate`.
1706+
*
1707+
* The reason why we cannot be sure is that we need to consider all ancestor macro
1708+
* calls.
1709+
*/
1710+
pragma[nomagic]
1711+
predicate resolveDollarCrate(RelevantPath p, CrateItemNode crate) {
1712+
isInDollarCrateMacroExpansionFromFile(crate.getASourceFile().getFile(), p)
1713+
}
17021714
}
17031715

17041716
pragma[nomagic]
@@ -1713,7 +1725,7 @@ private ItemNode resolvePathCand0(RelevantPath path, Namespace ns) {
17131725
else result = res
17141726
)
17151727
or
1716-
resolveDollarCrate(path, result) and
1728+
DollarCrateResolution::resolveDollarCrate(path, result) and
17171729
ns = result.getNamespace()
17181730
or
17191731
result = resolvePathCandQualified(_, _, path, ns)

0 commit comments

Comments
 (0)