From 4034ea34c544f3209a0caa3c404b4cc4f0052aeb Mon Sep 17 00:00:00 2001 From: Facundo Lerena Date: Fri, 24 Oct 2025 19:59:17 -0300 Subject: [PATCH] Update substringing when orig & sugg have equal prefixes --- compiler/rustc_errors/src/lib.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index 17cd466f96b87..9cd34a8747fff 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -75,7 +75,7 @@ pub use rustc_span::fatal_error::{FatalError, FatalErrorMarker}; use rustc_span::source_map::SourceMap; use rustc_span::{BytePos, DUMMY_SP, Loc, Span}; pub use snippet::Style; -use tracing::debug; +use tracing::{debug, instrument}; use crate::emitter::TimingEvent; use crate::registry::Registry; @@ -245,6 +245,7 @@ pub(crate) struct SubstitutionHighlight { impl SubstitutionPart { /// Try to turn a replacement into an addition when the span that is being /// overwritten matches either the prefix or suffix of the replacement. + #[instrument(level = "debug", skip(self, sm))] fn trim_trivial_replacements(self, sm: &SourceMap) -> TrimmedSubstitutionPart { let mut trimmed_part = TrimmedSubstitutionPart { original_span: self.span, @@ -306,6 +307,16 @@ impl TrimmedSubstitutionPart { /// `BB` is. Return the length of the prefix, the "trimmed" suggestion, and the length /// of the suffix. fn as_substr<'a>(original: &'a str, suggestion: &'a str) -> Option<(usize, &'a str, usize)> { + if suggestion.contains("::") + && suggestion.ends_with(original) + && suggestion.len() > original.len() + { + let prefix = &suggestion[..suggestion.len() - original.len()]; + if prefix.ends_with("::") && suggestion.chars().next() == original.chars().next() { + return Some((0, prefix, original.len())); + } + } + let common_prefix = original .chars() .zip(suggestion.chars())