Skip to content

Commit f5aa85a

Browse files
committed
Make final editorial revisions on name resolution
Many earlier passes of revisions have been squashed into the chapter itself. These edits are the final pass.
1 parent f6fa79f commit f5aa85a

File tree

1 file changed

+64
-56
lines changed

1 file changed

+64
-56
lines changed

src/names/name-resolution.md

Lines changed: 64 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ r[names.resolution.expansion.imports]
7474
r[names.resolution.expansion.imports.intro]
7575
All `use` declarations are fully resolved during this stage of resolution. [Type-relative paths] cannot be resolved at this stage and will produce an error.
7676
77-
```rust
77+
```rust,no_run
7878
mod m {
7979
pub const C: () = ();
8080
pub enum E { V }
@@ -105,8 +105,8 @@ let _ = m::E::C; // OK.
105105
# }
106106
# }
107107
// Invalid type-relative imports that can't resolve at expansion-time:
108-
use m::E::C; // ERROR: Unresolved import `m::E::C`.
109108
use m::A::V; // ERROR: Unresolved import `m::A::V`.
109+
use m::E::C; // ERROR: Unresolved import `m::E::C`.
110110
```
111111

112112
r[names.resolution.expansion.imports.shadowing]
@@ -127,14 +127,14 @@ pub mod m2 {
127127
128128
// This introduces the name `ambig` in the outer scope.
129129
use m1::ambig;
130-
fn f() {
130+
const _: () = {
131131
// This shadows `ambig` in the inner scope.
132132
use m2::ambig;
133133
// The inner candidate is selected here
134134
// as the resolution of `ambig`.
135135
use ambig::C;
136-
const { assert!(C == 2) };
137-
}
136+
assert!(C == 2);
137+
};
138138
```
139139

140140
r[names.resolution.expansion.imports.shadowing.shared-scope]
@@ -152,8 +152,6 @@ There are certain situations during expansion-time resolution where there are mu
152152
r[names.resolution.expansion.imports.ambiguity.glob-vs-glob]
153153
Names may not be resolved through ambiguous glob imports. Glob imports are allowed to import conflicting names in the same namespace as long as the name is not used. Names with conflicting candidates from ambiguous glob imports may still be shadowed by non-glob imports and used without producing an error. The errors occur at time of use, not time of import.
154154

155-
For example:
156-
157155
```rust,compile_fail,E0659
158156
mod m1 {
159157
pub struct Ambig;
@@ -163,17 +161,19 @@ mod m2 {
163161
pub struct Ambig;
164162
}
165163
166-
// OK: This brings conficting names into scope and namespace
164+
// OK: This brings conficting names in the same namespace into scope
167165
// but they have not been used yet.
168166
use m1::*;
169167
use m2::*;
170168
171-
fn ambiguous_use() {
169+
const _: () = {
170+
// The error happens when the name with the conflicting candidates
171+
// is used.
172172
let x = Ambig; // ERROR: `Ambig` is ambiguous.
173173
}
174174
```
175175

176-
```rust
176+
```rust,no_run
177177
# mod m1 {
178178
# pub struct Ambig;
179179
# }
@@ -184,39 +184,46 @@ fn ambiguous_use() {
184184
#
185185
# use m1::*;
186186
# use m2::*; // OK: No name conflict.
187-
fn ambiguous_shadow() {
188-
// This is permitted, since resolution is not through the ambiguous globs.
187+
const _: () = {
188+
// This is permitted, since resolution is not through the
189+
// ambiguous globs.
189190
struct Ambig;
190-
let x = Ambig;
191-
}
191+
let x = Ambig; // OK.
192+
};
192193
```
193194

194-
Multiple glob imports are allowed to import the same name, and that name is allowed to be used if the imports are of the same item (following re-exports). The visibility of the name is the maximum visibility of the imports.
195+
Multiple glob imports are allowed to import the same name, and that name is allowed to be used if the imports are of the same item (following reexports). The visibility of the name is the maximum visibility of the imports.
195196

196-
```rust
197+
```rust,no_run
197198
mod m1 {
198199
pub struct Ambig;
199200
}
200201
201202
mod m2 {
202-
// This re-exports the same `Ambig` item through a second module.
203+
// This reexports the same `Ambig` item from a second module.
203204
pub use super::m1::Ambig;
204205
}
205206
206-
// These both import the same `Ambig`.
207-
//
208-
// The visibility of `Ambig` is `pub` because that is the
209-
// maximum visibility between these two `use` declarations.
210207
mod m3 {
208+
// These both import the same `Ambig`.
209+
//
210+
// The visibility of `Ambig` is `pub` because that is the
211+
// maximum visibility between these two `use` declarations.
211212
pub use super::m1::*;
212213
use super::m2::*;
213214
}
214215
215-
fn main() {
216-
// `Ambig` can be used through the m3
217-
// globs and still has `pub` visibility.
218-
let _ = m3::Ambig;
216+
mod m4 {
217+
// `Ambig` can be used through the `m3` globs and still has
218+
// `pub` visibility.
219+
pub use crate::m3::Ambig;
219220
}
221+
222+
const _: () = {
223+
// Therefore, we can use it here.
224+
let _ = m4::Ambig; // OK.
225+
};
226+
# fn main() {}
220227
```
221228

222229
r[names.resolution.expansion.imports.ambiguity.glob-vs-outer]
@@ -243,7 +250,7 @@ const _: () = {
243250
```
244251

245252
```rust,compile_fail,E0659
246-
use m::f as ambig;
253+
// As above, but with macros.
247254
pub mod m {
248255
macro_rules! f {
249256
() => {};
@@ -257,6 +264,8 @@ pub mod glob {
257264
pub(crate) use f as ambig;
258265
}
259266
267+
use m::f as ambig;
268+
260269
const _: () = {
261270
use glob::*;
262271
ambig!(); // ERROR: `ambig` is ambiguous.
@@ -266,26 +275,26 @@ const _: () = {
266275
> [!NOTE]
267276
> These ambiguity errors are specific to expansion-time resolution. Having multiple candidates available for a given name during later stages of resolution is not considered an error. So long as none of the imports themselves are ambiguous, there will always be a single unambiguous closest resolution.
268277
>
269-
> ```rust
278+
> ```rust,no_run
270279
> mod glob {
271-
> pub const AMBIG: bool = true;
280+
> pub const AMBIG: u8 = 1;
272281
> }
273282
>
274283
> mod outer {
275-
> pub const AMBIG: bool = false;
284+
> pub const AMBIG: u8 = 2;
276285
> }
277286
>
278287
> use outer::AMBIG;
279288
>
280-
> pub fn f() {
289+
> const C: () = {
281290
> use glob::*;
282-
> assert!(AMBIG);
283-
> // ^--- This `AMBIG` is resolved during primary resolution.
284-
> }
291+
> assert!(AMBIG == 1);
292+
> // ^---- This `AMBIG` is resolved during primary resolution.
293+
> };
285294
> ```
286295
287296
r[names.resolution.expansion.imports.ambiguity.path-vs-textual-macro]
288-
Names may not be resolved through ambiguous macro re-exports. Macro re-exports are ambiguous when they would shadow a textual macro candidate for the same name in an [outer scope].
297+
Names may not be resolved through ambiguous macro reexports. Macro reexports are ambiguous when they would shadow a textual macro candidate for the same name in an [outer scope].
289298
290299
```rust,compile_fail,E0659
291300
// Textual macro candidate.
@@ -299,7 +308,7 @@ macro_rules! path_based {
299308
}
300309
301310
pub fn f() {
302-
// This re-export of the `path_based` macro definition
311+
// This reexport of the `path_based` macro definition
303312
// as `ambig` may not shadow the `ambig` macro definition
304313
// which is resolved via textual macro scope.
305314
use path_based as ambig;
@@ -329,22 +338,19 @@ The available scope kinds are visited in the following order. Each of these scop
329338
* [Builtin attributes]
330339

331340
> [!NOTE]
332-
>
333341
> The compiler will attempt to resolve derive helpers that are used before their associated macro introduces them into scope. This scope is visited after the scope for resolving derive helper candidates that are correctly in scope. This behavior is slated for removal.
334342
>
335343
> For more info see [derive helper scope].
336344
337345
> [!NOTE]
338-
>
339346
> This visitation order may change in the future, such as interleaving the
340347
> visitation of textual and path-based scope candidates based on their lexical
341348
> scopes.
342349
343350
> [!EDITION-2018]
344-
>
345-
> Starting in edition 2018 the `#[macro_use]` prelude is not visited when `#[no_implicit_prelude]` is present.
351+
> Starting in edition 2018 the `#[macro_use]` prelude is not visited when [`#[no_implicit_prelude]`][names.preludes.no_implicit_prelude] is present.
346352
347-
r[names.resolution.expansion.macros.derivehelpers]
353+
r[names.resolution.expansion.macros.derive-helpers]
348354
Derive helper scopes are not visited when resolving derive macros in the parent scope (starting scope).
349355

350356
r[names.resolution.expansion.macros.reserved-names]
@@ -370,15 +376,15 @@ macro_rules! ambig {
370376
() => {}
371377
}
372378
373-
// Introduce a second candidate definition for `ambig` inside of a macro
374-
// expansion.
379+
// Introduce a second candidate definition for `ambig` inside of a
380+
// macro expansion.
375381
define_ambig!();
376382
377383
// The definition of `ambig` from the second invocation
378384
// of `define_ambig` is the innermost canadidate.
379385
//
380-
// The definition of `ambig` from the first invocation of `define_ambig`
381-
// is the second candidate.
386+
// The definition of `ambig` from the first invocation of
387+
// `define_ambig` is the second candidate.
382388
//
383389
// The compiler checks that the first candidate is inside of a macro
384390
// expansion, that the second candidate is not from within the same
@@ -389,7 +395,7 @@ ambig!(); // ERROR: `ambig` is ambiguous.
389395

390396
The reverse is not considered ambiguous.
391397

392-
```rust
398+
```rust,no_run
393399
# macro_rules! define_ambig {
394400
# () => {
395401
# macro_rules! ambig {
@@ -409,7 +415,7 @@ ambig!();
409415

410416
Nor is it ambiguous if the invocation being resolved is within the innermost candidate's expansion.
411417

412-
```rust
418+
```rust,no_run
413419
macro_rules! ambig {
414420
() => {}
415421
}
@@ -430,7 +436,7 @@ macro_rules! define_and_invoke_ambig {
430436
define_and_invoke_ambig!();
431437
```
432438

433-
It doesn't matter if both definitions come from invocations of the same macro, the outermost candidate is still considered "less expanded" because it is not within the expansion containing the innermost candidate's definition.
439+
It doesn't matter if both definitions come from invocations of the same macro; the outermost candidate is still considered "less expanded" because it is not within the expansion containing the innermost candidate's definition.
434440

435441
```rust,compile_fail,E0659
436442
# macro_rules! define_ambig {
@@ -445,7 +451,7 @@ define_ambig!();
445451
ambig!(); // ERROR: `ambig` is ambiguous.
446452
```
447453

448-
This also applies to imports, so long as the innermost candidate for the invocation of name is from within a macro expansion.
454+
This also applies to imports so long as the innermost candidate for the name is from within a macro expansion.
449455

450456
```rust,compile_fail,E0659
451457
macro_rules! define_ambig {
@@ -468,16 +474,17 @@ const _: () = {
468474
};
469475
```
470476

471-
r[names.resolution.expansion.macros.ambiguity.builtin-attr]
472-
User defined attributes or derive macros may not shadow builtin non-macro attributes (e.g. inline).
477+
r[names.resolution.expansion.macros.ambiguity.built-in-attr]
478+
User-defined attributes or derive macros may not shadow built-in non-macro attributes (e.g. inline).
473479

474480
<!-- ignore: test doesn't support proc-macro -->
475481
```rust,ignore
476482
// with-helper/src/lib.rs
477483
# use proc_macro::TokenStream;
478484
#[proc_macro_derive(WithHelperAttr, attributes(non_exhaustive))]
479-
// ^------------- user attr candidate
480-
...
485+
// ^^^^^^^^^^^^^^
486+
// User-defined attribute candidate.
487+
// ...
481488
# pub fn derive_with_helper_attr(_item: TokenStream) -> TokenStream {
482489
# TokenStream::new()
483490
# }
@@ -492,16 +499,17 @@ struct S;
492499
```
493500

494501
> [!NOTE]
495-
> This applies regardless of the name the builtin attribute is a candidate for:
502+
> This applies regardless of the name the built-in attribute is a candidate for:
496503
>
497504
> <!-- ignore: test doesn't support proc-macro -->
498505
> ```rust,ignore
499506
> // with-helper/src/lib.rs
500507
> # use proc_macro::TokenStream;
501508
> #
502509
> #[proc_macro_derive(WithHelperAttr, attributes(helper))]
503-
> // ^----- user attr candidate
504-
> ...
510+
> // ^^^^^^
511+
> // User-defined attribute candidate.
512+
> // ...
505513
> # pub fn derive_with_helper_attr(_item: TokenStream) -> TokenStream {
506514
> # TokenStream::new()
507515
> # }
@@ -511,7 +519,7 @@ struct S;
511519
> ```rust,ignore
512520
> // src/lib.rs
513521
> use inline as helper;
514-
> // ^----- builtin attr candidate via re-export
522+
> // ^----- Built-in attribute candidate via reexport.
515523
>
516524
> #[derive(with_helper::WithHelperAttr)]
517525
> #[helper] // ERROR: `helper` is ambiguous.

0 commit comments

Comments
 (0)