Content-Length: 426038 | pFad | http://github.com/github/codeql/pull/19988/files

BF Rust: path resolution: handle items in `extern` blocks by aibaars · Pull Request #19988 · github/codeql · GitHub
Skip to content

Rust: path resolution: handle items in extern blocks #19988

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 3 commits into from
Jul 7, 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
48 changes: 3 additions & 45 deletions rust/ql/lib/codeql/rust/internal/PathResolution.qll
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,6 @@ abstract class ItemNode extends Locatable {
)
)
or
// items made available through macro calls are available to nodes that contain the macro call
exists(MacroCallItemNode call |
call = this.getASuccessorRec(_) and
result = call.(ItemNode).getASuccessorRec(name)
)
or
// a trait has access to the associated items of its supertraits
this =
any(TraitItemNode trait |
Expand Down Expand Up @@ -573,7 +567,7 @@ class ImplItemNode extends ImplOrTraitItemNode instanceof Impl {

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

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

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

Expand Down Expand Up @@ -680,32 +674,6 @@ private class ImplTraitTypeReprItemNode extends ItemNode instanceof ImplTraitTyp
override string getCanonicalPath(Crate c) { none() }
}

private class MacroCallItemNode extends AssocItemNode instanceof MacroCall {
override string getName() { result = "(macro call)" }

override predicate hasImplementation() { none() }

override Namespace getNamespace() { none() }

override TypeParam getTypeParam(int i) { none() }

override Visibility getVisibility() { none() }

override predicate providesCanonicalPathPrefixFor(Crate c, ItemNode child) {
any(ItemNode parent).providesCanonicalPathPrefixFor(c, this) and
child.getImmediateParent() = this
}

override string getCanonicalPathPrefixFor(Crate c, ItemNode child) {
result = this.getCanonicalPathPrefix(c) and
this.providesCanonicalPathPrefixFor(c, child)
}

override predicate hasCanonicalPath(Crate c) { none() }

override string getCanonicalPath(Crate c) { none() }
}

private class ModuleItemNode extends ModuleLikeNode instanceof Module {
override string getName() { result = Module.super.getName().getText() }

Expand All @@ -726,11 +694,6 @@ private class ModuleItemNode extends ModuleLikeNode instanceof Module {
)
or
this = child.getImmediateParent()
or
exists(ItemNode mid |
this.providesCanonicalPathPrefixFor(c, mid) and
mid.(MacroCallItemNode) = child.getImmediateParent()
)
)
}

Expand Down Expand Up @@ -797,7 +760,7 @@ class TraitItemNode extends ImplOrTraitItemNode, TypeItemNode instanceof Trait {
pragma[nomagic]
ItemNode resolveABound() { result = resolvePath(this.getABoundPath()) }

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

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

Expand Down Expand Up @@ -993,7 +956,7 @@ class TypeParamItemNode extends TypeItemNode instanceof TypeParam {

/** Holds if `item` has the name `name` and is a top-level item inside `f`. */
private predicate sourceFileEdge(SourceFile f, string name, ItemNode item) {
item = f.getAnItem() and
item = f.(ItemNode).getADescendant() and
name = item.getName()
}

Expand Down Expand Up @@ -1186,11 +1149,6 @@ private predicate declares(ItemNode item, Namespace ns, string name) {
useTreeDeclares(child.(Use).getUseTree(), name) and
exists(ns) // `use foo::bar` can refer to both a value and a type
)
or
exists(MacroCallItemNode call |
declares(call, ns, name) and
call.getImmediateParent() = item
)
}

/** A path that does not access a local variable. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ canonicalPath
| regular.rs:40:1:46:1 | fn enum_qualified_usage | test::regular::enum_qualified_usage |
| regular.rs:48:1:55:1 | fn enum_unqualified_usage | test::regular::enum_unqualified_usage |
| regular.rs:57:1:63:1 | fn enum_match | test::regular::enum_match |
| regular.rs:66:5:66:40 | fn is_alphanum | test::regular::is_alphanum |
| regular.rs:69:1:71:1 | fn is_number_or_letter | test::regular::is_number_or_letter |
canonicalPaths
| anonymous.rs:1:1:1:26 | use ...::Trait | None | None |
| anonymous.rs:3:1:32:1 | fn canonicals | repo::test | crate::anonymous::canonicals |
Expand Down Expand Up @@ -65,6 +67,9 @@ canonicalPaths
| regular.rs:48:1:55:1 | fn enum_unqualified_usage | repo::test | crate::regular::enum_unqualified_usage |
| regular.rs:51:5:51:18 | use MyEnum::* | None | None |
| regular.rs:57:1:63:1 | fn enum_match | repo::test | crate::regular::enum_match |
| regular.rs:65:1:67:1 | ExternBlock | None | None |
| regular.rs:66:5:66:40 | fn is_alphanum | repo::test | ::is_alphanum |
| regular.rs:69:1:71:1 | fn is_number_or_letter | repo::test | crate::regular::is_number_or_letter |
resolvedPaths
| anonymous.rs:27:17:27:30 | OtherStruct {...} | None | None |
| anonymous.rs:28:9:28:9 | s | None | None |
Expand Down Expand Up @@ -96,3 +101,5 @@ resolvedPaths
| regular.rs:59:9:59:24 | ...::Variant1 | repo::test | crate::regular::MyEnum::Variant1 |
| regular.rs:60:9:60:27 | ...::Variant2(...) | repo::test | crate::regular::MyEnum::Variant2 |
| regular.rs:61:9:61:31 | ...::Variant3 {...} | repo::test | crate::regular::MyEnum::Variant3 |
| regular.rs:70:14:70:24 | is_alphanum | repo::test | ::is_alphanum |
| regular.rs:70:26:70:28 | chr | None | None |
8 changes: 8 additions & 0 deletions rust/ql/test/extractor-tests/canonical_path/regular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,11 @@ fn enum_match(e: MyEnum) {
MyEnum::Variant3 { .. } => {}
}
}

extern "C" {
pub fn is_alphanum(chr: u8) -> bool;
}

pub fn is_number_or_letter(chr: u8) -> bool {
unsafe { is_alphanum(chr) }
}
Loading








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/github/codeql/pull/19988/files

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy