You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
76
76
77
-
```rust
77
+
```rust,no_run
78
78
mod m {
79
79
pub const C: () = ();
80
80
pub enum E { V }
@@ -105,8 +105,8 @@ let _ = m::E::C; // OK.
105
105
# }
106
106
# }
107
107
// Invalid type-relative imports that can't resolve at expansion-time:
108
-
use m::E::C; // ERROR: Unresolved import `m::E::C`.
109
108
use m::A::V; // ERROR: Unresolved import `m::A::V`.
109
+
use m::E::C; // ERROR: Unresolved import `m::E::C`.
110
110
```
111
111
112
112
r[names.resolution.expansion.imports.shadowing]
@@ -127,14 +127,14 @@ pub mod m2 {
127
127
128
128
// This introduces the name `ambig` in the outer scope.
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.
154
154
155
-
For example:
156
-
157
155
```rust,compile_fail,E0659
158
156
mod m1 {
159
157
pub struct Ambig;
@@ -163,17 +161,19 @@ mod m2 {
163
161
pub struct Ambig;
164
162
}
165
163
166
-
// OK: This brings conficting names into scope and namespace
164
+
// OK: This brings conficting names in the same namespace into scope
167
165
// but they have not been used yet.
168
166
use m1::*;
169
167
use m2::*;
170
168
171
-
fn ambiguous_use() {
169
+
const _: () = {
170
+
// The error happens when the name with the conflicting candidates
171
+
// is used.
172
172
let x = Ambig; // ERROR: `Ambig` is ambiguous.
173
173
}
174
174
```
175
175
176
-
```rust
176
+
```rust,no_run
177
177
# mod m1 {
178
178
# pub struct Ambig;
179
179
# }
@@ -184,39 +184,46 @@ fn ambiguous_use() {
184
184
#
185
185
# use m1::*;
186
186
# use m2::*; // OK: No name conflict.
187
-
fnambiguous_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.
189
190
struct Ambig;
190
-
letx=Ambig;
191
-
}
191
+
let x = Ambig; // OK.
192
+
};
192
193
```
193
194
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.
195
196
196
-
```rust
197
+
```rust,no_run
197
198
mod m1 {
198
199
pub struct Ambig;
199
200
}
200
201
201
202
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.
203
204
pub use super::m1::Ambig;
204
205
}
205
206
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.
210
207
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.
211
212
pub use super::m1::*;
212
213
use super::m2::*;
213
214
}
214
215
215
-
fnmain() {
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
> 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.
268
277
>
269
-
> ```rust
278
+
> ```rust,no_run
270
279
> mod glob {
271
-
> pubconstAMBIG:bool=true;
280
+
> pub const AMBIG: u8 = 1;
272
281
> }
273
282
>
274
283
> mod outer {
275
-
> pubconstAMBIG:bool=false;
284
+
> pub const AMBIG: u8 = 2;
276
285
> }
277
286
>
278
287
> use outer::AMBIG;
279
288
>
280
-
> pubfnf() {
289
+
> const C: () = {
281
290
> 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.
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].
289
298
290
299
```rust,compile_fail,E0659
291
300
// Textual macro candidate.
@@ -299,7 +308,7 @@ macro_rules! path_based {
299
308
}
300
309
301
310
pub fn f() {
302
-
// This re-export of the `path_based` macro definition
311
+
// This reexport of the `path_based` macro definition
303
312
// as `ambig` may not shadow the `ambig` macro definition
304
313
// which is resolved via textual macro scope.
305
314
use path_based as ambig;
@@ -329,22 +338,19 @@ The available scope kinds are visited in the following order. Each of these scop
329
338
*[Builtin attributes]
330
339
331
340
> [!NOTE]
332
-
>
333
341
> 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.
334
342
>
335
343
> For more info see [derive helper scope].
336
344
337
345
> [!NOTE]
338
-
>
339
346
> This visitation order may change in the future, such as interleaving the
340
347
> visitation of textual and path-based scope candidates based on their lexical
341
348
> scopes.
342
349
343
350
> [!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.
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.
434
440
435
441
```rust,compile_fail,E0659
436
442
# macro_rules! define_ambig {
@@ -445,7 +451,7 @@ define_ambig!();
445
451
ambig!(); // ERROR: `ambig` is ambiguous.
446
452
```
447
453
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.
0 commit comments