Skip to content

Conversation

@ehuss
Copy link
Contributor

@ehuss ehuss commented Sep 24, 2025

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.

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.
@ehuss
Copy link
Contributor Author

ehuss commented Sep 24, 2025

Opening as a draft because I'm wondering how intentional this is.

@traviscross
Copy link
Contributor

cc @yaahc

@yaahc
Copy link
Member

yaahc commented Oct 29, 2025

https://github.com/yaahc/rust/blob/adaa838976ff99a4f0661136322f64cb466b58a0/compiler/rustc_resolve/src/build_reduced_graph.rs#L630-L631 is the logic that controls this, it doesn't make a carveout for renaming imports but it doesn't mention them either so I'm not positive if that means it was intended to also disallow this case.

rust-lang/rust#45846 is the PR where this logic was introduced. I did a quick search of the discussion on this PR and the associated PR, tracking issue, and RFC (discussion and content) and honestly couldn't find any mention of the $crate syntax at all outside of the PR diff itself. If I had to guess I'd assume this was probably an oversight. @petrochenkov's comment rust-lang/rust#35612 (comment) seems to support this theory.

much better example: rust-lang/rust#146972 (comment)
in this one vadim explicitly mentions that renaming $crate should be allowed. Also looks like the contributor on this PR is quite actively working on improving the situation and consistency of this logic, I'll make sure any updates they make get properly integrated into the associated language in the reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants