From 8bbec390ca69c80be47e2d4f6be73c0b83d61e48 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 16 Aug 2023 18:23:16 -0700 Subject: [PATCH 1/2] Add test case that has a code block after 'help:' --- src/tests/erased-serde-trait-bound.rs | 43 +++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/tests/erased-serde-trait-bound.rs diff --git a/src/tests/erased-serde-trait-bound.rs b/src/tests/erased-serde-trait-bound.rs new file mode 100644 index 0000000..d95746c --- /dev/null +++ b/src/tests/erased-serde-trait-bound.rs @@ -0,0 +1,43 @@ +test_normalize! {" +error[E0277]: the trait bound `__T: serde::ser::Serialize` is not satisfied + --> src/main.rs:5:1 + | +5 | serialize_trait_object!(MyTrait); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `serde::ser::Serialize` is not implemented for `__T` + | + = note: required for `__T` to implement `erased_serde::Serialize` +note: required by a bound in `require_erased_serialize_impl` + --> /home/david/.cargo/registry/src/index.crates.io-6f17d22bba15001f/erased-serde-0.3.28/src/private.rs:14:17 + | +12 | pub fn require_erased_serialize_impl() + | ----------------------------- required by a bound in this function +13 | where +14 | T: ?Sized + crate::Serialize, + | ^^^^^^^^^^^^^^^^ required by this bound in `require_erased_serialize_impl` + = note: this error originates in the macro `$crate::__internal_serialize_trait_object` which comes from the expansion of the macro `serialize_trait_object` (in Nightly builds, run with -Z macro-backtrace for more info) +help: consider further restricting this bound + | +5 | serialize_trait_object!(MyTrait + serde::ser::Serialize); + | +++++++++++++++++++++++ +" " +error[E0277]: the trait bound `__T: serde::ser::Serialize` is not satisfied + --> src/main.rs:5:1 + | +5 | serialize_trait_object!(MyTrait); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `serde::ser::Serialize` is not implemented for `__T` + | + = note: required for `__T` to implement `erased_serde::Serialize` +note: required by a bound in `require_erased_serialize_impl` + --> $CARGO/erased-serde-0.3.28/src/private.rs + | + | pub fn require_erased_serialize_impl() + | ----------------------------- required by a bound in this function + | where + | T: ?Sized + crate::Serialize, + | ^^^^^^^^^^^^^^^^ required by this bound in `require_erased_serialize_impl` + = note: this error originates in the macro `$crate::__internal_serialize_trait_object` which comes from the expansion of the macro `serialize_trait_object` (in Nightly builds, run with -Z macro-backtrace for more info) +help: consider further restricting this bound + | +5 | serialize_trait_object!(MyTrait + serde::ser::Serialize); + | +++++++++++++++++++++++ +"} From a1a2d87a1d784246b9bcd0a0db3aa56fe3a276d9 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 16 Aug 2023 18:30:33 -0700 Subject: [PATCH 2/2] Normalize indentation of code blocks after 'help:' --- src/normalize.rs | 26 ++++++++++++++++---------- src/tests/erased-serde-trait-bound.rs | 6 +++--- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/normalize.rs b/src/normalize.rs index d738203..8171114 100644 --- a/src/normalize.rs +++ b/src/normalize.rs @@ -57,6 +57,7 @@ normalizations! { Unindent, AndOthers, StripLongTypeNameFiles, + UnindentAfterHelp, // New normalization steps are to be inserted here at the end so that any // snapshots saved before your normalization change remain passing. } @@ -139,9 +140,7 @@ fn apply(original: &str, normalization: Normalization, context: Context) -> Stri } } - if normalization >= Unindent { - normalized = unindent(normalized); - } + normalized = unindent(normalized, normalization); trim(normalized) } @@ -494,7 +493,11 @@ enum IndentedLineKind { Other(usize), } -fn unindent(diag: String) -> String { +fn unindent(diag: String, normalization: Normalization) -> String { + if normalization < Unindent { + return diag; + } + let mut normalized = String::new(); let mut lines = diag.lines(); @@ -502,7 +505,7 @@ fn unindent(diag: String) -> String { normalized.push_str(line); normalized.push('\n'); - if indented_line_kind(line) != IndentedLineKind::Heading { + if indented_line_kind(line, normalization) != IndentedLineKind::Heading { continue; } @@ -512,12 +515,12 @@ fn unindent(diag: String) -> String { None => continue, }; - if let IndentedLineKind::Code(indent) = indented_line_kind(next_line) { + if let IndentedLineKind::Code(indent) = indented_line_kind(next_line, normalization) { if next_line[indent + 1..].starts_with("--> ") { let mut lines_in_block = 1; let mut least_indent = indent; while let Some(line) = ahead.next() { - match indented_line_kind(line) { + match indented_line_kind(line, normalization) { IndentedLineKind::Heading => break, IndentedLineKind::Code(indent) => { lines_in_block += 1; @@ -536,7 +539,7 @@ fn unindent(diag: String) -> String { for _ in 0..lines_in_block { let line = lines.next().unwrap(); if let IndentedLineKind::Code(_) | IndentedLineKind::Other(_) = - indented_line_kind(line) + indented_line_kind(line, normalization) { let space = line.find(' ').unwrap(); normalized.push_str(&line[..space]); @@ -553,7 +556,7 @@ fn unindent(diag: String) -> String { normalized } -fn indented_line_kind(line: &str) -> IndentedLineKind { +fn indented_line_kind(line: &str, normalization: Normalization) -> IndentedLineKind { if let Some(heading_len) = if line.starts_with("error") { Some("error".len()) } else if line.starts_with("warning") { @@ -566,7 +569,10 @@ fn indented_line_kind(line: &str) -> IndentedLineKind { } } - if line.starts_with("note:") || line == "..." { + if line.starts_with("note:") + || line == "..." + || normalization >= UnindentAfterHelp && line.starts_with("help:") + { return IndentedLineKind::Note; } diff --git a/src/tests/erased-serde-trait-bound.rs b/src/tests/erased-serde-trait-bound.rs index d95746c..a4cc34a 100644 --- a/src/tests/erased-serde-trait-bound.rs +++ b/src/tests/erased-serde-trait-bound.rs @@ -37,7 +37,7 @@ note: required by a bound in `require_erased_serialize_impl` | ^^^^^^^^^^^^^^^^ required by this bound in `require_erased_serialize_impl` = note: this error originates in the macro `$crate::__internal_serialize_trait_object` which comes from the expansion of the macro `serialize_trait_object` (in Nightly builds, run with -Z macro-backtrace for more info) help: consider further restricting this bound - | -5 | serialize_trait_object!(MyTrait + serde::ser::Serialize); - | +++++++++++++++++++++++ + | +5 | serialize_trait_object!(MyTrait + serde::ser::Serialize); + | +++++++++++++++++++++++ "}