Skip to content

Commit 7a19ab7

Browse files
authored
Merge pull request #301 from dtolnay/multilinenote
Fix unindentation of multiline note
2 parents 2736f37 + 14cad05 commit 7a19ab7

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

src/normalize.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use self::Normalization::*;
66
use crate::directory::Directory;
77
use crate::run::PathDependency;
88
use std::cmp;
9+
use std::mem;
910
use std::path::Path;
1011

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

545-
if indented_line_kind(line, normalization) != IndentedLineKind::Heading {
547+
if indented_line_kind(line, &mut false, normalization) != IndentedLineKind::Heading {
546548
continue;
547549
}
548550

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

554-
if let IndentedLineKind::Code(indent) = indented_line_kind(next_line, normalization) {
556+
if let IndentedLineKind::Code(indent) =
557+
indented_line_kind(next_line, &mut false, normalization)
558+
{
555559
if next_line[indent + 1..].starts_with("--> ") {
556560
let mut lines_in_block = 1;
557561
let mut least_indent = indent;
562+
let mut previous_line_is_note = false;
558563
while let Some(line) = ahead.next() {
559-
match indented_line_kind(line, normalization) {
564+
match indented_line_kind(line, &mut previous_line_is_note, normalization) {
560565
IndentedLineKind::Heading => break,
561566
IndentedLineKind::Code(indent) => {
562567
lines_in_block += 1;
@@ -572,10 +577,11 @@ fn unindent(diag: String, normalization: Normalization) -> String {
572577
}
573578
}
574579
}
580+
previous_line_is_note = false;
575581
for _ in 0..lines_in_block {
576582
let line = lines.next().unwrap();
577583
if let IndentedLineKind::Code(_) | IndentedLineKind::Other(_) =
578-
indented_line_kind(line, normalization)
584+
indented_line_kind(line, &mut previous_line_is_note, normalization)
579585
{
580586
let space = line.find(' ').unwrap();
581587
normalized.push_str(&line[..space]);
@@ -592,7 +598,13 @@ fn unindent(diag: String, normalization: Normalization) -> String {
592598
normalized
593599
}
594600

595-
fn indented_line_kind(line: &str, normalization: Normalization) -> IndentedLineKind {
601+
fn indented_line_kind(
602+
line: &str,
603+
previous_line_is_note: &mut bool,
604+
normalization: Normalization,
605+
) -> IndentedLineKind {
606+
let previous_line_was_note = mem::replace(previous_line_is_note, false);
607+
596608
if let Some(heading_len) = if line.starts_with("error") {
597609
Some("error".len())
598610
} else if line.starts_with("warning") {
@@ -608,7 +620,11 @@ fn indented_line_kind(line: &str, normalization: Normalization) -> IndentedLineK
608620
if line.starts_with("note:")
609621
|| line == "..."
610622
|| normalization >= UnindentAfterHelp && line.starts_with("help:")
623+
|| normalization >= UnindentMultilineNote
624+
&& previous_line_was_note
625+
&& line.starts_with(" ")
611626
{
627+
*previous_line_is_note = true;
612628
return IndentedLineKind::Note;
613629
}
614630

src/tests/multiline-note.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
test_normalize! {"
2+
error[E0038]: the trait `MyTrait` is not dyn compatible
3+
--> src/main.rs:8:12
4+
|
5+
8 | let _: &dyn MyTrait;
6+
| ^^^^^^^^^^^^ `MyTrait` is not dyn compatible
7+
|
8+
note: for a trait to be dyn compatible it needs to allow building a vtable
9+
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
10+
--> /home/ferris/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/hash/mod.rs:199:8
11+
|
12+
199 | fn hash<H: Hasher>(&self, state: &mut H);
13+
| ^^^^ ...because method `hash` has generic type parameters
14+
|
15+
::: src/main.rs:3:7
16+
|
17+
3 | trait MyTrait: Hash {
18+
| ------- this trait is not dyn compatible...
19+
= help: consider moving `hash` to another trait
20+
21+
For more information about this error, try `rustc --explain E0038`.
22+
error: could not compile `testing` (bin \"testing\") due to 1 previous error
23+
" "
24+
error[E0038]: the trait `MyTrait` is not dyn compatible
25+
--> src/main.rs:8:12
26+
|
27+
8 | let _: &dyn MyTrait;
28+
| ^^^^^^^^^^^^ `MyTrait` is not dyn compatible
29+
|
30+
note: for a trait to be dyn compatible it needs to allow building a vtable
31+
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
32+
--> $RUST/core/src/hash/mod.rs
33+
|
34+
| fn hash<H: Hasher>(&self, state: &mut H);
35+
| ^^^^ ...because method `hash` has generic type parameters
36+
|
37+
::: src/main.rs:3:7
38+
|
39+
3 | trait MyTrait: Hash {
40+
| ------- this trait is not dyn compatible...
41+
= help: consider moving `hash` to another trait
42+
"}

0 commit comments

Comments
 (0)