Skip to content

Conversation

mu001999
Copy link
Contributor

@mu001999 mu001999 commented Sep 24, 2025

Fixes #146967

we have had UnnamedCrateRootImport for things like use crate::self, reusing it for $crate seems good enough

Emit error CrateImported when we meet use $crate::{self}

r? petrochenkov

@rustbot
Copy link
Collaborator

rustbot commented Sep 24, 2025

Failed to set assignee to [petrochenkov]: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 24, 2025
resolve_unnamed_crate_root_import =
crate root imports need to be explicitly named: `use crate as name;`
{$dollar ->
[true] crate root imports need to be explicitly named: `use $crate as name;`
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is a valid suggestion. Per this, use $crate as x; is not allowed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Though I suppose it is a separate question if that restriction makes sense if there is an as. Is there a reason to also prevent that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes! Updated, now will also emit $crate may not be imported for use $crate::{self} things

Copy link
Contributor

Choose a reason for hiding this comment

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

The whole pre-existing logic here is garbage (#119776 (comment)), and this PR adds more.
The only restriction that we really need here is for imports to always end up with a name (*) - #35612 (comment).

use super; // bad
use super as name; // good
use super::{self}; // bad
use super::{self as name}; // good
use self; // bad
use self as name; // good
use crate; // bad
use crate as name; // good
use crate::{self}; // bad
use crate::{self as name}; // good
use $crate; // bad
use $crate as name; // good
use $crate::{self}; // bad
use $crate::{self as name}; // good

// etc

Many of these examples do not work correctly atm for various reasons.

(*) The name can even be underscore _, but it will just always be reported as unused in this case.

Copy link
Contributor

Choose a reason for hiding this comment

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

More than a partial fixup, I'd rather want to see an exhaustive test listing the possible cases of imports using only path segment keywords (self, super, crate, $crate) in the input path with FIXMEs as a declaration of intent.

Copy link
Contributor

Choose a reason for hiding this comment

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

@mu001999
Could you add such a test?
Adding some partial fixup on top of that would also be fine, but ideally this stuff just needs to be rewritten.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for the case use $crate as name; // good in the above examples, @ehuss said in rust-lang/reference#2010 (comment):

but for some reason use $crate as foo doesn't work either.


And I'd like to rewrite the whole logic

ehuss added a commit to ehuss/reference that referenced this pull request 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.
@mu001999 mu001999 force-pushed the fix/use-dollar-crate branch from ce2578f to 49c425d Compare September 25, 2025 01:54
@mu001999 mu001999 changed the title Emit error UnnamedCrateRootImport also for DollarCrate Disable use $crate::{self} like use $crate Sep 25, 2025
@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

use $crate::{self}; should not be accepted
4 participants