Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ dependencies = [

[[package]]
name = "annotate-snippets"
version = "0.12.7"
version = "0.12.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47224528f74de27d1d06aad6a5dda4f865b6ebe2e56c538943d746a7270cb67e"
checksum = "025c7edcdffa4ccc5c0905f472a0ae3759378cfbef88ef518a3575e19ae3aebd"
dependencies = [
"anstyle",
"unicode-width 0.2.2",
Expand Down Expand Up @@ -3767,7 +3767,7 @@ dependencies = [
name = "rustc_errors"
version = "0.0.0"
dependencies = [
"annotate-snippets 0.12.7",
"annotate-snippets 0.12.8",
"anstream",
"anstyle",
"derive_setters",
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_errors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2024"

[dependencies]
# tidy-alphabetical-start
annotate-snippets = "0.12.7"
annotate-snippets = "0.12.8"
anstream = "0.6.20"
anstyle = "1.0.13"
derive_setters = "0.1.6"
Expand Down
11 changes: 6 additions & 5 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ 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,
Unicode,
AnnotateSnippet,
Short,
Default { short: bool },
AnnotateSnippet { short: bool, unicode: bool },
}

impl HumanReadableErrorType {
pub fn short(&self) -> bool {
*self == HumanReadableErrorType::Short
match self {
HumanReadableErrorType::Default { short }
| HumanReadableErrorType::AnnotateSnippet { short, .. } => *short,
}
}
}

Expand Down
50 changes: 34 additions & 16 deletions compiler/rustc_errors/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use rustc_span::hygiene::ExpnData;
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,
Expand Down Expand Up @@ -370,29 +371,46 @@ impl Diagnostic {
.insert(0, Diagnostic::from_sub_diagnostic(&diag.emitted_at_sub_diag(), &args, je));
}
let buf = BufWriter(Arc::new(Mutex::new(Vec::new())));
let short = je.json_rendered.short();
let dst: Destination = AutoStream::new(
Box::new(buf.clone()),
match je.color_config.to_color_choice() {
ColorChoice::Auto => ColorChoice::Always,
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);
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)
}
}
Comment on lines +381 to +412
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be nicer to do

Suggested change
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)
}
}
let (short, unicode) = match je.json_rendered {
HumanReadableErrorType::AnnotateSnippet { short, unicode } => (short, unicode),
HumanReadableErrorType::Default { short } => (short, false),
};
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 unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
.emit_diagnostic(diag, registry)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have AnnotateSnippetEmitter and HumanEmitter split here so that users of nightly cargo would have their output rendered by AnnotateSnippetEmitter, since cargo uses the rendered field of the JSON diagnostic messages for its output.


let buf = Arc::try_unwrap(buf.0).unwrap().into_inner().unwrap();
let buf = String::from_utf8(buf).unwrap();

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_errors/src/json/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
Some(sm),
translator,
true, // pretty
HumanReadableErrorType::Short,
HumanReadableErrorType::Default { short: true },
ColorConfig::Never,
);

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
json_rendered: HumanReadableErrorType::Default { short: false },
color_config: ColorConfig::Never,
};

Expand Down
24 changes: 17 additions & 7 deletions compiler/rustc_parse/src/parser/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![allow(rustc::symbol_intern_string_literal)]

use std::assert_matches::assert_matches;
use std::io::prelude::*;
use std::iter::Peekable;
Expand All @@ -12,6 +11,7 @@ use rustc_ast::token::{self, Delimiter, Token};
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
use rustc_ast::{self as ast, PatKind, visit};
use rustc_ast_pretty::pprust::item_to_string;
use rustc_errors::annotate_snippet_emitter_writer::AnnotateSnippetEmitter;
use rustc_errors::emitter::{HumanEmitter, OutputTheme};
use rustc_errors::translation::Translator;
use rustc_errors::{AutoStream, DiagCtxt, MultiSpan, PResult};
Expand Down Expand Up @@ -43,12 +43,22 @@ fn create_test_handler(theme: OutputTheme) -> (DiagCtxt, Arc<SourceMap>, Arc<Mut
let output = Arc::new(Mutex::new(Vec::new()));
let source_map = Arc::new(SourceMap::new(FilePathMapping::empty()));
let translator = Translator::with_fallback_bundle(vec![crate::DEFAULT_LOCALE_RESOURCE], false);
let mut emitter =
HumanEmitter::new(AutoStream::never(Box::new(Shared { data: output.clone() })), translator)
.sm(Some(source_map.clone()))
.diagnostic_width(Some(140));
emitter = emitter.theme(theme);
let dcx = DiagCtxt::new(Box::new(emitter));
let shared: Box<dyn Write + Send> = Box::new(Shared { data: output.clone() });
let auto_stream = AutoStream::never(shared);
let dcx = DiagCtxt::new(match theme {
OutputTheme::Ascii => Box::new(
HumanEmitter::new(auto_stream, translator)
.sm(Some(source_map.clone()))
.diagnostic_width(Some(140))
.theme(theme),
),
OutputTheme::Unicode => Box::new(
AnnotateSnippetEmitter::new(auto_stream, translator)
.sm(Some(source_map.clone()))
.diagnostic_width(Some(140))
.theme(theme),
),
});
(dcx, source_map, output)
}

Expand Down
86 changes: 65 additions & 21 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ pub enum ErrorOutputType {
/// Output meant for the consumption of humans.
#[default]
HumanReadable {
kind: HumanReadableErrorType = HumanReadableErrorType::Default,
kind: HumanReadableErrorType = HumanReadableErrorType::Default { short: false },
color_config: ColorConfig = ColorConfig::Auto,
},
/// Output that's consumed by other tools such as `rustfix` or the `RLS`.
Expand Down Expand Up @@ -2052,8 +2052,16 @@ 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) -> JsonConfig {
let mut json_rendered = HumanReadableErrorType::Default;
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 }
};
let mut json_color = ColorConfig::Never;
let mut json_artifact_notifications = false;
let mut json_unused_externs = JsonUnusedExterns::No;
Expand All @@ -2069,9 +2077,16 @@ pub fn parse_json(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> Json

for sub_option in option.split(',') {
match sub_option {
"diagnostic-short" => json_rendered = HumanReadableErrorType::Short,
"diagnostic-short" => {
json_rendered = if is_nightly_build {
HumanReadableErrorType::AnnotateSnippet { short: true, unicode: false }
} else {
HumanReadableErrorType::Default { short: true }
};
}
"diagnostic-unicode" => {
json_rendered = HumanReadableErrorType::Unicode;
json_rendered =
HumanReadableErrorType::AnnotateSnippet { short: false, unicode: true };
}
"diagnostic-rendered-ansi" => json_color = ColorConfig::Always,
"artifacts" => json_artifact_notifications = true,
Expand Down Expand Up @@ -2101,16 +2116,24 @@ 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 }
};
// 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
// `opt_present` because the latter will panic.
let error_format = if matches.opts_present(&["error-format".to_owned()]) {
match matches.opt_str("error-format").as_deref() {
None | Some("human") => ErrorOutputType::HumanReadable { color_config, .. },
None | Some("human") => {
ErrorOutputType::HumanReadable { color_config, kind: default_kind }
}
Some("human-annotate-rs") => ErrorOutputType::HumanReadable {
kind: HumanReadableErrorType::AnnotateSnippet,
kind: HumanReadableErrorType::AnnotateSnippet { short: false, unicode: false },
color_config,
},
Some("json") => {
Expand All @@ -2119,23 +2142,31 @@ pub fn parse_error_format(
Some("pretty-json") => {
ErrorOutputType::Json { pretty: true, json_rendered, color_config: json_color }
}
Some("short") => {
ErrorOutputType::HumanReadable { kind: HumanReadableErrorType::Short, color_config }
}
Some("short") => ErrorOutputType::HumanReadable {
kind: if is_nightly_build {
HumanReadableErrorType::AnnotateSnippet { short: true, unicode: false }
} else {
HumanReadableErrorType::Default { short: true }
},
color_config,
},
Some("human-unicode") => ErrorOutputType::HumanReadable {
kind: HumanReadableErrorType::Unicode,
kind: HumanReadableErrorType::AnnotateSnippet { short: false, unicode: true },
color_config,
},
Some(arg) => {
early_dcx.set_error_format(ErrorOutputType::HumanReadable { color_config, .. });
early_dcx.set_error_format(ErrorOutputType::HumanReadable {
color_config,
kind: default_kind,
});
early_dcx.early_fatal(format!(
"argument for `--error-format` must be `human`, `human-annotate-rs`, \
`human-unicode`, `json`, `pretty-json` or `short` (instead was `{arg}`)"
))
}
}
} else {
ErrorOutputType::HumanReadable { color_config, .. }
ErrorOutputType::HumanReadable { color_config, kind: default_kind }
};

match error_format {
Expand Down Expand Up @@ -2183,16 +2214,17 @@ pub fn parse_crate_edition(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches
fn check_error_format_stability(
early_dcx: &EarlyDiagCtxt,
unstable_opts: &UnstableOptions,
is_nightly_build: bool,
format: ErrorOutputType,
) {
if unstable_opts.unstable_options {
if unstable_opts.unstable_options || is_nightly_build {
return;
}
let format = match format {
ErrorOutputType::Json { pretty: true, .. } => "pretty-json",
ErrorOutputType::HumanReadable { kind, .. } => match kind {
HumanReadableErrorType::AnnotateSnippet => "human-annotate-rs",
HumanReadableErrorType::Unicode => "human-unicode",
HumanReadableErrorType::AnnotateSnippet { unicode: false, .. } => "human-annotate-rs",
HumanReadableErrorType::AnnotateSnippet { unicode: true, .. } => "human-unicode",
_ => return,
},
_ => return,
Expand Down Expand Up @@ -2613,16 +2645,25 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M

let edition = parse_crate_edition(early_dcx, matches);

let crate_name = matches.opt_str("crate-name");
let unstable_features = UnstableFeatures::from_environment(crate_name.as_deref());
let JsonConfig {
json_rendered,
json_color,
json_artifact_notifications,
json_timings,
json_unused_externs,
json_future_incompat,
} = parse_json(early_dcx, matches);
} = parse_json(early_dcx, matches, unstable_features.is_nightly_build());

let error_format = parse_error_format(early_dcx, matches, color, json_color, json_rendered);
let error_format = parse_error_format(
early_dcx,
matches,
color,
json_color,
json_rendered,
unstable_features.is_nightly_build(),
);

early_dcx.set_error_format(error_format);

Expand All @@ -2643,7 +2684,12 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
early_dcx.early_fatal("--json=timings is unstable and requires using `-Zunstable-options`");
}

check_error_format_stability(early_dcx, &unstable_opts, error_format);
check_error_format_stability(
early_dcx,
&unstable_opts,
unstable_features.is_nightly_build(),
error_format,
);

let output_types = parse_output_types(early_dcx, &unstable_opts, matches);

Expand Down Expand Up @@ -2830,8 +2876,6 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
)
}

let crate_name = matches.opt_str("crate-name");
let unstable_features = UnstableFeatures::from_environment(crate_name.as_deref());
// Parse any `-l` flags, which link to native libraries.
let libs = parse_native_libs(early_dcx, &unstable_opts, unstable_features, matches);

Expand Down
Loading
Loading