Skip to content

Commit 01e93ed

Browse files
committed
Clarify use $crate restriction
The previous wording for this restriction was pretty confusing to me. I don't remember what I was thinking when I wrote it, and I can't find any historical explanation either. `use` paths can use `$crate` as long as they have more than one segment (`use $crate::foo` is obviously OK). I have rewritten this to make it clear it is specifically about `use $crate`. One could say that restriction is already covered by the previous point that says `use crate;` requires an `as`, but for some reason `use $crate as foo` doesn't work either. So I have left this as a separate rule for now. cc rust-lang/rust#146972 (comment) for context.
1 parent 894d4d7 commit 01e93ed

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/items/use-declarations.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,18 @@ r[items.use.restrictions.duplicate-name]
375375
* As with any item definition, `use` imports cannot create duplicate bindings of the same name in the same namespace in a module or block.
376376

377377
r[items.use.restrictions.macro-crate]
378-
* `use` paths with `$crate` are not allowed in a [`macro_rules`] expansion.
378+
* `use $crate` is not allowed in a [`macro_rules`] expansion.
379+
380+
```rust,compile_fail
381+
macro_rules! example {
382+
() => {
383+
use $crate; // ERROR: `$crate` may not be imported
384+
use $crate as foo; // ERROR: `$crate` may not be imported
385+
};
386+
}
387+
388+
example!{}
389+
```
379390

380391
r[items.use.restrictions.variant]
381392
* `use` paths cannot refer to enum variants through a [type alias]. For example:

0 commit comments

Comments
 (0)