Skip to content

Commit 76ca5f8

Browse files
committed
Rust: Reduce size of unqualifiedPathLookup
1 parent b5b6f06 commit 76ca5f8

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

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

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,13 +1401,22 @@ class RelevantPath extends Path {
14011401
name = this.getText()
14021402
}
14031403

1404+
/**
1405+
* Holds if this is an unqualified path with the textual value `name` and
1406+
* enclosing item `encl`.
1407+
*/
14041408
pragma[nomagic]
1405-
predicate isCratePath(string name, ItemNode encl) {
1406-
name = ["crate", "$crate"] and
1409+
predicate isUnqualified(string name, ItemNode encl) {
14071410
this.isUnqualified(name) and
14081411
encl.getADescendant() = this
14091412
}
14101413

1414+
pragma[nomagic]
1415+
predicate isCratePath(string name, ItemNode encl) {
1416+
name = ["crate", "$crate"] and
1417+
this.isUnqualified(name, encl)
1418+
}
1419+
14111420
pragma[nomagic]
14121421
predicate isDollarCrateQualifiedPath(string name) {
14131422
this.getQualifier().(RelevantPath).isCratePath("$crate", _) and
@@ -1432,26 +1441,26 @@ private ItemNode getOuterScope(ItemNode i) {
14321441
}
14331442

14341443
/**
1435-
* Holds if the unqualified path `p` references an item named `name`, and `name`
1436-
* may be looked up in the `ns` namespace inside enclosing item `encl`.
1444+
* Holds if _some_ unqualified path in `encl` references an item named `name`,
1445+
* and `name` may be looked up in the `ns` namespace inside `ancestor`.
14371446
*/
14381447
pragma[nomagic]
1439-
private predicate unqualifiedPathLookup(ItemNode encl, string name, Namespace ns, RelevantPath p) {
1448+
private predicate unqualifiedPathLookup(ItemNode ancestor, string name, Namespace ns, ItemNode encl) {
14401449
// lookup in the immediately enclosing item
1441-
p.isUnqualified(name) and
1442-
encl.getADescendant() = p and
1450+
any(RelevantPath p).isUnqualified(name, encl) and
1451+
ancestor = encl and
14431452
exists(ns) and
14441453
not name = ["crate", "$crate", "super", "self"]
14451454
or
14461455
// lookup in an outer scope, but only if the item is not declared in inner scope
14471456
exists(ItemNode mid |
1448-
unqualifiedPathLookup(mid, name, ns, p) and
1457+
unqualifiedPathLookup(mid, name, ns, encl) and
14491458
not declares(mid, ns, name) and
14501459
not (
14511460
name = "Self" and
14521461
mid = any(ImplOrTraitItemNode i).getAnItemInSelfScope()
14531462
) and
1454-
encl = getOuterScope(mid)
1463+
ancestor = getOuterScope(mid)
14551464
)
14561465
}
14571466

@@ -1477,29 +1486,31 @@ private predicate sourceFileHasCratePathTc(ItemNode i1, ItemNode i2) =
14771486
* `name` may be looked up inside enclosing item `encl`.
14781487
*/
14791488
pragma[nomagic]
1480-
private predicate keywordLookup(ItemNode encl, string name, RelevantPath p) {
1489+
private predicate keywordLookup(ItemNode ancestor, string name, RelevantPath p) {
14811490
// For `($)crate`, jump directly to the root module
14821491
exists(ItemNode i | p.isCratePath(name, i) |
1483-
encl instanceof SourceFile and
1484-
encl = i
1492+
ancestor instanceof SourceFile and
1493+
ancestor = i
14851494
or
1486-
sourceFileHasCratePathTc(encl, i)
1495+
sourceFileHasCratePathTc(ancestor, i)
14871496
)
14881497
or
14891498
name = ["super", "self"] and
1490-
p.isUnqualified(name) and
1491-
encl.getADescendant() = p
1499+
p.isUnqualified(name, ancestor)
14921500
}
14931501

14941502
pragma[nomagic]
14951503
private ItemNode unqualifiedPathLookup(RelevantPath p, Namespace ns, SuccessorKind kind) {
1496-
exists(ItemNode encl, string name |
1497-
result = getASuccessor(encl, name, ns, kind, _) and
1504+
exists(ItemNode ancestor, string name |
1505+
result = getASuccessor(ancestor, pragma[only_bind_into](name), ns, kind, _) and
14981506
kind.isInternalOrBoth()
14991507
|
1500-
unqualifiedPathLookup(encl, name, ns, p)
1508+
exists(ItemNode encl |
1509+
unqualifiedPathLookup(ancestor, name, ns, encl) and
1510+
p.isUnqualified(pragma[only_bind_into](name), encl)
1511+
)
15011512
or
1502-
keywordLookup(encl, name, p) and exists(ns)
1513+
keywordLookup(ancestor, name, p) and exists(ns)
15031514
)
15041515
}
15051516

@@ -1880,10 +1891,13 @@ private module Debug {
18801891
}
18811892

18821893
predicate debugUnqualifiedPathLookup(
1883-
RelevantPath p, string name, Namespace ns, ItemNode encl, string path
1894+
RelevantPath p, string name, Namespace ns, ItemNode ancestor, string path
18841895
) {
18851896
p = getRelevantLocatable() and
1886-
unqualifiedPathLookup(encl, name, ns, p) and
1897+
exists(ItemNode encl |
1898+
unqualifiedPathLookup(encl, name, ns, ancestor) and
1899+
p.isUnqualified(name, encl)
1900+
) and
18871901
path = p.toStringDebug()
18881902
}
18891903

0 commit comments

Comments
 (0)