diff --git a/compiler/rustc_errors/src/json.rs b/compiler/rustc_errors/src/json.rs index 03ce1d82ef3c..260559a9ef33 100644 --- a/compiler/rustc_errors/src/json.rs +++ b/compiler/rustc_errors/src/json.rs @@ -25,6 +25,7 @@ use rustc_span::source_map::{FilePathMapping, SourceMap}; use serde::Serialize; +use crate::annotate_snippet_emitter_writer::AnnotateSnippetEmitter; use crate::diagnostic::IsLint; use crate::emitter::{ ColorConfig, Destination, Emitter, HumanEmitter, HumanReadableErrorType, OutputTheme, @@ -378,21 +379,44 @@ fn flush(&mut self) -> io::Result<()> { choice => choice, }, ); - HumanEmitter::new(dst, je.translator.clone()) - .short_message(short) - .sm(je.sm.clone()) - .diagnostic_width(je.diagnostic_width) - .macro_backtrace(je.macro_backtrace) - .track_diagnostics(je.track_diagnostics) - .terminal_url(je.terminal_url) - .ui_testing(je.ui_testing) - .ignored_directories_in_source_blocks(je.ignored_directories_in_source_blocks.clone()) - .theme(if let HumanReadableErrorType::Unicode = je.json_rendered { - OutputTheme::Unicode - } else { - OutputTheme::Ascii - }) - .emit_diagnostic(diag, registry); + if let HumanReadableErrorType::AnnotateSnippet = je.json_rendered { + AnnotateSnippetEmitter::new(dst, je.translator.clone()) + .short_message(short) + .sm(je.sm.clone()) + .diagnostic_width(je.diagnostic_width) + .macro_backtrace(je.macro_backtrace) + .track_diagnostics(je.track_diagnostics) + .terminal_url(je.terminal_url) + .ui_testing(je.ui_testing) + .ignored_directories_in_source_blocks( + je.ignored_directories_in_source_blocks.clone(), + ) + .theme(if let HumanReadableErrorType::Unicode = je.json_rendered { + OutputTheme::Unicode + } else { + OutputTheme::Ascii + }) + .emit_diagnostic(diag, registry); + } else { + HumanEmitter::new(dst, je.translator.clone()) + .short_message(short) + .sm(je.sm.clone()) + .diagnostic_width(je.diagnostic_width) + .macro_backtrace(je.macro_backtrace) + .track_diagnostics(je.track_diagnostics) + .terminal_url(je.terminal_url) + .ui_testing(je.ui_testing) + .ignored_directories_in_source_blocks( + je.ignored_directories_in_source_blocks.clone(), + ) + .theme(if let HumanReadableErrorType::Unicode = je.json_rendered { + OutputTheme::Unicode + } else { + OutputTheme::Ascii + }) + .emit_diagnostic(diag, registry); + } + let buf = Arc::try_unwrap(buf.0).unwrap().into_inner().unwrap(); let buf = String::from_utf8(buf).unwrap(); diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 16f9774554e1..fa8646d8e207 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -1501,15 +1501,27 @@ fn mk_emitter(output: ErrorOutputType) -> Box { let emitter: Box = match output { config::ErrorOutputType::HumanReadable { kind, color_config } => { let short = kind.short(); - Box::new( - HumanEmitter::new(stderr_destination(color_config), translator) - .theme(if let HumanReadableErrorType::Unicode = kind { - OutputTheme::Unicode - } else { - OutputTheme::Ascii - }) - .short_message(short), - ) + if let HumanReadableErrorType::AnnotateSnippet = kind { + Box::new( + AnnotateSnippetEmitter::new(stderr_destination(color_config), translator) + .theme(if let HumanReadableErrorType::Unicode = kind { + OutputTheme::Unicode + } else { + OutputTheme::Ascii + }) + .short_message(short), + ) + } else { + Box::new( + HumanEmitter::new(stderr_destination(color_config), translator) + .theme(if let HumanReadableErrorType::Unicode = kind { + OutputTheme::Unicode + } else { + OutputTheme::Ascii + }) + .short_message(short), + ) + } } config::ErrorOutputType::Json { pretty, json_rendered, color_config } => { Box::new(JsonEmitter::new( diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 44bac8197539..8ee2fc7a43ee 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -5,6 +5,7 @@ use rustc_data_structures::unord::UnordSet; use rustc_driver::USING_INTERNAL_FEATURES; use rustc_errors::TerminalUrl; +use rustc_errors::annotate_snippet_emitter_writer::AnnotateSnippetEmitter; use rustc_errors::codes::*; use rustc_errors::emitter::{ DynEmitter, HumanEmitter, HumanReadableErrorType, OutputTheme, stderr_destination, @@ -154,19 +155,35 @@ pub(crate) fn new_dcx( let emitter: Box = match error_format { ErrorOutputType::HumanReadable { kind, color_config } => { let short = kind.short(); - Box::new( - HumanEmitter::new(stderr_destination(color_config), translator) - .sm(source_map.map(|sm| sm as _)) - .short_message(short) - .diagnostic_width(diagnostic_width) - .track_diagnostics(unstable_opts.track_diagnostics) - .theme(if let HumanReadableErrorType::Unicode = kind { - OutputTheme::Unicode - } else { - OutputTheme::Ascii - }) - .ui_testing(unstable_opts.ui_testing), - ) + if let HumanReadableErrorType::AnnotateSnippet = kind { + Box::new( + AnnotateSnippetEmitter::new(stderr_destination(color_config), translator) + .sm(source_map.map(|sm| sm as _)) + .short_message(short) + .diagnostic_width(diagnostic_width) + .track_diagnostics(unstable_opts.track_diagnostics) + .theme(if let HumanReadableErrorType::Unicode = kind { + OutputTheme::Unicode + } else { + OutputTheme::Ascii + }) + .ui_testing(unstable_opts.ui_testing), + ) + } else { + Box::new( + HumanEmitter::new(stderr_destination(color_config), translator) + .sm(source_map.map(|sm| sm as _)) + .short_message(short) + .diagnostic_width(diagnostic_width) + .track_diagnostics(unstable_opts.track_diagnostics) + .theme(if let HumanReadableErrorType::Unicode = kind { + OutputTheme::Unicode + } else { + OutputTheme::Ascii + }) + .ui_testing(unstable_opts.ui_testing), + ) + } } ErrorOutputType::Json { pretty, json_rendered, color_config } => { let source_map = source_map.unwrap_or_else(|| {