Skip to content

Commit 32d9e2c

Browse files
committed
Clarify that {self} imports require a module parent
The `self` keyword in a use brace (e.g., `use m::{self}`) creates a binding for the parent entity. The note in `items.use.self.intro` says that `self` "means the current module of the parent segment", but there's no rule restricting what the parent can be. The parent path must resolve to a module, enumeration, or trait -- i.e., entities that act as modules for name resolution. Using `self` with other entities such as structs or unions is rejected by the compiler (as of rust-lang/rust#152996). Let's add a rule to make this restriction explicit.
1 parent d2715c0 commit 32d9e2c

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

src/items/use-declarations.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,26 @@ mod example {
203203
> [!NOTE]
204204
> `self` may also be used as the first segment of a path. The usage 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.module]
207+
When `self` is used within [brace syntax], the path preceding the brace group must resolve to a [module], [enumeration], or [trait].
208+
209+
```rust
210+
mod m {
211+
pub enum E { V1, V2 }
212+
pub trait Tr { fn f(&self); }
213+
}
214+
use m::{self as _}; // OK: Modules can be parents of `self`.
215+
use m::E::{self, V1}; // OK: Enums can be parents of `self`.
216+
use m::Tr::{self}; // OK: Traits can be parents of `self`.
217+
# fn main() {}
218+
```
219+
220+
```rust,compile_fail,E0432
221+
struct S {}
222+
use S::{self as _}; // ERROR: Structs cannot be parents of `self`.
223+
# fn main() {}
224+
```
225+
206226
r[items.use.self.namespace]
207227
`self` only creates a binding from the [type namespace] of the parent entity. For example, in the following, only the `foo` mod is imported:
208228

@@ -433,9 +453,11 @@ r[items.use.restrictions.variant]
433453
434454
[`$crate`]: paths.qualifiers.macro-crate
435455
[Attributes]: ../attributes.md
456+
[brace syntax]: items.use.multiple-syntax
436457
[Built-in types]: ../types.md
437458
[Derive macros]: macro.proc.derive
438459
[Enum variants]: enumerations.md
460+
[enumeration]: items.enum
439461
[`extern crate`]: extern-crates.md
440462
[`macro_rules`]: ../macros-by-example.md
441463
[`self`]: ../paths.md#self
@@ -444,10 +466,12 @@ r[items.use.restrictions.variant]
444466
[generic parameters]: generics.md
445467
[items]: ../items.md
446468
[local variables]: ../variables.md
469+
[module]: items.mod
447470
[name resolution ambiguities]: names.resolution.expansion.imports.ambiguity
448471
[namespace]: ../names/namespaces.md
449472
[namespaces]: ../names/namespaces.md
450473
[paths]: ../paths.md
451474
[tool attributes]: ../attributes.md#tool-attributes
475+
[trait]: items.traits
452476
[type alias]: type-aliases.md
453477
[type namespace]: ../names/namespaces.md

0 commit comments

Comments
 (0)