Skip to content

Commit 39b2137

Browse files
committed
Rename colorful-json to json-rendered and make it a selection instead of a bool
1 parent 0a842e8 commit 39b2137

File tree

8 files changed

+164
-123
lines changed

8 files changed

+164
-123
lines changed

src/librustc/session/config.rs

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use syntax::parse::token;
1919
use syntax::parse;
2020
use syntax::symbol::Symbol;
2121
use syntax::feature_gate::UnstableFeatures;
22+
use errors::emitter::HumanReadableErrorType;
2223

2324
use errors::{ColorConfig, FatalError, Handler};
2425

@@ -204,19 +205,18 @@ impl OutputType {
204205

205206
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
206207
pub enum ErrorOutputType {
207-
HumanReadable(ColorConfig),
208+
HumanReadable(HumanReadableErrorType),
208209
Json {
209210
/// Render the json in a human readable way (with indents and newlines)
210211
pretty: bool,
211-
/// The `rendered` field with the command line diagnostics include color codes
212-
colorful_rendered: bool,
212+
/// The way the `rendered` field is created
213+
json_rendered: HumanReadableErrorType,
213214
},
214-
Short(ColorConfig),
215215
}
216216

217217
impl Default for ErrorOutputType {
218218
fn default() -> ErrorOutputType {
219-
ErrorOutputType::HumanReadable(ColorConfig::Auto)
219+
ErrorOutputType::HumanReadable(HumanReadableErrorType::Default(ColorConfig::Auto))
220220
}
221221
}
222222

@@ -1350,8 +1350,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
13501350
"print some statistics about AST and HIR"),
13511351
always_encode_mir: bool = (false, parse_bool, [TRACKED],
13521352
"encode MIR of all functions into the crate metadata"),
1353-
colorful_json: bool = (false, parse_bool, [UNTRACKED],
1354-
"encode color codes in the `rendered` field of json diagnostics"),
1353+
json_rendered: Option<String> = (None, parse_opt_string, [UNTRACKED],
1354+
"describes how to render the `rendered` field of json diagnostics"),
13551355
unleash_the_miri_inside_of_you: bool = (false, parse_bool, [TRACKED],
13561356
"take the breaks off const evaluation. NOTE: this is unsound"),
13571357
osx_rpath_install_name: bool = (false, parse_bool, [TRACKED],
@@ -1807,9 +1807,9 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
18071807
),
18081808
opt::opt(
18091809
"",
1810-
"colorful-json",
1811-
"Emit ansi color codes to the `rendered` field of json diagnostics",
1812-
"TYPE",
1810+
"json-rendered",
1811+
"Choose `rendered` field of json diagnostics render scheme",
1812+
"plain|termcolor",
18131813
),
18141814
opt::opt_s(
18151815
"",
@@ -1951,22 +1951,32 @@ pub fn build_session_options_and_crate_config(
19511951
)
19521952
}
19531953

1954-
let colorful_rendered = matches.opt_present("colorful-json");
1954+
let json_rendered = matches.opt_str("json-rendered").and_then(|s| match s.as_str() {
1955+
"plain" => None,
1956+
"termcolor" => Some(HumanReadableErrorType::Default(ColorConfig::Always)),
1957+
_ => early_error(
1958+
ErrorOutputType::default(),
1959+
&format!(
1960+
"argument for --json-rendered must be `plain` or `termcolor` (instead was `{}`)",
1961+
s,
1962+
),
1963+
),
1964+
}).unwrap_or(HumanReadableErrorType::Default(ColorConfig::Never));
19551965

19561966
// We need the opts_present check because the driver will send us Matches
19571967
// with only stable options if no unstable options are used. Since error-format
19581968
// is unstable, it will not be present. We have to use opts_present not
19591969
// opt_present because the latter will panic.
19601970
let error_format = if matches.opts_present(&["error-format".to_owned()]) {
19611971
match matches.opt_str("error-format").as_ref().map(|s| &s[..]) {
1962-
Some("human") => ErrorOutputType::HumanReadable(color),
1963-
Some("json") => ErrorOutputType::Json { pretty: false, colorful_rendered },
1964-
Some("pretty-json") => ErrorOutputType::Json { pretty: true, colorful_rendered },
1965-
Some("short") => ErrorOutputType::Short(color),
1966-
None => ErrorOutputType::HumanReadable(color),
1972+
None |
1973+
Some("human") => ErrorOutputType::HumanReadable(HumanReadableErrorType::Default(color)),
1974+
Some("json") => ErrorOutputType::Json { pretty: false, json_rendered },
1975+
Some("pretty-json") => ErrorOutputType::Json { pretty: true, json_rendered },
1976+
Some("short") => ErrorOutputType::HumanReadable(HumanReadableErrorType::Short(color)),
19671977

19681978
Some(arg) => early_error(
1969-
ErrorOutputType::HumanReadable(color),
1979+
ErrorOutputType::HumanReadable(HumanReadableErrorType::Default(color)),
19701980
&format!(
19711981
"argument for --error-format must be `human`, `json` or \
19721982
`short` (instead was `{}`)",
@@ -1975,7 +1985,7 @@ pub fn build_session_options_and_crate_config(
19751985
),
19761986
}
19771987
} else {
1978-
ErrorOutputType::HumanReadable(color)
1988+
ErrorOutputType::HumanReadable(HumanReadableErrorType::Default(color))
19791989
};
19801990

19811991
let unparsed_crate_types = matches.opt_strs("crate-type");
@@ -1988,12 +1998,12 @@ pub fn build_session_options_and_crate_config(
19881998
let mut debugging_opts = build_debugging_options(matches, error_format);
19891999

19902000
if !debugging_opts.unstable_options {
1991-
if colorful_rendered {
1992-
early_error(error_format, "--colorful-json=true is unstable");
2001+
if matches.opt_str("json-rendered").is_some() {
2002+
early_error(error_format, "`--json-rendered=x` is unstable");
19932003
}
1994-
if let ErrorOutputType::Json { pretty: true, .. } = error_format {
2004+
if let ErrorOutputType::Json { pretty: true, json_rendered } = error_format {
19952005
early_error(
1996-
ErrorOutputType::Json { pretty: false, colorful_rendered: false },
2006+
ErrorOutputType::Json { pretty: false, json_rendered },
19972007
"--error-format=pretty-json is unstable",
19982008
);
19992009
}
@@ -2902,7 +2912,7 @@ mod tests {
29022912

29032913
const JSON: super::ErrorOutputType = super::ErrorOutputType::Json {
29042914
pretty: false,
2905-
colorful_rendered: false,
2915+
json_rendered: super::HumanReadableErrorType::Default(super::ColorConfig::Never),
29062916
};
29072917

29082918
// Reference

src/librustc/session/mod.rs

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,44 +1037,42 @@ fn default_emitter(
10371037
emitter_dest: Option<Box<dyn Write + Send>>,
10381038
) -> Box<dyn Emitter + sync::Send> {
10391039
match (sopts.error_format, emitter_dest) {
1040-
(config::ErrorOutputType::HumanReadable(color_config), None) => Box::new(
1041-
EmitterWriter::stderr(
1042-
color_config,
1043-
Some(source_map.clone()),
1044-
false,
1045-
sopts.debugging_opts.teach,
1046-
).ui_testing(sopts.debugging_opts.ui_testing),
1047-
),
1048-
(config::ErrorOutputType::HumanReadable(_), Some(dst)) => Box::new(
1049-
EmitterWriter::new(
1050-
dst, Some(source_map.clone()), false, false, sopts.debugging_opts.colorful_json,
1051-
).ui_testing(sopts.debugging_opts.ui_testing),
1052-
),
1053-
(config::ErrorOutputType::Json { pretty, colorful_rendered }, None) => Box::new(
1040+
(config::ErrorOutputType::HumanReadable(kind), dst) => {
1041+
let (short, color_config) = kind.unzip();
1042+
let emitter = match dst {
1043+
None => EmitterWriter::stderr(
1044+
color_config,
1045+
Some(source_map.clone()),
1046+
short,
1047+
sopts.debugging_opts.teach,
1048+
),
1049+
Some(dst) => EmitterWriter::new(
1050+
dst,
1051+
Some(source_map.clone()),
1052+
short,
1053+
false,
1054+
color_config.suggests_using_colors(),
1055+
),
1056+
};
1057+
Box::new(emitter.ui_testing(sopts.debugging_opts.ui_testing))
1058+
},
1059+
(config::ErrorOutputType::Json { pretty, json_rendered }, None) => Box::new(
10541060
JsonEmitter::stderr(
10551061
Some(registry),
10561062
source_map.clone(),
10571063
pretty,
1058-
colorful_rendered,
1064+
json_rendered,
10591065
).ui_testing(sopts.debugging_opts.ui_testing),
10601066
),
1061-
(config::ErrorOutputType::Json { pretty, colorful_rendered }, Some(dst)) => Box::new(
1067+
(config::ErrorOutputType::Json { pretty, json_rendered }, Some(dst)) => Box::new(
10621068
JsonEmitter::new(
10631069
dst,
10641070
Some(registry),
10651071
source_map.clone(),
10661072
pretty,
1067-
colorful_rendered,
1073+
json_rendered,
10681074
).ui_testing(sopts.debugging_opts.ui_testing),
10691075
),
1070-
(config::ErrorOutputType::Short(color_config), None) => Box::new(
1071-
EmitterWriter::stderr(color_config, Some(source_map.clone()), true, false),
1072-
),
1073-
(config::ErrorOutputType::Short(_), Some(dst)) => {
1074-
Box::new(EmitterWriter::new(
1075-
dst, Some(source_map.clone()), true, false, sopts.debugging_opts.colorful_json,
1076-
))
1077-
}
10781076
}
10791077
}
10801078

@@ -1319,14 +1317,12 @@ pub enum IncrCompSession {
13191317

13201318
pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
13211319
let emitter: Box<dyn Emitter + sync::Send> = match output {
1322-
config::ErrorOutputType::HumanReadable(color_config) => {
1323-
Box::new(EmitterWriter::stderr(color_config, None, false, false))
1324-
}
1325-
config::ErrorOutputType::Json { pretty, colorful_rendered } =>
1326-
Box::new(JsonEmitter::basic(pretty, colorful_rendered)),
1327-
config::ErrorOutputType::Short(color_config) => {
1328-
Box::new(EmitterWriter::stderr(color_config, None, true, false))
1320+
config::ErrorOutputType::HumanReadable(kind) => {
1321+
let (short, color_config) = kind.unzip();
1322+
Box::new(EmitterWriter::stderr(color_config, None, short, false))
13291323
}
1324+
config::ErrorOutputType::Json { pretty, json_rendered } =>
1325+
Box::new(JsonEmitter::basic(pretty, json_rendered)),
13301326
};
13311327
let handler = errors::Handler::with_emitter(true, None, emitter);
13321328
handler.emit(&MultiSpan::new(), msg, errors::Level::Fatal);
@@ -1335,14 +1331,12 @@ pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
13351331

13361332
pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
13371333
let emitter: Box<dyn Emitter + sync::Send> = match output {
1338-
config::ErrorOutputType::HumanReadable(color_config) => {
1339-
Box::new(EmitterWriter::stderr(color_config, None, false, false))
1340-
}
1341-
config::ErrorOutputType::Json { pretty, colorful_rendered } =>
1342-
Box::new(JsonEmitter::basic(pretty, colorful_rendered)),
1343-
config::ErrorOutputType::Short(color_config) => {
1344-
Box::new(EmitterWriter::stderr(color_config, None, true, false))
1334+
config::ErrorOutputType::HumanReadable(kind) => {
1335+
let (short, color_config) = kind.unzip();
1336+
Box::new(EmitterWriter::stderr(color_config, None, short, false))
13451337
}
1338+
config::ErrorOutputType::Json { pretty, json_rendered } =>
1339+
Box::new(JsonEmitter::basic(pretty, json_rendered)),
13461340
};
13471341
let handler = errors::Handler::with_emitter(true, None, emitter);
13481342
handler.emit(&MultiSpan::new(), msg, errors::Level::Warning);

src/librustc_errors/emitter.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,32 @@ use std::cmp::{min, Reverse};
1919
use termcolor::{StandardStream, ColorChoice, ColorSpec, BufferWriter, Ansi};
2020
use termcolor::{WriteColor, Color, Buffer};
2121

22+
/// Describes the way the content of the `rendered` field of the json output is generated
23+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
24+
pub enum HumanReadableErrorType {
25+
Default(ColorConfig),
26+
Short(ColorConfig),
27+
}
28+
29+
impl HumanReadableErrorType {
30+
/// Returns a (`short`, `color`) tuple
31+
pub fn unzip(self) -> (bool, ColorConfig) {
32+
match self {
33+
HumanReadableErrorType::Default(cc) => (false, cc),
34+
HumanReadableErrorType::Short(cc) => (true, cc),
35+
}
36+
}
37+
pub fn new_emitter(
38+
self,
39+
dst: Box<dyn Write + Send>,
40+
source_map: Option<Lrc<SourceMapperDyn>>,
41+
teach: bool,
42+
) -> EmitterWriter {
43+
let (short, color_config) = self.unzip();
44+
EmitterWriter::new(dst, source_map, short, teach, color_config.suggests_using_colors())
45+
}
46+
}
47+
2248
const ANONYMIZED_LINE_NUM: &str = "LL";
2349

2450
/// Emitter trait for emitting errors.
@@ -104,8 +130,8 @@ pub enum ColorConfig {
104130
}
105131

106132
impl ColorConfig {
107-
fn to_color_choice(&self) -> ColorChoice {
108-
match *self {
133+
fn to_color_choice(self) -> ColorChoice {
134+
match self {
109135
ColorConfig::Always => {
110136
if atty::is(atty::Stream::Stderr) {
111137
ColorChoice::Always
@@ -120,6 +146,14 @@ impl ColorConfig {
120146
ColorConfig::Auto => ColorChoice::Never,
121147
}
122148
}
149+
pub fn suggests_using_colors(self) -> bool {
150+
match self {
151+
| ColorConfig::Always
152+
| ColorConfig::Auto
153+
=> true,
154+
ColorConfig::Never => false,
155+
}
156+
}
123157
}
124158

125159
pub struct EmitterWriter {
@@ -1540,6 +1574,7 @@ fn emit_to_destination(rendered_buffer: &[Vec<StyledString>],
15401574
pub enum Destination {
15411575
Terminal(StandardStream),
15421576
Buffered(BufferWriter),
1577+
// The bool denotes whether we should be emitting ansi color codes or not
15431578
Raw(Box<(dyn Write + Send)>, bool),
15441579
}
15451580

src/librustdoc/config.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt;
33
use std::path::PathBuf;
44

55
use errors;
6-
use errors::emitter::ColorConfig;
6+
use errors::emitter::{ColorConfig, HumanReadableErrorType};
77
use getopts;
88
use rustc::lint::Level;
99
use rustc::session::early_error;
@@ -256,11 +256,17 @@ impl Options {
256256
};
257257
// FIXME: deduplicate this code from the identical code in librustc/session/config.rs
258258
let error_format = match matches.opt_str("error-format").as_ref().map(|s| &s[..]) {
259-
Some("human") => ErrorOutputType::HumanReadable(color),
260-
Some("json") => ErrorOutputType::Json { pretty: false, colorful_rendered: false },
261-
Some("pretty-json") => ErrorOutputType::Json { pretty: true, colorful_rendered: false },
262-
Some("short") => ErrorOutputType::Short(color),
263-
None => ErrorOutputType::HumanReadable(color),
259+
None |
260+
Some("human") => ErrorOutputType::HumanReadable(HumanReadableErrorType::Default(color)),
261+
Some("json") => ErrorOutputType::Json {
262+
pretty: false,
263+
json_rendered: HumanReadableErrorType::Default(color),
264+
},
265+
Some("pretty-json") => ErrorOutputType::Json {
266+
pretty: true,
267+
json_rendered: HumanReadableErrorType::Default(color),
268+
},
269+
Some("short") => ErrorOutputType::HumanReadable(HumanReadableErrorType::Short(color)),
264270
Some(arg) => {
265271
early_error(ErrorOutputType::default(),
266272
&format!("argument for --error-format must be `human`, `json` or \

src/librustdoc/core.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -299,33 +299,29 @@ pub fn new_handler(error_format: ErrorOutputType,
299299
// stick to the defaults
300300
let sessopts = Options::default();
301301
let emitter: Box<dyn Emitter + sync::Send> = match error_format {
302-
ErrorOutputType::HumanReadable(color_config) => Box::new(
303-
EmitterWriter::stderr(
304-
color_config,
305-
source_map.map(|cm| cm as _),
306-
false,
307-
sessopts.debugging_opts.teach,
308-
).ui_testing(ui_testing)
309-
),
310-
ErrorOutputType::Json { pretty, colorful_rendered } => {
302+
ErrorOutputType::HumanReadable(kind) => {
303+
let (short, color_config) = kind.unzip();
304+
Box::new(
305+
EmitterWriter::stderr(
306+
color_config,
307+
source_map.map(|cm| cm as _),
308+
short,
309+
sessopts.debugging_opts.teach,
310+
).ui_testing(ui_testing)
311+
)
312+
},
313+
ErrorOutputType::Json { pretty, json_rendered } => {
311314
let source_map = source_map.unwrap_or_else(
312315
|| Lrc::new(source_map::SourceMap::new(sessopts.file_path_mapping())));
313316
Box::new(
314317
JsonEmitter::stderr(
315318
None,
316319
source_map,
317320
pretty,
318-
colorful_rendered,
321+
json_rendered,
319322
).ui_testing(ui_testing)
320323
)
321324
},
322-
ErrorOutputType::Short(color_config) => Box::new(
323-
EmitterWriter::stderr(
324-
color_config,
325-
source_map.map(|cm| cm as _),
326-
true,
327-
false)
328-
),
329325
};
330326

331327
errors::Handler::with_emitter_and_flags(

0 commit comments

Comments
 (0)