Skip to content

Commit 63b9153

Browse files
authored
Merge pull request #22517 from github/rust/path-resolution-toolchain
Rust: Make path resolution tests work with Rust 1.96 and fix `m::{self}` when `m` is a trait
2 parents 4239fee + c9198ea commit 63b9153

5 files changed

Lines changed: 366 additions & 330 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Fix path resolution for `m::{self}` paths where `m` is a trait.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ abstract class ItemNode extends Locatable {
427427
if
428428
this instanceof Module or
429429
this instanceof Enum or
430-
this instanceof Struct or
430+
this instanceof Trait or
431431
this instanceof Crate
432432
then (
433433
kind.isBoth() and

rust/ql/test/library-tests/path-resolution/invalid/main.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,15 @@ struct A; // A1
44
struct A; // A2
55

66
fn f(x: A) {} // $ item=A2 (the latter occurence takes precedence)
7+
8+
/// Test `m::{self}` where `m` is a struct. Per the Rust specification `m` must
9+
/// resolve to a module, trait, or enum:
10+
/// https://doc.rust-lang.org/reference/items/use-declarations.html#r-items.use.self.module
11+
mod self_import_from_struct {
12+
struct MyStruct; // Struct
13+
14+
#[rustfmt::skip]
15+
use self::MyStruct::{ // $ item=Struct
16+
self // $ SPURIOUS: item=self_import_from_struct
17+
};
18+
}

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -685,29 +685,38 @@ mod m18 {
685685
}
686686
}
687687

688-
mod m21 {
689-
mod m22 {
688+
/// Test importing modules, traits, and enums with `{self}`.
689+
mod self_imports {
690+
mod definitions {
691+
pub mod my_module {
692+
pub fn f() {} // I107
693+
} // I104
694+
pub trait MyTrait {} // I105
690695
pub enum MyEnum {
691-
A, // I104
692-
} // I105
696+
A, // I108
697+
} // I106
698+
}
693699

694-
pub struct MyStruct; // I106
695-
} // I107
700+
mod imports {
701+
#[rustfmt::skip]
702+
use super::definitions::my_module::{ // $ item=I104
703+
self // $ item=I104
704+
};
696705

697-
mod m33 {
698706
#[rustfmt::skip]
699-
use super::m22::MyEnum::{ // $ item=I105
707+
use super::definitions::MyTrait::{ // $ item=I105
700708
self // $ item=I105
701709
};
702710

703711
#[rustfmt::skip]
704-
use super::m22::MyStruct::{ // $ item=I106
712+
use super::definitions::MyEnum::{ // $ item=I106
705713
self // $ item=I106
706714
};
707715

708-
fn f() {
709-
let _ = MyEnum::A; // $ item=I104
710-
let _ = MyStruct {}; // $ item=I106
716+
#[rustfmt::skip]
717+
fn f<T: MyTrait>() { // $ item=I105
718+
my_module::f(); // $ item=I107
719+
let _ = MyEnum::A; // $ item=I108
711720
}
712721
}
713722
}

0 commit comments

Comments
 (0)