Skip to content

Commit 3bf33c3

Browse files
Don't trim substitution if it's only whitespace
1 parent bd54846 commit 3bf33c3

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

compiler/rustc_errors/src/emitter.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,10 @@ pub trait Emitter: Translate {
268268
SuggestionStyle::ShowAlways,
269269
].contains(&sugg.style)
270270
{
271-
let substitution = &sugg.substitutions[0].parts[0].snippet.trim();
271+
// Don't trim the substitution if it's only whitespace changes
272+
let substitution = &sugg.substitutions[0].parts[0].snippet;
273+
let substitution =
274+
if substitution.trim().is_empty() { substitution } else { substitution.trim() };
272275
let msg = if substitution.is_empty() || sugg.style.hide_inline() {
273276
// This substitution is only removal OR we explicitly don't want to show the
274277
// code inline (`hide_inline`). Therefore, we don't show the substitution.
@@ -1880,16 +1883,23 @@ impl EmitterWriter {
18801883
let span_start_pos = sm.lookup_char_pos(part.span.lo()).col_display;
18811884
let span_end_pos = sm.lookup_char_pos(part.span.hi()).col_display;
18821885

1886+
// If this addition is _only_ whitespace, then don't trim it,
1887+
// or else we're just not rendering anything.
1888+
let is_whitespace_addition = part.snippet.trim().is_empty();
1889+
18831890
// Do not underline the leading...
1884-
let start = part.snippet.len().saturating_sub(part.snippet.trim_start().len());
1891+
let start = if is_whitespace_addition {
1892+
0
1893+
} else {
1894+
part.snippet.len().saturating_sub(part.snippet.trim_start().len())
1895+
};
18851896
// ...or trailing spaces. Account for substitutions containing unicode
18861897
// characters.
1887-
let sub_len: usize = part
1888-
.snippet
1889-
.trim()
1890-
.chars()
1891-
.map(|ch| unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
1892-
.sum();
1898+
let sub_len: usize =
1899+
if is_whitespace_addition { &part.snippet } else { part.snippet.trim() }
1900+
.chars()
1901+
.map(|ch| unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
1902+
.sum();
18931903

18941904
let offset: isize = offsets
18951905
.iter()
@@ -2130,7 +2140,7 @@ impl EmitterWriter {
21302140
}
21312141
}
21322142

2133-
#[derive(Clone, Copy)]
2143+
#[derive(Clone, Copy, Debug)]
21342144
enum DisplaySuggestion {
21352145
Underline,
21362146
Diff,

src/test/ui/rust-2021/reserved-prefixes-migration.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ LL | #![warn(rust_2021_prefixes_incompatible_syntax)]
1414
help: insert whitespace here to avoid this being parsed as a prefix in Rust 2021
1515
|
1616
LL | m2!(z "hey");
17-
|
17+
| +
1818

1919
warning: prefix `prefix` is unknown
2020
--> $DIR/reserved-prefixes-migration.rs:19:9
@@ -27,7 +27,7 @@ LL | m2!(prefix"hey");
2727
help: insert whitespace here to avoid this being parsed as a prefix in Rust 2021
2828
|
2929
LL | m2!(prefix "hey");
30-
|
30+
| +
3131

3232
warning: prefix `hey` is unknown
3333
--> $DIR/reserved-prefixes-migration.rs:22:9
@@ -40,7 +40,7 @@ LL | m3!(hey#123);
4040
help: insert whitespace here to avoid this being parsed as a prefix in Rust 2021
4141
|
4242
LL | m3!(hey #123);
43-
|
43+
| +
4444

4545
warning: prefix `hey` is unknown
4646
--> $DIR/reserved-prefixes-migration.rs:25:9
@@ -53,7 +53,7 @@ LL | m3!(hey#hey);
5353
help: insert whitespace here to avoid this being parsed as a prefix in Rust 2021
5454
|
5555
LL | m3!(hey #hey);
56-
|
56+
| +
5757

5858
warning: prefix `kind` is unknown
5959
--> $DIR/reserved-prefixes-migration.rs:35:14
@@ -66,7 +66,7 @@ LL | #name = #kind#value
6666
help: insert whitespace here to avoid this being parsed as a prefix in Rust 2021
6767
|
6868
LL | #name = #kind #value
69-
|
69+
| +
7070

7171
warning: 5 warnings emitted
7272

src/test/ui/rust-2021/reserved-prefixes.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | demo3!(foo#bar);
88
help: consider inserting whitespace here
99
|
1010
LL | demo3!(foo #bar);
11-
|
11+
| +
1212

1313
error: prefix `foo` is unknown
1414
--> $DIR/reserved-prefixes.rs:17:12
@@ -20,7 +20,7 @@ LL | demo2!(foo"bar");
2020
help: consider inserting whitespace here
2121
|
2222
LL | demo2!(foo "bar");
23-
|
23+
| +
2424

2525
error: prefix `foo` is unknown
2626
--> $DIR/reserved-prefixes.rs:18:12
@@ -32,7 +32,7 @@ LL | demo2!(foo'b');
3232
help: consider inserting whitespace here
3333
|
3434
LL | demo2!(foo 'b');
35-
|
35+
| +
3636

3737
error: prefix `foo` is unknown
3838
--> $DIR/reserved-prefixes.rs:20:12
@@ -44,7 +44,7 @@ LL | demo2!(foo'b);
4444
help: consider inserting whitespace here
4545
|
4646
LL | demo2!(foo 'b);
47-
|
47+
| +
4848

4949
error: prefix `foo` is unknown
5050
--> $DIR/reserved-prefixes.rs:21:12
@@ -56,7 +56,7 @@ LL | demo3!(foo# bar);
5656
help: consider inserting whitespace here
5757
|
5858
LL | demo3!(foo # bar);
59-
|
59+
| +
6060

6161
error: prefix `foo` is unknown
6262
--> $DIR/reserved-prefixes.rs:22:12
@@ -68,7 +68,7 @@ LL | demo4!(foo#! bar);
6868
help: consider inserting whitespace here
6969
|
7070
LL | demo4!(foo #! bar);
71-
|
71+
| +
7272

7373
error: prefix `foo` is unknown
7474
--> $DIR/reserved-prefixes.rs:23:12
@@ -80,7 +80,7 @@ LL | demo4!(foo## bar);
8080
help: consider inserting whitespace here
8181
|
8282
LL | demo4!(foo ## bar);
83-
|
83+
| +
8484

8585
error: prefix `foo` is unknown
8686
--> $DIR/reserved-prefixes.rs:25:12
@@ -92,7 +92,7 @@ LL | demo4!(foo#bar#);
9292
help: consider inserting whitespace here
9393
|
9494
LL | demo4!(foo #bar#);
95-
|
95+
| +
9696

9797
error: prefix `bar` is unknown
9898
--> $DIR/reserved-prefixes.rs:25:16
@@ -104,7 +104,7 @@ LL | demo4!(foo#bar#);
104104
help: consider inserting whitespace here
105105
|
106106
LL | demo4!(foo#bar #);
107-
|
107+
| +
108108

109109
error: aborting due to 9 previous errors
110110

0 commit comments

Comments
 (0)