stabilize annotate-snippet as default formatter

This commit is contained in:
Kivooeo 2025-12-16 10:42:14 +00:00
parent 8188f6c808
commit 6d6068f6c5
9 changed files with 42 additions and 133 deletions

View file

@ -46,17 +46,14 @@ const DEFAULT_COLUMN_WIDTH: usize = 140;
/// Describes the way the content of the `rendered` field of the json output is generated
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum HumanReadableErrorType {
Default { short: bool },
AnnotateSnippet { short: bool, unicode: bool },
pub struct HumanReadableErrorType {
pub short: bool,
pub unicode: bool,
}
impl HumanReadableErrorType {
pub fn short(&self) -> bool {
match self {
HumanReadableErrorType::Default { short }
| HumanReadableErrorType::AnnotateSnippet { short, .. } => *short,
}
self.short
}
}
@ -607,7 +604,7 @@ pub enum OutputTheme {
Unicode,
}
/// Handles the writing of `HumanReadableErrorType::Default` and `HumanReadableErrorType::Short`
/// Handles the writing of `HumanReadableErrorType`
#[derive(Setters)]
pub struct HumanEmitter {
#[setters(skip)]

View file

@ -28,8 +28,8 @@ use serde::Serialize;
use crate::annotate_snippet_emitter_writer::AnnotateSnippetEmitter;
use crate::diagnostic::IsLint;
use crate::emitter::{
ColorConfig, Destination, Emitter, HumanEmitter, HumanReadableErrorType, OutputTheme,
TimingEvent, should_show_source_code,
ColorConfig, Destination, Emitter, HumanReadableErrorType, OutputTheme, TimingEvent,
should_show_source_code,
};
use crate::registry::Registry;
use crate::timings::{TimingRecord, TimingSection};
@ -378,38 +378,17 @@ impl Diagnostic {
choice => choice,
},
);
match je.json_rendered {
HumanReadableErrorType::AnnotateSnippet { short, unicode } => {
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 unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
.emit_diagnostic(diag, registry)
}
HumanReadableErrorType::Default { short } => {
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(OutputTheme::Ascii)
.emit_diagnostic(diag, registry)
}
}
AnnotateSnippetEmitter::new(dst, je.translator.clone())
.short_message(je.json_rendered.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 je.json_rendered.unicode { 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();

View file

@ -54,7 +54,7 @@ fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
Some(sm),
translator,
true, // pretty
HumanReadableErrorType::Default { short: true },
HumanReadableErrorType { short: true, unicode: false },
ColorConfig::Never,
);

View file

@ -321,7 +321,7 @@ fn test_search_paths_tracking_hash_different_order() {
let early_dcx = EarlyDiagCtxt::new(JSON);
const JSON: ErrorOutputType = ErrorOutputType::Json {
pretty: false,
json_rendered: HumanReadableErrorType::Default { short: false },
json_rendered: HumanReadableErrorType { short: false, unicode: false },
color_config: ColorConfig::Never,
};

View file

@ -806,7 +806,7 @@ pub enum ErrorOutputType {
/// Output meant for the consumption of humans.
#[default]
HumanReadable {
kind: HumanReadableErrorType = HumanReadableErrorType::Default { short: false },
kind: HumanReadableErrorType = HumanReadableErrorType { short: false, unicode: false },
color_config: ColorConfig = ColorConfig::Auto,
},
/// Output that's consumed by other tools such as `rustfix` or the `RLS`.
@ -1965,16 +1965,8 @@ impl JsonUnusedExterns {
///
/// The first value returned is how to render JSON diagnostics, and the second
/// is whether or not artifact notifications are enabled.
pub fn parse_json(
early_dcx: &EarlyDiagCtxt,
matches: &getopts::Matches,
is_nightly_build: bool,
) -> JsonConfig {
let mut json_rendered = if is_nightly_build {
HumanReadableErrorType::AnnotateSnippet { short: false, unicode: false }
} else {
HumanReadableErrorType::Default { short: false }
};
pub fn parse_json(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> JsonConfig {
let mut json_rendered = HumanReadableErrorType { short: false, unicode: false };
let mut json_color = ColorConfig::Never;
let mut json_artifact_notifications = false;
let mut json_unused_externs = JsonUnusedExterns::No;
@ -1991,15 +1983,10 @@ pub fn parse_json(
for sub_option in option.split(',') {
match sub_option {
"diagnostic-short" => {
json_rendered = if is_nightly_build {
HumanReadableErrorType::AnnotateSnippet { short: true, unicode: false }
} else {
HumanReadableErrorType::Default { short: true }
};
json_rendered = HumanReadableErrorType { short: true, unicode: false };
}
"diagnostic-unicode" => {
json_rendered =
HumanReadableErrorType::AnnotateSnippet { short: false, unicode: true };
json_rendered = HumanReadableErrorType { short: false, unicode: true };
}
"diagnostic-rendered-ansi" => json_color = ColorConfig::Always,
"artifacts" => json_artifact_notifications = true,
@ -2029,13 +2016,8 @@ pub fn parse_error_format(
color_config: ColorConfig,
json_color: ColorConfig,
json_rendered: HumanReadableErrorType,
is_nightly_build: bool,
) -> ErrorOutputType {
let default_kind = if is_nightly_build {
HumanReadableErrorType::AnnotateSnippet { short: false, unicode: false }
} else {
HumanReadableErrorType::Default { short: false }
};
let default_kind = HumanReadableErrorType { short: false, unicode: false };
// We need the `opts_present` check because the driver will send us Matches
// with only stable options if no unstable options are used. Since error-format
// is unstable, it will not be present. We have to use `opts_present` not
@ -2046,7 +2028,7 @@ pub fn parse_error_format(
ErrorOutputType::HumanReadable { color_config, kind: default_kind }
}
Some("human-annotate-rs") => ErrorOutputType::HumanReadable {
kind: HumanReadableErrorType::AnnotateSnippet { short: false, unicode: false },
kind: HumanReadableErrorType { short: false, unicode: false },
color_config,
},
Some("json") => {
@ -2056,15 +2038,11 @@ pub fn parse_error_format(
ErrorOutputType::Json { pretty: true, json_rendered, color_config: json_color }
}
Some("short") => ErrorOutputType::HumanReadable {
kind: if is_nightly_build {
HumanReadableErrorType::AnnotateSnippet { short: true, unicode: false }
} else {
HumanReadableErrorType::Default { short: true }
},
kind: HumanReadableErrorType { short: true, unicode: false },
color_config,
},
Some("human-unicode") => ErrorOutputType::HumanReadable {
kind: HumanReadableErrorType::AnnotateSnippet { short: false, unicode: true },
kind: HumanReadableErrorType { short: false, unicode: true },
color_config,
},
Some(arg) => {
@ -2136,8 +2114,8 @@ fn check_error_format_stability(
let format = match format {
ErrorOutputType::Json { pretty: true, .. } => "pretty-json",
ErrorOutputType::HumanReadable { kind, .. } => match kind {
HumanReadableErrorType::AnnotateSnippet { unicode: false, .. } => "human-annotate-rs",
HumanReadableErrorType::AnnotateSnippet { unicode: true, .. } => "human-unicode",
HumanReadableErrorType { unicode: false, .. } => "human-annotate-rs",
HumanReadableErrorType { unicode: true, .. } => "human-unicode",
_ => return,
},
_ => return,
@ -2465,16 +2443,9 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
json_timings,
json_unused_externs,
json_future_incompat,
} = parse_json(early_dcx, matches, unstable_features.is_nightly_build());
} = parse_json(early_dcx, matches);
let error_format = parse_error_format(
early_dcx,
matches,
color,
json_color,
json_rendered,
unstable_features.is_nightly_build(),
);
let error_format = parse_error_format(early_dcx, matches, color, json_color, json_rendered);
early_dcx.set_error_format(error_format);

View file

@ -13,9 +13,7 @@ use rustc_data_structures::profiling::{SelfProfiler, SelfProfilerRef};
use rustc_data_structures::sync::{DynSend, DynSync, Lock, MappedReadGuard, ReadGuard, RwLock};
use rustc_errors::annotate_snippet_emitter_writer::AnnotateSnippetEmitter;
use rustc_errors::codes::*;
use rustc_errors::emitter::{
DynEmitter, HumanEmitter, HumanReadableErrorType, OutputTheme, stderr_destination,
};
use rustc_errors::emitter::{DynEmitter, HumanReadableErrorType, OutputTheme, stderr_destination};
use rustc_errors::json::JsonEmitter;
use rustc_errors::timings::TimingSectionHandler;
use rustc_errors::translation::Translator;
@ -920,7 +918,7 @@ fn default_emitter(
match sopts.error_format {
config::ErrorOutputType::HumanReadable { kind, color_config } => match kind {
HumanReadableErrorType::AnnotateSnippet { short, unicode } => {
HumanReadableErrorType { short, unicode } => {
let emitter =
AnnotateSnippetEmitter::new(stderr_destination(color_config), translator)
.sm(source_map)
@ -938,20 +936,6 @@ fn default_emitter(
);
Box::new(emitter.ui_testing(sopts.unstable_opts.ui_testing))
}
HumanReadableErrorType::Default { short } => {
let emitter = HumanEmitter::new(stderr_destination(color_config), translator)
.sm(source_map)
.short_message(short)
.diagnostic_width(sopts.diagnostic_width)
.macro_backtrace(macro_backtrace)
.track_diagnostics(track_diagnostics)
.terminal_url(terminal_url)
.theme(OutputTheme::Ascii)
.ignored_directories_in_source_blocks(
sopts.unstable_opts.ignore_directory_in_diagnostics_source_blocks.clone(),
);
Box::new(emitter.ui_testing(sopts.unstable_opts.ui_testing))
}
},
config::ErrorOutputType::Json { pretty, json_rendered, color_config } => Box::new(
JsonEmitter::new(
@ -1460,16 +1444,11 @@ fn mk_emitter(output: ErrorOutputType) -> Box<DynEmitter> {
Translator::with_fallback_bundle(vec![rustc_errors::DEFAULT_LOCALE_RESOURCE], false);
let emitter: Box<DynEmitter> = match output {
config::ErrorOutputType::HumanReadable { kind, color_config } => match kind {
HumanReadableErrorType::AnnotateSnippet { short, unicode } => Box::new(
HumanReadableErrorType { short, unicode } => Box::new(
AnnotateSnippetEmitter::new(stderr_destination(color_config), translator)
.theme(if unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
.short_message(short),
),
HumanReadableErrorType::Default { short } => Box::new(
HumanEmitter::new(stderr_destination(color_config), translator)
.theme(OutputTheme::Ascii)
.short_message(short),
),
},
config::ErrorOutputType::Json { pretty, json_rendered, color_config } => {
Box::new(JsonEmitter::new(

View file

@ -404,15 +404,9 @@ impl Options {
let unstable_features =
rustc_feature::UnstableFeatures::from_environment(crate_name.as_deref());
let config::JsonConfig { json_rendered, json_unused_externs, json_color, .. } =
config::parse_json(early_dcx, matches, unstable_features.is_nightly_build());
let error_format = config::parse_error_format(
early_dcx,
matches,
color,
json_color,
json_rendered,
unstable_features.is_nightly_build(),
);
config::parse_json(early_dcx, matches);
let error_format =
config::parse_error_format(early_dcx, matches, color, json_color, json_rendered);
let diagnostic_width = matches.opt_get("diagnostic-width").unwrap_or_default();
let mut target_modifiers = BTreeMap::<OptionsTargetModifiers, String>::new();

View file

@ -7,9 +7,7 @@ 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,
};
use rustc_errors::emitter::{DynEmitter, HumanReadableErrorType, OutputTheme, stderr_destination};
use rustc_errors::json::JsonEmitter;
use rustc_feature::UnstableFeatures;
use rustc_hir::def::Res;
@ -162,7 +160,7 @@ pub(crate) fn new_dcx(
let translator = rustc_driver::default_translator();
let emitter: Box<DynEmitter> = match error_format {
ErrorOutputType::HumanReadable { kind, color_config } => match kind {
HumanReadableErrorType::AnnotateSnippet { short, unicode } => Box::new(
HumanReadableErrorType { short, unicode } => Box::new(
AnnotateSnippetEmitter::new(stderr_destination(color_config), translator)
.sm(source_map.map(|sm| sm as _))
.short_message(short)
@ -171,15 +169,6 @@ pub(crate) fn new_dcx(
.theme(if unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
.ui_testing(unstable_opts.ui_testing),
),
HumanReadableErrorType::Default { 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(OutputTheme::Ascii)
.ui_testing(unstable_opts.ui_testing),
),
},
ErrorOutputType::Json { pretty, json_rendered, color_config } => {
let source_map = source_map.unwrap_or_else(|| {

View file

@ -625,7 +625,7 @@ fn run_test(
]);
if let ErrorOutputType::HumanReadable { kind, color_config } = rustdoc_options.error_format {
let short = kind.short();
let unicode = kind == HumanReadableErrorType::AnnotateSnippet { unicode: true, short };
let unicode = kind == HumanReadableErrorType { unicode: true, short };
if short {
compiler_args.extend_from_slice(&["--error-format".to_owned(), "short".to_owned()]);