Skip to content

Improve E0603 suggestions for private grouped imports - #158445

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

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

Conversation

@raushan728

@raushan728 raushan728 commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

After #156244 removed broken suggestions for nested imports, E0603 was left without any suggestion for grouped use statements.

This PR restores those suggestions - it splits the failing item out of the group into a corrected use statement, replaces the whole statement if the group would become empty, and handles re-export chains.

Fixes #157453.

@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 Jun 26, 2026
@rustbot

rustbot commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

r? @jackh726

rustbot has assigned @jackh726.
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 73 candidates
  • Random selection from 18 candidates

@rustbot rustbot assigned petrochenkov and unassigned jackh726 Jun 26, 2026
@petrochenkov

Copy link
Copy Markdown
Contributor

I reviewed everything except the compiler/rustc_resolve/src/error_helper.rs part, which is full of byte pos arithmetics and other wonders.

r? @fee1-dead please review, since you wanted this.

@rustbot rustbot assigned fee1-dead and unassigned petrochenkov Jun 29, 2026
@rustbot

rustbot commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

fee1-dead is not on the review rotation at the moment.
They may take a while to respond.

Comment thread compiler/rustc_resolve/src/error_helper.rs Outdated
Comment thread compiler/rustc_resolve/src/error_helper.rs Outdated
Comment thread compiler/rustc_resolve/src/error_helper.rs Outdated
Comment thread compiler/rustc_resolve/src/error_helper.rs Outdated
Comment thread compiler/rustc_resolve/src/ident.rs Outdated
@rustbot rustbot 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 Jul 9, 2026
@rustbot

rustbot commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot

This comment has been minimized.

@raushan728

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 10, 2026
@rust-bors

This comment has been minimized.

@rustbot

This comment has been minimized.

@raushan728

This comment has been minimized.

@raushan728
raushan728 requested a review from fee1-dead July 15, 2026 11:04

@fee1-dead fee1-dead left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A lot of these string fiddling is still not sitting well with me. It shouldn't be too hard to use the string search functions Rust already have to do some of these. Please use them, as we want compiler code to be readable.

View changes since this review

@rustbot rustbot 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 Jul 22, 2026
@rustbot

This comment has been minimized.

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Aug 2, 2026
@fee1-dead fee1-dead 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 Aug 2, 2026
@oli-obk

oli-obk commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

I invite you to join https://rust-lang.zulipchat.com/join/rlfvpemsaacs3pfi6kwqnqjb/ and start a thread asking for a mentor if you want to make progress here.

Comment thread compiler/rustc_resolve/src/diagnostics/impls.rs Outdated
Comment thread compiler/rustc_resolve/src/diagnostics/impls.rs Outdated
Comment thread compiler/rustc_resolve/src/diagnostics/impls.rs Outdated
if !inner.contains(',') && !inner.is_empty() {
replacement = format!("{}{}", replacement[..open].trim_end(), inner);
}
}

@cjgillot cjgillot Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Don't try to be smart with braces. The user has rustfmt.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure I understood this comment my intention wasn't just formatting but to produce a cleaner suggestion (use foo::Bar instead of use foo::{Bar}) when only one item remains. does rustfmt rewrite use foo::{Bar} into use foo::Bar, or is the idea that we shouldn't try to simplify the syntax in diagnostics?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

we shouldn't try to simplify the syntax in diagnostics?

i dont thing the UI should change regardless of what we do in the code.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

does rustfmt rewrite use foo::{Bar} into use foo::Bar, or is the idea that we shouldn't try to simplify the syntax in diagnostics?

Both.

Simplifying the syntax is ok when it makes rustc code simpler too. If you need to count BytePos for span arithmetic, it's not worth it. (Span arithmetic is known to cause ICEs when the user uses multibyte characters.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

As far i can tell report_privacy_error only has root_span and span_to_remove not the UseTree AST, so I couldn't find existing helper that would simplify {Bar} to Bar without doing manual BytePos-based span arithmetic. check_unused.rs can do this because it still has the UseTree spans available.

i wanted the diagnostic to present the import in the form users would normally write use foo::Bar rather than use foo::{Bar} especially for a single remaining item. That said, if the extra span arithmetic isn't considered worth the complexity, i'm happy to drop brace simplification but, personally i dont want we should suggest {Bar} instead of Bar for a single item.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What should i do here should i remove?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I just removed.

Comment on lines +2854 to +2864
// Replace the entire `use` instead of leaving `use foo::{}`.
if leaves_empty_group {
let line_span = self.tcx.sess.source_map().span_extend_to_line(root_span);
let suggestion_text = format!("{indentation}use {path};");
err.multipart_suggestion(
msg,
vec![(line_span, suggestion_text)],
Applicability::MachineApplicable,
);
break;
}

@cjgillot cjgillot Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not just replace the contents of root_span with path?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's what the current version does now. In the single-item case I replace root_span with path which reuses the existing use keyword instead of replacing the whole statement.

Comment on lines +2873 to +2877
// Insert before `root_span` to reuse the existing `use`.
err.multipart_suggestion(
msg,
vec![
(root_span.shrink_to_lo(), format!("{path};\n{indentation}use ")),

@cjgillot cjgillot Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would it be simpler to insert a clean line at line_span.shrink_to_lo()?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I considered that, but I chose root_span.shrink_to_lo so the suggestion only depends on the import path span and preserves formatting when the use statement isn't alone on its line (for example inside inline blocks).

Comment thread compiler/rustc_resolve/src/lib.rs
@cjgillot cjgillot self-assigned this Aug 5, 2026
@rustbot

This comment has been minimized.

@raushan728
raushan728 requested a review from cjgillot August 5, 2026 09:45
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 5, 2026
Suggest direct imports for private items inside grouped imports. Split grouped imports when necessary and replace single-item groups with direct imports.
@rustbot

rustbot commented Sep 27, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rustbot rustbot assigned BoxyUwU and unassigned cjgillot Oct 1, 2026
@raushan728

Copy link
Copy Markdown
Contributor Author

@rustbot reroll

@rustbot rustbot assigned folkertdev and unassigned BoxyUwU Oct 1, 2026
@folkertdev

Copy link
Copy Markdown
Contributor

I definitely don't have context here, @cjgillot @fee1-dead @petrochenkov can one of you pick this up?

@raushan728 raushan728 left a comment •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Please do not review for now

View changes since this review

@raushan728
raushan728 marked this pull request as draft October 4, 2026 13:55
@rustbot rustbot 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 Oct 4, 2026
@raushan728 raushan728 closed this Oct 4, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Oct 4, 2026
@raushan728
raushan728 deleted the issues/157453 branch October 4, 2026 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

9 participants