1
Fork 0

Rollup merge of #101700 - compiler-errors:deletion-span, r=davidtwco

A `SubstitutionPart` is not considered a deletion if it replaces nothing with nothing

Fixes #101689
This commit is contained in:
Dylan DPC 2022-09-13 16:51:31 +05:30 committed by GitHub
commit adcd1fbf2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 159 additions and 220 deletions

View file

@ -1704,7 +1704,7 @@ impl EmitterWriter {
{
notice_capitalization |= only_capitalization;
let has_deletion = parts.iter().any(|p| p.is_deletion());
let has_deletion = parts.iter().any(|p| p.is_deletion(sm));
let is_multiline = complete.lines().count() > 1;
if let Some(span) = span.primary_span() {
@ -1880,16 +1880,23 @@ impl EmitterWriter {
let span_start_pos = sm.lookup_char_pos(part.span.lo()).col_display;
let span_end_pos = sm.lookup_char_pos(part.span.hi()).col_display;
// If this addition is _only_ whitespace, then don't trim it,
// or else we're just not rendering anything.
let is_whitespace_addition = part.snippet.trim().is_empty();
// Do not underline the leading...
let start = part.snippet.len().saturating_sub(part.snippet.trim_start().len());
let start = if is_whitespace_addition {
0
} else {
part.snippet.len().saturating_sub(part.snippet.trim_start().len())
};
// ...or trailing spaces. Account for substitutions containing unicode
// characters.
let sub_len: usize = part
.snippet
.trim()
.chars()
.map(|ch| unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
.sum();
let sub_len: usize =
if is_whitespace_addition { &part.snippet } else { part.snippet.trim() }
.chars()
.map(|ch| unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
.sum();
let offset: isize = offsets
.iter()
@ -2130,7 +2137,7 @@ impl EmitterWriter {
}
}
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
enum DisplaySuggestion {
Underline,
Diff,

View file

@ -150,21 +150,20 @@ pub struct SubstitutionHighlight {
impl SubstitutionPart {
pub fn is_addition(&self, sm: &SourceMap) -> bool {
!self.snippet.is_empty()
&& sm
.span_to_snippet(self.span)
.map_or(self.span.is_empty(), |snippet| snippet.trim().is_empty())
!self.snippet.is_empty() && !self.replaces_meaningful_content(sm)
}
pub fn is_deletion(&self) -> bool {
self.snippet.trim().is_empty()
pub fn is_deletion(&self, sm: &SourceMap) -> bool {
self.snippet.trim().is_empty() && self.replaces_meaningful_content(sm)
}
pub fn is_replacement(&self, sm: &SourceMap) -> bool {
!self.snippet.is_empty()
&& sm
.span_to_snippet(self.span)
.map_or(!self.span.is_empty(), |snippet| !snippet.trim().is_empty())
!self.snippet.is_empty() && self.replaces_meaningful_content(sm)
}
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())
}
}