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
Macros to be imported with `#[macro_use]` must be exported with
369
-
`#[macro_export]`, which is described below.
368
+
Macros to be imported with `macro_use` must be exported with
369
+
[`macro_export`][macro.decl.scope.macro_export].
370
370
371
-
r[macro.decl.scope.path]
372
-
### Path-based scope
371
+
<!-- template:attributes -->
372
+
r[macro.decl.scope.macro_export]
373
+
### The `macro_export` attribute
373
374
374
-
r[macro.decl.scope.path.intro]
375
-
By default, a macro has no path-based scope. However, if it has the
376
-
`#[macro_export]` attribute, then it is declared in the crate root scope and can
377
-
be referred to normally as such:
375
+
r[macro.decl.scope.macro_export.intro]
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.
378
377
379
-
```rust
380
-
self::m!();
381
-
m!(); // OK: Path-based lookup finds m in the current module.
378
+
> [!EXAMPLE]
379
+
> ```rust
380
+
> self::m!();
381
+
> // ^^^^ OK: Path-based lookup finds `m` in the current module.
382
+
> m!(); // As above.
383
+
>
384
+
> modinner {
385
+
> super::m!();
386
+
> crate::m!();
387
+
> }
388
+
>
389
+
> modmac {
390
+
> #[macro_export]
391
+
> macro_rules!m {
392
+
> () => {};
393
+
> }
394
+
> }
395
+
> ```
382
396
383
-
modinner {
384
-
super::m!();
385
-
crate::m!();
386
-
}
397
+
r[macro.decl.scope.macro_export.syntax]
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.
402
+
403
+
> [!NOTE]
404
+
> `rustc` ignoresusein other positions but lints against it.This may become an error in the future.
405
+
406
+
r[macro.decl.scope.macro_export.duplicates]
407
+
Only the first use of `macro_export` on a macro has effect.
408
+
409
+
> [!NOTE]
410
+
> `rustc` lints against any use following the first.
395
411
396
-
r[macro.decl.scope.path.export]
397
-
Macros labeled with `#[macro_export]` are always `pub` and can be referred to
398
-
by other crates, either by path or by `#[macro_use]` as described above.
412
+
r[macro.decl.scope.macro_export.path-based]
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
+
> ```
438
+
439
+
r[macro.decl.scope.macro_export.export]
440
+
The `macro_export` attributecausesamacrotobeexportedfromthecraterootsothatitcanbereferredtoinothercratesbypath.
> Thisisintendedprimarilyasatooltomigratecodewrittenbefore [`$crate`] wasaddedtothelanguagetoworkwithRust2018'spath-basedimportsofmacros.Itsuse is discouraged in new code.
> **Version differences**: Prior to Rust 1.30, `$crate` and
499
-
> `local_inner_macros` (below) were unsupported. They were added alongside
500
-
> path-based imports of macros (described above), to ensure that helper macros
501
-
> did not need to be manually imported by users of a macro-exporting crate.
502
-
> Crates written for earlier versions of Rust that use helper macros need to be
503
-
> modified to use `$crate` or `local_inner_macros` to work well with path-based
504
-
> imports.
505
-
506
-
r[macro.decl.hygiene.local_inner_macros]
507
-
When a macro is exported, the `#[macro_export]` attribute can have the
508
-
`local_inner_macros` keyword added to automatically prefix all contained macro
509
-
invocations with `$crate::`. This is intended primarily as a tool to migrate
510
-
code written before `$crate` was added to the language to work with Rust 2018's
511
-
path-based imports of macros. Its use is discouraged in new code.
512
-
513
-
```rust
514
-
#[macro_export(local_inner_macros)]
515
-
macro_rules!helped {
516
-
() => { helper!() } // Automatically converted to $crate::helper!().
517
-
}
518
-
519
-
#[macro_export]
520
-
macro_rules!helper {
521
-
() => { () }
522
-
}
523
-
```
606
+
> [!NOTE]
607
+
> Prior to Rust 1.30, `$crate` and [`local_inner_macros`][macro.decl.scope.macro_export.local_inner_macros] were unsupported. They were added alongside [path-based imports of macros][macro.decl.scope.macro_export], to ensure that helper macros did not need to be manually imported by users of a macro-exporting crate. Crates written for earlier versions of Rust that use helper macros need to be modified to use `$crate` or `local_inner_macros` to work well with path-based imports.
0 commit comments