Skip to content

Commit 581920f

Browse files
authored
Merge pull request rust-lang#2237 from rust-lang/TC/trailing-self-in-paths
Document trailing `self` in paths
2 parents a2904a0 + cf92e58 commit 581920f

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

src/items/use-declarations.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,21 @@ mod example {
203203
> [!NOTE]
204204
> `self` may also be used as the first segment of a path. The use of `self` as the first segment and inside a `use` brace is logically the same; it means the current module of the parent segment, or the current module if there is no parent segment. See [`self`] in the paths chapter for more information on the meaning of a leading `self`.
205205
206+
r[items.use.self.trailing]
207+
`self` may appear as the last segment of a `use` path, preceded by `::`. A path of the form `P::self` is equivalent to `P::{self}`, and `P::self as name` is equivalent to `P::{self as name}`.
208+
209+
```rust
210+
mod m {
211+
pub enum E { V1, V2 }
212+
}
213+
use m::self as _; // Equivalent to `use m::{self as _};`.
214+
use m::E::self; // Equivalent to `use m::E::{self};`.
215+
# fn main() {}
216+
```
217+
218+
> [!NOTE]
219+
> See [paths.qualifiers.mod-self.trailing] for restrictions on the preceding path.
220+
206221
r[items.use.self.module]
207222
When `self` is used within [brace syntax], the path preceding the brace group must resolve to a [module], [enumeration], or [trait].
208223

src/paths.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,30 @@ r[paths.qualifiers.mod-self.intro]
233233
`self` resolves the path relative to the current module.
234234

235235
r[paths.qualifiers.mod-self.restriction]
236-
`self` can only be used as the first segment, without a preceding `::`.
236+
`self` may only be used as the first segment of a path (without a preceding `::`) or as the last segment (preceded by `::`).
237+
238+
r[paths.qualifiers.mod-self.trailing]
239+
When `self` appears as the last segment of a path, it refers to the entity named by the preceding segment. The preceding path must resolve to a [module], [enumeration], or [trait].
240+
241+
```rust
242+
mod m {
243+
pub enum E { V1 }
244+
pub trait Tr {}
245+
pub(in crate::m::self) fn g() {} // OK: Modules can be parents of `self`.
246+
}
247+
type Ty = m::E::self; // OK: Enumerations can be parents of `self`.
248+
fn f<T: m::Tr::self>() {} // OK: Traits can be parents of `self`.
249+
# fn main() { let _: Ty = m::E::V1; }
250+
```
251+
252+
```rust,compile_fail,E0223
253+
struct S;
254+
type Ty = S::self; // ERROR: Structs cannot be parents of `self`.
255+
# fn main() {}
256+
```
257+
258+
> [!NOTE]
259+
> See [items.use.self] for additional rules about `self` in `use` declarations.
237260
238261
r[paths.qualifiers.self-pat]
239262
In a method body, a path which consists of a single `self` segment resolves to the method's self parameter.
@@ -482,6 +505,7 @@ mod without { // crate::without
482505
[macro transcribers]: macros-by-example.md
483506
[macros]: macros.md
484507
[mbe]: macros-by-example.md
508+
[module]: items/modules.md
485509
[patterns]: patterns.md
486510
[struct]: items/structs.md
487511
[trait implementations]: items/implementations.md#trait-implementations

0 commit comments

Comments
 (0)