Skip to content

Rust: More path resolution improvements #18823

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

Merged
merged 4 commits into from
Feb 24, 2025
Merged
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
67 changes: 62 additions & 5 deletions rust/ql/lib/codeql/rust/elements/internal/PathResolution.qll
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,31 @@ abstract class ItemNode extends AstNode {
)
or
// a trait has access to the associated items of its supertraits
result = this.(TraitItemNode).resolveABound().getASuccessorRec(name) and
result instanceof AssocItemNode
this =
any(TraitItemNode trait |
result = trait.resolveABound().getASuccessorRec(name) and
result instanceof AssocItemNode and
not trait.hasAssocItem(name)
)
or
// items made available by an implementation where `this` is the implementing type
exists(ItemNode node |
this = node.(ImplItemNode).resolveSelfTy() and
result = node.getASuccessorRec(name) and
result instanceof AssocItemNode
)
or
// trait items with default implementations made available in an implementation
exists(ImplItemNode impl, ItemNode trait |
this = impl and
trait = impl.resolveTraitTy() and
result = trait.getASuccessorRec(name) and
result.(AssocItemNode).hasImplementation() and
not impl.hasAssocItem(name)
)
or
// type parameters have access to the associated items of its bounds
result = this.(TypeParamItemNode).resolveABound().getASuccessorRec(name).(AssocItemNode)
}

/** Gets a successor named `name` of this item, if any. */
Expand All @@ -163,6 +179,9 @@ abstract class ItemNode extends AstNode {
name = "crate" and
result.(SourceFileItemNode).getFile() = this.getFile()
}

/** Gets the location of this item. */
Location getLocation() { result = super.getLocation() }
}

/** A module or a source file. */
Expand Down Expand Up @@ -191,11 +210,16 @@ private class SourceFileItemNode extends ModuleLikeNode, SourceFile {
}

/** An item that can occur in a trait or an `impl` block. */
abstract private class AssocItemNode extends ItemNode { }
abstract private class AssocItemNode extends ItemNode, AssocItem {
/** Holds if this associated item has an implementation. */
abstract predicate hasImplementation();
}

private class ConstItemNode extends AssocItemNode instanceof Const {
override string getName() { result = Const.super.getName().getText() }

override predicate hasImplementation() { super.hasBody() }

override Namespace getNamespace() { result.isValue() }

override Visibility getVisibility() { result = Const.super.getVisibility() }
Expand All @@ -219,9 +243,11 @@ private class VariantItemNode extends ItemNode instanceof Variant {
override Visibility getVisibility() { result = Variant.super.getVisibility() }
}

private class FunctionItemNode extends AssocItemNode instanceof Function {
class FunctionItemNode extends AssocItemNode instanceof Function {
override string getName() { result = Function.super.getName().getText() }

override predicate hasImplementation() { super.hasBody() }

override Namespace getNamespace() { result.isValue() }

override Visibility getVisibility() { result = Function.super.getVisibility() }
Expand All @@ -242,7 +268,19 @@ abstract private class ImplOrTraitItemNode extends ItemNode {
}

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

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

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

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

/** Holds if this `impl` block declares an associated item named `name`. */
pragma[nomagic]
predicate hasAssocItem(string name) {
name = super.getAssocItemList().getAnAssocItem().(AssocItemNode).getName()
}

override string getName() { result = "(impl)" }

Expand All @@ -256,6 +294,8 @@ class ImplItemNode extends ImplOrTraitItemNode instanceof Impl {
private class MacroCallItemNode extends AssocItemNode instanceof MacroCall {
override string getName() { result = "(macro call)" }

override predicate hasImplementation() { none() }

override Namespace getNamespace() { none() }

override Visibility getVisibility() { none() }
Expand Down Expand Up @@ -290,6 +330,12 @@ class TraitItemNode extends ImplOrTraitItemNode instanceof Trait {

ItemNode resolveABound() { result = resolvePath(this.getABoundPath()) }

/** Holds if this trait declares an associated item named `name`. */
pragma[nomagic]
predicate hasAssocItem(string name) {
name = super.getAssocItemList().getAnAssocItem().(AssocItemNode).getName()
}

override string getName() { result = Trait.super.getName().getText() }

override Namespace getNamespace() { result.isType() }
Expand All @@ -300,6 +346,8 @@ class TraitItemNode extends ImplOrTraitItemNode instanceof Trait {
class TypeAliasItemNode extends AssocItemNode instanceof TypeAlias {
override string getName() { result = TypeAlias.super.getName().getText() }

override predicate hasImplementation() { super.hasTypeRepr() }

override Namespace getNamespace() { result.isType() }

override Visibility getVisibility() { result = TypeAlias.super.getVisibility() }
Expand Down Expand Up @@ -330,11 +378,20 @@ private class BlockExprItemNode extends ItemNode instanceof BlockExpr {
}

private class TypeParamItemNode extends ItemNode instanceof TypeParam {
pragma[nomagic]
Path getABoundPath() {
result = super.getTypeBoundList().getABound().getTypeRepr().(PathTypeRepr).getPath()
}

ItemNode resolveABound() { result = resolvePath(this.getABoundPath()) }

override string getName() { result = TypeParam.super.getName().getText() }

override Namespace getNamespace() { result.isType() }

override Visibility getVisibility() { none() }

override Location getLocation() { result = TypeParam.super.getName().getLocation() }
}

/** Holds if `item` has the name `name` and is a top-level item inside `f`. */
Expand Down
40 changes: 40 additions & 0 deletions rust/ql/test/library-tests/path-resolution/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@ mod m16 {
fn g(&self) -> T // $ item=I84
; // I85

fn h(&self) -> T { // $ item=I84
Self::g(&self); // $ item=I85
self.g() // $ MISSING: item=I85
} // I96

const c: T // $ item=I84
; // I94
} // I86
Expand Down Expand Up @@ -425,6 +430,8 @@ mod m16 {
>::f(&x); // $ MISSING: item=I93
S::g(&x); // $ item=I92
x.g(); // $ MISSING: item=I92
S::h(&x); // $ item=I96
x.h(); // $ MISSING: item=I96
S::c; // $ item=I95
<S // $ item=I90
as Trait1<
Expand All @@ -434,6 +441,38 @@ mod m16 {
} // I83
}

mod m17 {
trait MyTrait {
fn f(&self); // I1
} // I2

struct S; // I3

#[rustfmt::skip]
impl MyTrait // $ item=I2
for S { // $ item=I3
fn f(&self) {
println!("M17::MyTrait::f");
} // I4
}

#[rustfmt::skip]
fn g<T: // I5
MyTrait // $ item=I2
>(x: T) { // $ item=I5
x.f(); // $ MISSING: item=I1
T::f(&x); // $ item=I1
MyTrait::f(&x); // $ item=I1
} // I6

#[rustfmt::skip]
pub fn f() {
g( // $ item=I6
S // $ item=I3
);
} // I99
}

fn main() {
my::nested::nested1::nested2::f(); // $ item=I4
my::f(); // $ item=I38
Expand All @@ -455,4 +494,5 @@ fn main() {
m11::f(); // $ item=I63
m15::f(); // $ item=I75
m16::f(); // $ item=I83
m17::f(); // $ item=I99
}
Loading