Skip to content

Commit e47cd86

Browse files
committed
refactor: Make -Ztrack-diagnostics emit like a note
1 parent 03f4a7a commit e47cd86

File tree

8 files changed

+35
-16
lines changed

8 files changed

+35
-16
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,7 @@ impl HumanEmitter {
13921392
label: &str,
13931393
override_style: Option<Style>,
13941394
) -> usize {
1395+
let line_offset = buffer.num_lines().saturating_sub(1);
13951396
// The extra 5 ` ` is padding that's always needed to align to the `note: `:
13961397
//
13971398
// error: message
@@ -1447,12 +1448,20 @@ impl HumanEmitter {
14471448
for (i, line) in lines.iter().enumerate() {
14481449
if i != 0 {
14491450
line_number += 1;
1450-
buffer.append(line_number, &padding, Style::NoStyle);
1451+
buffer.append(line_number + line_offset, &padding, Style::NoStyle);
14511452
}
1452-
buffer.append(line_number, line, style_or_override(*style, override_style));
1453+
buffer.append(
1454+
line_number + line_offset,
1455+
line,
1456+
style_or_override(*style, override_style),
1457+
);
14531458
}
14541459
} else {
1455-
buffer.append(line_number, text, style_or_override(*style, override_style));
1460+
buffer.append(
1461+
line_number + line_offset,
1462+
text,
1463+
style_or_override(*style, override_style),
1464+
);
14561465
}
14571466
}
14581467
line_number
@@ -2470,8 +2479,9 @@ impl HumanEmitter {
24702479
level,
24712480
max_line_num_len,
24722481
false,
2473-
!children.is_empty()
2474-
|| suggestions.iter().any(|s| s.style != SuggestionStyle::CompletelyHidden),
2482+
emitted_at.is_some()
2483+
|| (!children.is_empty()
2484+
|| suggestions.iter().any(|s| s.style != SuggestionStyle::CompletelyHidden)),
24752485
) {
24762486
Ok(()) => {
24772487
let mut buffer = StyledBuffer::new();
@@ -2480,7 +2490,16 @@ impl HumanEmitter {
24802490
if !self.short_message {
24812491
self.draw_col_separator_no_space(&mut buffer, 0, max_line_num_len + 1);
24822492
let track = format!("-Ztrack-diagnostics: created at {tracked}");
2483-
buffer.append(1, &track, Style::NoStyle);
2493+
2494+
self.draw_secondary_message(
2495+
&mut buffer,
2496+
&[(DiagMessage::Str(Cow::Owned(track)), Style::NoStyle)],
2497+
args,
2498+
1,
2499+
&Level::Note,
2500+
max_line_num_len,
2501+
children.first().is_some_and(|c| c.span.primary_spans().is_empty()),
2502+
);
24842503
}
24852504
} else if !children.is_empty()
24862505
|| suggestions.iter().any(|s| s.style != SuggestionStyle::CompletelyHidden)

tests/rustdoc-ui/track-diagnostics.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0308]: mismatched types
44
LL | pub const S: A = B;
55
| ^ expected `A`, found `B`
66
|
7-
-Ztrack-diagnostics: created at compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs:LL:CC
7+
= note: -Ztrack-diagnostics: created at compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs:LL:CC
88

99
error: aborting due to 1 previous error
1010

tests/ui/track-diagnostics/track.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ error[E0425]: cannot find value `rust` in this scope
44
LL | break rust
55
| ^^^^ not found in this scope
66
|
7-
-Ztrack-diagnostics: created at compiler/rustc_resolve/src/late/diagnostics.rs:LL:CC
7+
= note: -Ztrack-diagnostics: created at compiler/rustc_resolve/src/late/diagnostics.rs:LL:CC
88

99
error[E0268]: `break` outside of a loop or labeled block
1010
--> $DIR/track.rs:LL:CC
1111
|
1212
LL | break rust
1313
| ^^^^^^^^^^ cannot `break` outside of a loop or labeled block
1414
|
15-
-Ztrack-diagnostics: created at compiler/rustc_hir_typeck/src/loops.rs:LL:CC
15+
= note: -Ztrack-diagnostics: created at compiler/rustc_hir_typeck/src/loops.rs:LL:CC
1616

1717
error: internal compiler error: It looks like you're trying to break rust; would you like some ICE?
1818
--> $DIR/track.rs:LL:CC
1919
|
2020
LL | break rust
2121
| ^^^^^^^^^^
2222
|
23-
-Ztrack-diagnostics: created at compiler/rustc_hir_typeck/src/lib.rs:LL:CC
23+
= note: -Ztrack-diagnostics: created at compiler/rustc_hir_typeck/src/lib.rs:LL:CC
2424
= note: the compiler expectedly panicked. this is a feature.
2525
= note: we would appreciate a joke overview: https://github.com/rust-lang/rust/issues/43162#issuecomment-320764675
2626
= note: rustc $VERSION running on $TARGET

tests/ui/track-diagnostics/track2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | let _moved @ _from = String::from("foo");
77
| | value moved here
88
| value used here after move
99
|
10-
-Ztrack-diagnostics: created at compiler/rustc_borrowck/src/borrowck_errors.rs:LL:CC
10+
= note: -Ztrack-diagnostics: created at compiler/rustc_borrowck/src/borrowck_errors.rs:LL:CC
1111
help: borrow this binding in the pattern to avoid moving the value
1212
|
1313
LL | let ref _moved @ ref _from = String::from("foo");

tests/ui/track-diagnostics/track3.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ error[E0422]: cannot find struct, variant or union type `Blah` in this scope
44
LL | let _unimported = Blah { field: u8 };
55
| ^^^^ not found in this scope
66
|
7-
-Ztrack-diagnostics: created at compiler/rustc_resolve/src/late/diagnostics.rs:LL:CC
7+
= note: -Ztrack-diagnostics: created at compiler/rustc_resolve/src/late/diagnostics.rs:LL:CC
88

99
error[E0423]: expected value, found builtin type `u8`
1010
--> $DIR/track3.rs:LL:CC
1111
|
1212
LL | let _unimported = Blah { field: u8 };
1313
| ^^ not a value
1414
|
15-
-Ztrack-diagnostics: created at compiler/rustc_resolve/src/late/diagnostics.rs:LL:CC
15+
= note: -Ztrack-diagnostics: created at compiler/rustc_resolve/src/late/diagnostics.rs:LL:CC
1616

1717
error: aborting due to 2 previous errors
1818

tests/ui/track-diagnostics/track4.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: missing `enum` for enum definition
44
LL | pub onion {
55
| ^^^^^^^^^
66
|
7-
-Ztrack-diagnostics: created at compiler/rustc_parse/src/parser/item.rs:LL:CC
7+
= note: -Ztrack-diagnostics: created at compiler/rustc_parse/src/parser/item.rs:LL:CC
88
help: add `enum` here to parse `onion` as an enum
99
|
1010
LL | pub enum onion {

tests/ui/track-diagnostics/track5.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: unexpected closing delimiter: `}`
44
LL | }
55
| ^ unexpected closing delimiter
66
|
7-
-Ztrack-diagnostics: created at compiler/rustc_parse/src/lexer/tokentrees.rs:LL:CC
7+
= note: -Ztrack-diagnostics: created at compiler/rustc_parse/src/lexer/tokentrees.rs:LL:CC
88

99
error: aborting due to 1 previous error
1010

tests/ui/track-diagnostics/track6.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0658]: specialization is unstable
44
LL | default fn bar() {}
55
| ^^^^^^^^^^^^^^^^^^^
66
|
7-
-Ztrack-diagnostics: created at compiler/rustc_ast_passes/src/feature_gate.rs:LL:CC
7+
= note: -Ztrack-diagnostics: created at compiler/rustc_ast_passes/src/feature_gate.rs:LL:CC
88
= note: see issue #31844 <https://github.com/rust-lang/rust/issues/31844> for more information
99
= help: add `#![feature(specialization)]` to the crate attributes to enable
1010
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

0 commit comments

Comments
 (0)