Skip to content

Commit 52ce896

Browse files
authored
Merge pull request #1886 from ehuss/macro_use
Update `macro_use` to use the attribute template
2 parents 182f2c6 + 4647e87 commit 52ce896

File tree

1 file changed

+70
-30
lines changed

1 file changed

+70
-30
lines changed

src/macros-by-example.md

Lines changed: 70 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -326,47 +326,86 @@ fn foo() {
326326
// m!(); // Error: m is not in scope.
327327
```
328328

329+
<!-- template:attributes -->
329330
r[macro.decl.scope.macro_use]
330331
### The `macro_use` attribute
331332

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
334-
module's macro scope not end when the module is closed, by applying it to a
335-
module:
333+
r[macro.decl.scope.macro_use.intro]
334+
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 into the [`macro_use` prelude].
336335

337-
```rust
338-
#[macro_use]
339-
mod inner {
340-
macro_rules! m {
341-
() => {};
342-
}
343-
}
336+
> [!EXAMPLE]
337+
> ```rust
338+
> #[macro_use]
339+
> mod inner {
340+
> macro_rules! m {
341+
> () => {};
342+
> }
343+
> }
344+
> m!();
345+
> ```
346+
>
347+
> ```rust,ignore
348+
> #[macro_use]
349+
> extern crate log;
350+
> ```
344351
345-
m!();
346-
```
352+
r[macro.decl.scope.macro_use.syntax]
353+
When used on modules, the `macro_use` attribute uses the [MetaWord] syntax.
354+
355+
When used on `extern crate`, it uses the [MetaWord] and [MetaListIdents] syntaxes. For more on how these syntaxes may be used, see [macro.decl.scope.macro_use.prelude].
356+
357+
r[macro.decl.scope.macro_use.allowed-positions]
358+
The `macro_use` attribute may be applied to modules or `extern crate`.
359+
360+
> [!NOTE]
361+
> `rustc` ignores use in other positions but lints against it. This may become an error in the future.
362+
363+
r[macro.decl.scope.macro_use.extern-crate-self]
364+
The `macro_use` attribute may not be used on [`extern crate self`].
365+
366+
r[macro.decl.scope.macro_use.duplicates]
367+
The `macro_use` attribute may be used any number of times on a form.
368+
369+
Multiple instances of `macro_use` in the [MetaListIdents] syntax may be specified. The union of all specified macros will be imported.
370+
371+
> [!NOTE]
372+
> On modules, `rustc` lints against any [MetaWord] `macro_use` attributes following the first.
373+
>
374+
> On `extern crate`, `rustc` lints against any `macro_use` attributes that have no effect due to not importing any macros not already imported by another `macro_use` attribute. If two or more [MetaListIdents] `macro_use` attributes import the same macro, the first is linted against. If any [MetaWord] `macro_use` attributes are present, all [MetaListIdents] `macro_use` attributes are linted against. If two or more [MetaWord] `macro_use` attributes are present, the ones following the first are linted against.
375+
376+
r[macro.decl.scope.macro_use.mod-decl]
377+
When `macro_use` is used on a module, the module's macro scope extends beyond the module's lexical scope.
378+
379+
> [!EXAMPLE]
380+
> ```rust
381+
> #[macro_use]
382+
> mod inner {
383+
> macro_rules! m {
384+
> () => {};
385+
> }
386+
> }
387+
> m!(); // OK
388+
> ```
347389
348390
r[macro.decl.scope.macro_use.prelude]
349-
Second, it can be used to import macros from another crate, by attaching it to
350-
an `extern crate` declaration appearing in the crate's root module. Macros
351-
imported this way are imported into the [`macro_use` prelude], not textually,
352-
which means that they can be shadowed by any other name. While macros imported
353-
by `#[macro_use]` can be used before the import statement, in case of a
354-
conflict, the last macro imported wins. Optionally, a list of macros to import
355-
can be specified using the [MetaListIdents] syntax; this is not supported
356-
when `#[macro_use]` is applied to a module.
391+
Specifying `macro_use` on an `extern crate` declaration in the crate root imports exported macros from that crate.
357392
358-
<!-- ignore: requires external crates -->
359-
```rust,ignore
360-
#[macro_use(lazy_static)] // Or #[macro_use] to import all macros.
361-
extern crate lazy_static;
393+
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.
362394
363-
lazy_static!{}
364-
// self::lazy_static!{} // Error: lazy_static is not defined in `self`
365-
```
395+
When using the [MetaWord] syntax, all exported macros are imported. When using the [MetaListIdents] syntax, only the specified macros are imported.
396+
397+
> [!EXAMPLE]
398+
> <!-- ignore: requires external crates -->
399+
> ```rust,ignore
400+
> #[macro_use(lazy_static)] // Or `#[macro_use]` to import all macros.
401+
> extern crate lazy_static;
402+
>
403+
> lazy_static!{}
404+
> // self::lazy_static!{} // ERROR: lazy_static is not defined in `self`.
405+
> ```
366406
367407
r[macro.decl.scope.macro_use.export]
368-
Macros to be imported with `macro_use` must be exported with
369-
[`macro_export`][macro.decl.scope.macro_export].
408+
Macros to be imported with `macro_use` must be exported with [`macro_export`][macro.decl.scope.macro_export].
370409
371410
<!-- template:attributes -->
372411
r[macro.decl.scope.macro_export]
@@ -669,6 +708,7 @@ expansions, taking separators into account. This means:
669708

670709
For more detail, see the [formal specification].
671710

711+
[`extern crate self`]: items.extern-crate.self
672712
[`macro_use` prelude]: names/preludes.md#macro_use-prelude
673713
[block labels]: expressions/loop-expr.md#labelled-block-expressions
674714
[delimiters]: tokens.md#delimiters

0 commit comments

Comments
 (0)