From 6bef238b6346911d29a1ee0b067dc003da15f828 Mon Sep 17 00:00:00 2001 From: Scott Schafer Date: Mon, 2 Jun 2025 23:40:01 -0600 Subject: refactor: Make -Ztrack-diagnostics emit like a note --- compiler/rustc_errors/src/diagnostic.rs | 9 +++++++++ compiler/rustc_errors/src/emitter.rs | 17 ++++------------- compiler/rustc_errors/src/json.rs | 7 +++++-- 3 files changed, 18 insertions(+), 15 deletions(-) (limited to 'compiler/rustc_errors') diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index fe9797026de..5746c28a2ab 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -417,6 +417,15 @@ impl DiagInner { self.args = std::mem::take(&mut self.reserved_args); } + pub fn emitted_at_sub_diag(&self) -> Subdiag { + let track = format!("-Ztrack-diagnostics: created at {}", self.emitted_at); + Subdiag { + level: crate::Level::Note, + messages: vec![(DiagMessage::Str(Cow::Owned(track)), Style::NoStyle)], + span: MultiSpan::new(), + } + } + /// Fields used for Hash, and PartialEq trait. fn keys( &self, diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 3f5872f34a6..2f398cea926 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -28,7 +28,6 @@ use rustc_span::{FileLines, FileName, SourceFile, Span, char_width, str_width}; use termcolor::{Buffer, BufferWriter, Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; use tracing::{debug, instrument, trace, warn}; -use crate::diagnostic::DiagLocation; use crate::registry::Registry; use crate::snippet::{ Annotation, AnnotationColumn, AnnotationType, Line, MultilineAnnotation, Style, StyledString, @@ -505,6 +504,10 @@ impl Emitter for HumanEmitter { fn emit_diagnostic(&mut self, mut diag: DiagInner, _registry: &Registry) { let fluent_args = to_fluent_args(diag.args.iter()); + if self.track_diagnostics && diag.span.has_primary_spans() && !diag.span.is_dummy() { + diag.children.insert(0, diag.emitted_at_sub_diag()); + } + let mut suggestions = diag.suggestions.unwrap_tag(); self.primary_span_formatted(&mut diag.span, &mut suggestions, &fluent_args); @@ -523,7 +526,6 @@ impl Emitter for HumanEmitter { &diag.span, &diag.children, &suggestions, - self.track_diagnostics.then_some(&diag.emitted_at), ); } @@ -1468,7 +1470,6 @@ impl HumanEmitter { level: &Level, max_line_num_len: usize, is_secondary: bool, - emitted_at: Option<&DiagLocation>, is_cont: bool, ) -> io::Result<()> { let mut buffer = StyledBuffer::new(); @@ -1978,12 +1979,6 @@ impl HumanEmitter { trace!("buffer: {:#?}", buffer.render()); } - if let Some(tracked) = emitted_at { - let track = format!("-Ztrack-diagnostics: created at {tracked}"); - let len = buffer.num_lines(); - buffer.append(len, &track, Style::NoStyle); - } - // final step: take our styled buffer, render it, then output it emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?; @@ -2478,7 +2473,6 @@ impl HumanEmitter { span: &MultiSpan, children: &[Subdiag], suggestions: &[CodeSuggestion], - emitted_at: Option<&DiagLocation>, ) { let max_line_num_len = if self.ui_testing { ANONYMIZED_LINE_NUM.len() @@ -2495,7 +2489,6 @@ impl HumanEmitter { level, max_line_num_len, false, - emitted_at, !children.is_empty() || suggestions.iter().any(|s| s.style != SuggestionStyle::CompletelyHidden), ) { @@ -2541,7 +2534,6 @@ impl HumanEmitter { &child.level, max_line_num_len, true, - None, !should_close, ) { panic!("failed to emit error: {err}"); @@ -2561,7 +2553,6 @@ impl HumanEmitter { &Level::Help, max_line_num_len, true, - None, // FIXME: this needs to account for the suggestion type, // some don't take any space. i + 1 != suggestions.len(), diff --git a/compiler/rustc_errors/src/json.rs b/compiler/rustc_errors/src/json.rs index 4348610be0a..719d4ca625a 100644 --- a/compiler/rustc_errors/src/json.rs +++ b/compiler/rustc_errors/src/json.rs @@ -372,13 +372,16 @@ impl Diagnostic { }; let level = diag.level.to_str(); let spans = DiagnosticSpan::from_multispan(&diag.span, &args, je); - let children = diag + let mut children: Vec = diag .children .iter() .map(|c| Diagnostic::from_sub_diagnostic(c, &args, je)) .chain(sugg) .collect(); - + if je.track_diagnostics && diag.span.has_primary_spans() && !diag.span.is_dummy() { + children + .insert(0, Diagnostic::from_sub_diagnostic(&diag.emitted_at_sub_diag(), &args, je)); + } let buf = BufWriter::default(); let mut dst: Destination = Box::new(buf.clone()); let short = je.json_rendered.short(); -- cgit 1.4.1-3-g733a5 From e631555a4838aaa5ea8e10dadf7e54cf9711ed10 Mon Sep 17 00:00:00 2001 From: klensy Date: Fri, 4 Jul 2025 11:43:43 +0300 Subject: bump termize dep --- Cargo.lock | 16 +++++++++++++--- compiler/rustc_errors/Cargo.toml | 2 +- compiler/rustc_session/Cargo.toml | 2 +- 3 files changed, 15 insertions(+), 5 deletions(-) (limited to 'compiler/rustc_errors') diff --git a/Cargo.lock b/Cargo.lock index 7cf10628b00..3901e07c5fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -581,7 +581,7 @@ dependencies = [ "serde_json", "syn 2.0.104", "tempfile", - "termize", + "termize 0.1.1", "tokio", "toml 0.7.8", "ui_test", @@ -3761,7 +3761,7 @@ dependencies = [ "serde", "serde_json", "termcolor", - "termize", + "termize 0.2.0", "tracing", "windows 0.61.3", ] @@ -4522,7 +4522,7 @@ dependencies = [ "rustc_serialize", "rustc_span", "rustc_target", - "termize", + "termize 0.2.0", "tracing", "windows 0.61.3", ] @@ -5294,6 +5294,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "termize" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8da106d1a19c5b9c53c03311936568a0439926a7607815bd3461139cbab1cc" +dependencies = [ + "libc", + "windows-sys 0.60.2", +] + [[package]] name = "test-float-parse" version = "0.1.0" diff --git a/compiler/rustc_errors/Cargo.toml b/compiler/rustc_errors/Cargo.toml index 82e7468211d..c4181a62a35 100644 --- a/compiler/rustc_errors/Cargo.toml +++ b/compiler/rustc_errors/Cargo.toml @@ -28,7 +28,7 @@ rustc_type_ir = { path = "../rustc_type_ir" } serde = { version = "1.0.125", features = [ "derive" ] } serde_json = "1.0.59" termcolor = "1.2.0" -termize = "0.1.1" +termize = "0.2" tracing = "0.1" # tidy-alphabetical-end diff --git a/compiler/rustc_session/Cargo.toml b/compiler/rustc_session/Cargo.toml index 5b88a7017c5..0516982aeab 100644 --- a/compiler/rustc_session/Cargo.toml +++ b/compiler/rustc_session/Cargo.toml @@ -22,7 +22,7 @@ rustc_macros = { path = "../rustc_macros" } rustc_serialize = { path = "../rustc_serialize" } rustc_span = { path = "../rustc_span" } rustc_target = { path = "../rustc_target" } -termize = "0.1.1" +termize = "0.2" tracing = "0.1" # tidy-alphabetical-end -- cgit 1.4.1-3-g733a5