Skip to content

fix(E0603): suggest splitting grouped imports with private items#158445

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

fix(E0603): suggest splitting grouped imports with private items#158445
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

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

For use crate::two::{One, Two} with a private One:

  + use crate::one::One;
  ~ use crate::two::Two;

Single item groups get a direct replacement. Braces are removed when one item remains. Re-export chains are handled.

Fixes #157453.

r? @petrochenkov

@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 on lines +2796 to +2805
while temp_start > 0 {
let ch = source_text[..temp_start].chars().next_back().unwrap();
if ch.is_whitespace() {
temp_start -= ch.len_utf8();
} else if ch == ',' {
temp_start -= ch.len_utf8();
break;
} else {
break;
}

@fee1-dead fee1-dead Jul 9, 2026

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.

For all of these while loops can we not use char_indices and just iterate over?

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.

Extracted the identifier removal logic into expand_span_to_surrounding_comma, which uses char_indices iteration and avoids manual byte pushes. The function handles both trailing and leading comma cases.

Comment on lines +2823 to +2831
let indentation = {
if let Ok(line) = self.tcx.sess.source_map().span_to_snippet(line_span) {
let indent_len = line.chars().take_while(|c| c.is_whitespace()).count();
" ".repeat(indent_len)
} else {
let pos = self.tcx.sess.source_map().lookup_char_pos(stmt_span.lo());
" ".repeat(pos.col.0.saturating_sub(4) as usize)
}
};

@fee1-dead fee1-dead Jul 9, 2026

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.

can't we just scope the span so that it only starts at the use, specifically when we don't change it into multiple lines?

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.

In the branch where the group becomes empty, I now use the full line span line_span with a multipart suggestion and the get_indentation helper to reconstruct the line. This preserves the original indentation without manual column calculations. The else fallback is removed.

Comment thread compiler/rustc_resolve/src/error_helper.rs Outdated
Comment on lines +1358 to +1364
use_stmt_span: if path_span != root_span {
// `root_span` spans the entire `use` tree, which is needed
// to correctly generate a multipart suggestion for a grouped import.
Some(root_span)
} else {
None
},

@fee1-dead fee1-dead Jul 9, 2026

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.

what is the significance of using an Option here?

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.

Added a doc comment explaining that the span is only needed (and set) when single_nested is true, so Option avoids carrying meaningless data for non grouped imports.

@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.

Add suggestion for E0603 in grouped use statements.
When a private item is inside , suggest a new import line
with the correct path and remove the item from the group.
Single-item groups get a full-line replacement instead.
@rustbot

rustbot commented Jul 13, 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.

@raushan728

Copy link
Copy Markdown
Contributor Author

☔ The latest upstream changes (presumably #159192) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

But, there is no conflicts.

@raushan728
raushan728 requested a review from fee1-dead July 15, 2026 11:04
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

5 participants