Skip to content

Commit

Permalink
Merge pull request #301 from dtolnay/multilinenote
Browse files Browse the repository at this point in the history
Fix unindentation of multiline note
  • Loading branch information
dtolnay authored Jan 23, 2025
2 parents 2736f37 + 14cad05 commit 7a19ab7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use self::Normalization::*;
use crate::directory::Directory;
use crate::run::PathDependency;
use std::cmp;
use std::mem;
use std::path::Path;

#[derive(Copy, Clone)]
Expand Down Expand Up @@ -59,6 +60,7 @@ normalizations! {
StripLongTypeNameFiles,
UnindentAfterHelp,
AndOthersVerbose,
UnindentMultilineNote,
// New normalization steps are to be inserted here at the end so that any
// snapshots saved before your normalization change remain passing.
}
Expand Down Expand Up @@ -542,7 +544,7 @@ fn unindent(diag: String, normalization: Normalization) -> String {
normalized.push_str(line);
normalized.push('\n');

if indented_line_kind(line, normalization) != IndentedLineKind::Heading {
if indented_line_kind(line, &mut false, normalization) != IndentedLineKind::Heading {
continue;
}

Expand All @@ -551,12 +553,15 @@ fn unindent(diag: String, normalization: Normalization) -> String {
continue;
};

if let IndentedLineKind::Code(indent) = indented_line_kind(next_line, normalization) {
if let IndentedLineKind::Code(indent) =
indented_line_kind(next_line, &mut false, normalization)
{
if next_line[indent + 1..].starts_with("--> ") {
let mut lines_in_block = 1;
let mut least_indent = indent;
let mut previous_line_is_note = false;
while let Some(line) = ahead.next() {
match indented_line_kind(line, normalization) {
match indented_line_kind(line, &mut previous_line_is_note, normalization) {
IndentedLineKind::Heading => break,
IndentedLineKind::Code(indent) => {
lines_in_block += 1;
Expand All @@ -572,10 +577,11 @@ fn unindent(diag: String, normalization: Normalization) -> String {
}
}
}
previous_line_is_note = false;
for _ in 0..lines_in_block {
let line = lines.next().unwrap();
if let IndentedLineKind::Code(_) | IndentedLineKind::Other(_) =
indented_line_kind(line, normalization)
indented_line_kind(line, &mut previous_line_is_note, normalization)
{
let space = line.find(' ').unwrap();
normalized.push_str(&line[..space]);
Expand All @@ -592,7 +598,13 @@ fn unindent(diag: String, normalization: Normalization) -> String {
normalized
}

fn indented_line_kind(line: &str, normalization: Normalization) -> IndentedLineKind {
fn indented_line_kind(
line: &str,
previous_line_is_note: &mut bool,
normalization: Normalization,
) -> IndentedLineKind {
let previous_line_was_note = mem::replace(previous_line_is_note, false);

if let Some(heading_len) = if line.starts_with("error") {
Some("error".len())
} else if line.starts_with("warning") {
Expand All @@ -608,7 +620,11 @@ fn indented_line_kind(line: &str, normalization: Normalization) -> IndentedLineK
if line.starts_with("note:")
|| line == "..."
|| normalization >= UnindentAfterHelp && line.starts_with("help:")
|| normalization >= UnindentMultilineNote
&& previous_line_was_note
&& line.starts_with(" ")
{
*previous_line_is_note = true;
return IndentedLineKind::Note;
}

Expand Down
42 changes: 42 additions & 0 deletions src/tests/multiline-note.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
test_normalize! {"
error[E0038]: the trait `MyTrait` is not dyn compatible
--> src/main.rs:8:12
|
8 | let _: &dyn MyTrait;
| ^^^^^^^^^^^^ `MyTrait` is not dyn compatible
|
note: for a trait to be dyn compatible it needs to allow building a vtable
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> /home/ferris/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/hash/mod.rs:199:8
|
199 | fn hash<H: Hasher>(&self, state: &mut H);
| ^^^^ ...because method `hash` has generic type parameters
|
::: src/main.rs:3:7
|
3 | trait MyTrait: Hash {
| ------- this trait is not dyn compatible...
= help: consider moving `hash` to another trait
For more information about this error, try `rustc --explain E0038`.
error: could not compile `testing` (bin \"testing\") due to 1 previous error
" "
error[E0038]: the trait `MyTrait` is not dyn compatible
--> src/main.rs:8:12
|
8 | let _: &dyn MyTrait;
| ^^^^^^^^^^^^ `MyTrait` is not dyn compatible
|
note: for a trait to be dyn compatible it needs to allow building a vtable
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $RUST/core/src/hash/mod.rs
|
| fn hash<H: Hasher>(&self, state: &mut H);
| ^^^^ ...because method `hash` has generic type parameters
|
::: src/main.rs:3:7
|
3 | trait MyTrait: Hash {
| ------- this trait is not dyn compatible...
= help: consider moving `hash` to another trait
"}

0 comments on commit 7a19ab7

Please sign in to comment.