Skip to content

Commit cb67cdf

Browse files
committed
WIP
1 parent a8b8581 commit cb67cdf

File tree

2 files changed

+88
-96
lines changed

2 files changed

+88
-96
lines changed

src/items/use-declarations.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ mod clashing {
303303
}
304304
```
305305

306+
> [!NOTE]
307+
>
308+
> For areas where shadowing is not allowed, see [name resolution ambiguities].
309+
306310
r[items.use.glob.last-segment-only]
307311
`*` cannot be used as the first or intermediate segments.
308312

@@ -390,10 +394,6 @@ r[items.use.restrictions.variant]
390394
use TypeAlias::MyVariant; //~ ERROR
391395
```
392396

393-
TODO mention ambiguities and link to name-res. Moved to name-res because
394-
ambiguities are fundamentally a product of the place of use, not the use
395-
declaration.
396-
397397
[`extern crate`]: extern-crates.md
398398
[`macro_rules`]: ../macros-by-example.md
399399
[`self`]: ../paths.md#self
@@ -412,3 +412,4 @@ declaration.
412412
[tool attributes]: ../attributes.md#tool-attributes
413413
[type alias]: type-aliases.md
414414
[type namespace]: ../names/namespaces.md
415+
[name resolution ambiguities]: ../names/name-resolution.html#r-names.resolution.expansion.imports.errors.ambiguity

src/names/name-resolution.md

Lines changed: 83 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,13 @@ _NOT_ permitted, otherwise known as ambiguity errors:
132132
r[names.resolution.expansion.imports.errors.ambiguity]
133133
## Ambiguities
134134

135-
r[items.use.ambiguities.intro]
135+
r[names.resolution.expansion.imports.errors.ambiguity.intro]
136136
Some situations are an error when there is an ambiguity as to which name a
137137
`use` declaration refers. This happens when there are two name candidates that
138138
do not resolve to the same entity where neither import is
139139
[permitted] to shadow the other.
140140

141-
r[names.resolution.early.imports.errors.ambiguity.globvsglob]
142-
* it is an error to name an item through ambiguous use declarations
143-
* two globs imports which both have an item matching that name where the items are different
144-
* this is not an error even if is a third non glob binding resolution to an item with the same name
145-
* it is not an error to have two glob imports which include items which would be ambiguous so long as you do not name one of those items through the ambiguous glob imports
146-
* Should this live alongside use decls item page or in the name resolution page?
147-
148-
r[items.use.ambiguities.glob]
141+
r[names.resolution.expansion.imports.errors.ambiguity.glob]
149142
Glob imports are allowed to import conflicting names in the same namespace as
150143
long as the name is not used. Names may not be resolved through ambiguous glob
151144
statements. Conflicting names from ambiguous glob statements may still be
@@ -200,87 +193,6 @@ fn main() {
200193
}
201194
```
202195

203-
r[names.resolution.early.imports.errors.ambiguity.builtin-attr]
204-
It is an error to use a user defined attribute or derive macro with the same
205-
name as a builtin attribute (e.g. inline)
206-
* I think we may special case this one and allow certain kinds of ambiguities
207-
where the builtin-attr is shadowed by a user attribute (not sure if this
208-
actually exists or is just proposed, TODO investigate)
209-
210-
<!-- ignore: test doesn't support proc-macro -->
211-
```rust,ignore
212-
// myinline/src/lib.rs
213-
use proc_macro::TokenStream;
214-
215-
#[proc_macro_attribute]
216-
pub fn inline(_attr: TokenStream, item: TokenStream) -> TokenStream {
217-
item
218-
}
219-
```
220-
221-
<!-- ignore: requires external crates -->
222-
```rust,ignore
223-
// src/lib.rs
224-
use myinline::inline;
225-
use myinline::inline as myinline;
226-
227-
#[myinline::inline]
228-
pub fn foo() {}
229-
230-
#[crate::inline]
231-
pub fn bar() {}
232-
233-
#[myinline]
234-
pub fn baz() {}
235-
236-
#[inline] // ERROR `inline` is ambiguous
237-
pub fn qux() {}
238-
```
239-
240-
r[names.resolution.early.imports.errors.ambiguity.derivehelper]
241-
* derive helpers used before their associated derive may not shadow other attributes or other derive helpers that are otherwise in scope after their derive
242-
* TODO example? This ones harder to do concisely afaik
243-
244-
Helper attributes may not be used before the macro that introduces them.
245-
246-
* What happens if two macros introduce the same helper, will the second one not
247-
be able to see the attribute of the first anymore, assuming their order is
248-
"firstmacro" "helper" "secondmacro"?
249-
250-
> [!NOTE]
251-
> rustc currently allows derive helpers to be used before their attribute macro
252-
> introduces them into scope so long as they do not shadow any other attributes
253-
> or derive helpers that are otherwise correctly in scope. This behavior
254-
> deprecated and slated for removal.
255-
> <!-- ignore: requires external crates -->
256-
> ```rust,ignore
257-
> #[helper] // deprecated, hard error in the future
258-
> #[derive(WithHelperAttr)]
259-
> struct Struct {
260-
> field: (),
261-
> }
262-
> ```
263-
>
264-
> For more details, see [Rust issue #79202](https://github.com/rust-lang/rust/issues/79202).
265-
266-
267-
r[names.resolution.early.imports.errors.ambiguity.pathvstextualmacro]
268-
Path-based scope bindings for macros may not shadow textual scope bindings to macros.
269-
270-
```rust,compile_fail
271-
#[macro_export]
272-
macro_rules! m2 {
273-
() => {}
274-
}
275-
macro_rules! m {
276-
() => {}
277-
}
278-
pub fn foo() {
279-
m!(); // ERROR `m` is ambiguous
280-
use crate::m2 as m; // in scope for entire function body
281-
}
282-
```
283-
284196
r[names.resolution.early.imports.errors.ambiguity.globvsouter]
285197
it is an error to shadow an outer name binding with a glob import.
286198

@@ -352,7 +264,7 @@ pub fn foo() {
352264
> }
353265
> ```
354266
355-
r[macro.decl.scope.textual.ambiguity.moreexpandedvsouter]
267+
r[names.resolution.early.imports.errors.ambiguity.moreexpandedvsouter]
356268
* it is an error for name bindings from macro expansions to shadow name bindings from outside of those expansions
357269
358270
```rust,compile_fail
@@ -374,10 +286,89 @@ fn foo() {
374286
}
375287
```
376288
377-
378289
r[names.resolution.early.imports.errors.ambiguity.globvsexpanded]
379290
* Grey Area
380291

292+
r[names.resolution.early.imports.errors.ambiguity.pathvstextualmacro]
293+
Path-based scope bindings for macros may not shadow textual scope bindings to macros.
294+
295+
```rust,compile_fail
296+
#[macro_export]
297+
macro_rules! m2 {
298+
() => {}
299+
}
300+
macro_rules! m {
301+
() => {}
302+
}
303+
pub fn foo() {
304+
m!(); // ERROR `m` is ambiguous
305+
use crate::m2 as m; // in scope for entire function body
306+
}
307+
```
308+
309+
r[names.resolution.early.imports.errors.ambiguity.builtin-attr]
310+
It is an error to use a user defined attribute or derive macro with the same
311+
name as a builtin attribute (e.g. inline)
312+
* I think we may special case this one and allow certain kinds of ambiguities
313+
where the builtin-attr is shadowed by a user attribute (not sure if this
314+
actually exists or is just proposed, TODO investigate)
315+
316+
<!-- ignore: test doesn't support proc-macro -->
317+
```rust,ignore
318+
// myinline/src/lib.rs
319+
use proc_macro::TokenStream;
320+
321+
#[proc_macro_attribute]
322+
pub fn inline(_attr: TokenStream, item: TokenStream) -> TokenStream {
323+
item
324+
}
325+
```
326+
327+
<!-- ignore: requires external crates -->
328+
```rust,ignore
329+
// src/lib.rs
330+
use myinline::inline;
331+
use myinline::inline as myinline;
332+
333+
#[myinline::inline]
334+
pub fn foo() {}
335+
336+
#[crate::inline]
337+
pub fn bar() {}
338+
339+
#[myinline]
340+
pub fn baz() {}
341+
342+
#[inline] // ERROR `inline` is ambiguous
343+
pub fn qux() {}
344+
```
345+
346+
r[names.resolution.early.imports.errors.ambiguity.derivehelper]
347+
* derive helpers used before their associated derive may not shadow other attributes or other derive helpers that are otherwise in scope after their derive
348+
* TODO example? This ones harder to do concisely afaik
349+
350+
Helper attributes may not be used before the macro that introduces them.
351+
352+
* What happens if two macros introduce the same helper, will the second one not
353+
be able to see the attribute of the first anymore, assuming their order is
354+
"firstmacro" "helper" "secondmacro"?
355+
356+
> [!NOTE]
357+
> rustc currently allows derive helpers to be used before their attribute macro
358+
> introduces them into scope so long as they do not shadow any other attributes
359+
> or derive helpers that are otherwise correctly in scope. This behavior
360+
> deprecated and slated for removal.
361+
> <!-- ignore: requires external crates -->
362+
> ```rust,ignore
363+
> #[helper] // deprecated, hard error in the future
364+
> #[derive(WithHelperAttr)]
365+
> struct Struct {
366+
> field: (),
367+
> }
368+
> ```
369+
>
370+
> For more details, see [Rust issue #79202](https://github.com/rust-lang/rust/issues/79202).
371+
381372
r[names.resolution.expansion.macros]
382373
383374
* .visitation-order

0 commit comments

Comments
 (0)