Skip to content

Commit 722903f

Browse files
committed
rewrite bullet points as prose
Co-authored-by: Vadim Petrochenkov <[email protected]> Update src/macros-by-example.md Co-authored-by: Eric Huss <[email protected]> Update src/names/name-resolution.md Co-authored-by: Tshepang Mbambo <[email protected]> Update src/names/name-resolution.md Co-authored-by: Eric Huss <[email protected]> Apply suggestions from code review Co-authored-by: Eric Huss <[email protected]> Update src/names/name-resolution.md Co-authored-by: Eric Huss <[email protected]> Update src/names/name-resolution.md Co-authored-by: Eric Huss <[email protected]> Update src/names/name-resolution.md Co-authored-by: Eric Huss <[email protected]> split expansion intro into multiple sections add backticks around use for `use` declarations
1 parent 3d7f6c5 commit 722903f

File tree

6 files changed

+339
-309
lines changed

6 files changed

+339
-309
lines changed

src/items/use-declarations.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ They may create bindings for:
116116
* [Built-in types]
117117
* [Attributes]
118118
* [Derive macros]
119-
* [Macros by example]
119+
* [`macro_rules`]
120120

121121
r[items.use.path.disallowed]
122122
They cannot import [associated items], [generic parameters], [local variables], paths with [`Self`], or [tool attributes]. More restrictions are described below.
@@ -303,6 +303,10 @@ mod clashing {
303303
}
304304
```
305305

306+
> [!NOTE]
307+
>
308+
> For areas where shadowing is not allowed, see [name resolution ambiguities].
309+
306310
r[items.use.glob.last-segment-only]
307311
`*` cannot be used as the first or intermediate segments.
308312

@@ -390,22 +394,19 @@ r[items.use.restrictions.variant]
390394
use TypeAlias::MyVariant; //~ ERROR
391395
```
392396

393-
TODO mention ambiguities and link to name-res. Moved to name-res because
394-
ambiguities are fundamentally a product of the place of use, not the use
395-
declaration.
396-
397-
[`extern crate`]: extern-crates.md
398-
[`macro_rules`]: ../macros-by-example.md
399-
[`self`]: ../paths.md#self
400-
[associated items]: associated-items.md
401397
[Attributes]: ../attributes.md
402398
[Built-in types]: ../types.md
403399
[Derive macros]: macro.proc.derive
404400
[Enum variants]: enumerations.md
401+
[`extern crate`]: extern-crates.md
402+
[`macro_rules`]: ../macros-by-example.md
403+
[`self`]: ../paths.md#self
404+
[associated items]: associated-items.md
405405
[extern prelude]: ../names/preludes.md#extern-prelude
406406
[generic parameters]: generics.md
407407
[items]: ../items.md
408408
[local variables]: ../variables.md
409+
[name resolution ambiguities]: names.resolution.expansion.imports.ambiguity
409410
[namespace]: ../names/namespaces.md
410411
[namespaces]: ../names/namespaces.md
411412
[paths]: ../paths.md

src/macros-by-example.md

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

329-
* textual scope name bindings for macros may shadow path-based scope bindings
330-
to macros
329+
r[macro.decl.scope.textual.shadow.path-based]
330+
Textual scope name bindings for macros shadow path-based scope bindings to macros.
331331

332332
```rust
333-
macro_rules! m {
333+
#[macro_export]
334+
macro_rules! m2 {
334335
() => {
335-
println!("m");
336+
println!("m2");
336337
};
337338
}
338339

339-
#[macro_export]
340-
macro_rules! m2 {
340+
m!(); // prints "m2\n"
341+
342+
macro_rules! m {
341343
() => {
342-
println!("m2");
344+
println!("m");
343345
};
344346
}
345347

@@ -348,6 +350,53 @@ use crate::m2 as m;
348350
m!(); // prints "m\n"
349351
```
350352

353+
> [!NOTE]
354+
>
355+
> For areas where shadowing is not allowed, see [name resolution ambiguities].
356+
357+
r[macro.decl.scope.path-based]
358+
### Path-based scope
359+
360+
r[macro.decl.scope.path-based.intro]
361+
By default, a macro has no path-based scope. Macros can gain path-based scope in two ways:
362+
363+
* [Use declaration re-export]
364+
* [`macro_export`]
365+
366+
r[macro.decl.scope.path.reexport]
367+
Macros can be re-exported to give them path-based scope from a module other than the crate root.
368+
369+
```rust
370+
mac::m!(); // OK: Path-based lookup finds m in the mac module.
371+
372+
mod mac {
373+
macro_rules! m {
374+
() => {};
375+
}
376+
pub(crate) use m;
377+
}
378+
```
379+
380+
r[macro.decl.scope.path-based.visibility]
381+
Macros have an implicit visibility of `pub(crate)`. `#[macro_export]` changes the implicit visibility to `pub`.
382+
383+
```rust,compile_fail,E0364
384+
macro_rules! private_m {
385+
() => {};
386+
}
387+
388+
#[macro_export]
389+
macro_rules! pub_m {
390+
() => {};
391+
}
392+
393+
pub(crate) use private_m as private_macro; // OK
394+
pub use pub_m as pub_macro; // OK
395+
396+
pub use private_m; // ERROR: `private_m` is only public within
397+
// the crate and cannot be re-exported outside
398+
```
399+
351400
<!-- template:attributes -->
352401
r[macro.decl.scope.macro_use]
353402
### The `macro_use` attribute
@@ -502,25 +551,6 @@ By default, macros only have [textual scope][macro.decl.scope.textual] and canno
502551
> # fn main() {}
503552
> ```
504553
505-
r[macro.decl.scope.path.reexport]
506-
507-
* macros can be re-exported to give them path-based scope from a module other than the crate root.
508-
* there's some visibility stuff here that may already be mentioned
509-
elsewhere. I'm pretty sure that w/o a #[macro_export] the macro being
510-
re-exported is implicitly pub(crate) and with one it is implicitly pub.
511-
The later is mentioned below, don't remember where I saw the former.
512-
513-
```
514-
mac::m!(); // OK: Path-based lookup finds m in the mac module.
515-
516-
mod mac {
517-
macro_rules! m {
518-
() => {};
519-
}
520-
pub(crate) use m;
521-
}
522-
```
523-
524554
r[macro.decl.scope.macro_export.export]
525555
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.
526556
@@ -754,14 +784,17 @@ expansions, taking separators into account. This means:
754784

755785
For more detail, see the [formal specification].
756786

787+
[Hygiene]: #hygiene
788+
[Metavariables]: #metavariables
789+
[Repetitions]: #repetitions
790+
[Use declaration re-export]: items/use-declarations.md#use-visibility
791+
[`macro_export`]: #the-macro_export-attribute
792+
[`$crate`]: macro.decl.hygiene.crate
757793
[`extern crate self`]: items.extern-crate.self
758794
[`macro_use` prelude]: names/preludes.md#macro_use-prelude
759795
[block labels]: expr.loop.block-labels
760796
[delimiters]: tokens.md#delimiters
761797
[formal specification]: macro-ambiguity.md
762-
[Hygiene]: #hygiene
763798
[loop labels]: expressions/loop-expr.md#loop-labels
764-
[Metavariables]: #metavariables
765-
[Repetitions]: #repetitions
799+
[name resolution ambiguities]: names/name-resolution.md#r-names.resolution.expansion.imports.ambiguity
766800
[token]: tokens.md
767-
[`$crate`]: macro.decl.hygiene.crate

src/macros.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,25 @@ macro_rules! example {
106106
example!();
107107
```
108108

109+
r[macro.invocation.name-resolution]
110+
111+
Macros invocations can be resolved via two kinds of scopes:
112+
113+
* Textual Scope
114+
* [Textual scope `macro_rules`](macros-by-example.md#r-macro.decl.scope.textual)
115+
* Path-based scope
116+
* [Path-based scope `macro_rules`](macros-by-example.md#r-macro.decl.scope.path-based)
117+
* [Procedural macros]
118+
119+
[External blocks]: items/external-blocks.md
109120
[Macros by Example]: macros-by-example.md
110121
[Procedural Macros]: procedural-macros.md
122+
[`macro_rules`]: macros-by-example.md
111123
[associated items]: items/associated-items.md
112124
[delimiters]: tokens.md#delimiters
113125
[expressions]: expressions.md
114126
[items]: items.md
115-
[`macro_rules`]: macros-by-example.md
116127
[patterns]: patterns.md
117128
[statements]: statements.md
118129
[types]: types.md
119130
[visibility qualifiers]: visibility-and-privacy.md
120-
[External blocks]: items/external-blocks.md

0 commit comments

Comments
 (0)