Skip to content

Commit b9e8286

Browse files
author
Lukas Markeffsky
committed
add debug assertion for suggestions with overlapping parts
1 parent a6269da commit b9e8286

File tree

2 files changed

+45
-27
lines changed

2 files changed

+45
-27
lines changed

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -629,19 +629,27 @@ impl Diagnostic {
629629
applicability: Applicability,
630630
style: SuggestionStyle,
631631
) -> &mut Self {
632-
assert!(!suggestion.is_empty());
633-
debug_assert!(
634-
!(suggestion.iter().any(|(sp, text)| sp.is_empty() && text.is_empty())),
635-
"Span must not be empty and have no suggestion"
632+
let mut parts = suggestion
633+
.into_iter()
634+
.map(|(span, snippet)| SubstitutionPart { snippet, span })
635+
.collect::<Vec<_>>();
636+
637+
parts.sort_unstable_by_key(|part| part.span);
638+
639+
assert!(!parts.is_empty());
640+
debug_assert_eq!(
641+
parts.iter().find(|part| part.span.is_empty() && part.snippet.is_empty()),
642+
None,
643+
"Span must not be empty and have no suggestion",
644+
);
645+
debug_assert_eq!(
646+
parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
647+
None,
648+
"suggestion must not have overlapping parts",
636649
);
637650

638651
self.push_suggestion(CodeSuggestion {
639-
substitutions: vec![Substitution {
640-
parts: suggestion
641-
.into_iter()
642-
.map(|(span, snippet)| SubstitutionPart { snippet, span })
643-
.collect(),
644-
}],
652+
substitutions: vec![Substitution { parts }],
645653
msg: self.subdiagnostic_message_to_diagnostic_message(msg),
646654
style,
647655
applicability,
@@ -802,25 +810,34 @@ impl Diagnostic {
802810
suggestions: impl IntoIterator<Item = Vec<(Span, String)>>,
803811
applicability: Applicability,
804812
) -> &mut Self {
805-
let suggestions: Vec<_> = suggestions.into_iter().collect();
806-
debug_assert!(
807-
!(suggestions
808-
.iter()
809-
.flatten()
810-
.any(|(sp, suggestion)| sp.is_empty() && suggestion.is_empty())),
811-
"Span must not be empty and have no suggestion"
812-
);
813+
let substitutions = suggestions
814+
.into_iter()
815+
.map(|sugg| {
816+
let mut parts = sugg
817+
.into_iter()
818+
.map(|(span, snippet)| SubstitutionPart { snippet, span })
819+
.collect::<Vec<_>>();
820+
821+
parts.sort_unstable_by_key(|part| part.span);
822+
823+
assert!(!parts.is_empty());
824+
debug_assert_eq!(
825+
parts.iter().find(|part| part.span.is_empty() && part.snippet.is_empty()),
826+
None,
827+
"Span must not be empty and have no suggestion",
828+
);
829+
debug_assert_eq!(
830+
parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
831+
None,
832+
"suggestion must not have overlapping parts",
833+
);
834+
835+
Substitution { parts }
836+
})
837+
.collect();
813838

814839
self.push_suggestion(CodeSuggestion {
815-
substitutions: suggestions
816-
.into_iter()
817-
.map(|sugg| Substitution {
818-
parts: sugg
819-
.into_iter()
820-
.map(|(span, snippet)| SubstitutionPart { snippet, span })
821-
.collect(),
822-
})
823-
.collect(),
840+
substitutions,
824841
msg: self.subdiagnostic_message_to_diagnostic_message(msg),
825842
style: SuggestionStyle::ShowCode,
826843
applicability,

compiler/rustc_errors/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! This module contains the code for creating and emitting diagnostics.
44
55
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
6+
#![feature(array_windows)]
67
#![feature(drain_filter)]
78
#![feature(if_let_guard)]
89
#![feature(is_terminal)]

0 commit comments

Comments
 (0)