Skip to content

Commit 6a4426f

Browse files
committed
feat: Add support for annotate-snippets's no-graphics output format
1 parent fb13423 commit 6a4426f

15 files changed

Lines changed: 201 additions & 19 deletions

File tree

‎compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub struct AnnotateSnippetEmitter {
4545
track_diagnostics: bool,
4646
terminal_url: TerminalUrl,
4747
theme: OutputTheme,
48+
no_graphics: bool,
4849
}
4950

5051
impl Debug for AnnotateSnippetEmitter {
@@ -60,6 +61,7 @@ impl Debug for AnnotateSnippetEmitter {
6061
track_diagnostics,
6162
terminal_url,
6263
theme,
64+
no_graphics,
6365
} = self;
6466

6567
f.debug_struct("AnnotateSnippetEmitter")
@@ -73,6 +75,7 @@ impl Debug for AnnotateSnippetEmitter {
7375
.field("track_diagnostics", track_diagnostics)
7476
.field("terminal_url", terminal_url)
7577
.field("theme", theme)
78+
.field("no_graphics", no_graphics)
7679
.finish()
7780
}
7881
}
@@ -153,6 +156,7 @@ impl AnnotateSnippetEmitter {
153156
track_diagnostics: false,
154157
terminal_url: TerminalUrl::No,
155158
theme: OutputTheme::Ascii,
159+
no_graphics: false,
156160
}
157161
}
158162

@@ -543,6 +547,7 @@ impl AnnotateSnippetEmitter {
543547
.anonymized_snippet_line_numbers(self.ui_testing)
544548
.decor_style(decor_style)
545549
.short_message(self.short_message)
550+
.no_graphics(self.no_graphics)
546551
}
547552

548553
fn pre_style_msgs(&self, msgs: &[(DiagMessage, Style)], args: &DiagArgMap) -> String {

‎compiler/rustc_errors/src/emitter.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use crate::{
3333
/// Describes the way the content of the `rendered` field of the json output is generated
3434
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
3535
pub struct HumanReadableErrorType {
36+
pub no_graphics: bool,
3637
pub short: bool,
3738
pub unicode: bool,
3839
}

‎compiler/rustc_errors/src/json.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ impl Diagnostic {
362362
.ui_testing(je.ui_testing)
363363
.ignored_directories_in_source_blocks(je.ignored_directories_in_source_blocks.clone())
364364
.theme(if je.json_rendered.unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
365+
.no_graphics(je.json_rendered.no_graphics)
365366
.emit_diagnostic(diag);
366367

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

‎compiler/rustc_errors/src/json/tests.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
5151
Box::new(Shared { data: output.clone() }),
5252
Some(sm),
5353
true, // pretty
54-
HumanReadableErrorType { short: true, unicode: false },
54+
HumanReadableErrorType { no_graphics: false, short: true, unicode: false },
5555
ColorConfig::Never,
5656
);
5757

‎compiler/rustc_interface/src/tests.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ fn test_search_paths_tracking_hash_different_order() {
346346
let early_dcx = EarlyDiagCtxt::new(JSON);
347347
const JSON: ErrorOutputType = ErrorOutputType::Json {
348348
pretty: false,
349-
json_rendered: HumanReadableErrorType { short: false, unicode: false },
349+
json_rendered: HumanReadableErrorType { no_graphics: false, short: false, unicode: false },
350350
color_config: ColorConfig::Never,
351351
};
352352

‎compiler/rustc_session/src/config.rs‎

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ pub enum ErrorOutputType {
887887
/// Output meant for the consumption of humans.
888888
#[default]
889889
HumanReadable {
890-
kind: HumanReadableErrorType = HumanReadableErrorType { short: false, unicode: false },
890+
kind: HumanReadableErrorType = HumanReadableErrorType { no_graphics: false, short: false, unicode: false },
891891
color_config: ColorConfig = ColorConfig::Auto,
892892
},
893893
/// Output that's consumed by other tools such as `rustfix` or the `RLS`.
@@ -2337,7 +2337,8 @@ impl JsonUnusedExterns {
23372337
/// The first value returned is how to render JSON diagnostics, and the second
23382338
/// is whether or not artifact notifications are enabled.
23392339
pub fn parse_json(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> JsonConfig {
2340-
let mut json_rendered = HumanReadableErrorType { short: false, unicode: false };
2340+
let mut json_rendered =
2341+
HumanReadableErrorType { no_graphics: false, short: false, unicode: false };
23412342
let mut json_color = ColorConfig::Never;
23422343
let mut json_artifact_notifications = false;
23432344
let mut json_unused_externs = JsonUnusedExterns::No;
@@ -2354,10 +2355,16 @@ pub fn parse_json(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> Json
23542355
for sub_option in option.split(',') {
23552356
match sub_option {
23562357
"diagnostic-short" => {
2357-
json_rendered = HumanReadableErrorType { short: true, unicode: false };
2358+
json_rendered =
2359+
HumanReadableErrorType { no_graphics: false, short: true, unicode: false };
2360+
}
2361+
"diagnostic-no-graphics" => {
2362+
json_rendered =
2363+
HumanReadableErrorType { no_graphics: true, short: false, unicode: false };
23582364
}
23592365
"diagnostic-unicode" => {
2360-
json_rendered = HumanReadableErrorType { short: false, unicode: true };
2366+
json_rendered =
2367+
HumanReadableErrorType { no_graphics: false, short: false, unicode: true };
23612368
}
23622369
"diagnostic-rendered-ansi" => json_color = ColorConfig::Always,
23632370
"artifacts" => json_artifact_notifications = true,
@@ -2388,7 +2395,7 @@ pub fn parse_error_format(
23882395
json_color: ColorConfig,
23892396
json_rendered: HumanReadableErrorType,
23902397
) -> ErrorOutputType {
2391-
let default_kind = HumanReadableErrorType { short: false, unicode: false };
2398+
let default_kind = HumanReadableErrorType { no_graphics: false, short: false, unicode: false };
23922399
// We need the `opts_present` check because the driver will send us Matches
23932400
// with only stable options if no unstable options are used. Since error-format
23942401
// is unstable, it will not be present. We have to use `opts_present` not
@@ -2405,11 +2412,15 @@ pub fn parse_error_format(
24052412
ErrorOutputType::Json { pretty: true, json_rendered, color_config: json_color }
24062413
}
24072414
Some("short") => ErrorOutputType::HumanReadable {
2408-
kind: HumanReadableErrorType { short: true, unicode: false },
2415+
kind: HumanReadableErrorType { no_graphics: false, short: true, unicode: false },
2416+
color_config,
2417+
},
2418+
Some("human-no-graphics") => ErrorOutputType::HumanReadable {
2419+
kind: HumanReadableErrorType { no_graphics: true, short: false, unicode: false },
24092420
color_config,
24102421
},
24112422
Some("human-unicode") => ErrorOutputType::HumanReadable {
2412-
kind: HumanReadableErrorType { short: false, unicode: true },
2423+
kind: HumanReadableErrorType { no_graphics: false, short: false, unicode: true },
24132424
color_config,
24142425
},
24152426
Some(arg) => {
@@ -2418,8 +2429,8 @@ pub fn parse_error_format(
24182429
kind: default_kind,
24192430
});
24202431
early_dcx.early_fatal(format!(
2421-
"argument for `--error-format` must be `human`, `human-unicode`, \
2422-
`json`, `pretty-json` or `short` (instead was `{arg}`)"
2432+
"argument for `--error-format` must be `human`, `human-no-graphics`, \
2433+
`human-unicode`, `json`, `pretty-json` or `short` (instead was `{arg}`)"
24232434
))
24242435
}
24252436
}
@@ -2494,6 +2505,7 @@ fn check_error_format_stability(
24942505
let format = match format {
24952506
ErrorOutputType::Json { pretty: true, .. } => "pretty-json",
24962507
ErrorOutputType::HumanReadable { kind, .. } => match kind {
2508+
HumanReadableErrorType { no_graphics: true, .. } => "human-no-graphics",
24972509
HumanReadableErrorType { unicode: true, .. } => "human-unicode",
24982510
_ => return,
24992511
},

‎compiler/rustc_session/src/session.rs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,7 @@ fn default_emitter(sopts: &config::Options, source_map: Arc<SourceMap>) -> Box<D
13101310

13111311
match sopts.error_format {
13121312
config::ErrorOutputType::HumanReadable { kind, color_config } => match kind {
1313-
HumanReadableErrorType { short, unicode } => {
1313+
HumanReadableErrorType { no_graphics, short, unicode } => {
13141314
let emitter = AnnotateSnippetEmitter::new(stderr_destination(color_config))
13151315
.sm(source_map)
13161316
.short_message(short)
@@ -1321,7 +1321,8 @@ fn default_emitter(sopts: &config::Options, source_map: Arc<SourceMap>) -> Box<D
13211321
.theme(if unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
13221322
.ignored_directories_in_source_blocks(
13231323
sopts.unstable_opts.ignore_directory_in_diagnostics_source_blocks.clone(),
1324-
);
1324+
)
1325+
.no_graphics(no_graphics);
13251326
Box::new(emitter.ui_testing(sopts.unstable_opts.ui_testing))
13261327
}
13271328
},
@@ -1963,10 +1964,11 @@ impl EarlyDiagCtxt {
19631964
fn mk_emitter(output: ErrorOutputType) -> Box<DynEmitter> {
19641965
let emitter: Box<DynEmitter> = match output {
19651966
config::ErrorOutputType::HumanReadable { kind, color_config } => match kind {
1966-
HumanReadableErrorType { short, unicode } => Box::new(
1967+
HumanReadableErrorType { no_graphics, short, unicode } => Box::new(
19671968
AnnotateSnippetEmitter::new(stderr_destination(color_config))
19681969
.theme(if unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
1969-
.short_message(short),
1970+
.short_message(short)
1971+
.no_graphics(no_graphics),
19701972
),
19711973
},
19721974
config::ErrorOutputType::Json { pretty, json_rendered, color_config } => {

‎src/librustdoc/core.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,15 @@ pub(crate) fn new_dcx(
172172
) -> rustc_errors::DiagCtxt {
173173
let emitter: Box<DynEmitter> = match error_format {
174174
ErrorOutputType::HumanReadable { kind, color_config } => match kind {
175-
HumanReadableErrorType { short, unicode } => Box::new(
175+
HumanReadableErrorType { no_graphics, short, unicode } => Box::new(
176176
AnnotateSnippetEmitter::new(stderr_destination(color_config))
177177
.sm(source_map.map(|sm| sm as _))
178178
.short_message(short)
179179
.diagnostic_width(diagnostic_width)
180180
.track_diagnostics(unstable_opts.track_diagnostics)
181181
.theme(if unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
182-
.ui_testing(unstable_opts.ui_testing),
182+
.ui_testing(unstable_opts.ui_testing)
183+
.no_graphics(no_graphics),
183184
),
184185
},
185186
ErrorOutputType::Json { pretty, json_rendered, color_config } => {

‎src/librustdoc/doctest.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,9 +648,12 @@ fn run_test(
648648
},
649649
]);
650650
if let ErrorOutputType::HumanReadable { kind, color_config } = rustdoc_options.error_format {
651-
let short = kind.short();
652-
let unicode = kind == HumanReadableErrorType { unicode: true, short };
651+
let HumanReadableErrorType { no_graphics, short, unicode } = kind;
653652

653+
if no_graphics {
654+
compiler_args
655+
.extend_from_slice(&["--error-format".to_owned(), "human-no-graphics".to_owned()]);
656+
}
654657
if short {
655658
compiler_args.extend_from_slice(&["--error-format".to_owned(), "short".to_owned()]);
656659
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//! Check that compile errors are formatted in the "no-graphics" style
2+
//! when `--error-format=human-no-graphics` is used.
3+
4+
//@ compile-flags: --error-format=human-no-graphics
5+
6+
fn foo(_: u32) {}
7+
8+
fn main() {
9+
foo("Bonjour".to_owned());
10+
let x = 0u32;
11+
x.salut();
12+
}

0 commit comments

Comments
 (0)