diff options
| author | Michael Goulet <michael@errs.io> | 2025-02-13 02:54:07 +0000 |
|---|---|---|
| committer | Jubilee Young <workingjubilee@gmail.com> | 2025-02-14 00:27:13 -0800 |
| commit | b480a9214a7037813d29bc04d9e9dbe92ce10cf3 (patch) | |
| tree | 748aa792f61b46d7c679f157c8c8a3ad9df380f6 /compiler/rustc_errors | |
| parent | 6dfeab5c9e8a17a6636c1479037baabc4b1e9562 (diff) | |
| download | rust-b480a9214a7037813d29bc04d9e9dbe92ce10cf3.tar.gz rust-b480a9214a7037813d29bc04d9e9dbe92ce10cf3.zip | |
Use underline suggestions for purely 'additive' replacements
Diffstat (limited to 'compiler/rustc_errors')
| -rw-r--r-- | compiler/rustc_errors/src/emitter.rs | 3 | ||||
| -rw-r--r-- | compiler/rustc_errors/src/lib.rs | 11 |
2 files changed, 13 insertions, 1 deletions
diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 4824dc098ad..7d58e6f29da 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -1982,7 +1982,8 @@ impl HumanEmitter { { debug!(?complete, ?parts, ?highlights); - let has_deletion = parts.iter().any(|p| p.is_deletion(sm) || p.is_replacement(sm)); + let has_deletion = + parts.iter().any(|p| p.is_deletion(sm) || p.is_destructive_replacement(sm)); let is_multiline = complete.lines().count() > 1; if i == 0 { diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index 9af17db9a6e..4a186565a7b 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -230,6 +230,17 @@ impl SubstitutionPart { !self.snippet.is_empty() && self.replaces_meaningful_content(sm) } + /// Whether this is a replacement that overwrites source with a snippet + /// in a way that isn't a superset of the original string. For example, + /// replacing "abc" with "abcde" is not destructive, but replacing it + /// it with "abx" is, since the "c" character is lost. + pub fn is_destructive_replacement(&self, sm: &SourceMap) -> bool { + self.is_replacement(sm) + && !sm + .span_to_snippet(self.span) + .is_ok_and(|snippet| self.snippet.trim_start().starts_with(snippet.trim_start())) + } + fn replaces_meaningful_content(&self, sm: &SourceMap) -> bool { sm.span_to_snippet(self.span) .map_or(!self.span.is_empty(), |snippet| !snippet.trim().is_empty()) |
