Skip to content

Commit 1c79c5e

Browse files
authored
Merge pull request #19292 from github/aibaars/rust-shadow-prelude
Rust: allow shadowing of prelude items
2 parents eda1bc9 + b27ae98 commit 1c79c5e

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ abstract class ItemNode extends Locatable {
178178
Stages::PathResolutionStage::ref() and
179179
result = this.getASuccessorRec(name)
180180
or
181-
preludeEdge(this, name, result)
181+
preludeEdge(this, name, result) and not declares(this, _, name)
182182
or
183183
name = "super" and
184184
if this instanceof Module or this instanceof SourceFile

rust/ql/test/library-tests/path-resolution/my.rs

+18
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,21 @@ mod my4 {
1616
}
1717

1818
pub use my4::my5::f as nested_f; // $ item=I201
19+
20+
type Result<
21+
T, // T
22+
> = ::std::result::Result<
23+
T, // $ item=T
24+
String,
25+
>; // my::Result
26+
27+
fn int_div(
28+
x: i32, //
29+
y: i32,
30+
) -> Result<i32> // $ item=my::Result
31+
{
32+
if y == 0 {
33+
return Err("Div by zero".to_string());
34+
}
35+
Ok(x / y)
36+
}

rust/ql/test/library-tests/path-resolution/path-resolution.expected

+7
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,13 @@ resolvePath
322322
| my.rs:18:9:18:11 | my4 | my.rs:14:1:16:1 | mod my4 |
323323
| my.rs:18:9:18:16 | ...::my5 | my.rs:15:5:15:16 | mod my5 |
324324
| my.rs:18:9:18:19 | ...::f | my/my4/my5/mod.rs:1:1:3:1 | fn f |
325+
| my.rs:22:5:22:9 | std | file:///RUSTUP_HOME/toolchain/lib/rustlib/src/rust/library/std/src/lib.rs:0:0:0:0 | Crate([email protected]) |
326+
| my.rs:22:5:22:17 | ...::result | file://:0:0:0:0 | mod result |
327+
| my.rs:22:5:25:1 | ...::Result::<...> | file://:0:0:0:0 | enum Result |
328+
| my.rs:23:5:23:5 | T | my.rs:21:5:21:5 | T |
329+
| my.rs:30:6:30:16 | Result::<...> | my.rs:20:1:25:2 | type Result<...> |
330+
| my.rs:33:16:33:18 | Err | file://:0:0:0:0 | Err |
331+
| my.rs:35:5:35:6 | Ok | file://:0:0:0:0 | Ok |
325332
| my/nested.rs:9:13:9:13 | f | my/nested.rs:3:9:5:9 | fn f |
326333
| my/nested.rs:15:9:15:15 | nested2 | my/nested.rs:2:5:11:5 | mod nested2 |
327334
| my/nested.rs:15:9:15:18 | ...::f | my/nested.rs:3:9:5:9 | fn f |

rust/ql/test/library-tests/path-resolution/path-resolution.ql

+12-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ import TestUtils
55

66
query predicate mod(Module m) { toBeTested(m) }
77

8-
query predicate resolvePath(Path p, ItemNode i) {
8+
class ItemNodeLoc extends Locatable instanceof ItemNode {
9+
predicate hasLocationInfo(
10+
string filepath, int startline, int startcolumn, int endline, int endcolumn
11+
) {
12+
exists(string file |
13+
super.getLocation().hasLocationInfo(file, startline, startcolumn, endline, endcolumn) and
14+
filepath = file.regexpReplaceAll("^/.*/.rustup/toolchains/[^/]+/", "/RUSTUP_HOME/toolchain/")
15+
)
16+
}
17+
}
18+
19+
query predicate resolvePath(Path p, ItemNodeLoc i) {
920
toBeTested(p) and not p.isInMacroExpansion() and i = resolvePath(p)
1021
}

0 commit comments

Comments
 (0)