Skip to content

Commit db5285f

Browse files
ehusstraviscross
authored andcommitted
Rewrite local_inner_macros
This moves it to the macro_export section (since it is part of that macro). There are slight wording differences. One new addition is to clarify that it only works for single-segment paths.
1 parent 9a90151 commit db5285f

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/macros-by-example.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ r[macro.decl.scope.macro_export.syntax]
396396
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].
397397
398398
r[macro.decl.scope.macro_export.allowed-positions]
399-
The `macro_export` attribute can be applied to `macro_rules` definitions.
399+
The `macro_export` attribute may be applied to `macro_rules` definitions.
400400
401401
> [!NOTE]
402402
> `rustc` currently warns in other positions, but this may be rejected in the future.
@@ -454,6 +454,22 @@ r[macro.decl.scope.macro_export.macro_use]
454454
> }
455455
> ```
456456
457+
r[macro.decl.scope.macro_export.local_inner_macros]
458+
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.
459+
460+
> [!EXAMPLE]
461+
> ```rust
462+
> #[macro_export(local_inner_macros)]
463+
> macro_rules! helped {
464+
> () => { helper!() } // Automatically converted to $crate::helper!().
465+
> }
466+
>
467+
> #[macro_export]
468+
> macro_rules! helper {
469+
> () => { () }
470+
> }
471+
> ```
472+
457473
r[macro.decl.hygiene]
458474
## Hygiene
459475
@@ -553,22 +569,7 @@ fn foo() {}
553569
```
554570

555571
> [!NOTE]
556-
> Prior to Rust 1.30, `$crate` and `local_inner_macros` (below) were unsupported. They were added alongside path-based imports of macros (described above), 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.
557-
558-
r[macro.decl.hygiene.local_inner_macros]
559-
When a macro is exported, the `#[macro_export]` attribute can have the `local_inner_macros` keyword added to automatically prefix all contained macro invocations with `$crate::`. 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.
560-
561-
```rust
562-
#[macro_export(local_inner_macros)]
563-
macro_rules! helped {
564-
() => { helper!() } // Automatically converted to $crate::helper!().
565-
}
566-
567-
#[macro_export]
568-
macro_rules! helper {
569-
() => { () }
570-
}
571-
```
572+
> 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.
572573
573574
r[macro.decl.follow-set]
574575
## Follow-set ambiguity restrictions

0 commit comments

Comments
 (0)