You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
377
377
378
378
> [!EXAMPLE]
379
379
> ```rust
380
380
> 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.
382
383
>
383
384
> modinner {
384
385
> super::m!();
@@ -394,7 +395,7 @@ The *`macro_export` [attribute][attributes]* marks a macro to be publicly export
394
395
> ```
395
396
396
397
r[macro.decl.scope.macro_export.syntax]
397
-
The `macro_export` attributeusesthe [MetaWord] syntax, orthe [MetaListIdents] syntaxwithasinglevalueof[`local_inner_macros`][macro.decl.scope.macro_export.local_inner_macros].
398
+
The `macro_export` attributeusesthe [MetaWord] and [MetaListIdents] syntaxes.Withthe [MetaListIdents] syntax, itacceptsasingle [`local_inner_macros`][macro.decl.scope.macro_export.local_inner_macros]value.
The `macro_export` attributemaybeappliedto `macro_rules` definitions.
@@ -409,20 +410,47 @@ Only the first use of `macro_export` on a macro has effect.
409
410
> `rustc` lints against any use following the first.
410
411
411
412
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 macroin 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
+
> # fnmain() {}
425
+
> ```
426
+
>
427
+
> With `macro_export`, path-basedresolutionworks.
428
+
>
429
+
> ```rust
430
+
> #[macro_export]
431
+
> macro_rules!m {
432
+
> () => {};
433
+
> }
434
+
> self::m!(); // OK
435
+
> crate::m!(); // OK
436
+
> # fnmain() {}
437
+
> ```
413
438
414
439
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` attributecausesamacrotobeexportedfromthecraterootsothatitcanbereferredtoinothercratesbypath.
416
441
417
442
> [!EXAMPLE]
418
-
> Given the following defined in a crate named `log`:
Adding `local_inner_macros` tothe `macro_export` attributealsocausesallsingle-segmentmacroinvocationsinthemacrodefinitiontohaveanimplicit `$crate::` prefix.Thisisintendedprimarilyasatooltomigratecodewrittenbefore [`$crate`] wasaddedtothelanguagetoworkwithRust2018'spath-basedimportsofmacros.Itsuse is discouraged in new code.
> Thisisintendedprimarilyasatooltomigratecodewrittenbefore [`$crate`] wasaddedtothelanguagetoworkwithRust2018'spath-basedimportsofmacros.Itsuse is discouraged in new code.
0 commit comments