Skip to content

Commit b17d041

Browse files
yaahcjackh726
authored andcommitted
Expand name resolution stub
1 parent 32e5312 commit b17d041

4 files changed

Lines changed: 481 additions & 53 deletions

File tree

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+
* [`macro_rules`]
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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,59 @@ fn foo() {
326326
// m!(); // Error: m is not in scope.
327327
```
328328

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.
332+
333+
```rust
334+
macro_rules! m {
335+
() => {
336+
println!("m");
337+
};
338+
}
339+
340+
#[macro_export]
341+
macro_rules! m2 {
342+
() => {
343+
println!("m2");
344+
};
345+
}
346+
347+
use crate::m2 as m;
348+
349+
m!(); // prints "m\n"
350+
```
351+
352+
TODO: note that the opposite is not true, link to relevant name-res ambiguity error section
353+
354+
r[macro.decl.scope.path-based]
355+
### Path-based scope
356+
357+
r[macro.decl.scope.path-based.intro]
358+
By default, a macro has no path-based scope. Macros can gain path-based scope in two ways:
359+
360+
* [Use declaration re-export]
361+
* [`#[macro_export]`](macros-by-example.html#the-macro_export-attribute)
362+
363+
r[macro.decl.scope.path.reexport]
364+
Macros can be re-exported to give them path-based scope from a module other than the crate root.
365+
366+
```rust
367+
mac::m!(); // OK: Path-based lookup finds m in the mac module.
368+
369+
mod mac {
370+
macro_rules! m {
371+
() => {};
372+
}
373+
pub(crate) use m;
374+
}
375+
```
376+
377+
r[macro.decl.scope.path-based.visibility]
378+
* macros have an implicit visibility of `pub(crate)`
379+
* `#[macro_export]` changes the implicit visibility to `pub`
380+
* macro definitions do not support direct visibility modifiers
381+
329382
<!-- template:attributes -->
330383
r[macro.decl.scope.macro_use]
331384
### The `macro_use` attribute
@@ -724,3 +777,4 @@ For more detail, see the [formal specification].
724777
[Repetitions]: #repetitions
725778
[token]: tokens.md
726779
[`$crate`]: macro.decl.hygiene.crate
780+
[Use declaration re-export]: items/use-declarations.html#use-visibility

0 commit comments

Comments
 (0)