Skip to content

Commit d4eb51d

Browse files
committed
rewrite bullet points as prose
1 parent 887077f commit d4eb51d

File tree

5 files changed

+289
-230
lines changed

5 files changed

+289
-230
lines changed

src/items/use-declarations.md

Lines changed: 6 additions & 5 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,10 +394,6 @@ 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-
397397
[`extern crate`]: extern-crates.md
398398
[`macro_rules`]: ../macros-by-example.md
399399
[`self`]: ../paths.md#self
@@ -412,3 +412,4 @@ declaration.
412412
[tool attributes]: ../attributes.md#tool-attributes
413413
[type alias]: type-aliases.md
414414
[type namespace]: ../names/namespaces.md
415+
[name resolution ambiguities]: ../names/name-resolution.html#r-names.resolution.expansion.imports.errors.ambiguity

src/macros-by-example.md

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -326,20 +326,23 @@ 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 may shadow path-based scope bindings to
331+
macros.
331332

332333
```rust
333-
macro_rules! m {
334+
#[macro_export]
335+
macro_rules! m2 {
334336
() => {
335-
println!("m");
337+
println!("m2");
336338
};
337339
}
338340

339-
#[macro_export]
340-
macro_rules! m2 {
341+
m!(); // prints "m2\n"
342+
343+
macro_rules! m {
341344
() => {
342-
println!("m2");
345+
println!("m");
343346
};
344347
}
345348

@@ -348,6 +351,54 @@ use crate::m2 as m;
348351
m!(); // prints "m\n"
349352
```
350353

354+
> [!NOTE]
355+
>
356+
> For areas where shadowing is not allowed, see [name resolution ambiguities].
357+
358+
r[macro.decl.scope.path-based]
359+
### Path-based scope
360+
361+
r[macro.decl.scope.path-based.intro]
362+
By default, a macro has no path-based scope. Macros can gain path-based scope in two ways:
363+
364+
* [Use declaration re-export]
365+
* [`#[macro_export]`](macros-by-example.html#the-macro_export-attribute)
366+
367+
r[macro.decl.scope.path.reexport]
368+
Macros can be re-exported to give them path-based scope from a module other than the crate root.
369+
370+
```rust
371+
mac::m!(); // OK: Path-based lookup finds m in the mac module.
372+
373+
mod mac {
374+
macro_rules! m {
375+
() => {};
376+
}
377+
pub(crate) use m;
378+
}
379+
```
380+
381+
r[macro.decl.scope.path-based.visibility]
382+
Macros have an implicit visibility of `pub(crate)`. `#[macro_export]` changes
383+
the implicit visibility to `pub`.
384+
385+
```rust,compile_fail,E0364
386+
macro_rules! private_m {
387+
() => {};
388+
}
389+
390+
#[macro_export]
391+
macro_rules! pub_m {
392+
() => {};
393+
}
394+
395+
pub(crate) use private_m as private_macro; // OK
396+
pub use pub_m as pub_macro; // OK
397+
398+
pub use private_m; // ERROR: `private_m` is only public within
399+
// the crate and cannot be re-exported outside
400+
```
401+
351402
<!-- template:attributes -->
352403
r[macro.decl.scope.macro_use]
353404
### The `macro_use` attribute
@@ -502,25 +553,6 @@ By default, macros only have [textual scope][macro.decl.scope.textual] and canno
502553
> # fn main() {}
503554
> ```
504555
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-
524556
r[macro.decl.scope.macro_export.export]
525557
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.
526558
@@ -765,3 +797,5 @@ For more detail, see the [formal specification].
765797
[Repetitions]: #repetitions
766798
[token]: tokens.md
767799
[`$crate`]: macro.decl.hygiene.crate
800+
[Use declaration re-export]: items/use-declarations.html#use-visibility
801+
[name resolution ambiguities]: ../names/name-resolution.html#r-names.resolution.expansion.imports.errors.ambiguity

src/macros.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ 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+
* Path-based scope
113+
* All procedural macros, attributes
114+
* macro by example
115+
* Textual Scope
116+
* macro by example
117+
109118
[Macros by Example]: macros-by-example.md
110119
[Procedural Macros]: procedural-macros.md
111120
[associated items]: items/associated-items.md

0 commit comments

Comments
 (0)