Skip to content

Commit 3a82c35

Browse files
committed
refactor: Make -Ztrack-diagnostics emit like a note
1 parent 25face9 commit 3a82c35

File tree

19 files changed

+64
-38
lines changed

19 files changed

+64
-38
lines changed

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,15 @@ impl DiagInner {
417417
self.args = std::mem::take(&mut self.reserved_args);
418418
}
419419

420+
pub fn emitted_at_sub_diag(&self) -> Subdiag {
421+
let track = format!("-Ztrack-diagnostics: created at {}", self.emitted_at);
422+
Subdiag {
423+
level: crate::Level::Note,
424+
messages: vec![(DiagMessage::Str(Cow::Owned(track)), Style::NoStyle)],
425+
span: MultiSpan::new(),
426+
}
427+
}
428+
420429
/// Fields used for Hash, and PartialEq trait.
421430
fn keys(
422431
&self,

compiler/rustc_errors/src/emitter.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use rustc_span::{FileLines, FileName, SourceFile, Span, char_width, str_width};
2828
use termcolor::{Buffer, BufferWriter, Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
2929
use tracing::{debug, instrument, trace, warn};
3030

31-
use crate::diagnostic::DiagLocation;
3231
use crate::registry::Registry;
3332
use crate::snippet::{
3433
Annotation, AnnotationColumn, AnnotationType, Line, MultilineAnnotation, Style, StyledString,
@@ -505,6 +504,10 @@ impl Emitter for HumanEmitter {
505504
fn emit_diagnostic(&mut self, mut diag: DiagInner, _registry: &Registry) {
506505
let fluent_args = to_fluent_args(diag.args.iter());
507506

507+
if self.track_diagnostics && diag.span.has_primary_spans() && !diag.span.is_dummy() {
508+
diag.children.insert(0, diag.emitted_at_sub_diag());
509+
}
510+
508511
let mut suggestions = diag.suggestions.unwrap_tag();
509512
self.primary_span_formatted(&mut diag.span, &mut suggestions, &fluent_args);
510513

@@ -523,7 +526,6 @@ impl Emitter for HumanEmitter {
523526
&diag.span,
524527
&diag.children,
525528
&suggestions,
526-
self.track_diagnostics.then_some(&diag.emitted_at),
527529
);
528530
}
529531

@@ -1468,7 +1470,6 @@ impl HumanEmitter {
14681470
level: &Level,
14691471
max_line_num_len: usize,
14701472
is_secondary: bool,
1471-
emitted_at: Option<&DiagLocation>,
14721473
is_cont: bool,
14731474
) -> io::Result<()> {
14741475
let mut buffer = StyledBuffer::new();
@@ -1978,12 +1979,6 @@ impl HumanEmitter {
19781979
trace!("buffer: {:#?}", buffer.render());
19791980
}
19801981

1981-
if let Some(tracked) = emitted_at {
1982-
let track = format!("-Ztrack-diagnostics: created at {tracked}");
1983-
let len = buffer.num_lines();
1984-
buffer.append(len, &track, Style::NoStyle);
1985-
}
1986-
19871982
// final step: take our styled buffer, render it, then output it
19881983
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
19891984

@@ -2478,7 +2473,6 @@ impl HumanEmitter {
24782473
span: &MultiSpan,
24792474
children: &[Subdiag],
24802475
suggestions: &[CodeSuggestion],
2481-
emitted_at: Option<&DiagLocation>,
24822476
) {
24832477
let max_line_num_len = if self.ui_testing {
24842478
ANONYMIZED_LINE_NUM.len()
@@ -2495,7 +2489,6 @@ impl HumanEmitter {
24952489
level,
24962490
max_line_num_len,
24972491
false,
2498-
emitted_at,
24992492
!children.is_empty()
25002493
|| suggestions.iter().any(|s| s.style != SuggestionStyle::CompletelyHidden),
25012494
) {
@@ -2541,7 +2534,6 @@ impl HumanEmitter {
25412534
&child.level,
25422535
max_line_num_len,
25432536
true,
2544-
None,
25452537
!should_close,
25462538
) {
25472539
panic!("failed to emit error: {err}");
@@ -2561,7 +2553,6 @@ impl HumanEmitter {
25612553
&Level::Help,
25622554
max_line_num_len,
25632555
true,
2564-
None,
25652556
// FIXME: this needs to account for the suggestion type,
25662557
// some don't take any space.
25672558
i + 1 != suggestions.len(),

compiler/rustc_errors/src/json.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,16 @@ impl Diagnostic {
372372
};
373373
let level = diag.level.to_str();
374374
let spans = DiagnosticSpan::from_multispan(&diag.span, &args, je);
375-
let children = diag
375+
let mut children: Vec<Diagnostic> = diag
376376
.children
377377
.iter()
378378
.map(|c| Diagnostic::from_sub_diagnostic(c, &args, je))
379379
.chain(sugg)
380380
.collect();
381-
381+
if je.track_diagnostics && diag.span.has_primary_spans() && !diag.span.is_dummy() {
382+
children
383+
.insert(0, Diagnostic::from_sub_diagnostic(&diag.emitted_at_sub_diag(), &args, je));
384+
}
382385
let buf = BufWriter::default();
383386
let mut dst: Destination = Box::new(buf.clone());
384387
let short = je.json_rendered.short();

src/tools/clippy/tests/ui/track-diagnostics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ struct A;
88
struct B;
99
const S: A = B;
1010
//~^ ERROR: mismatched types
11+
//~| NOTE: created at
1112

1213
fn main() {}

src/tools/clippy/tests/ui/track-diagnostics.stderr

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

89
error: aborting due to 1 previous error
910

tests/rustdoc-ui/track-diagnostics.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@ compile-flags: -Z track-diagnostics
2-
//@ error-pattern: created at
32

43
// Normalize the emitted location so this doesn't need
54
// updating everytime someone adds or removes a line.
@@ -8,4 +7,7 @@
87
struct A;
98
struct B;
109

11-
pub const S: A = B; //~ ERROR mismatched types
10+
pub const S: A = B;
11+
//~^ ERROR mismatched types
12+
//~| NOTE created at
13+
//~| NOTE expected `A`, found `B`

tests/rustdoc-ui/track-diagnostics.stderr

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

89
error: aborting due to 1 previous error
910

tests/ui/track-diagnostics/track.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ compile-flags: -Z track-diagnostics
2-
//@ error-pattern: created at
2+
//@ dont-require-annotations: NOTE
33
//@ rustc-env:RUST_BACKTRACE=0
44
//@ failure-status: 101
55

@@ -16,6 +16,9 @@
1616
fn main() {
1717
break rust
1818
//~^ ERROR cannot find value `rust` in this scope
19+
//~| NOTE created at
1920
//~| ERROR `break` outside of a loop or labeled block
21+
//~| NOTE created at
2022
//~| ERROR It looks like you're trying to break rust; would you like some ICE?
23+
//~| NOTE created at
2124
}

tests/ui/track-diagnostics/track.stderr

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

89
error[E0268]: `break` outside of a loop or labeled block
910
--> $DIR/track.rs:LL:CC
1011
|
1112
LL | break rust
1213
| ^^^^^^^^^^ cannot `break` outside of a loop or labeled block
13-
-Ztrack-diagnostics: created at compiler/rustc_hir_typeck/src/loops.rs:LL:CC
14+
|
15+
= note: -Ztrack-diagnostics: created at compiler/rustc_hir_typeck/src/loops.rs:LL:CC
1416

1517
error: internal compiler error: It looks like you're trying to break rust; would you like some ICE?
1618
--> $DIR/track.rs:LL:CC
1719
|
1820
LL | break rust
1921
| ^^^^^^^^^^
20-
-Ztrack-diagnostics: created at compiler/rustc_hir_typeck/src/lib.rs:LL:CC
2122
|
23+
= note: -Ztrack-diagnostics: created at compiler/rustc_hir_typeck/src/lib.rs:LL:CC
2224
= note: the compiler expectedly panicked. this is a feature.
2325
= note: we would appreciate a joke overview: https://github.com/rust-lang/rust/issues/43162#issuecomment-320764675
2426
= note: rustc $VERSION running on $TARGET

tests/ui/track-diagnostics/track2.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
//@ compile-flags: -Z track-diagnostics
2-
//@ error-pattern: created at
2+
//@ dont-require-annotations: NOTE
33

44
// Normalize the emitted location so this doesn't need
55
// updating everytime someone adds or removes a line.
66
//@ normalize-stderr: ".rs:\d+:\d+" -> ".rs:LL:CC"
77

88
fn main() {
9-
let _moved @ _from = String::from("foo"); //~ ERROR use of moved value
9+
let _moved @ _from = String::from("foo");
10+
//~^ ERROR use of moved value
11+
//~| NOTE created at
1012
}

tests/ui/track-diagnostics/track2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ LL | let _moved @ _from = String::from("foo");
66
| | |
77
| | value moved here
88
| value used here after move
9-
-Ztrack-diagnostics: created at compiler/rustc_borrowck/src/borrowck_errors.rs:LL:CC
109
|
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.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ compile-flags: -Z track-diagnostics
2-
//@ error-pattern: created at
2+
//@ dont-require-annotations: NOTE
33

44
// Normalize the emitted location so this doesn't need
55
// updating everytime someone adds or removes a line.
@@ -8,5 +8,7 @@
88
fn main() {
99
let _unimported = Blah { field: u8 };
1010
//~^ ERROR cannot find struct, variant or union type `Blah` in this scope
11+
//~| NOTE created at
1112
//~| ERROR expected value, found builtin type `u8`
13+
//~| NOTE created at
1214
}

tests/ui/track-diagnostics/track3.stderr

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

89
error[E0423]: expected value, found builtin type `u8`
910
--> $DIR/track3.rs:LL:CC
1011
|
1112
LL | let _unimported = Blah { field: u8 };
1213
| ^^ not a value
13-
-Ztrack-diagnostics: created at compiler/rustc_resolve/src/late/diagnostics.rs:LL:CC
14+
|
15+
= note: -Ztrack-diagnostics: created at compiler/rustc_resolve/src/late/diagnostics.rs:LL:CC
1416

1517
error: aborting due to 2 previous errors
1618

tests/ui/track-diagnostics/track4.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
//@ compile-flags: -Z track-diagnostics
2-
//@ error-pattern: created at
2+
//@ dont-require-annotations: NOTE
33

44
// Normalize the emitted location so this doesn't need
55
// updating everytime someone adds or removes a line.
66
//@ normalize-stderr: ".rs:\d+:\d+" -> ".rs:LL:CC"
77

8-
pub onion { //~ ERROR missing `enum` for enum definition
8+
pub onion {
9+
//~^ ERROR missing `enum` for enum definition
10+
//~| NOTE created at
911
Owo(u8),
1012
Uwu(i8),
1113
}

tests/ui/track-diagnostics/track4.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ error: missing `enum` for enum definition
33
|
44
LL | pub onion {
55
| ^^^^^^^^^
6-
-Ztrack-diagnostics: created at compiler/rustc_parse/src/parser/item.rs:LL:CC
76
|
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.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//@ compile-flags: -Z track-diagnostics
2-
//@ error-pattern: created at
2+
//@ dont-require-annotations: NOTE
33

44
// Normalize the emitted location so this doesn't need
55
// updating everytime someone adds or removes a line.
66
//@ normalize-stderr: ".rs:\d+:\d+" -> ".rs:LL:CC"
77

8-
} //~ ERROR unexpected closing delimiter: `}`
8+
}
9+
//~^ ERROR unexpected closing delimiter: `}`
10+
//~| NOTE created at

tests/ui/track-diagnostics/track5.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ error: unexpected closing delimiter: `}`
33
|
44
LL | }
55
| ^ unexpected closing delimiter
6-
-Ztrack-diagnostics: created at compiler/rustc_parse/src/lexer/tokentrees.rs:LL:CC
6+
|
7+
= note: -Ztrack-diagnostics: created at compiler/rustc_parse/src/lexer/tokentrees.rs:LL:CC
78

89
error: aborting due to 1 previous error
910

tests/ui/track-diagnostics/track6.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ compile-flags: -Z track-diagnostics
2-
//@ error-pattern: created at
2+
//@ dont-require-annotations: NOTE
33

44
// Normalize the emitted location so this doesn't need
55
// updating everytime someone adds or removes a line.
@@ -11,7 +11,9 @@ pub trait Foo {
1111
}
1212

1313
impl <T> Foo for T {
14-
default fn bar() {} //~ ERROR specialization is unstable
14+
default fn bar() {}
15+
//~^ ERROR specialization is unstable
16+
//~| NOTE created at
1517
}
1618

1719
fn main() {}

tests/ui/track-diagnostics/track6.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ error[E0658]: specialization is unstable
33
|
44
LL | default fn bar() {}
55
| ^^^^^^^^^^^^^^^^^^^
6-
-Ztrack-diagnostics: created at compiler/rustc_ast_passes/src/feature_gate.rs:LL:CC
76
|
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)