From 370c816a71742373401fd3c75699c04f1ceaf81f Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sun, 11 Sep 2022 19:19:07 +0000 Subject: A SubstitutionPart is not a deletion if it replaces nothing with nothing --- compiler/rustc_errors/src/emitter.rs | 2 +- compiler/rustc_errors/src/lib.rs | 19 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) (limited to 'compiler/rustc_errors') diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 1d260f7cafc..880006cf1fc 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -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() { diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index fb7ae6f99cf..888128f3f74 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -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()) } } -- cgit 1.4.1-3-g733a5 From c2cff68d8445fc2bcaff060a8b78993939f33878 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sun, 11 Sep 2022 20:02:33 +0000 Subject: Don't trim substitution if it's only whitespace --- compiler/rustc_errors/src/emitter.rs | 28 +++++++++++++++------- .../rust-2021/reserved-prefixes-migration.stderr | 10 ++++---- src/test/ui/rust-2021/reserved-prefixes.stderr | 18 +++++++------- 3 files changed, 33 insertions(+), 23 deletions(-) (limited to 'compiler/rustc_errors') diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 880006cf1fc..016404c5f67 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -268,7 +268,10 @@ pub trait Emitter: Translate { SuggestionStyle::ShowAlways, ].contains(&sugg.style) { - let substitution = &sugg.substitutions[0].parts[0].snippet.trim(); + // Don't trim the substitution if it's only whitespace changes + let substitution = &sugg.substitutions[0].parts[0].snippet; + let substitution = + if substitution.trim().is_empty() { substitution } else { substitution.trim() }; let msg = if substitution.is_empty() || sugg.style.hide_inline() { // This substitution is only removal OR we explicitly don't want to show the // code inline (`hide_inline`). Therefore, we don't show the substitution. @@ -1880,16 +1883,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 +2140,7 @@ impl EmitterWriter { } } -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Debug)] enum DisplaySuggestion { Underline, Diff, diff --git a/src/test/ui/rust-2021/reserved-prefixes-migration.stderr b/src/test/ui/rust-2021/reserved-prefixes-migration.stderr index 94f9bb57cc8..c6bc082cf18 100644 --- a/src/test/ui/rust-2021/reserved-prefixes-migration.stderr +++ b/src/test/ui/rust-2021/reserved-prefixes-migration.stderr @@ -14,7 +14,7 @@ LL | #![warn(rust_2021_prefixes_incompatible_syntax)] help: insert whitespace here to avoid this being parsed as a prefix in Rust 2021 | LL | m2!(z "hey"); - | + | + warning: prefix `prefix` is unknown --> $DIR/reserved-prefixes-migration.rs:19:9 @@ -27,7 +27,7 @@ LL | m2!(prefix"hey"); help: insert whitespace here to avoid this being parsed as a prefix in Rust 2021 | LL | m2!(prefix "hey"); - | + | + warning: prefix `hey` is unknown --> $DIR/reserved-prefixes-migration.rs:22:9 @@ -40,7 +40,7 @@ LL | m3!(hey#123); help: insert whitespace here to avoid this being parsed as a prefix in Rust 2021 | LL | m3!(hey #123); - | + | + warning: prefix `hey` is unknown --> $DIR/reserved-prefixes-migration.rs:25:9 @@ -53,7 +53,7 @@ LL | m3!(hey#hey); help: insert whitespace here to avoid this being parsed as a prefix in Rust 2021 | LL | m3!(hey #hey); - | + | + warning: prefix `kind` is unknown --> $DIR/reserved-prefixes-migration.rs:35:14 @@ -66,7 +66,7 @@ LL | #name = #kind#value help: insert whitespace here to avoid this being parsed as a prefix in Rust 2021 | LL | #name = #kind #value - | + | + warning: 5 warnings emitted diff --git a/src/test/ui/rust-2021/reserved-prefixes.stderr b/src/test/ui/rust-2021/reserved-prefixes.stderr index fcda5fcac5b..807d6d98bd3 100644 --- a/src/test/ui/rust-2021/reserved-prefixes.stderr +++ b/src/test/ui/rust-2021/reserved-prefixes.stderr @@ -8,7 +8,7 @@ LL | demo3!(foo#bar); help: consider inserting whitespace here | LL | demo3!(foo #bar); - | + | + error: prefix `foo` is unknown --> $DIR/reserved-prefixes.rs:17:12 @@ -20,7 +20,7 @@ LL | demo2!(foo"bar"); help: consider inserting whitespace here | LL | demo2!(foo "bar"); - | + | + error: prefix `foo` is unknown --> $DIR/reserved-prefixes.rs:18:12 @@ -32,7 +32,7 @@ LL | demo2!(foo'b'); help: consider inserting whitespace here | LL | demo2!(foo 'b'); - | + | + error: prefix `foo` is unknown --> $DIR/reserved-prefixes.rs:20:12 @@ -44,7 +44,7 @@ LL | demo2!(foo'b); help: consider inserting whitespace here | LL | demo2!(foo 'b); - | + | + error: prefix `foo` is unknown --> $DIR/reserved-prefixes.rs:21:12 @@ -56,7 +56,7 @@ LL | demo3!(foo# bar); help: consider inserting whitespace here | LL | demo3!(foo # bar); - | + | + error: prefix `foo` is unknown --> $DIR/reserved-prefixes.rs:22:12 @@ -68,7 +68,7 @@ LL | demo4!(foo#! bar); help: consider inserting whitespace here | LL | demo4!(foo #! bar); - | + | + error: prefix `foo` is unknown --> $DIR/reserved-prefixes.rs:23:12 @@ -80,7 +80,7 @@ LL | demo4!(foo## bar); help: consider inserting whitespace here | LL | demo4!(foo ## bar); - | + | + error: prefix `foo` is unknown --> $DIR/reserved-prefixes.rs:25:12 @@ -92,7 +92,7 @@ LL | demo4!(foo#bar#); help: consider inserting whitespace here | LL | demo4!(foo #bar#); - | + | + error: prefix `bar` is unknown --> $DIR/reserved-prefixes.rs:25:16 @@ -104,7 +104,7 @@ LL | demo4!(foo#bar#); help: consider inserting whitespace here | LL | demo4!(foo#bar #); - | + | + error: aborting due to 9 previous errors -- cgit 1.4.1-3-g733a5 From cd962e66cf2c3b48a4a54884c475e27e44ddf260 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 13 Sep 2022 00:44:02 +0000 Subject: Don't render inline suggestions of only spaces --- compiler/rustc_errors/src/emitter.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'compiler/rustc_errors') diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 016404c5f67..66fbb8f1213 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -268,10 +268,7 @@ pub trait Emitter: Translate { SuggestionStyle::ShowAlways, ].contains(&sugg.style) { - // Don't trim the substitution if it's only whitespace changes - let substitution = &sugg.substitutions[0].parts[0].snippet; - let substitution = - if substitution.trim().is_empty() { substitution } else { substitution.trim() }; + let substitution = &sugg.substitutions[0].parts[0].snippet.trim(); let msg = if substitution.is_empty() || sugg.style.hide_inline() { // This substitution is only removal OR we explicitly don't want to show the // code inline (`hide_inline`). Therefore, we don't show the substitution. -- cgit 1.4.1-3-g733a5