Skip to content

Commit 9642289

Browse files
Auto merge of #148188 - Muscraft:annotate-snippets-default-on-nightly, r=<try>
feat: Use annotate-snippets by default on nightly
2 parents 9ea8d67 + 32fab73 commit 9642289

File tree

18 files changed

+216
-157
lines changed

18 files changed

+216
-157
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,16 @@ const DEFAULT_COLUMN_WIDTH: usize = 140;
4747
/// Describes the way the content of the `rendered` field of the json output is generated
4848
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4949
pub enum HumanReadableErrorType {
50-
Default,
51-
Unicode,
52-
AnnotateSnippet,
53-
Short,
50+
Default { short: bool },
51+
AnnotateSnippet { short: bool, unicode: bool },
5452
}
5553

5654
impl HumanReadableErrorType {
5755
pub fn short(&self) -> bool {
58-
*self == HumanReadableErrorType::Short
56+
match self {
57+
HumanReadableErrorType::Default { short }
58+
| HumanReadableErrorType::AnnotateSnippet { short, .. } => *short,
59+
}
5960
}
6061
}
6162

compiler/rustc_errors/src/json.rs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use rustc_span::hygiene::ExpnData;
2525
use rustc_span::source_map::{FilePathMapping, SourceMap};
2626
use serde::Serialize;
2727

28+
use crate::annotate_snippet_emitter_writer::AnnotateSnippetEmitter;
2829
use crate::diagnostic::IsLint;
2930
use crate::emitter::{
3031
ColorConfig, Destination, Emitter, HumanEmitter, HumanReadableErrorType, OutputTheme,
@@ -370,29 +371,46 @@ impl Diagnostic {
370371
.insert(0, Diagnostic::from_sub_diagnostic(&diag.emitted_at_sub_diag(), &args, je));
371372
}
372373
let buf = BufWriter(Arc::new(Mutex::new(Vec::new())));
373-
let short = je.json_rendered.short();
374374
let dst: Destination = AutoStream::new(
375375
Box::new(buf.clone()),
376376
match je.color_config.to_color_choice() {
377377
ColorChoice::Auto => ColorChoice::Always,
378378
choice => choice,
379379
},
380380
);
381-
HumanEmitter::new(dst, je.translator.clone())
382-
.short_message(short)
383-
.sm(je.sm.clone())
384-
.diagnostic_width(je.diagnostic_width)
385-
.macro_backtrace(je.macro_backtrace)
386-
.track_diagnostics(je.track_diagnostics)
387-
.terminal_url(je.terminal_url)
388-
.ui_testing(je.ui_testing)
389-
.ignored_directories_in_source_blocks(je.ignored_directories_in_source_blocks.clone())
390-
.theme(if let HumanReadableErrorType::Unicode = je.json_rendered {
391-
OutputTheme::Unicode
392-
} else {
393-
OutputTheme::Ascii
394-
})
395-
.emit_diagnostic(diag, registry);
381+
match je.json_rendered {
382+
HumanReadableErrorType::AnnotateSnippet { short, unicode } => {
383+
AnnotateSnippetEmitter::new(dst, je.translator.clone())
384+
.short_message(short)
385+
.sm(je.sm.clone())
386+
.diagnostic_width(je.diagnostic_width)
387+
.macro_backtrace(je.macro_backtrace)
388+
.track_diagnostics(je.track_diagnostics)
389+
.terminal_url(je.terminal_url)
390+
.ui_testing(je.ui_testing)
391+
.ignored_directories_in_source_blocks(
392+
je.ignored_directories_in_source_blocks.clone(),
393+
)
394+
.theme(if unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
395+
.emit_diagnostic(diag, registry)
396+
}
397+
HumanReadableErrorType::Default { short } => {
398+
HumanEmitter::new(dst, je.translator.clone())
399+
.short_message(short)
400+
.sm(je.sm.clone())
401+
.diagnostic_width(je.diagnostic_width)
402+
.macro_backtrace(je.macro_backtrace)
403+
.track_diagnostics(je.track_diagnostics)
404+
.terminal_url(je.terminal_url)
405+
.ui_testing(je.ui_testing)
406+
.ignored_directories_in_source_blocks(
407+
je.ignored_directories_in_source_blocks.clone(),
408+
)
409+
.theme(OutputTheme::Ascii)
410+
.emit_diagnostic(diag, registry)
411+
}
412+
}
413+
396414
let buf = Arc::try_unwrap(buf.0).unwrap().into_inner().unwrap();
397415
let buf = String::from_utf8(buf).unwrap();
398416

compiler/rustc_interface/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ fn test_search_paths_tracking_hash_different_order() {
321321
let early_dcx = EarlyDiagCtxt::new(JSON);
322322
const JSON: ErrorOutputType = ErrorOutputType::Json {
323323
pretty: false,
324-
json_rendered: HumanReadableErrorType::Default,
324+
json_rendered: HumanReadableErrorType::Default { short: false },
325325
color_config: ColorConfig::Never,
326326
};
327327

compiler/rustc_parse/src/parser/tests.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![allow(rustc::symbol_intern_string_literal)]
2-
32
use std::assert_matches::assert_matches;
43
use std::io::prelude::*;
54
use std::iter::Peekable;
@@ -12,7 +11,8 @@ use rustc_ast::token::{self, Delimiter, Token};
1211
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
1312
use rustc_ast::{self as ast, PatKind, visit};
1413
use rustc_ast_pretty::pprust::item_to_string;
15-
use rustc_errors::emitter::{HumanEmitter, OutputTheme};
14+
use rustc_errors::annotate_snippet_emitter_writer::AnnotateSnippetEmitter;
15+
use rustc_errors::emitter::{DynamicHumanEmitter, HumanEmitter, OutputTheme};
1616
use rustc_errors::translation::Translator;
1717
use rustc_errors::{AutoStream, DiagCtxt, MultiSpan, PResult};
1818
use rustc_session::parse::ParseSess;
@@ -43,11 +43,17 @@ fn create_test_handler(theme: OutputTheme) -> (DiagCtxt, Arc<SourceMap>, Arc<Mut
4343
let output = Arc::new(Mutex::new(Vec::new()));
4444
let source_map = Arc::new(SourceMap::new(FilePathMapping::empty()));
4545
let translator = Translator::with_fallback_bundle(vec![crate::DEFAULT_LOCALE_RESOURCE], false);
46-
let mut emitter =
47-
HumanEmitter::new(AutoStream::never(Box::new(Shared { data: output.clone() })), translator)
46+
let auto_stream = AutoStream::never(Box::new(Shared { data: output.clone() }));
47+
let emitter = match theme {
48+
OutputTheme::Ascii => HumanEmitter::new(auto_stream, translator)
49+
.sm(Some(source_map.clone()))
50+
.diagnostic_width(Some(140))
51+
.theme(theme),
52+
OutputTheme::Unicode => AnnotateSnippetEmitter::new(auto_stream, translator)
4853
.sm(Some(source_map.clone()))
49-
.diagnostic_width(Some(140));
50-
emitter = emitter.theme(theme);
54+
.diagnostic_width(Some(140))
55+
.theme(theme),
56+
};
5157
let dcx = DiagCtxt::new(Box::new(emitter));
5258
(dcx, source_map, output)
5359
}

compiler/rustc_session/src/config.rs

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ pub enum ErrorOutputType {
837837
/// Output meant for the consumption of humans.
838838
#[default]
839839
HumanReadable {
840-
kind: HumanReadableErrorType = HumanReadableErrorType::Default,
840+
kind: HumanReadableErrorType = HumanReadableErrorType::Default { short: false },
841841
color_config: ColorConfig = ColorConfig::Auto,
842842
},
843843
/// Output that's consumed by other tools such as `rustfix` or the `RLS`.
@@ -2050,8 +2050,16 @@ impl JsonUnusedExterns {
20502050
///
20512051
/// The first value returned is how to render JSON diagnostics, and the second
20522052
/// is whether or not artifact notifications are enabled.
2053-
pub fn parse_json(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> JsonConfig {
2054-
let mut json_rendered = HumanReadableErrorType::Default;
2053+
pub fn parse_json(
2054+
early_dcx: &EarlyDiagCtxt,
2055+
matches: &getopts::Matches,
2056+
is_nightly_build: bool,
2057+
) -> JsonConfig {
2058+
let mut json_rendered = if is_nightly_build {
2059+
HumanReadableErrorType::AnnotateSnippet { short: false, unicode: false }
2060+
} else {
2061+
HumanReadableErrorType::Default { short: false }
2062+
};
20552063
let mut json_color = ColorConfig::Never;
20562064
let mut json_artifact_notifications = false;
20572065
let mut json_unused_externs = JsonUnusedExterns::No;
@@ -2067,9 +2075,16 @@ pub fn parse_json(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> Json
20672075

20682076
for sub_option in option.split(',') {
20692077
match sub_option {
2070-
"diagnostic-short" => json_rendered = HumanReadableErrorType::Short,
2078+
"diagnostic-short" => {
2079+
json_rendered = if is_nightly_build {
2080+
HumanReadableErrorType::AnnotateSnippet { short: true, unicode: false }
2081+
} else {
2082+
HumanReadableErrorType::Default { short: true }
2083+
};
2084+
}
20712085
"diagnostic-unicode" => {
2072-
json_rendered = HumanReadableErrorType::Unicode;
2086+
json_rendered =
2087+
HumanReadableErrorType::AnnotateSnippet { short: false, unicode: true };
20732088
}
20742089
"diagnostic-rendered-ansi" => json_color = ColorConfig::Always,
20752090
"artifacts" => json_artifact_notifications = true,
@@ -2099,16 +2114,24 @@ pub fn parse_error_format(
20992114
color_config: ColorConfig,
21002115
json_color: ColorConfig,
21012116
json_rendered: HumanReadableErrorType,
2117+
is_nightly_build: bool,
21022118
) -> ErrorOutputType {
2119+
let default_kind = if is_nightly_build {
2120+
HumanReadableErrorType::AnnotateSnippet { short: false, unicode: false }
2121+
} else {
2122+
HumanReadableErrorType::Default { short: false }
2123+
};
21032124
// We need the `opts_present` check because the driver will send us Matches
21042125
// with only stable options if no unstable options are used. Since error-format
21052126
// is unstable, it will not be present. We have to use `opts_present` not
21062127
// `opt_present` because the latter will panic.
21072128
let error_format = if matches.opts_present(&["error-format".to_owned()]) {
21082129
match matches.opt_str("error-format").as_deref() {
2109-
None | Some("human") => ErrorOutputType::HumanReadable { color_config, .. },
2130+
None | Some("human") => {
2131+
ErrorOutputType::HumanReadable { color_config, kind: default_kind }
2132+
}
21102133
Some("human-annotate-rs") => ErrorOutputType::HumanReadable {
2111-
kind: HumanReadableErrorType::AnnotateSnippet,
2134+
kind: HumanReadableErrorType::AnnotateSnippet { short: false, unicode: false },
21122135
color_config,
21132136
},
21142137
Some("json") => {
@@ -2117,23 +2140,31 @@ pub fn parse_error_format(
21172140
Some("pretty-json") => {
21182141
ErrorOutputType::Json { pretty: true, json_rendered, color_config: json_color }
21192142
}
2120-
Some("short") => {
2121-
ErrorOutputType::HumanReadable { kind: HumanReadableErrorType::Short, color_config }
2122-
}
2143+
Some("short") => ErrorOutputType::HumanReadable {
2144+
kind: if is_nightly_build {
2145+
HumanReadableErrorType::AnnotateSnippet { short: true, unicode: false }
2146+
} else {
2147+
HumanReadableErrorType::Default { short: true }
2148+
},
2149+
color_config,
2150+
},
21232151
Some("human-unicode") => ErrorOutputType::HumanReadable {
2124-
kind: HumanReadableErrorType::Unicode,
2152+
kind: HumanReadableErrorType::AnnotateSnippet { short: false, unicode: true },
21252153
color_config,
21262154
},
21272155
Some(arg) => {
2128-
early_dcx.set_error_format(ErrorOutputType::HumanReadable { color_config, .. });
2156+
early_dcx.set_error_format(ErrorOutputType::HumanReadable {
2157+
color_config,
2158+
kind: default_kind,
2159+
});
21292160
early_dcx.early_fatal(format!(
21302161
"argument for `--error-format` must be `human`, `human-annotate-rs`, \
21312162
`human-unicode`, `json`, `pretty-json` or `short` (instead was `{arg}`)"
21322163
))
21332164
}
21342165
}
21352166
} else {
2136-
ErrorOutputType::HumanReadable { color_config, .. }
2167+
ErrorOutputType::HumanReadable { color_config, kind: default_kind }
21372168
};
21382169

21392170
match error_format {
@@ -2181,16 +2212,17 @@ pub fn parse_crate_edition(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches
21812212
fn check_error_format_stability(
21822213
early_dcx: &EarlyDiagCtxt,
21832214
unstable_opts: &UnstableOptions,
2215+
is_nightly_build: bool,
21842216
format: ErrorOutputType,
21852217
) {
2186-
if unstable_opts.unstable_options {
2218+
if unstable_opts.unstable_options || is_nightly_build {
21872219
return;
21882220
}
21892221
let format = match format {
21902222
ErrorOutputType::Json { pretty: true, .. } => "pretty-json",
21912223
ErrorOutputType::HumanReadable { kind, .. } => match kind {
2192-
HumanReadableErrorType::AnnotateSnippet => "human-annotate-rs",
2193-
HumanReadableErrorType::Unicode => "human-unicode",
2224+
HumanReadableErrorType::AnnotateSnippet { unicode: false, .. } => "human-annotate-rs",
2225+
HumanReadableErrorType::AnnotateSnippet { unicode: true, .. } => "human-unicode",
21942226
_ => return,
21952227
},
21962228
_ => return,
@@ -2611,16 +2643,25 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26112643

26122644
let edition = parse_crate_edition(early_dcx, matches);
26132645

2646+
let crate_name = matches.opt_str("crate-name");
2647+
let unstable_features = UnstableFeatures::from_environment(crate_name.as_deref());
26142648
let JsonConfig {
26152649
json_rendered,
26162650
json_color,
26172651
json_artifact_notifications,
26182652
json_timings,
26192653
json_unused_externs,
26202654
json_future_incompat,
2621-
} = parse_json(early_dcx, matches);
2655+
} = parse_json(early_dcx, matches, unstable_features.is_nightly_build());
26222656

2623-
let error_format = parse_error_format(early_dcx, matches, color, json_color, json_rendered);
2657+
let error_format = parse_error_format(
2658+
early_dcx,
2659+
matches,
2660+
color,
2661+
json_color,
2662+
json_rendered,
2663+
unstable_features.is_nightly_build(),
2664+
);
26242665

26252666
early_dcx.set_error_format(error_format);
26262667

@@ -2641,7 +2682,12 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26412682
early_dcx.early_fatal("--json=timings is unstable and requires using `-Zunstable-options`");
26422683
}
26432684

2644-
check_error_format_stability(early_dcx, &unstable_opts, error_format);
2685+
check_error_format_stability(
2686+
early_dcx,
2687+
&unstable_opts,
2688+
unstable_features.is_nightly_build(),
2689+
error_format,
2690+
);
26452691

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

@@ -2828,8 +2874,6 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
28282874
)
28292875
}
28302876

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

0 commit comments

Comments
 (0)