Skip to content

Commit e7c0021

Browse files
authored
Merge pull request #2221 from rust-lang/TC/use-self-module-restriction
Clarify that `{self}` imports require a module parent
2 parents 8d1af73 + d5687ff commit e7c0021

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

src/items/use-declarations.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ r[items.use.self]
182182
## `self` imports
183183

184184
r[items.use.self.intro]
185-
The keyword `self` may be used within [brace syntax](#brace-syntax) to create a binding of the parent entity under its own name.
185+
The keyword `self` may be used within [brace syntax] to create a binding of the parent entity under its own name.
186186

187187
```rust
188188
mod stuff {
@@ -203,6 +203,26 @@ 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.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)