Skip to content

Commit 0f40b3c

Browse files
authored
Merge pull request #20842 from hvitved/rust/path-resolution-extern-crate-visibility
Rust: Handle `pub extern crate` in path resolution
2 parents 4d4a677 + 3d49eff commit 0f40b3c

File tree

6 files changed

+15
-10
lines changed

6 files changed

+15
-10
lines changed

rust/ql/integration-tests/hello-workspace/exe/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ mod a_module;
77
fn main() {
88
my_macro2!(); // $ item=my_macro2
99
hello(); // $ item=HELLO
10+
lib::extern_crate_alias::a_module::hello(); // $ item=HELLO
1011
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| exe/src/main.rs:7:1:10:1 | fn main |
1+
| exe/src/main.rs:7:1:11:1 | fn main |
22
| lib/src/a_module/mod.rs:1:1:4:1 | fn hello |

rust/ql/integration-tests/hello-workspace/lib/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ mod macros {
1515
}
1616

1717
pub mod a_module;
18+
19+
pub extern crate self as extern_crate_alias;

rust/ql/integration-tests/hello-workspace/summary.cargo.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
| Inconsistencies - Path resolution | 0 |
1010
| Inconsistencies - SSA | 0 |
1111
| Inconsistencies - data flow | 0 |
12-
| Lines of code extracted | 21 |
13-
| Lines of user code extracted | 21 |
12+
| Lines of code extracted | 23 |
13+
| Lines of user code extracted | 23 |
1414
| Macro calls - resolved | 10 |
1515
| Macro calls - total | 10 |
1616
| Macro calls - unresolved | 0 |

rust/ql/integration-tests/hello-workspace/summary.rust-project.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
| Inconsistencies - Path resolution | 0 |
1010
| Inconsistencies - SSA | 0 |
1111
| Inconsistencies - data flow | 0 |
12-
| Lines of code extracted | 21 |
13-
| Lines of user code extracted | 21 |
12+
| Lines of code extracted | 23 |
13+
| Lines of user code extracted | 23 |
1414
| Macro calls - resolved | 10 |
1515
| Macro calls - total | 10 |
1616
| Macro calls - unresolved | 0 |

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,7 @@ abstract class ItemNode extends Locatable {
259259
kind.isInternal() and
260260
useOpt.isNone()
261261
or
262-
externCrateEdge(this, name, result) and
263-
kind.isInternal() and
262+
externCrateEdge(this, name, kind, result) and
264263
useOpt.isNone()
265264
or
266265
macroExportEdge(this, name, result) and
@@ -276,7 +275,7 @@ abstract class ItemNode extends Locatable {
276275
result = use_.getASuccessor(name, kind, _)
277276
)
278277
or
279-
exists(ExternCrateItemNode ec | result = ec.(ItemNode).getASuccessor(name, kind, useOpt) |
278+
exists(ExternCrateItemNode ec | result = ec.getASuccessor(name, kind, useOpt) |
280279
ec = this.getASuccessor(_, _, _)
281280
or
282281
// if the extern crate appears in the crate root, then the crate name is also added
@@ -527,7 +526,7 @@ class ExternCrateItemNode extends ItemNode instanceof ExternCrate {
527526

528527
override Namespace getNamespace() { none() }
529528

530-
override Visibility getVisibility() { none() }
529+
override Visibility getVisibility() { result = ExternCrate.super.getVisibility() }
531530

532531
override Attr getAnAttr() { result = ExternCrate.super.getAnAttr() }
533532

@@ -2107,8 +2106,11 @@ private predicate useImportEdge(Use use, string name, ItemNode item, SuccessorKi
21072106

21082107
/** Holds if `ec` imports `crate` as `name`. */
21092108
pragma[nomagic]
2110-
private predicate externCrateEdge(ExternCrateItemNode ec, string name, CrateItemNode crate) {
2109+
private predicate externCrateEdge(
2110+
ExternCrateItemNode ec, string name, SuccessorKind kind, CrateItemNode crate
2111+
) {
21112112
name = ec.getName() and
2113+
(if ec.isPublic() then kind.isBoth() else kind.isInternal()) and
21122114
exists(SourceFile f, string s |
21132115
ec.getFile() = f.getFile() and
21142116
s = ec.(ExternCrate).getIdentifier().getText()

0 commit comments

Comments
 (0)