Skip to content

Rust: Remove source vs library deduplication logic #19577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 10 additions & 36 deletions rust/ql/lib/codeql/rust/internal/PathResolution.qll
Original file line number Diff line number Diff line change
Expand Up @@ -524,17 +524,17 @@ abstract class ImplOrTraitItemNode extends ItemNode {

pragma[nomagic]
private TypeParamItemNode resolveTypeParamPathTypeRepr(PathTypeRepr ptr) {
result = resolvePathFull(ptr.getPath())
result = resolvePath(ptr.getPath())
}

class ImplItemNode extends ImplOrTraitItemNode instanceof Impl {
Path getSelfPath() { result = super.getSelfTy().(PathTypeRepr).getPath() }

Path getTraitPath() { result = super.getTrait().(PathTypeRepr).getPath() }

ItemNode resolveSelfTy() { result = resolvePathFull(this.getSelfPath()) }
ItemNode resolveSelfTy() { result = resolvePath(this.getSelfPath()) }

TraitItemNode resolveTraitTy() { result = resolvePathFull(this.getTraitPath()) }
TraitItemNode resolveTraitTy() { result = resolvePath(this.getTraitPath()) }

override AssocItemNode getAnAssocItem() { result = super.getAssocItemList().getAnAssocItem() }

Expand Down Expand Up @@ -733,7 +733,7 @@ class TraitItemNode extends ImplOrTraitItemNode instanceof Trait {
}

pragma[nomagic]
ItemNode resolveABound() { result = resolvePathFull(this.getABoundPath()) }
ItemNode resolveABound() { result = resolvePath(this.getABoundPath()) }

override AssocItemNode getAnAssocItem() { result = super.getAssocItemList().getAnAssocItem() }

Expand Down Expand Up @@ -872,7 +872,7 @@ class TypeParamItemNode extends ItemNode instanceof TypeParam {
}

pragma[nomagic]
ItemNode resolveABound() { result = resolvePathFull(this.getABoundPath()) }
ItemNode resolveABound() { result = resolvePath(this.getABoundPath()) }

/**
* Holds if this type parameter has a trait bound. Examples:
Expand Down Expand Up @@ -1285,14 +1285,9 @@ private predicate pathUsesNamespace(Path p, Namespace n) {
)
}

/**
* Gets the item that `path` resolves to, if any.
*
* Whenever `path` can resolve to both a function in source code and in library
* code, both are included
*/
pragma[nomagic]
private ItemNode resolvePathFull(RelevantPath path) {
/** Gets the item that `path` resolves to, if any. */
cached
ItemNode resolvePath(RelevantPath path) {
exists(Namespace ns | result = resolvePath0(path, ns) |
pathUsesNamespace(path, ns)
or
Expand All @@ -1301,30 +1296,9 @@ private ItemNode resolvePathFull(RelevantPath path) {
)
}

pragma[nomagic]
private predicate resolvesSourceFunction(RelevantPath path) {
resolvePathFull(path).(Function).fromSource()
}

/** Gets the item that `path` resolves to, if any. */
cached
ItemNode resolvePath(RelevantPath path) {
result = resolvePathFull(path) and
(
// when a function exists in both source code and in library code, it is because
// we also extracted the source code as library code, and hence we only want
// the function from source code
result.fromSource()
or
not result instanceof Function
or
not resolvesSourceFunction(path)
)
}

pragma[nomagic]
private ItemNode resolvePathQualifier(RelevantPath path, string name) {
result = resolvePathFull(path.getQualifier()) and
result = resolvePath(path.getQualifier()) and
name = path.getText()
}

Expand Down Expand Up @@ -1370,7 +1344,7 @@ private ItemNode resolveUseTreeListItemQualifier(
pragma[nomagic]
private ItemNode resolveUseTreeListItem(Use use, UseTree tree) {
tree = use.getUseTree() and
result = resolvePathFull(tree.getPath())
result = resolvePath(tree.getPath())
or
result = resolveUseTreeListItem(use, tree, tree.getPath())
}
Expand Down
16 changes: 3 additions & 13 deletions rust/ql/lib/codeql/rust/internal/TypeInference.qll
Original file line number Diff line number Diff line change
Expand Up @@ -1231,9 +1231,10 @@ private module Cached {
f = any(Impl impl | impl.hasTrait()).(ImplItemNode).getAnAssocItem()
}

private Function resolveMethodCallTargetFrom(MethodCall mc, boolean fromSource) {
/** Gets a method that the method call `mc` resolves to, if any. */
cached
Function resolveMethodCallTarget(MethodCall mc) {
result = inferMethodCallTarget(mc) and
(if result.fromSource() then fromSource = true else fromSource = false) and
(
// prioritize inherent implementation methods first
isInherentImplFunction(result)
Expand All @@ -1255,17 +1256,6 @@ private module Cached {
)
}

/** Gets a method that the method call `mc` resolves to, if any. */
cached
Function resolveMethodCallTarget(MethodCall mc) {
// Functions in source code also gets extracted as library code, due to
// this duplication we prioritize functions from source code.
result = resolveMethodCallTargetFrom(mc, true)
or
not exists(resolveMethodCallTargetFrom(mc, true)) and
result = resolveMethodCallTargetFrom(mc, false)
}

pragma[inline]
private Type inferRootTypeDeref(AstNode n) {
result = inferType(n) and
Expand Down