Skip to content

Commit 0d30ef6

Browse files
committed
move macro related ambiguities to macros subsection
1 parent 1dcac03 commit 0d30ef6

File tree

2 files changed

+66
-39
lines changed

2 files changed

+66
-39
lines changed

src/names/name-resolution.md

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ All name resolution candidates selected during macro expansion are considered sp
6565
> // Expansion-time resolution finalizes resolutions by re-
6666
> // resolving all imports and macro invocations, sees the
6767
> // introduced ambiguity and reports it as an error.
68-
> m::f!(); // ERROR: `bar` is ambiguous.
68+
> m::f!(); // ERROR: `m` is ambiguous.
6969
> };
7070
> ```
7171
@@ -261,10 +261,9 @@ pub fn qux() {
261261
> ```
262262
263263
r[names.resolution.expansion.imports.ambiguity.path-vs-textual-macro]
264-
Path-based scope bindings for macros may not shadow textual scope bindings to macros. For bindings from [`use` declarations], this applies regardless of their [sub-namespace].
264+
Path-based scope bindings for macros may not shadow textual scope bindings to macros.
265265
266266
```rust,compile_fail,E0659
267-
#[macro_export]
268267
macro_rules! m2 {
269268
() => {}
270269
}
@@ -273,43 +272,10 @@ macro_rules! m {
273272
}
274273
pub fn foo() {
275274
m!(); // ERROR: `m` is ambiguous
276-
use crate::m2 as m; // In scope for entire function body.
275+
use m2 as m; // In scope for entire function body.
277276
}
278277
```
279278
280-
r[names.resolution.expansion.imports.ambiguity.builtin-attr]
281-
It is an error to use a user defined attribute or derive macro with the same name as a builtin attribute (e.g. inline).
282-
283-
<!-- ignore: test doesn't support proc-macro -->
284-
```rust,ignore
285-
// myinline/src/lib.rs
286-
use proc_macro::TokenStream;
287-
288-
#[proc_macro_attribute]
289-
pub fn inline(_attr: TokenStream, item: TokenStream) -> TokenStream {
290-
item
291-
}
292-
```
293-
294-
<!-- ignore: requires external crates -->
295-
```rust,ignore
296-
// src/lib.rs
297-
use myinline::inline;
298-
use myinline::inline as myinline;
299-
300-
#[myinline::inline]
301-
pub fn foo() {}
302-
303-
#[crate::inline]
304-
pub fn bar() {}
305-
306-
#[myinline]
307-
pub fn baz() {}
308-
309-
#[inline] // ERROR: `inline` is ambiguous
310-
pub fn qux() {}
311-
```
312-
313279
r[names.resolution.expansion.macros]
314280
### Macros
315281

@@ -328,7 +294,7 @@ The available scopes are visited in the following order.
328294

329295
> [!NOTE]
330296
>
331-
> The compiler will attempt to resolve derive helpers that are used before their associated macro introduces them into scope after resolving derive helper candidates that are correctly in scope. This behavior is slated for removal.
297+
> 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.
332298
>
333299
> For more info see [derive helper scope].
334300
@@ -367,6 +333,57 @@ fn foo() {
367333
}
368334
```
369335

336+
r[names.resolution.expansion.macros.ambiguity.builtin-attr]
337+
User defined attributes or derive macros may not shadow builtin non-macro attributes (e.g. inline).
338+
339+
<!-- ignore: test doesn't support proc-macro -->
340+
```rust,ignore
341+
// with-helper/src/lib.rs
342+
# use proc_macro::TokenStream;
343+
#
344+
#[proc_macro_derive(WithHelperAttr, attributes(non_exhaustive))]
345+
// ^------------- user attr candidate
346+
...
347+
# pub fn derive_with_helper_attr(_item: TokenStream) -> TokenStream {
348+
# TokenStream::new()
349+
# }
350+
```
351+
352+
<!-- ignore: requires external crates -->
353+
```rust,ignore
354+
// src/lib.rs
355+
#[derive(with_helper::WithHelperAttr)]
356+
#[non_exhaustive] // ERROR: `non_exhaustive` is ambiguous
357+
struct S;
358+
```
359+
360+
> [!NOTE]
361+
> This applies regardless of the name the builtin attribute is a candidate for:
362+
>
363+
> <!-- ignore: test doesn't support proc-macro -->
364+
> ```rust,ignore
365+
> // with-helper/src/lib.rs
366+
> # use proc_macro::TokenStream;
367+
> #
368+
> #[proc_macro_derive(WithHelperAttr, attributes(helper))]
369+
> // ^----- user attr candidate
370+
> ...
371+
> # pub fn derive_with_helper_attr(_item: TokenStream) -> TokenStream {
372+
> # TokenStream::new()
373+
> # }
374+
> ```
375+
>
376+
> <!-- ignore: requires external crates -->
377+
> ```rust,ignore
378+
> // src/lib.rs
379+
> use inline as helper;
380+
> // ^----- builtin attr candidate via re-export
381+
>
382+
> #[derive(with_helper::WithHelperAttr)]
383+
> #[helper] // ERROR: `helper` is ambiguous
384+
> struct S;
385+
> ```
386+
370387
r[names.resolution.primary]
371388
## Primary name resolution
372389
> [!NOTE]

src/names/namespaces.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,18 @@ This prevents one style from shadowing another.
117117

118118
For example, the [`cfg` attribute] and the [`cfg` macro] are two different entities with the same name in the macro namespace, but they can still be used in their respective context.
119119

120+
<!-- ignore: requires external crates -->
120121
> [!NOTE]
121-
> For restrictions on shadowing macro sub-namespaces with [use declaration]s, see [name resolution ambiguity errors].
122+
> `use` imports still cannot create duplicate bindings of the same name in a module or block, regardless of sub-namespace.
123+
>
124+
> ```rust,ignore
125+
> #[macro_export]
126+
> macro_rules! mymac {
127+
> () => {};
128+
> }
129+
>
130+
> use myattr::mymac; // error[E0252]: the name `mymac` is defined multiple times
131+
> ```
122132
123133
[Associated const declarations]: ../items/associated-items.md#associated-constants
124134
[Associated function declarations]: ../items/associated-items.md#associated-functions-and-methods

0 commit comments

Comments
 (0)