Skip to content

Commit d667f7d

Browse files
authored
Merge pull request #19732 from hvitved/rust/builtin-canonical-paths
Rust: Generate canonical paths for builtins
2 parents 85e9f5a + 383cc5c commit d667f7d

File tree

8 files changed

+24
-4
lines changed

8 files changed

+24
-4
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
private import rust
66
private import codeql.rust.elements.internal.generated.ParentChild
77
private import codeql.rust.internal.CachedStages
8-
private import codeql.rust.frameworks.stdlib.Bultins as Builtins
8+
private import codeql.rust.frameworks.stdlib.Builtins as Builtins
99

1010
private newtype TNamespace =
1111
TTypeNamespace() or
@@ -374,6 +374,9 @@ class CrateItemNode extends ItemNode instanceof Crate {
374374
not file = child.(SourceFileItemNode).getSuper() and
375375
file = super.getSourceFile()
376376
)
377+
or
378+
this.getName() = "core" and
379+
child instanceof Builtins::BuiltinType
377380
}
378381

379382
override string getCanonicalPath(Crate c) { c = this and result = Crate.super.getName() }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ private import Type as T
77
private import TypeMention
88
private import codeql.typeinference.internal.TypeInference
99
private import codeql.rust.frameworks.stdlib.Stdlib
10-
private import codeql.rust.frameworks.stdlib.Bultins as Builtins
10+
private import codeql.rust.frameworks.stdlib.Builtins as Builtins
1111

1212
class Type = T::Type;
1313

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
canonicalPath
22
| anonymous.rs:3:1:32:1 | fn canonicals | test::anonymous::canonicals |
33
| anonymous.rs:34:1:36:1 | fn other | test::anonymous::other |
4+
| {EXTERNAL LOCATION} | fn trim | <core::str>::trim |
45
| lib.rs:1:1:1:14 | mod anonymous | test::anonymous |
56
| lib.rs:2:1:2:12 | mod regular | test::regular |
67
| regular.rs:1:1:2:18 | struct Struct | test::regular::Struct |

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import rust
22
import TestUtils
3+
private import codeql.rust.internal.PathResolution
4+
private import codeql.rust.frameworks.stdlib.Builtins
35

46
query predicate canonicalPath(Addressable a, string path) {
5-
toBeTested(a) and
7+
(
8+
toBeTested(a)
9+
or
10+
// test that we also generate canonical paths for builtins
11+
a =
12+
any(ImplItemNode i |
13+
i.resolveSelfTy() instanceof Str and
14+
not i.(Impl).hasTrait()
15+
).getAnAssocItem() and
16+
a.(Function).getName().getText() = "trim"
17+
) and
618
path = a.getCanonicalPath(_)
719
}
820

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: canonical_paths.ql
2+
postprocess: utils/test/ExternalLocationPostProcessing.ql

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
canonicalPath
22
| anonymous.rs:6:1:35:1 | fn canonicals | test::anonymous::canonicals |
33
| anonymous.rs:37:1:39:1 | fn other | test::anonymous::other |
4+
| {EXTERNAL LOCATION} | fn trim | <core::str>::trim |
45
| lib.rs:1:1:1:14 | mod anonymous | test::anonymous |
56
| lib.rs:2:1:2:12 | mod regular | test::regular |
67
| regular.rs:4:1:5:18 | struct Struct | test::regular::Struct |
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
extractor-tests/canonical_path/canonical_paths.ql
1+
query: extractor-tests/canonical_path/canonical_paths.ql
2+
postprocess: utils/test/ExternalLocationPostProcessing.ql

0 commit comments

Comments
 (0)