Skip to content

Commit f87f52d

Browse files
committed
Address review comments
1 parent dcaf4a7 commit f87f52d

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

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

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,15 @@ abstract class ItemNode extends Locatable {
233233
// a trait has access to the associated items of its supertraits
234234
this =
235235
any(TraitItemNodeImpl trait |
236-
result = trait.resolveABound().getASuccessor(name, kind) and
236+
result = trait.resolveABoundCand().getASuccessor(name, kind) and
237237
kind.isExternalOrBoth() and
238238
result instanceof AssocItemNode and
239239
not trait.hasAssocItem(name)
240240
)
241241
or
242242
// items made available by an implementation where `this` is the implementing type
243243
exists(ItemNode node |
244-
this = node.(ImplItemNodeImpl).resolveSelfTy() and
244+
this = node.(ImplItemNodeImpl).resolveSelfTyCand() and
245245
result = node.getASuccessor(name, kind) and
246246
kind.isExternalOrBoth() and
247247
result instanceof AssocItemNode
@@ -250,22 +250,26 @@ abstract class ItemNode extends Locatable {
250250
// trait items with default implementations made available in an implementation
251251
exists(ImplItemNodeImpl impl, ItemNode trait |
252252
this = impl and
253-
trait = impl.resolveTraitTy() and
253+
trait = impl.resolveTraitTyCand() and
254254
result = trait.getASuccessor(name, kind) and
255255
result.(AssocItemNode).hasImplementation() and
256256
kind.isExternalOrBoth() and
257257
not impl.hasAssocItem(name)
258258
)
259259
or
260260
// type parameters have access to the associated items of its bounds
261-
result = this.(TypeParamItemNodeImpl).resolveABound().getASuccessor(name, kind).(AssocItemNode) and
261+
result =
262+
this.(TypeParamItemNodeImpl).resolveABoundCand().getASuccessor(name, kind).(AssocItemNode) and
262263
kind.isExternalOrBoth()
263264
or
264265
result =
265-
this.(ImplTraitTypeReprItemNodeImpl).resolveABound().getASuccessor(name, kind).(AssocItemNode) and
266+
this.(ImplTraitTypeReprItemNodeImpl)
267+
.resolveABoundCand()
268+
.getASuccessor(name, kind)
269+
.(AssocItemNode) and
266270
kind.isExternalOrBoth()
267271
or
268-
result = this.(TypeAliasItemNodeImpl).resolveAlias().getASuccessor(name, kind) and
272+
result = this.(TypeAliasItemNodeImpl).resolveAliasCand().getASuccessor(name, kind) and
269273
kind.isExternalOrBoth()
270274
or
271275
name = "super" and
@@ -746,7 +750,7 @@ final private class ImplTraitTypeReprItemNode extends TypeItemNode instanceof Im
746750

747751
private class ImplTraitTypeReprItemNodeImpl extends ImplTraitTypeReprItemNode {
748752
pragma[nomagic]
749-
ItemNode resolveABound() { result = resolvePathCand(this.getABoundPath()) }
753+
ItemNode resolveABoundCand() { result = resolvePathCand(this.getABoundPath()) }
750754
}
751755

752756
private class ModuleItemNode extends ModuleLikeNode instanceof Module {
@@ -792,9 +796,9 @@ private class ModuleItemNode extends ModuleLikeNode instanceof Module {
792796
}
793797

794798
private class ImplItemNodeImpl extends ImplItemNode {
795-
TypeItemNode resolveSelfTy() { result = resolvePathCand(this.getSelfPath()) }
799+
TypeItemNode resolveSelfTyCand() { result = resolvePathCand(this.getSelfPath()) }
796800

797-
TraitItemNode resolveTraitTy() { result = resolvePathCand(this.getTraitPath()) }
801+
TraitItemNode resolveTraitTyCand() { result = resolvePathCand(this.getTraitPath()) }
798802
}
799803

800804
private class StructItemNode extends TypeItemNode instanceof Struct {
@@ -889,7 +893,7 @@ final class TraitItemNode extends ImplOrTraitItemNode, TypeItemNode instanceof T
889893

890894
final private class TraitItemNodeImpl extends TraitItemNode {
891895
pragma[nomagic]
892-
ItemNode resolveABound() { result = resolvePathCand(this.getABoundPath()) }
896+
ItemNode resolveABoundCand() { result = resolvePathCand(this.getABoundPath()) }
893897
}
894898

895899
final class TypeAliasItemNode extends TypeItemNode, AssocItemNode instanceof TypeAlias {
@@ -913,7 +917,9 @@ final class TypeAliasItemNode extends TypeItemNode, AssocItemNode instanceof Typ
913917

914918
private class TypeAliasItemNodeImpl extends TypeAliasItemNode instanceof TypeAlias {
915919
pragma[nomagic]
916-
ItemNode resolveAlias() { result = resolvePathCand(super.getTypeRepr().(PathTypeRepr).getPath()) }
920+
ItemNode resolveAliasCand() {
921+
result = resolvePathCand(super.getTypeRepr().(PathTypeRepr).getPath())
922+
}
917923
}
918924

919925
private class UnionItemNode extends TypeItemNode instanceof Union {
@@ -1053,7 +1059,7 @@ final class TypeParamItemNode extends TypeItemNode instanceof TypeParam {
10531059
final private class TypeParamItemNodeImpl extends TypeParamItemNode instanceof TypeParam {
10541060
/** Gets a where predicate for this type parameter, if any */
10551061
pragma[nomagic]
1056-
private WherePred getAWherePred() {
1062+
private WherePred getAWherePredCand() {
10571063
exists(ItemNode declaringItem |
10581064
this = resolvePathCand(getWherePredPath(result)) and
10591065
result = declaringItem.getADescendant() and
@@ -1062,23 +1068,25 @@ final private class TypeParamItemNodeImpl extends TypeParamItemNode instanceof T
10621068
}
10631069

10641070
pragma[nomagic]
1065-
TypeBound getTypeBoundAt(int i, int j) {
1071+
TypeBound getTypeBoundAtCand(int i, int j) {
10661072
exists(TypeBoundList tbl | result = tbl.getBound(j) |
10671073
tbl = super.getTypeBoundList() and i = 0
10681074
or
10691075
exists(WherePred wp |
1070-
wp = this.getAWherePred() and
1076+
wp = this.getAWherePredCand() and
10711077
tbl = wp.getTypeBoundList() and
10721078
wp = any(WhereClause wc).getPredicate(i)
10731079
)
10741080
)
10751081
}
10761082

10771083
pragma[nomagic]
1078-
Path getABoundPath() { result = this.getTypeBoundAt(_, _).getTypeRepr().(PathTypeRepr).getPath() }
1084+
Path getABoundPathCand() {
1085+
result = this.getTypeBoundAtCand(_, _).getTypeRepr().(PathTypeRepr).getPath()
1086+
}
10791087

10801088
pragma[nomagic]
1081-
ItemNode resolveABound() { result = resolvePathCand(this.getABoundPath()) }
1089+
ItemNode resolveABoundCand() { result = resolvePathCand(this.getABoundPathCand()) }
10821090
}
10831091

10841092
/** Holds if `item` has the name `name` and is a top-level item inside `f`. */
@@ -1245,7 +1253,10 @@ private predicate crateDependencyEdge(SourceFileItemNode file, string name, Crat
12451253
crateDependency(file, name, dep)
12461254
or
12471255
// As a fallback, give all files access to crates that do not conflict with known dependencies
1248-
// and declarations.
1256+
// and declarations. This is in order to workaround incomplete crate dependency information
1257+
// provided by the extractor, as well as `CrateItemNode.getASourceFile()` being unable to map
1258+
// a given file to its crate (for example, if the file is `mod` imported inside a macro that the
1259+
// extractor is unable to expand).
12491260
name = dep.getName() and
12501261
not declaresDirectly(file, TTypeNamespace(), name) and
12511262
not crateDependency(file, name, _)
@@ -1418,7 +1429,7 @@ private ItemNode resolvePathCand0(RelevantPath path, Namespace ns) {
14181429
not any(RelevantPath parent).getQualifier() = path and
14191430
isUnqualifiedSelfPath(path) and
14201431
res instanceof ImplItemNode
1421-
then result = res.(ImplItemNodeImpl).resolveSelfTy()
1432+
then result = res.(ImplItemNodeImpl).resolveSelfTyCand()
14221433
else result = res
14231434
)
14241435
or
@@ -1482,6 +1493,9 @@ private predicate pathUsesNamespace(Path p, Namespace n) {
14821493
* Unlike `resolvePath`, this predicate does not attempt to make resolution
14831494
* of qualifiers consistent with resolution of their parents, and should
14841495
* only be used internally within this library.
1496+
*
1497+
* Note that the path resolution logic cannot use `resolvePath`, as that would
1498+
* result in non-monotonic recursion.
14851499
*/
14861500
pragma[nomagic]
14871501
private ItemNode resolvePathCand(RelevantPath path) {
@@ -1515,16 +1529,10 @@ ItemNode resolvePath(RelevantPath path) {
15151529
// if `path` is the qualifier of a resolvable `parent`, then we should
15161530
// resolve `path` to something consistent with what `parent` resolves to
15171531
exists(RelevantPath parent |
1518-
resolvePathCandQualified(path, result, parent, _) = resolvePathParent(path, parent)
1532+
resolvePathCandQualified(path, result, parent, _) = resolvePath(parent)
15191533
)
15201534
}
15211535

1522-
pragma[nomagic]
1523-
private ItemNode resolvePathParent(RelevantPath path, RelevantPath parent) {
1524-
result = resolvePath(parent) and
1525-
path = parent.getQualifier()
1526-
}
1527-
15281536
private predicate isUseTreeSubPath(UseTree tree, RelevantPath path) {
15291537
path = tree.getPath()
15301538
or

0 commit comments

Comments
 (0)