Skip to content

Commit 5f2babc

Browse files
ehusstraviscross
authored andcommitted
Rework macro_use to use the attribute template
1 parent 7f32d0a commit 5f2babc

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

src/macros-by-example.md

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -329,34 +329,62 @@ fn foo() {
329329
r[macro.decl.scope.macro_use]
330330
### The `macro_use` attribute
331331

332-
r[macro.decl.scope.macro_use.mod-decl]
333-
The *`macro_use` attribute* has two purposes. First, it can be used to make a module's macro scope not end when the module is closed, by applying it to a module:
332+
r[macro.decl.scope.macro_use.intro]
333+
The *`macro_use` [attribute][attributes]* has two purposes. It may be used on modules to extend the scope of macros defined within them, and it may be used on [`extern crate`][items.extern-crate] to import macros from another crate.
334334

335-
```rust
336-
#[macro_use]
337-
mod inner {
338-
macro_rules! m {
339-
() => {};
340-
}
341-
}
335+
r[macro.decl.scope.macro_use.syntax]
336+
When used on a module, the `macro_use` attribute uses the [MetaWord] syntax and thus does not take any inputs.
342337

343-
m!();
344-
```
338+
When used on an `extern crate`, it uses either the [MetaWord] or [MetaListIdents] syntax.
339+
340+
r[macro.decl.scope.macro_use.allowed-positions]
341+
The `macro_use` attribute may be applied to modules or `extern crate`.
342+
343+
> [!NOTE]
344+
> `rustc` currently warns in other positions, but this may be rejected in the future.
345+
346+
r[macro.decl.scope.macro_use.duplicates]
347+
Duplicate instances of `macro_use` that are in the [MetaWord] syntax have no effect if there is already a `macro_use` with the [MetaWord] syntax.
348+
349+
Multiple instances of `macro_use` that are in the [MetaListIdents] syntax may be specified. The union of all specified macros to import will be imported.
350+
351+
> [!NOTE]
352+
> `rustc` warns about duplicate [MetaWord] `macro_use` attributes.
353+
354+
r[macro.decl.scope.macro_use.mod-decl]
355+
When `macro_use` is used on a module, it causes the module's macro scope to not end when the module is closed.
356+
357+
> [!EXAMPLE]
358+
> ```rust
359+
> #[macro_use]
360+
> mod inner {
361+
> macro_rules! m {
362+
> () => {};
363+
> }
364+
> }
365+
>
366+
> m!();
367+
> ```
345368
346369
r[macro.decl.scope.macro_use.prelude]
347-
Second, it can be used to import macros from another crate, by attaching it to an `extern crate` declaration appearing in the crate's root module. Macros imported this way are imported into the [`macro_use` prelude], not textually, which means that they can be shadowed by any other name. While macros imported by `#[macro_use]` can be used before the import statement, in case of a conflict, the last macro imported wins. Optionally, a list of macros to import can be specified using the [MetaListIdents] syntax; this is not supported when `#[macro_use]` is applied to a module.
370+
When `macro_use` is used on an `extern crate` declaration in the crate root, it imports exported macros from that crate.
348371
349-
<!-- ignore: requires external crates -->
350-
```rust,ignore
351-
#[macro_use(lazy_static)] // Or #[macro_use] to import all macros.
352-
extern crate lazy_static;
372+
Macros imported this way are imported into the [`macro_use` prelude], not textually, which means that they can be shadowed by any other name. While macros imported by `macro_use` can be used before the import statement, in case of a conflict, the last macro imported wins.
353373
354-
lazy_static!{}
355-
// self::lazy_static!{} // Error: lazy_static is not defined in `self`
356-
```
374+
When using the [MetaWord] syntax, all exported macros are imported. When using the [MetaListIdents] syntax, only the specified macros are imported.
375+
376+
> [!EXAMPLE]
377+
> <!-- ignore: requires external crates -->
378+
> ```rust,ignore
379+
> #[macro_use(lazy_static)] // Or #[macro_use] to import all macros.
380+
> extern crate lazy_static;
381+
>
382+
> lazy_static!{}
383+
> // self::lazy_static!{} // Error: lazy_static is not defined in `self`
384+
> ```
357385
358386
r[macro.decl.scope.macro_use.export]
359-
Macros to be imported with `#[macro_use]` must be exported with `#[macro_export]`, which is described below.
387+
Macros to be imported with `macro_use` must be exported with `macro_export`.
360388
361389
r[macro.decl.scope.path]
362390
### Path-based scope

0 commit comments

Comments
 (0)