Skip to content

Commit aef357c

Browse files
authored
Merge pull request #19988 from github/aibaars/extern-blocks
Rust: path resolution: handle items in `extern` blocks
2 parents 8d16d02 + 7721d14 commit aef357c

File tree

3 files changed

+18
-45
lines changed

3 files changed

+18
-45
lines changed

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

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,6 @@ abstract class ItemNode extends Locatable {
147147
)
148148
)
149149
or
150-
// items made available through macro calls are available to nodes that contain the macro call
151-
exists(MacroCallItemNode call |
152-
call = this.getASuccessorRec(_) and
153-
result = call.(ItemNode).getASuccessorRec(name)
154-
)
155-
or
156150
// a trait has access to the associated items of its supertraits
157151
this =
158152
any(TraitItemNode trait |
@@ -573,7 +567,7 @@ class ImplItemNode extends ImplOrTraitItemNode instanceof Impl {
573567

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

576-
override AssocItemNode getAnAssocItem() { result = super.getAssocItemList().getAnAssocItem() }
570+
override AssocItemNode getAnAssocItem() { result = this.getADescendant() }
577571

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

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

683-
private class MacroCallItemNode extends AssocItemNode instanceof MacroCall {
684-
override string getName() { result = "(macro call)" }
685-
686-
override predicate hasImplementation() { none() }
687-
688-
override Namespace getNamespace() { none() }
689-
690-
override TypeParam getTypeParam(int i) { none() }
691-
692-
override Visibility getVisibility() { none() }
693-
694-
override predicate providesCanonicalPathPrefixFor(Crate c, ItemNode child) {
695-
any(ItemNode parent).providesCanonicalPathPrefixFor(c, this) and
696-
child.getImmediateParent() = this
697-
}
698-
699-
override string getCanonicalPathPrefixFor(Crate c, ItemNode child) {
700-
result = this.getCanonicalPathPrefix(c) and
701-
this.providesCanonicalPathPrefixFor(c, child)
702-
}
703-
704-
override predicate hasCanonicalPath(Crate c) { none() }
705-
706-
override string getCanonicalPath(Crate c) { none() }
707-
}
708-
709677
private class ModuleItemNode extends ModuleLikeNode instanceof Module {
710678
override string getName() { result = Module.super.getName().getText() }
711679

@@ -726,11 +694,6 @@ private class ModuleItemNode extends ModuleLikeNode instanceof Module {
726694
)
727695
or
728696
this = child.getImmediateParent()
729-
or
730-
exists(ItemNode mid |
731-
this.providesCanonicalPathPrefixFor(c, mid) and
732-
mid.(MacroCallItemNode) = child.getImmediateParent()
733-
)
734697
)
735698
}
736699

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

800-
override AssocItemNode getAnAssocItem() { result = super.getAssocItemList().getAnAssocItem() }
763+
override AssocItemNode getAnAssocItem() { result = this.getADescendant() }
801764

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

@@ -993,7 +956,7 @@ class TypeParamItemNode extends TypeItemNode instanceof TypeParam {
993956

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

@@ -1186,11 +1149,6 @@ private predicate declares(ItemNode item, Namespace ns, string name) {
11861149
useTreeDeclares(child.(Use).getUseTree(), name) and
11871150
exists(ns) // `use foo::bar` can refer to both a value and a type
11881151
)
1189-
or
1190-
exists(MacroCallItemNode call |
1191-
declares(call, ns, name) and
1192-
call.getImmediateParent() = item
1193-
)
11941152
}
11951153

11961154
/** A path that does not access a local variable. */

rust/ql/test/extractor-tests/canonical_path/canonical_paths.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ canonicalPath
2525
| regular.rs:40:1:46:1 | fn enum_qualified_usage | test::regular::enum_qualified_usage |
2626
| regular.rs:48:1:55:1 | fn enum_unqualified_usage | test::regular::enum_unqualified_usage |
2727
| regular.rs:57:1:63:1 | fn enum_match | test::regular::enum_match |
28+
| regular.rs:66:5:66:40 | fn is_alphanum | test::regular::is_alphanum |
29+
| regular.rs:69:1:71:1 | fn is_number_or_letter | test::regular::is_number_or_letter |
2830
canonicalPaths
2931
| anonymous.rs:1:1:1:26 | use ...::Trait | None | None |
3032
| anonymous.rs:3:1:32:1 | fn canonicals | repo::test | crate::anonymous::canonicals |
@@ -65,6 +67,9 @@ canonicalPaths
6567
| regular.rs:48:1:55:1 | fn enum_unqualified_usage | repo::test | crate::regular::enum_unqualified_usage |
6668
| regular.rs:51:5:51:18 | use MyEnum::* | None | None |
6769
| regular.rs:57:1:63:1 | fn enum_match | repo::test | crate::regular::enum_match |
70+
| regular.rs:65:1:67:1 | ExternBlock | None | None |
71+
| regular.rs:66:5:66:40 | fn is_alphanum | repo::test | ::is_alphanum |
72+
| regular.rs:69:1:71:1 | fn is_number_or_letter | repo::test | crate::regular::is_number_or_letter |
6873
resolvedPaths
6974
| anonymous.rs:27:17:27:30 | OtherStruct {...} | None | None |
7075
| anonymous.rs:28:9:28:9 | s | None | None |
@@ -96,3 +101,5 @@ resolvedPaths
96101
| regular.rs:59:9:59:24 | ...::Variant1 | repo::test | crate::regular::MyEnum::Variant1 |
97102
| regular.rs:60:9:60:27 | ...::Variant2(...) | repo::test | crate::regular::MyEnum::Variant2 |
98103
| regular.rs:61:9:61:31 | ...::Variant3 {...} | repo::test | crate::regular::MyEnum::Variant3 |
104+
| regular.rs:70:14:70:24 | is_alphanum | repo::test | ::is_alphanum |
105+
| regular.rs:70:26:70:28 | chr | None | None |

rust/ql/test/extractor-tests/canonical_path/regular.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,11 @@ fn enum_match(e: MyEnum) {
6161
MyEnum::Variant3 { .. } => {}
6262
}
6363
}
64+
65+
extern "C" {
66+
pub fn is_alphanum(chr: u8) -> bool;
67+
}
68+
69+
pub fn is_number_or_letter(chr: u8) -> bool {
70+
unsafe { is_alphanum(chr) }
71+
}

0 commit comments

Comments
 (0)