Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 4 additions & 52 deletions src/items/use-declarations.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ They may create bindings for:
* [Built-in types]
* [Attributes]
* [Derive macros]
* [`macro_rules`]

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

r[items.use.ambiguities]
## Ambiguities

> [!NOTE]
> This section is incomplete.

r[items.use.ambiguities.intro]
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.

r[items.use.ambiguities.glob]
Glob imports are allowed to import conflicting names in the same namespace as long as the name is not used.
For example:

```rust
mod foo {
pub struct Qux;
}

mod bar {
pub struct Qux;
}

use foo::*;
use bar::*; //~ OK, no name conflict.

fn main() {
// This would be an error, due to the ambiguity.
//let x = Qux;
}
```

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:

```rust
mod foo {
pub struct Qux;
}

mod bar {
pub use super::foo::Qux;
}

// These both import the same `Qux`. The visibility of `Qux`
// is `pub` because that is the maximum visibility between
// these two `use` declarations.
pub use bar::*;
use foo::*;

fn main() {
let _: Qux = Qux;
}
```
TODO mention ambiguities and link to name-res. Moved to name-res because
ambiguities are fundamentally a product of the place of use, not the use
declaration.

[`extern crate`]: extern-crates.md
[`macro_rules`]: ../macros-by-example.md
Expand Down
54 changes: 54 additions & 0 deletions src/macros-by-example.md
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: come back and add a section mentioning the macro-related ambiguity errors and linking to the name-res chapter

Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,59 @@ fn foo() {
// m!(); // Error: m is not in scope.
```

r[macro.decl.scope.textual.shadow.path-based]
Textual scope name bindings for macros may shadow path-based scope bindings to
macros.

```rust
macro_rules! m {
() => {
println!("m");
};
}

#[macro_export]
macro_rules! m2 {
() => {
println!("m2");
};
}

use crate::m2 as m;

m!(); // prints "m\n"
```

TODO: note that the opposite is not true, link to relevant name-res ambiguity error section

r[macro.decl.scope.path-based]
### Path-based scope

r[macro.decl.scope.path-based.intro]
By default, a macro has no path-based scope. Macros can gain path-based scope in two ways:

* [Use declaration re-export]
* [`#[macro_export]`](macros-by-example.html#the-macro_export-attribute)

r[macro.decl.scope.path.reexport]
Macros can be re-exported to give them path-based scope from a module other than the crate root.

```rust
mac::m!(); // OK: Path-based lookup finds m in the mac module.

mod mac {
macro_rules! m {
() => {};
}
pub(crate) use m;
}
```

r[macro.decl.scope.path-based.visibility]
* macros have an implicit visibility of `pub(crate)`
* `#[macro_export]` changes the implicit visibility to `pub`
* macro definitions do not support direct visibility modifiers

<!-- template:attributes -->
r[macro.decl.scope.macro_use]
### The `macro_use` attribute
Expand Down Expand Up @@ -724,3 +777,4 @@ For more detail, see the [formal specification].
[Repetitions]: #repetitions
[token]: tokens.md
[`$crate`]: macro.decl.hygiene.crate
[Use declaration re-export]: items/use-declarations.html#use-visibility
Loading