Improve E0603 suggestions for private grouped imports - #158445
Improve E0603 suggestions for private grouped imports#158445raushan728 wants to merge 2 commits into
E0603 suggestions for private grouped imports#158445Conversation
|
r? @jackh726 rustbot has assigned @jackh726. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
I reviewed everything except the r? @fee1-dead please review, since you wanted this. |
|
|
|
Reminder, once the PR becomes ready for a review, use |
59ad765 to
4706b08
Compare
This comment has been minimized.
This comment has been minimized.
|
@rustbot ready |
This comment has been minimized.
This comment has been minimized.
4706b08 to
12d8c86
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
12d8c86 to
567fb3a
Compare
This comment has been minimized.
This comment has been minimized.
567fb3a to
7599264
Compare
This comment has been minimized.
This comment has been minimized.
I removed the manual span/index computations and simplifying the data flow. Could you take a another look? thanks! |
7599264 to
0c6885b
Compare
|
Sorry for the previous revisions. I've cleaned up the code and addressed the earlier feedback where I could. I think it's in a much better state now and hopefully easier to review. |
|
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. |
| if !inner.contains(',') && !inner.is_empty() { | ||
| replacement = format!("{}{}", replacement[..open].trim_end(), inner); | ||
| } | ||
| } |
There was a problem hiding this comment.
Don't try to be smart with braces. The user has rustfmt.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
does
rustfmtrewriteuse foo::{Bar}intouse 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.)
There was a problem hiding this comment.
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.
| // 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; | ||
| } |
There was a problem hiding this comment.
Why not just replace the contents of root_span with path?
There was a problem hiding this comment.
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.
| // Insert before `root_span` to reuse the existing `use`. | ||
| err.multipart_suggestion( | ||
| msg, | ||
| vec![ | ||
| (root_span.shrink_to_lo(), format!("{path};\n{indentation}use ")), |
There was a problem hiding this comment.
Would it be simpler to insert a clean line at line_span.shrink_to_lo()?
There was a problem hiding this comment.
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).
0c6885b to
e8eb880
Compare
|
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. |
Suggest direct imports for private items inside grouped imports. Split grouped imports when necessary and replace single-item groups with direct imports.
e8eb880 to
fb597c3
Compare
View all comments
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 privateOne:Single item groups get a direct replacement. Braces are removed when one item remains. Re-export chains are handled.
Fixes #157453.
r? @petrochenkov