Skip to content
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

Remove overlapping parts of multipart suggestions #106916

Merged
merged 4 commits into from
Jan 28, 2023

Conversation

lukas-code
Copy link
Member

This PR adds a debug assertion that the parts of a single substitution cannot overlap, fixes a overlapping substitution from the testsuite, and fixes #106870.

Note that a single suggestion can still have multiple overlapping substitutions / possible edits, we just don't suggest overlapping replacements in a single edit anymore.

I've also included a fix for an unrelated bug where rustfix for explicit_outlives_requirements would produce multiple trailing commas for a where clause.

@rustbot
Copy link
Collaborator

rustbot commented Jan 15, 2023

r? @WaffleLapkin

(rustbot has picked a reviewer for you, use r? to override)

@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 Jan 15, 2023
Comment on lines 639 to 650
for (index, part_outer) in parts.iter().enumerate() {
debug_assert!(
!part_outer.span.is_empty() || !part_outer.snippet.is_empty(),
"Span must not be empty and have no suggestion",
);
for part_inner in parts.iter().skip(index + 1) {
debug_assert!(
!part_outer.span.overlaps(part_inner.span),
"suggestion must not have overlapping parts:\n{part_outer:?}\n{part_inner:?}",
);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to sort parts by the span? this way you only need to check the two adjacent ones. But, we shouldn't do this just for the debug assertions maybe...

Either way, I think it would be better to enclose this in if cfg!(debug_assertions) {}, so llvm has an easier time removing the useless loops.

Also, can we somehow test this assertion?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is perfectly safe to always unconditionally sort here.

@WaffleLapkin
Copy link
Member

r? @estebank

@rustbot rustbot assigned estebank and unassigned WaffleLapkin Jan 17, 2023
Comment on lines 639 to 650
for (index, part_outer) in parts.iter().enumerate() {
debug_assert!(
!part_outer.span.is_empty() || !part_outer.snippet.is_empty(),
"Span must not be empty and have no suggestion",
);
for part_inner in parts.iter().skip(index + 1) {
debug_assert!(
!part_outer.span.overlaps(part_inner.span),
"suggestion must not have overlapping parts:\n{part_outer:?}\n{part_inner:?}",
);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

After sorting suggestion:

Suggested change
for (index, part_outer) in parts.iter().enumerate() {
debug_assert!(
!part_outer.span.is_empty() || !part_outer.snippet.is_empty(),
"Span must not be empty and have no suggestion",
);
for part_inner in parts.iter().skip(index + 1) {
debug_assert!(
!part_outer.span.overlaps(part_inner.span),
"suggestion must not have overlapping parts:\n{part_outer:?}\n{part_inner:?}",
);
}
}
let mut parts = parts.iter.peekable();
while let Some(part) = parts.next() {
debug_assert!(
!partspan.is_empty() || !part.snippet.is_empty(),
"Span must not be empty and have no suggestion",
);
let Some(next) = parts.peek() else { break; }
debug_assert!(
!part.span.overlaps(next.span),
"suggestion must not have overlapping parts:\n{part:?}\n{next:?}",
);
}

Copy link
Member

Choose a reason for hiding this comment

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

I think this can be written nicer with parts.array_windows(). Something like

debug_assert!(parts.iter().all(|part| !part.span.is_empty() || !part.snipper.is_empty()), "Span must not be empty and have no suggestion");
parts.array_windows(|[a, b]| debug_assert!(!a.span.overlaps(b.span), "suggestion must not have overlapping parts:\n{a:?}\n{b:?}"));

let rhs_tt = &rhs.tts[i];
let ctxt = tt.span().ctxt();
match (&mut tt, rhs_tt) {
// preserve the delim spans if able
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice

@estebank
Copy link
Contributor

r=me after addressing the sorting/assertion nitpick

@WaffleLapkin
Copy link
Member

@bors r=estebank

@bors
Copy link
Contributor

bors commented Jan 17, 2023

📌 Commit d18768a has been approved by estebank

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 17, 2023
@lukas-code
Copy link
Member Author

For the record, this is how this assertion looks now:

thread 'rustc' panicked at 'assertion failed: `(left == right)`
  left: `None`,
 right: `Some([SubstitutionPart { span: /home/lukas/code/rust/tests/ui/parser/issues/issue-44406.rs:3:12: 3:24 (#4), snippet: " { " }, SubstitutionPart { span: /home/lukas/code/rust/tests/ui/parser/issues/issue-44406.rs:3:12: 3:24 (#4), snippet: " }" }])`: suggestion must not have overlapping parts', /home/lukas/code/rust/compiler/rustc_errors/src/diagnostic.rs:646:9

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 18, 2023
…stebank

Remove overlapping parts of multipart suggestions

This PR adds a debug assertion that the parts of a single substitution cannot overlap, fixes a overlapping substitution from the testsuite, and fixes rust-lang#106870.

Note that a single suggestion can still have multiple overlapping substitutions / possible edits, we just don't suggest overlapping replacements in a single edit anymore.

I've also included a fix for an unrelated bug where rustfix for `explicit_outlives_requirements` would produce multiple trailing commas for a where clause.
@matthiaskrgr
Copy link
Member

Might break clippy tests

@WaffleLapkin
Copy link
Member

It does look like it breaks clippy tests (diff from #107020):

-error: variables can be used directly in the `format!` string
-   |
-   |
-LL |     println!("{0} {0}", local_i32);
-   |
-help: change this to
-   |
-   |
-LL -     println!("{0} {0}", local_i32);
-LL +     println!("{local_i32} {local_i32}");
+thread 'rustc' panicked at 'assertion failed: `(left == right)`
+  left: `None`,
+  left: `None`,
+ right: `Some([SubstitutionPart { span: $DIR/uninlined_format_args.rs:78:23: 78:34 (#0), snippet: "" }, SubstitutionPart { span: $DIR/uninlined_format_args.rs:78:23: 78:34 (#0), snippet: "" }])`: suggestion must not have overlapping parts', /checkout/compiler/rustc_errors/src/diagnostic.rs:646:9

Not sure why clippy removes the same span twice though 🤔

@WaffleLapkin
Copy link
Member

@bors r-

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jan 18, 2023
@rustbot
Copy link
Collaborator

rustbot commented Jan 19, 2023

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@lukas-code
Copy link
Member Author

I fixed the clippy failure and also changed the signature of multipart_suggestion_* to take impl IntoIterator instead of Vec, because I didn't like the .into_iter().collect().into_iter().collect(). I hope this doesn't affect compile time too much.

@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 Jan 19, 2023
Copy link
Member

@WaffleLapkin WaffleLapkin left a comment

Choose a reason for hiding this comment

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

Generally it's better to make smaller PR and separate refactorings, but this looks good overall, except for a couple of nitpicks.

Comment on lines 828 to 829
let sugg =
trait_should_be_self.iter().map(|span| (*span, "Self".to_string())).collect::<Vec<_>>();
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it make more sense to remove the collect altogether?

Copy link
Member Author

Choose a reason for hiding this comment

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

This vector is moved into a MultiSpan below. Alternatively this can be .clone().into_iter().map() instead of .iter().map().collect(), but I don't know if that's any better?

Comment on lines 2244 to 2251
let lint_spans = lint_spans.into_iter().collect::<Vec<_>>();

cx.emit_spanned_lint(
EXPLICIT_OUTLIVES_REQUIREMENTS,
lint_spans.clone(),
Copy link
Member

Choose a reason for hiding this comment

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

Remove collect? also the clone seems unnecessary? Can't this just be

Suggested change
let lint_spans = lint_spans.into_iter().collect::<Vec<_>>();
cx.emit_spanned_lint(
EXPLICIT_OUTLIVES_REQUIREMENTS,
lint_spans.clone(),
cx.emit_spanned_lint(
EXPLICIT_OUTLIVES_REQUIREMENTS,
lint_spans,

Copy link
Member Author

Choose a reason for hiding this comment

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

The clone is necessary, because we need one vector for the MultiSpan of the lint itself and one for the suggestion.

The collect is turning the HashSet into a Vec for the MultiSpan. But it would probably be better to just sort and dedup a vector instead of using a hash set, will change that.

.map(|(span, snippet)| SubstitutionPart { snippet, span })
.collect(),
}],
substitutions: vec![Substitution { parts }],
Copy link
Member

Choose a reason for hiding this comment

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

💭 would small vec be good here?

@WaffleLapkin
Copy link
Member

I assume you've removed the generic changes, so let's see what perf says @bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 27, 2023
@bors
Copy link
Contributor

bors commented Jan 27, 2023

⌛ Trying commit 228ddf0 with merge 5bfca626acb506f9da84b4eac21a87e0847f8785...

@WaffleLapkin
Copy link
Member

@bors try

@bors
Copy link
Contributor

bors commented Jan 27, 2023

⌛ Trying commit 228ddf0 with merge cddc29a75100f2566a46e957e0b0df708bc12c68...

@bors
Copy link
Contributor

bors commented Jan 27, 2023

☀️ Try build successful - checks-actions
Build commit: cddc29a75100f2566a46e957e0b0df708bc12c68 (cddc29a75100f2566a46e957e0b0df708bc12c68)

1 similar comment
@bors
Copy link
Contributor

bors commented Jan 27, 2023

☀️ Try build successful - checks-actions
Build commit: cddc29a75100f2566a46e957e0b0df708bc12c68 (cddc29a75100f2566a46e957e0b0df708bc12c68)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (cddc29a75100f2566a46e957e0b0df708bc12c68): comparison URL.

Overall result: ❌ regressions - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.4% [0.4%, 0.4%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
2.7% [2.7%, 2.7%] 1
Regressions ❌
(secondary)
2.7% [2.4%, 2.9%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.7% [2.7%, 2.7%] 1

Cycles

This benchmark run did not return any relevant results for this metric.

@rustbot rustbot removed S-waiting-on-perf Status: Waiting on a perf run to be completed. perf-regression Performance regression. labels Jan 27, 2023
@lukas-code
Copy link
Member Author

Regression is from the improvements in #107343 (comment), so probably noise.

@WaffleLapkin
Copy link
Member

@bors r=estebank

@bors
Copy link
Contributor

bors commented Jan 27, 2023

📌 Commit 228ddf0 has been approved by estebank

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 27, 2023
@bors
Copy link
Contributor

bors commented Jan 28, 2023

⌛ Testing commit 228ddf0 with merge d8da513...

@bors
Copy link
Contributor

bors commented Jan 28, 2023

☀️ Test successful - checks-actions
Approved by: estebank
Pushing d8da513 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jan 28, 2023
@bors bors merged commit d8da513 into rust-lang:master Jan 28, 2023
@rustbot rustbot added this to the 1.69.0 milestone Jan 28, 2023
@lukas-code lukas-code deleted the overlapping-substs branch January 28, 2023 13:13
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (d8da513): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.6% [-1.2%, -0.1%] 2
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
2.7% [2.6%, 2.8%] 2
Regressions ❌
(secondary)
1.5% [1.1%, 2.2%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.7% [2.6%, 2.8%] 2

Cycles

This benchmark run did not return any relevant results for this metric.

@WaffleLapkin
Copy link
Member

@lukas-code do you still have the commit/diff of making diagnostic functions accept impl Interator? I would like to experiment with this a bit

@lukas-code
Copy link
Member Author

@WaffleLapkin
Copy link
Member

@lukas-code thanks!

flip1995 pushed a commit to flip1995/rust that referenced this pull request Feb 10, 2023
…ebank

Remove overlapping parts of multipart suggestions

This PR adds a debug assertion that the parts of a single substitution cannot overlap, fixes a overlapping substitution from the testsuite, and fixes rust-lang#106870.

Note that a single suggestion can still have multiple overlapping substitutions / possible edits, we just don't suggest overlapping replacements in a single edit anymore.

I've also included a fix for an unrelated bug where rustfix for `explicit_outlives_requirements` would produce multiple trailing commas for a where clause.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

[ICE]: 'attempt to add with overflow', compiler/rustc_errors/src/emitter.rs:2229:17
7 participants