Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -2130,7 +2137,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