Skip to content

Commit 6fc4e4e

Browse files
committed
Expand name resolution stub
1 parent e122eef commit 6fc4e4e

File tree

4 files changed

+416
-53
lines changed

4 files changed

+416
-53
lines changed

src/items/use-declarations.md

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

120121
r[items.use.path.disallowed]
121122
They cannot import [associated items], [generic parameters], [local variables], paths with [`Self`], or [tool attributes]. More restrictions are described below.
@@ -389,58 +390,9 @@ r[items.use.restrictions.variant]
389390
use TypeAlias::MyVariant; //~ ERROR
390391
```
391392

392-
r[items.use.ambiguities]
393-
## Ambiguities
394-
395-
> [!NOTE]
396-
> This section is incomplete.
397-
398-
r[items.use.ambiguities.intro]
399-
Some situations are an error when there is an ambiguity as to which name a `use` declaration refers. This happens when there are two name candidates that do not resolve to the same entity.
400-
401-
r[items.use.ambiguities.glob]
402-
Glob imports are allowed to import conflicting names in the same namespace as long as the name is not used.
403-
For example:
404-
405-
```rust
406-
mod foo {
407-
pub struct Qux;
408-
}
409-
410-
mod bar {
411-
pub struct Qux;
412-
}
413-
414-
use foo::*;
415-
use bar::*; //~ OK, no name conflict.
416-
417-
fn main() {
418-
// This would be an error, due to the ambiguity.
419-
//let x = Qux;
420-
}
421-
```
422-
423-
Multiple glob imports are allowed to import the same name, and that name is allowed to be used, if the imports are of the same item (following re-exports). The visibility of the name is the maximum visibility of the imports. For example:
424-
425-
```rust
426-
mod foo {
427-
pub struct Qux;
428-
}
429-
430-
mod bar {
431-
pub use super::foo::Qux;
432-
}
433-
434-
// These both import the same `Qux`. The visibility of `Qux`
435-
// is `pub` because that is the maximum visibility between
436-
// these two `use` declarations.
437-
pub use bar::*;
438-
use foo::*;
439-
440-
fn main() {
441-
let _: Qux = Qux;
442-
}
443-
```
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.
444396

445397
[`extern crate`]: extern-crates.md
446398
[`macro_rules`]: ../macros-by-example.md

src/macros-by-example.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,28 @@ 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
331+
332+
```rust
333+
macro_rules! m {
334+
() => {
335+
println!("m");
336+
};
337+
}
338+
339+
#[macro_export]
340+
macro_rules! m2 {
341+
() => {
342+
println!("m2");
343+
};
344+
}
345+
346+
use crate::m2 as m;
347+
348+
m!(); // prints "m\n"
349+
```
350+
329351
<!-- template:attributes -->
330352
r[macro.decl.scope.macro_use]
331353
### The `macro_use` attribute
@@ -480,6 +502,25 @@ By default, macros only have [textual scope][macro.decl.scope.textual] and canno
480502
> # fn main() {}
481503
> ```
482504
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+
483524
r[macro.decl.scope.macro_export.export]
484525
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.
485526

0 commit comments

Comments
 (0)