Skip to content

Commit e551456

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

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -630,18 +630,27 @@ impl Diagnostic {
630630
style: SuggestionStyle,
631631
) -> &mut Self {
632632
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"
633+
634+
let mut parts = suggestion
635+
.into_iter()
636+
.map(|(span, snippet)| SubstitutionPart { snippet, span })
637+
.collect::<Vec<_>>();
638+
639+
parts.sort_unstable_by_key(|part| part.span);
640+
641+
debug_assert_eq!(
642+
None,
643+
parts.iter().find(|part| part.span.is_empty() && part.snippet.is_empty()),
644+
"Span must not be empty and have no suggestion",
645+
);
646+
debug_assert_eq!(
647+
None,
648+
parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
649+
"suggestion must not have overlapping parts",
636650
);
637651

638652
self.push_suggestion(CodeSuggestion {
639-
substitutions: vec![Substitution {
640-
parts: suggestion
641-
.into_iter()
642-
.map(|(span, snippet)| SubstitutionPart { snippet, span })
643-
.collect(),
644-
}],
653+
substitutions: vec![Substitution { parts }],
645654
msg: self.subdiagnostic_message_to_diagnostic_message(msg),
646655
style,
647656
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)