-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
More robust fallback for use
suggestion
#94584
Conversation
…uence, then we just suggest the first legal position where you could inject a use. To do this, I added `inject_use_span` field to `ModSpans`, and populate it in parser (it is the span of the first token found after inner attributes, if any). Then I rewrote the use-suggestion code to utilize it, and threw out some stuff that is now unnecessary with this in place. (I think the result is easier to understand.) Then I added a test of issue 87613.
Some changes occurred in src/tools/rustfmt. |
r? @estebank (rust-highfive has picked a reviewer for you, use r? to override) |
There is still one case that this arguably doesn't handle perfectly, namely this: /* hey
whazzup */ fn main() {
Command::new("git outta here");
} where this still suggests:
(but that suggestion isn't wrong, and I expect that case to be absurdly rare, so I think its reasonable to just go ahead with this approach.) |
This comment has been minimized.
This comment has been minimized.
Edit: Disregard the above. Believe manual formatting on the subtree is required when editing in tree, in part because of those differences between the two repos in how formatting is executed |
Nice! There is a duplicate use placement finder at Can you also update --- a/src/test/ui/resolve/use_suggestion_placement.rs
+++ b/src/test/ui/resolve/use_suggestion_placement.rs
@@ -10,11 +10,7 @@ mod m {
}
mod foo {
- // FIXME: UsePlacementFinder is broken because active attributes are
- // removed, and thus the `derive` attribute here is not in the AST.
- // An inert attribute should work, though.
- // #[derive(Debug)]
- #[allow(warnings)]
+ #[derive(Debug)]
pub struct Foo;
// test whether the use suggestion isn't |
Oh, I don't know! I didn't know about that, but it seems obvious that I should try to do that.
Will do! |
… PR fixes. (There is another issue, in that the fixed output is not ideally indented, but that is a pre-existing issue and should not block this PR.)
This comment has been minimized.
This comment has been minimized.
Okay, I looked into doing this. Unfortunately, the code I'm adding here is using the AST, but by the time typeck runs, the AST is long gone. I don't want to block this PR on figuring out the best way to bring these two parts into sync. (E.g. one could thread down the same span information that I attached to the AST.) I will add a fixme at least, though. |
@calebcartwright can you explain this part of the rustfmt formatting error to me? (see below) Mismatch at src/parse/parser.rs:113:
let result = catch_unwind(AssertUnwindSafe(|| {
let mut parser = new_parser_from_file(sess.inner(), path, Some(span));
match parser.parse_mod(&TokenKind::Eof) {
- Ok((a,
+ a,
ast::ModSpans {
inner_span,
inner_span,
namely, why on earth is the diff saying that its removing Was there a line that got removed in the report? I would have perhaps expected to see:
|
…ng it in sync with the method added in PR 94584.
It does look a bit odd @pnkfelix, I haven't noticed it before. Worth noting though that these tests are using an internal, rudimentary diff emitter because of where/how the test is constructed, and it's not the same emitter and output for standard end user invocations of May take a look at this if we get time, but not too concerned given it's only seen by the small audience of those modifying rustfmt source. Here's what an end user would see and what I would expect:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
r=me with or without the change to the test.
The follow up work to keep the suggestion span in the hir might be a good first-timer issue.
// FIXME: UsePlacementFinder is broken because active attributes are | ||
// removed, and thus the `derive` attribute here is not in the AST. | ||
// An inert attribute should work, though. | ||
// #[derive(Debug)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This bug has driven me nuts for a really long time, so thanks for fixing it ❤️
@bors r=ekuber rollup+ |
📌 Commit 8f4c6b0 has been approved by |
Filed as #94941 |
☀️ Test successful - checks-actions |
Finished benchmarking commit (95561b3): comparison url. Summary: This benchmark run did not return any relevant results. If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf. @rustbot label: -perf-regression |
… r=ekuber More robust fallback for `use` suggestion Our old way to suggest where to add `use`s would first look for pre-existing `use`s in the relevant crate/module, and if there are *no* uses, it would fallback on trying to use another item as the basis for the suggestion. But this was fragile, as illustrated in issue rust-lang#87613 This PR instead identifies span of the first token after any inner attributes, and uses *that* as the fallback for the `use` suggestion. Fix rust-lang#87613
…cement, r=pnkfelix remove find_use_placement A more robust solution to finding where to place use suggestions was added in rust-lang#94584. The algorithm uses the AST to find the span for the suggestion so we pass this span down to the HIR during lowering and use it instead of calling `find_use_placement` Fixes rust-lang#94941
…cement, r=pnkfelix remove find_use_placement A more robust solution to finding where to place use suggestions was added in rust-lang#94584. The algorithm uses the AST to find the span for the suggestion so we pass this span down to the HIR during lowering and use it instead of calling `find_use_placement` Fixes rust-lang#94941
…cement, r=pnkfelix remove find_use_placement A more robust solution to finding where to place use suggestions was added in rust-lang#94584. The algorithm uses the AST to find the span for the suggestion so we pass this span down to the HIR during lowering and use it instead of calling `find_use_placement` Fixes rust-lang#94941
…cement, r=pnkfelix remove find_use_placement A more robust solution to finding where to place use suggestions was added in rust-lang#94584. The algorithm uses the AST to find the span for the suggestion so we pass this span down to the HIR during lowering and use it instead of calling `find_use_placement` Fixes rust-lang#94941
…cement, r=pnkfelix remove find_use_placement A more robust solution to finding where to place use suggestions was added in rust-lang#94584. The algorithm uses the AST to find the span for the suggestion so we pass this span down to the HIR during lowering and use it instead of calling `find_use_placement` Fixes rust-lang#94941
Our old way to suggest where to add
use
s would first look for pre-existinguse
s in the relevant crate/module, and if there are no uses, it would fallback on trying to use another item as the basis for the suggestion.But this was fragile, as illustrated in issue #87613
This PR instead identifies span of the first token after any inner attributes, and uses that as the fallback for the
use
suggestion.Fix #87613