Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imports_granularity = "Module" causes compile error when path contains self #4681

Closed
Tracked by #4991
Aloso opened this issue Jan 31, 2021 · 8 comments · Fixed by #5253
Closed
Tracked by #4991

imports_granularity = "Module" causes compile error when path contains self #4681

Aloso opened this issue Jan 31, 2021 · 8 comments · Fixed by #5253
Labels
a-imports `use` syntax bug Panic, non-idempotency, invalid code, etc. help wanted only-with-option requires a non-default option value to reproduce

Comments

@Aloso
Copy link

Aloso commented Jan 31, 2021

Describe the bug

If a path contains self, this keyword must be enclosed in braces:

use crate::lexer::{self}; // valid
use crate::lexer::self;   // invalid

rustfmt with imports_granularity = "Module" sometimes splits use statements so that invalid code like above is emitted.

To Reproduce

use crate::lexer::{self, tokens::TokenData};

rustfmt formats it like this:

use crate::lexer::self;
use crate::lexer::tokens::TokenData;

When formatting the file again, the ::self is removed.

Expected behavior

rustfmt should emit the following code without having to run it twice:

use crate::lexer;
use crate::lexer::tokens::TokenData;

Note that use crate::lexer; and use crate::lexer::{self}; are not actually equivalent: The latter only imports the lexer module, while the former looks in all namespaces, so it can import up to 3 items at once, e.g. a function, a module and a macro of the same name.

To make sure that the behaviour doesn't change, it needs to be formatted like so:

use crate::lexer::{self};
use crate::lexer::tokens::TokenData;

However, I would find this rather annoying, because of the way rust-analyzer handles auto-imports.

Meta

  • rustfmt version: rustfmt 1.4.34-nightly (ea268b9 2021-01-28)
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt: cargo fmt
@Aloso Aloso added the bug Panic, non-idempotency, invalid code, etc. label Jan 31, 2021
@calebcartwright
Copy link
Member

Thanks for the report. We're aware of the differences between the two, which is why you'll notice these self cases handled with Item and Crate variants, but something definitely got missed here!

@calebcartwright calebcartwright added only-with-option requires a non-default option value to reproduce a-imports `use` syntax help wanted labels Feb 2, 2021
@davidBar-On
Copy link
Contributor

I am willing to try to fix this, but from the description of the expected behavior it is not clear whether the output should be crate::lexer or crate::lexer::{self}.

Also, if this is a bug, why is the issue labeled only-with-option? If an option is needed, I am not sure how to write a test case when the option is not set, since the tests\target file will include use crate::lexer::self; which will be formatted as use crate::lexer; and the test will fail.

@calebcartwright
Copy link
Member

I am willing to try to fix this

Awesome, thank you!

but from the description of the expected behavior it is not clear whether the output should be crate::lexer or crate::lexer::{self}.

It has to be the latter, because as noted in the issue description these are not equivalent and changing what the developer wrote has the potential to cause compilation issues in the emitted formatted code (rust-lang/rust#60941)

Also, if this is a bug, why is the issue labeled only-with-option?

I've added a description to the label that hopefully clarifies, but it's used to indicate that the issue cannot occur with the default rustfmt behavior and requires the use of an overridden value for a configuration option.

If an option is needed, I am not sure how to write a test case when the option is not set, since the tests\target file will include use crate::lexer::self; which will be formatted as use crate::lexer; and the test will fail.

I'm not sure I understand this. All options have default values, but this particular issue requires a non-default value in order to reproduce. The files used for the system/idempotence will need to have the corresponding config option self to Module via the standard comments at the top of the files.

I'd expect the input file to include:

use crate::lexer::{self, tokens::TokenData};

and the target file to inculde:

use crate::lexer::{self};
use crate::lexer::tokens::TokenData;

@davidBar-On
Copy link
Contributor

If an option is needed, I am not sure how to write a test case when the option is not set, since the tests\target file will include use crate::lexer::self; which will be formatted as use crate::lexer; and the test will fail.

I'm not sure I understand this. ....

No problem - my misunderstanding. I thought that the meaning of the label only-with-option is that a new option should be added for whether use the modified formatting.

@tommilligan
Copy link
Contributor

Cross linking #4991: required to be closed before imports_granularity is stabilised

@Aloso
Copy link
Author

Aloso commented Mar 5, 2022

@davidBar-On is this issue fixed by #4716?

@tommilligan
Copy link
Contributor

@Aloso this issue is still present in master - I just opened a fix in #5253 (there is a test added which reproduces the failure).

The fix from #4716 appears to be present in the rc2 branch but not in master, I guess it got lost in a merge somewhere.

There are more details in the description of #5253

@calebcartwright
Copy link
Member

The fix from #4716 appears to be present in the rc2 branch but not in master, I guess it got lost in a merge somewhere.

Correct on the existence of the commit in different branches, but not quite on the cause, see #4801

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-imports `use` syntax bug Panic, non-idempotency, invalid code, etc. help wanted only-with-option requires a non-default option value to reproduce
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants