Skip to content

Commit e7f0a9c

Browse files
committed
Revise macro_export text
In addition to some editorial clean-ups, let's add an example demonstrating the difference between textual-only scope and path-based resolution, and let's move some verbiage about intended use into an admonition.
1 parent d040583 commit e7f0a9c

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

src/macros-by-example.md

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,13 @@ r[macro.decl.scope.macro_export]
373373
### The `macro_export` attribute
374374

375375
r[macro.decl.scope.macro_export.intro]
376-
The *`macro_export` [attribute][attributes]* marks a macro to be publicly exported from the crate, and makes it available in the root of the crate for path-based resolution.
376+
The *`macro_export` [attribute][attributes]* exports the macro from the crate and makes it available in the root of the crate for path-based resolution.
377377

378378
> [!EXAMPLE]
379379
> ```rust
380380
> self::m!();
381-
> m!(); // OK: Path-based lookup finds m in the current module.
381+
> // ^^^^ OK: Path-based lookup finds `m` in the current module.
382+
> m!(); // As above.
382383
>
383384
> mod inner {
384385
> super::m!();
@@ -394,7 +395,7 @@ The *`macro_export` [attribute][attributes]* marks a macro to be publicly export
394395
> ```
395396
396397
r[macro.decl.scope.macro_export.syntax]
397-
The `macro_export` attribute uses the [MetaWord] syntax, or the [MetaListIdents] syntax with a single value of [`local_inner_macros`][macro.decl.scope.macro_export.local_inner_macros].
398+
The `macro_export` attribute uses the [MetaWord] and [MetaListIdents] syntaxes. With the [MetaListIdents] syntax, it accepts a single [`local_inner_macros`][macro.decl.scope.macro_export.local_inner_macros] value.
398399
399400
r[macro.decl.scope.macro_export.allowed-positions]
400401
The `macro_export` attribute may be applied to `macro_rules` definitions.
@@ -409,20 +410,47 @@ Only the first use of `macro_export` on a macro has effect.
409410
> `rustc` lints against any use following the first.
410411
411412
r[macro.decl.scope.macro_export.path-based]
412-
By default, macros only have [textually-based scoping](#textual-scope). When the `macro_export` attribute is used, the macro is reexported in the crate root, and can be referred to using a path to the macro in the crate root.
413+
By default, macros only have [textual scope][macro.decl.scope.textual] and cannot be resolved by path. When the `macro_export` attribute is used, the macro is made available in the crate root and can be referred to by its path.
414+
415+
> [!EXAMPLE]
416+
> Without `macro_export`, macros only have textual scope, so path-based resolution of the macro fails.
417+
>
418+
> ```rust,compile_fail,E0433
419+
> macro_rules! m {
420+
> () => {};
421+
> }
422+
> self::m!(); // ERROR
423+
> crate::m!(); // ERROR
424+
> # fn main() {}
425+
> ```
426+
>
427+
> With `macro_export`, path-based resolution works.
428+
>
429+
> ```rust
430+
> #[macro_export]
431+
> macro_rules! m {
432+
> () => {};
433+
> }
434+
> self::m!(); // OK
435+
> crate::m!(); // OK
436+
> # fn main() {}
437+
> ```
413438
414439
r[macro.decl.scope.macro_export.export]
415-
The `macro_export` attribute causes a macro to be publicly exported from the crate root so that it can be referred to by other crates using a path.
440+
The `macro_export` attribute causes a macro to be exported from the crate root so that it can be referred to in other crates by path.
416441
417442
> [!EXAMPLE]
418-
> Given the following defined in a crate named `log`:
443+
> Given the following in a `log` crate:
444+
>
419445
> ```rust
420446
> #[macro_export]
421447
> macro_rules! warn {
422448
> ($message:expr) => { eprintln!("WARN: {}", $message) };
423449
> }
424450
> ```
425-
> Then you can refer to the macro via a path to the crate:
451+
>
452+
> From another crate, you can refer to the macro by path:
453+
>
426454
> <!-- ignore: requires external crates -->
427455
> ```rust,ignore
428456
> fn main() {
@@ -431,17 +459,20 @@ The `macro_export` attribute causes a macro to be publicly exported from the cra
431459
> ```
432460
433461
r[macro.decl.scope.macro_export.macro_use]
434-
`macro_export` also allows the use of [`macro_use`][macro.decl.scope.macro_use] on an `extern crate` to import the macro into the [`macro_use` prelude].
462+
`macro_export` allows the use of [`macro_use`][macro.decl.scope.macro_use] on an `extern crate` to import the macro into the [`macro_use` prelude].
435463
436464
> [!EXAMPLE]
437-
> Given the following defined in a crate named `log`:
465+
> Given the following in a `log` crate:
466+
>
438467
> ```rust
439468
> #[macro_export]
440469
> macro_rules! warn {
441470
> ($message:expr) => { eprintln!("WARN: {}", $message) };
442471
> }
443472
> ```
444-
> Using `macro_use` in a dependent crate means the macro can be used from the prelude:
473+
>
474+
> Using `macro_use` in a dependent crate allows you to use the macro from the prelude:
475+
>
445476
> <!-- ignore: requires external crates -->
446477
> ```rust,ignore
447478
> #[macro_use]
@@ -456,7 +487,10 @@ r[macro.decl.scope.macro_export.macro_use]
456487
> ```
457488
458489
r[macro.decl.scope.macro_export.local_inner_macros]
459-
Adding `local_inner_macros` to the `macro_export` attribute also causes all single-segment macro invocations in the macro definition to have an implicit `$crate::` prefix. This is intended primarily as a tool to migrate code written before [`$crate`] was added to the language to work with Rust 2018's path-based imports of macros. Its use is discouraged in new code.
490+
Adding `local_inner_macros` to the `macro_export` attribute causes all single-segment macro invocations in the macro definition to have an implicit `$crate::` prefix.
491+
492+
> [!NOTE]
493+
> This is intended primarily as a tool to migrate code written before [`$crate`] was added to the language to work with Rust 2018's path-based imports of macros. Its use is discouraged in new code.
460494
461495
> [!EXAMPLE]
462496
> ```rust

0 commit comments

Comments
 (0)