Skip to content

Commit 5e0f6bb

Browse files
authored
Rollup merge of rust-lang#101700 - compiler-errors:deletion-span, r=davidtwco
A `SubstitutionPart` is not considered a deletion if it replaces nothing with nothing Fixes rust-lang#101689
2 parents dc842dd + cd962e6 commit 5e0f6bb

21 files changed

+159
-220
lines changed

compiler/rustc_errors/src/emitter.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -1704,7 +1704,7 @@ impl EmitterWriter {
17041704
{
17051705
notice_capitalization |= only_capitalization;
17061706

1707-
let has_deletion = parts.iter().any(|p| p.is_deletion());
1707+
let has_deletion = parts.iter().any(|p| p.is_deletion(sm));
17081708
let is_multiline = complete.lines().count() > 1;
17091709

17101710
if let Some(span) = span.primary_span() {
@@ -1880,16 +1880,23 @@ impl EmitterWriter {
18801880
let span_start_pos = sm.lookup_char_pos(part.span.lo()).col_display;
18811881
let span_end_pos = sm.lookup_char_pos(part.span.hi()).col_display;
18821882

1883+
// If this addition is _only_ whitespace, then don't trim it,
1884+
// or else we're just not rendering anything.
1885+
let is_whitespace_addition = part.snippet.trim().is_empty();
1886+
18831887
// Do not underline the leading...
1884-
let start = part.snippet.len().saturating_sub(part.snippet.trim_start().len());
1888+
let start = if is_whitespace_addition {
1889+
0
1890+
} else {
1891+
part.snippet.len().saturating_sub(part.snippet.trim_start().len())
1892+
};
18851893
// ...or trailing spaces. Account for substitutions containing unicode
18861894
// 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();
1895+
let sub_len: usize =
1896+
if is_whitespace_addition { &part.snippet } else { part.snippet.trim() }
1897+
.chars()
1898+
.map(|ch| unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
1899+
.sum();
18931900

18941901
let offset: isize = offsets
18951902
.iter()
@@ -2130,7 +2137,7 @@ impl EmitterWriter {
21302137
}
21312138
}
21322139

2133-
#[derive(Clone, Copy)]
2140+
#[derive(Clone, Copy, Debug)]
21342141
enum DisplaySuggestion {
21352142
Underline,
21362143
Diff,

compiler/rustc_errors/src/lib.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -150,21 +150,20 @@ pub struct SubstitutionHighlight {
150150

151151
impl SubstitutionPart {
152152
pub fn is_addition(&self, sm: &SourceMap) -> bool {
153-
!self.snippet.is_empty()
154-
&& sm
155-
.span_to_snippet(self.span)
156-
.map_or(self.span.is_empty(), |snippet| snippet.trim().is_empty())
153+
!self.snippet.is_empty() && !self.replaces_meaningful_content(sm)
157154
}
158155

159-
pub fn is_deletion(&self) -> bool {
160-
self.snippet.trim().is_empty()
156+
pub fn is_deletion(&self, sm: &SourceMap) -> bool {
157+
self.snippet.trim().is_empty() && self.replaces_meaningful_content(sm)
161158
}
162159

163160
pub fn is_replacement(&self, sm: &SourceMap) -> bool {
164-
!self.snippet.is_empty()
165-
&& sm
166-
.span_to_snippet(self.span)
167-
.map_or(!self.span.is_empty(), |snippet| !snippet.trim().is_empty())
161+
!self.snippet.is_empty() && self.replaces_meaningful_content(sm)
162+
}
163+
164+
fn replaces_meaningful_content(&self, sm: &SourceMap) -> bool {
165+
sm.span_to_snippet(self.span)
166+
.map_or(!self.span.is_empty(), |snippet| !snippet.trim().is_empty())
168167
}
169168
}
170169

src/test/ui/dyn-keyword/dyn-2018-edition-lint.stderr

+14-21
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ LL | #[deny(bare_trait_objects)]
1313
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
1414
help: use `dyn`
1515
|
16-
LL - fn function(x: &SomeTrait, y: Box<SomeTrait>) {
17-
LL + fn function(x: &dyn SomeTrait, y: Box<SomeTrait>) {
18-
|
16+
LL | fn function(x: &dyn SomeTrait, y: Box<SomeTrait>) {
17+
| +++
1918

2019
error: trait objects without an explicit `dyn` are deprecated
2120
--> $DIR/dyn-2018-edition-lint.rs:4:35
@@ -27,9 +26,8 @@ LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
2726
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
2827
help: use `dyn`
2928
|
30-
LL - fn function(x: &SomeTrait, y: Box<SomeTrait>) {
31-
LL + fn function(x: &SomeTrait, y: Box<dyn SomeTrait>) {
32-
|
29+
LL | fn function(x: &SomeTrait, y: Box<dyn SomeTrait>) {
30+
| +++
3331

3432
error: trait objects without an explicit `dyn` are deprecated
3533
--> $DIR/dyn-2018-edition-lint.rs:17:14
@@ -41,9 +39,8 @@ LL | let _x: &SomeTrait = todo!();
4139
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
4240
help: use `dyn`
4341
|
44-
LL - let _x: &SomeTrait = todo!();
45-
LL + let _x: &dyn SomeTrait = todo!();
46-
|
42+
LL | let _x: &dyn SomeTrait = todo!();
43+
| +++
4744

4845
error: trait objects without an explicit `dyn` are deprecated
4946
--> $DIR/dyn-2018-edition-lint.rs:4:17
@@ -55,9 +52,8 @@ LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
5552
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
5653
help: use `dyn`
5754
|
58-
LL - fn function(x: &SomeTrait, y: Box<SomeTrait>) {
59-
LL + fn function(x: &dyn SomeTrait, y: Box<SomeTrait>) {
60-
|
55+
LL | fn function(x: &dyn SomeTrait, y: Box<SomeTrait>) {
56+
| +++
6157

6258
error: trait objects without an explicit `dyn` are deprecated
6359
--> $DIR/dyn-2018-edition-lint.rs:4:17
@@ -69,9 +65,8 @@ LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
6965
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
7066
help: use `dyn`
7167
|
72-
LL - fn function(x: &SomeTrait, y: Box<SomeTrait>) {
73-
LL + fn function(x: &dyn SomeTrait, y: Box<SomeTrait>) {
74-
|
68+
LL | fn function(x: &dyn SomeTrait, y: Box<SomeTrait>) {
69+
| +++
7570

7671
error: trait objects without an explicit `dyn` are deprecated
7772
--> $DIR/dyn-2018-edition-lint.rs:4:35
@@ -83,9 +78,8 @@ LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
8378
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
8479
help: use `dyn`
8580
|
86-
LL - fn function(x: &SomeTrait, y: Box<SomeTrait>) {
87-
LL + fn function(x: &SomeTrait, y: Box<dyn SomeTrait>) {
88-
|
81+
LL | fn function(x: &SomeTrait, y: Box<dyn SomeTrait>) {
82+
| +++
8983

9084
error: trait objects without an explicit `dyn` are deprecated
9185
--> $DIR/dyn-2018-edition-lint.rs:4:35
@@ -97,9 +91,8 @@ LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
9791
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
9892
help: use `dyn`
9993
|
100-
LL - fn function(x: &SomeTrait, y: Box<SomeTrait>) {
101-
LL + fn function(x: &SomeTrait, y: Box<dyn SomeTrait>) {
102-
|
94+
LL | fn function(x: &SomeTrait, y: Box<dyn SomeTrait>) {
95+
| +++
10396

10497
error: aborting due to 7 previous errors
10598

src/test/ui/dyn-keyword/dyn-2021-edition-error.stderr

+4-6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
66
|
77
help: add `dyn` keyword before this trait
88
|
9-
LL - fn function(x: &SomeTrait, y: Box<SomeTrait>) {
10-
LL + fn function(x: &dyn SomeTrait, y: Box<SomeTrait>) {
11-
|
9+
LL | fn function(x: &dyn SomeTrait, y: Box<SomeTrait>) {
10+
| +++
1211

1312
error[E0782]: trait objects must include the `dyn` keyword
1413
--> $DIR/dyn-2021-edition-error.rs:3:35
@@ -18,9 +17,8 @@ LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
1817
|
1918
help: add `dyn` keyword before this trait
2019
|
21-
LL - fn function(x: &SomeTrait, y: Box<SomeTrait>) {
22-
LL + fn function(x: &SomeTrait, y: Box<dyn SomeTrait>) {
23-
|
20+
LL | fn function(x: &SomeTrait, y: Box<dyn SomeTrait>) {
21+
| +++
2422

2523
error: aborting due to 2 previous errors
2624

src/test/ui/dyn-keyword/dyn-angle-brackets.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ LL | #![deny(bare_trait_objects)]
1313
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
1414
help: use `dyn`
1515
|
16-
LL - <fmt::Debug>::fmt(self, f)
17-
LL + <dyn fmt::Debug>::fmt(self, f)
18-
|
16+
LL | <dyn fmt::Debug>::fmt(self, f)
17+
| +++
1918

2019
error: aborting due to previous error
2120

src/test/ui/impl-trait/generic-with-implicit-hrtb-without-dyn.edition2021.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ LL | fn ice() -> impl AsRef<Fn(&())> {
66
|
77
help: add `dyn` keyword before this trait
88
|
9-
LL - fn ice() -> impl AsRef<Fn(&())> {
10-
LL + fn ice() -> impl AsRef<dyn Fn(&())> {
11-
|
9+
LL | fn ice() -> impl AsRef<dyn Fn(&())> {
10+
| +++
1211

1312
error[E0277]: the trait bound `(): AsRef<(dyn for<'r> Fn(&'r ()) + 'static)>` is not satisfied
1413
--> $DIR/generic-with-implicit-hrtb-without-dyn.rs:6:13

src/test/ui/issues/issue-86756.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ LL | eq::<dyn, Foo>
2525
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
2626
help: use `dyn`
2727
|
28-
LL - eq::<dyn, Foo>
29-
LL + eq::<dyn, dyn Foo>
30-
|
28+
LL | eq::<dyn, dyn Foo>
29+
| +++
3130

3231
error[E0107]: missing generics for trait `Foo`
3332
--> $DIR/issue-86756.rs:5:15

src/test/ui/lint/force-warn/allowed-group-warn-by-default-lint.stderr

+6-9
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
99
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
1010
help: use `dyn`
1111
|
12-
LL - pub fn function(_x: Box<SomeTrait>) {}
13-
LL + pub fn function(_x: Box<dyn SomeTrait>) {}
14-
|
12+
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
13+
| +++
1514

1615
warning: trait objects without an explicit `dyn` are deprecated
1716
--> $DIR/allowed-group-warn-by-default-lint.rs:10:25
@@ -23,9 +22,8 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
2322
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
2423
help: use `dyn`
2524
|
26-
LL - pub fn function(_x: Box<SomeTrait>) {}
27-
LL + pub fn function(_x: Box<dyn SomeTrait>) {}
28-
|
25+
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
26+
| +++
2927

3028
warning: trait objects without an explicit `dyn` are deprecated
3129
--> $DIR/allowed-group-warn-by-default-lint.rs:10:25
@@ -37,9 +35,8 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
3735
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
3836
help: use `dyn`
3937
|
40-
LL - pub fn function(_x: Box<SomeTrait>) {}
41-
LL + pub fn function(_x: Box<dyn SomeTrait>) {}
42-
|
38+
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
39+
| +++
4340

4441
warning: 3 warnings emitted
4542

src/test/ui/lint/force-warn/cap-lints-allow.stderr

+6-9
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
99
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
1010
help: use `dyn`
1111
|
12-
LL - pub fn function(_x: Box<SomeTrait>) {}
13-
LL + pub fn function(_x: Box<dyn SomeTrait>) {}
14-
|
12+
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
13+
| +++
1514

1615
warning: trait objects without an explicit `dyn` are deprecated
1716
--> $DIR/cap-lints-allow.rs:8:25
@@ -23,9 +22,8 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
2322
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
2423
help: use `dyn`
2524
|
26-
LL - pub fn function(_x: Box<SomeTrait>) {}
27-
LL + pub fn function(_x: Box<dyn SomeTrait>) {}
28-
|
25+
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
26+
| +++
2927

3028
warning: trait objects without an explicit `dyn` are deprecated
3129
--> $DIR/cap-lints-allow.rs:8:25
@@ -37,9 +35,8 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
3735
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
3836
help: use `dyn`
3937
|
40-
LL - pub fn function(_x: Box<SomeTrait>) {}
41-
LL + pub fn function(_x: Box<dyn SomeTrait>) {}
42-
|
38+
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
39+
| +++
4340

4441
warning: 3 warnings emitted
4542

src/test/ui/lint/force-warn/lint-group-allowed-cli-warn-by-default-lint.stderr

+6-9
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
99
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
1010
help: use `dyn`
1111
|
12-
LL - pub fn function(_x: Box<SomeTrait>) {}
13-
LL + pub fn function(_x: Box<dyn SomeTrait>) {}
14-
|
12+
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
13+
| +++
1514

1615
warning: trait objects without an explicit `dyn` are deprecated
1716
--> $DIR/lint-group-allowed-cli-warn-by-default-lint.rs:8:25
@@ -23,9 +22,8 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
2322
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
2423
help: use `dyn`
2524
|
26-
LL - pub fn function(_x: Box<SomeTrait>) {}
27-
LL + pub fn function(_x: Box<dyn SomeTrait>) {}
28-
|
25+
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
26+
| +++
2927

3028
warning: trait objects without an explicit `dyn` are deprecated
3129
--> $DIR/lint-group-allowed-cli-warn-by-default-lint.rs:8:25
@@ -37,9 +35,8 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
3735
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
3836
help: use `dyn`
3937
|
40-
LL - pub fn function(_x: Box<SomeTrait>) {}
41-
LL + pub fn function(_x: Box<dyn SomeTrait>) {}
42-
|
38+
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
39+
| +++
4340

4441
warning: 3 warnings emitted
4542

src/test/ui/lint/force-warn/lint-group-allowed-lint-group.stderr

+6-9
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
99
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
1010
help: use `dyn`
1111
|
12-
LL - pub fn function(_x: Box<SomeTrait>) {}
13-
LL + pub fn function(_x: Box<dyn SomeTrait>) {}
14-
|
12+
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
13+
| +++
1514

1615
warning: trait objects without an explicit `dyn` are deprecated
1716
--> $DIR/lint-group-allowed-lint-group.rs:10:25
@@ -23,9 +22,8 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
2322
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
2423
help: use `dyn`
2524
|
26-
LL - pub fn function(_x: Box<SomeTrait>) {}
27-
LL + pub fn function(_x: Box<dyn SomeTrait>) {}
28-
|
25+
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
26+
| +++
2927

3028
warning: trait objects without an explicit `dyn` are deprecated
3129
--> $DIR/lint-group-allowed-lint-group.rs:10:25
@@ -37,9 +35,8 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
3735
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
3836
help: use `dyn`
3937
|
40-
LL - pub fn function(_x: Box<SomeTrait>) {}
41-
LL + pub fn function(_x: Box<dyn SomeTrait>) {}
42-
|
38+
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
39+
| +++
4340

4441
warning: 3 warnings emitted
4542

0 commit comments

Comments
 (0)