Skip to content

Restore E0603 suggestions for private imports in grouped use - #165642

Open
raushan728 wants to merge 6 commits into
rust-lang:mainfrom
raushan728:issues/157453
Open

raushan728 wants to merge 6 commits into
rust-lang:mainfrom
raushan728:issues/157453

Conversation

@raushan728

@raushan728 raushan728 commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #157453.

For a single-item group like use a::{B};, the whole use tree is replaced with the path so no braces are left. for a multi item group like use a::{B, C};, the failing item is removed from the group and a new use <path> is added above it. nested groups work the same way.

Spans come from the AST only, with no source-text scanning. The span formula is factored out of calc_unused_spans into nested_item_removal_span, with no behavior change. PrivacyError::single_nested is replaced by nested Option<NestedImportRemoval>, stored on ImportData so globs inside groups are covered too.

Braces are left as they are in the multi-item case (use a::{Two}; after extraction), to avoid span arithmetic. Removing them would need byte position arithmetic on the source. because it can cause ICEs with multibyte characters rustfmt will handle this.

No suggestion is emitted for renames, globs, macro-expanded spans, or multi-item groups with pub or attributes, since the replacement would drop them.

AI assistance: Claude (browser) for discussion, free models in the IDE for coding help. I reviewed and tested all changes myself

@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 Nov 8, 2026
@rustbot

rustbot commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator

r? @davidtwco

rustbot has assigned @davidtwco.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 79 candidates
  • Random selection from 21 candidates

@Kivooeo

Kivooeo commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Hi, can you please disclose LLM usage?

Because from my perspective this code is slightly over commented, there is so much comments and they are all so detailed which is not a usual thing

@raushan728

raushan728 commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi, can you please disclose LLM usage?

Yes, i used LLM. In the browser i used claude (sonnet) for my discussion. In my IDE i used free models for coding assistance. i don't have a fixed model there because the free model changed several times during the work

Because from my perspective this code is slightly over commented, there is so much comments and they are all so detailed which is not a usual thing

The PR is a bit large and has many test cases, so i added comments to explain the spans, the conditions and why some cases get no suggestion. If any of them are too detailed, i am happy to trim them

In my earlier attempt (#158445), reviewers asked for explanations of the spans and conditions

Also, the PR is split into small commits, so it may be easier to review commit by commit

@raushan728

Copy link
Copy Markdown
Contributor Author

r? @mu001999

cc @fee1-dead @petrochenkov

@rustbot rustbot assigned mu001999 and unassigned davidtwco Nov 8, 2026
@rust-log-analyzer

This comment has been minimized.

@Kivooeo

Kivooeo commented Nov 8, 2026

Copy link
Copy Markdown
Member

Have you found a dedicated LLM reviewer for this?

nested is stored on ImportData rather than ImportKind::Single so that
glob imports inside groups are also treated as nested.
When a private item is imported inside a group where it is the only
item of the root use-tree (e.g. `use a::{B};`, `use a::{b::{C}};`),
emit an in-place suggestion replacing the root use-tree with the
accessible path.

Single-item groups carrying renames (`use a::{B as C};`) or globs
(`use a::{m::*};`), as well as imports expanded from macros where the
root span cannot be used for suggestions, are skipped to prevent
dropping aliases, trailing glob wildcards, or proposing edits inside
macro definitions.
…in E0603

When a private item is imported inside a multi-item grouped import
statement (e.g. `use a::{B, C};` where `B` is private), emit a
multipart suggestion extracting `B` into its own standalone `use`
statement prepended before the root import and removing `B` from the
group.

Extraction is restricted to `use` items with inherited visibility and
empty attributes to avoid losing visibility or attributes, and
skipped for renames (`as Renamed`), globs (`::*`), and spans
originating from macro expansions where spans cannot be used for
suggestions.
@raushan728

Copy link
Copy Markdown
Contributor Author

No, I don't have a dedicated LLM reviewer. I do use LLMs as an assistant in all my PRs, including this one. I'm a student and I use them to learn the codebase and move faster.

@apiraino

apiraino commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Hi @raushan728, moderator here.

Please allow some time for a review. Understand that we don't need to move faster, we just need to find time for a review (see reminder from our bot). We're a community of contributors, not a soulless code-churning machine.

We appreciate your interest in the Rust project but your influx of pull requests is work for us; many mentions create background noise.

You've been already warned, please be respectful and allow us some time.

@mu001999

mu001999 commented Nov 8, 2026

Copy link
Copy Markdown
Member

How does this differ from #158445?

@raushan728

Copy link
Copy Markdown
Contributor Author

@apiraino Understood, I'm sorry for the noise. I use LLMs as an assistant, but this PR is not blindly generated.

@raushan728

Copy link
Copy Markdown
Contributor Author

How does this differ from #158445?

The goal is the same, but the implementation is different. I closed that PR because it was not correct

The old PR found the item to remove by reading the source text and counting byte positions. That was fragile it could break with multibyte characters. This PR does not read the source text at all. It takes the removal span directly from the AST, using the same formula that calc_unused_spans already uses (I moved it into nested_item_removal_span).

The old PR also copied a lot of the suggestion code and tried to remove the braces by hand. This PR reuses the existing suggestion code and leaves the braces as they are (use a::{Two};).

You can compare the test cases of both PRs. If you run the test cases of this PR on the old implementation, it generates wrong output for some of them. That is why I remade it instead of patching the old PR.

This branch has not been deployed

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

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

E0603: import directly/through re-export should attach good suggestions for grouped imports

7 participants