From a2b48332bac36ede29a14e3fbbed6a45f1527677 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Thu, 23 Oct 2025 16:10:53 +0000 Subject: [PATCH 1/2] Revert "Auto merge of #146121 - Muscraft:filter-suggestion-parts, r=petrochenkov" This reverts commit 99317ef14d0be42fa4039eea7c5ce50cb4e9aee7, reversing changes made to 9cd272dc85320e85a8c83a1a338870de52c005f3. --- compiler/rustc_errors/src/diagnostic.rs | 5 ++++ compiler/rustc_errors/src/emitter.rs | 1 + compiler/rustc_errors/src/lib.rs | 27 +++++++------------ .../tests/ui/bool_assert_comparison.stderr | 12 ++++++--- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index ae23ef1e25536..96a4ed3218fbf 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -945,6 +945,11 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> { None, "Span must not be empty and have no suggestion", ); + debug_assert_eq!( + parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)), + None, + "suggestion must not have overlapping parts", + ); self.push_suggestion(CodeSuggestion { substitutions: vec![Substitution { parts }], diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 2775e61902fbb..9e32a85e361a5 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -2350,6 +2350,7 @@ impl HumanEmitter { .sum(); let underline_start = (span_start_pos + start) as isize + offset; let underline_end = (span_start_pos + start + sub_len) as isize + offset; + assert!(underline_start >= 0 && underline_end >= 0); let padding: usize = max_line_num_len + 3; for p in underline_start..underline_end { if let DisplaySuggestion::Underline = show_code_change diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index c6685fa7a6499..17cd466f96b87 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -400,17 +400,6 @@ impl CodeSuggestion { // Assumption: all spans are in the same file, and all spans // are disjoint. Sort in ascending order. substitution.parts.sort_by_key(|part| part.span.lo()); - // Verify the assumption that all spans are disjoint - assert_eq!( - substitution.parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)), - None, - "all spans must be disjoint", - ); - - // Account for cases where we are suggesting the same code that's already - // there. This shouldn't happen often, but in some cases for multipart - // suggestions it's much easier to handle it here than in the origin. - substitution.parts.retain(|p| is_different(sm, &p.snippet, p.span)); // Find the bounding span. let lo = substitution.parts.iter().map(|part| part.span.lo()).min()?; @@ -505,12 +494,16 @@ impl CodeSuggestion { _ => 1, }) .sum(); - - line_highlight.push(SubstitutionHighlight { - start: (cur_lo.col.0 as isize + acc) as usize, - end: (cur_lo.col.0 as isize + acc + len) as usize, - }); - + if !is_different(sm, &part.snippet, part.span) { + // Account for cases where we are suggesting the same code that's already + // there. This shouldn't happen often, but in some cases for multipart + // suggestions it's much easier to handle it here than in the origin. + } else { + line_highlight.push(SubstitutionHighlight { + start: (cur_lo.col.0 as isize + acc) as usize, + end: (cur_lo.col.0 as isize + acc + len) as usize, + }); + } buf.push_str(&part.snippet); let cur_hi = sm.lookup_char_pos(part.span.hi()); // Account for the difference between the width of the current code and the diff --git a/src/tools/clippy/tests/ui/bool_assert_comparison.stderr b/src/tools/clippy/tests/ui/bool_assert_comparison.stderr index 72aa6303a2026..f823f08f31dca 100644 --- a/src/tools/clippy/tests/ui/bool_assert_comparison.stderr +++ b/src/tools/clippy/tests/ui/bool_assert_comparison.stderr @@ -272,8 +272,10 @@ LL | assert_eq!(a!(), true); | help: replace it with `assert!(..)` | -LL - assert_eq!(a!(), true); -LL + assert!(a!()); +LL | true +... +LL | +LL ~ assert!(a!()); | error: used `assert_eq!` with a literal bool @@ -284,8 +286,10 @@ LL | assert_eq!(true, b!()); | help: replace it with `assert!(..)` | -LL - assert_eq!(true, b!()); -LL + assert!(b!()); +LL | true +... +LL | +LL ~ assert!(b!()); | error: used `debug_assert_eq!` with a literal bool From dd83c576749c66061c2109b96ebec050bb8de5de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Thu, 23 Oct 2025 16:33:56 +0000 Subject: [PATCH 2/2] add `crashes` tests for overlapping spans --- tests/crashes/146261.rs | 13 +++++++++++++ tests/crashes/146706.rs | 15 +++++++++++++++ tests/crashes/147973.rs | 14 ++++++++++++++ .../crashes/auxiliary/overlapping_spans_helper.rs | 15 +++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 tests/crashes/146261.rs create mode 100644 tests/crashes/146706.rs create mode 100644 tests/crashes/147973.rs create mode 100644 tests/crashes/auxiliary/overlapping_spans_helper.rs diff --git a/tests/crashes/146261.rs b/tests/crashes/146261.rs new file mode 100644 index 0000000000000..f901497a769c2 --- /dev/null +++ b/tests/crashes/146261.rs @@ -0,0 +1,13 @@ +// This is part of series of regression tests for some diagnostics ICEs encountered in the wild with +// suggestions having overlapping parts under https://github.com/rust-lang/rust/pull/146121. + +//@ needs-rustc-debug-assertions +//@ known-bug: #146261 + +enum U { + B(), +} + +fn main() { + A(U::C) +} diff --git a/tests/crashes/146706.rs b/tests/crashes/146706.rs new file mode 100644 index 0000000000000..358ce3e8600a8 --- /dev/null +++ b/tests/crashes/146706.rs @@ -0,0 +1,15 @@ +// This is part of series of regression tests for some diagnostics ICEs encountered in the wild with +// suggestions having overlapping parts under https://github.com/rust-lang/rust/pull/146121. + +//@ needs-rustc-debug-assertions +//@ known-bug: #146706 + +type Alias<'a, T> = Foo; + +enum Foo { + Bar { t: T }, +} + +fn main() { + Alias::Bar:: { t: 0 }; +} diff --git a/tests/crashes/147973.rs b/tests/crashes/147973.rs new file mode 100644 index 0000000000000..7271c54846f15 --- /dev/null +++ b/tests/crashes/147973.rs @@ -0,0 +1,14 @@ +// This is part of series of regression tests for some diagnostics ICEs encountered in the wild with +// suggestions having overlapping parts under https://github.com/rust-lang/rust/pull/146121. +// This is one MCVE from the beta crater run regressions from issue 147973. + +//@ needs-rustc-debug-assertions +//@ known-bug: #147973 + +//@ aux-build: overlapping_spans_helper.rs +extern crate overlapping_spans_helper; + +fn main() { + let _name = Some(1); + overlapping_spans_helper::do_loop!(_name); +} diff --git a/tests/crashes/auxiliary/overlapping_spans_helper.rs b/tests/crashes/auxiliary/overlapping_spans_helper.rs new file mode 100644 index 0000000000000..e449fcd36c376 --- /dev/null +++ b/tests/crashes/auxiliary/overlapping_spans_helper.rs @@ -0,0 +1,15 @@ +// Auxiliary lib for the issue 147973 regression test with ICEs due to overlapping spans. + +#[macro_export] +macro_rules! identity { + ($x:ident) => { + $x + }; +} + +#[macro_export] +macro_rules! do_loop { + ($x:ident) => { + for $crate::identity!($x) in $x {} + }; +}