Skip to content

Commit

Permalink
Rollup merge of rust-lang#101700 - compiler-errors:deletion-span, r=d…
Browse files Browse the repository at this point in the history
…avidtwco

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

Fixes rust-lang#101689
  • Loading branch information
GuillaumeGomez authored Sep 12, 2022
2 parents ba287f8 + 3bf33c3 commit 091e699
Show file tree
Hide file tree
Showing 21 changed files with 163 additions and 221 deletions.
30 changes: 20 additions & 10 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -1704,7 +1707,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() {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -2130,7 +2140,7 @@ impl EmitterWriter {
}
}

#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
enum DisplaySuggestion {
Underline,
Diff,
Expand Down
19 changes: 9 additions & 10 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand Down
35 changes: 14 additions & 21 deletions src/test/ui/dyn-keyword/dyn-2018-edition-lint.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ LL | #[deny(bare_trait_objects)]
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
help: use `dyn`
|
LL - fn function(x: &SomeTrait, y: Box<SomeTrait>) {
LL + fn function(x: &dyn SomeTrait, y: Box<SomeTrait>) {
|
LL | fn function(x: &dyn SomeTrait, y: Box<SomeTrait>) {
| +++

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

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

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

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

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

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

error: aborting due to 7 previous errors

10 changes: 4 additions & 6 deletions src/test/ui/dyn-keyword/dyn-2021-edition-error.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
|
help: add `dyn` keyword before this trait
|
LL - fn function(x: &SomeTrait, y: Box<SomeTrait>) {
LL + fn function(x: &dyn SomeTrait, y: Box<SomeTrait>) {
|
LL | fn function(x: &dyn SomeTrait, y: Box<SomeTrait>) {
| +++

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

error: aborting due to 2 previous errors

Expand Down
5 changes: 2 additions & 3 deletions src/test/ui/dyn-keyword/dyn-angle-brackets.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ LL | #![deny(bare_trait_objects)]
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
help: use `dyn`
|
LL - <fmt::Debug>::fmt(self, f)
LL + <dyn fmt::Debug>::fmt(self, f)
|
LL | <dyn fmt::Debug>::fmt(self, f)
| +++

error: aborting due to previous error

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ LL | fn ice() -> impl AsRef<Fn(&())> {
|
help: add `dyn` keyword before this trait
|
LL - fn ice() -> impl AsRef<Fn(&())> {
LL + fn ice() -> impl AsRef<dyn Fn(&())> {
|
LL | fn ice() -> impl AsRef<dyn Fn(&())> {
| +++

error[E0277]: the trait bound `(): AsRef<(dyn for<'r> Fn(&'r ()) + 'static)>` is not satisfied
--> $DIR/generic-with-implicit-hrtb-without-dyn.rs:6:13
Expand Down
5 changes: 2 additions & 3 deletions src/test/ui/issues/issue-86756.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ LL | eq::<dyn, Foo>
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
help: use `dyn`
|
LL - eq::<dyn, Foo>
LL + eq::<dyn, dyn Foo>
|
LL | eq::<dyn, dyn Foo>
| +++

error[E0107]: missing generics for trait `Foo`
--> $DIR/issue-86756.rs:5:15
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
help: use `dyn`
|
LL - pub fn function(_x: Box<SomeTrait>) {}
LL + pub fn function(_x: Box<dyn SomeTrait>) {}
|
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
| +++

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

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

warning: 3 warnings emitted

15 changes: 6 additions & 9 deletions src/test/ui/lint/force-warn/cap-lints-allow.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
help: use `dyn`
|
LL - pub fn function(_x: Box<SomeTrait>) {}
LL + pub fn function(_x: Box<dyn SomeTrait>) {}
|
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
| +++

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

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

warning: 3 warnings emitted

Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
help: use `dyn`
|
LL - pub fn function(_x: Box<SomeTrait>) {}
LL + pub fn function(_x: Box<dyn SomeTrait>) {}
|
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
| +++

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

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

warning: 3 warnings emitted

15 changes: 6 additions & 9 deletions src/test/ui/lint/force-warn/lint-group-allowed-lint-group.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
help: use `dyn`
|
LL - pub fn function(_x: Box<SomeTrait>) {}
LL + pub fn function(_x: Box<dyn SomeTrait>) {}
|
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
| +++

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

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

warning: 3 warnings emitted

Loading

0 comments on commit 091e699

Please sign in to comment.