Skip to content

Commit 74be072

Browse files
committed
Auto merge of #45737 - oli-obk:json, r=petrochenkov
Pretty print json in ui tests I found the json output in one line to not be useful for reviewing r? @petrochenkov
2 parents 19402f1 + 6d6fb2e commit 74be072

File tree

8 files changed

+471
-80
lines changed

8 files changed

+471
-80
lines changed

src/librustc/session/config.rs

+61-55
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl OutputType {
155155
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
156156
pub enum ErrorOutputType {
157157
HumanReadable(ColorConfig),
158-
Json,
158+
Json(bool),
159159
Short(ColorConfig),
160160
}
161161

@@ -1433,7 +1433,8 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
14331433
let error_format = if matches.opts_present(&["error-format".to_owned()]) {
14341434
match matches.opt_str("error-format").as_ref().map(|s| &s[..]) {
14351435
Some("human") => ErrorOutputType::HumanReadable(color),
1436-
Some("json") => ErrorOutputType::Json,
1436+
Some("json") => ErrorOutputType::Json(false),
1437+
Some("pretty-json") => ErrorOutputType::Json(true),
14371438
Some("short") => ErrorOutputType::Short(color),
14381439

14391440
None => ErrorOutputType::HumanReadable(color),
@@ -1474,6 +1475,11 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
14741475

14751476
let debugging_opts = build_debugging_options(matches, error_format);
14761477

1478+
if !debugging_opts.unstable_options && error_format == ErrorOutputType::Json(true) {
1479+
early_error(ErrorOutputType::Json(false),
1480+
"--error-format=pretty-json is unstable");
1481+
}
1482+
14771483
let mut output_types = BTreeMap::new();
14781484
if !debugging_opts.parse_only {
14791485
for list in matches.opt_strs("emit") {
@@ -2254,46 +2260,46 @@ mod tests {
22542260
let mut v5 = super::basic_options();
22552261

22562262
// Reference
2257-
v1.search_paths.add_path("native=abc", super::ErrorOutputType::Json);
2258-
v1.search_paths.add_path("crate=def", super::ErrorOutputType::Json);
2259-
v1.search_paths.add_path("dependency=ghi", super::ErrorOutputType::Json);
2260-
v1.search_paths.add_path("framework=jkl", super::ErrorOutputType::Json);
2261-
v1.search_paths.add_path("all=mno", super::ErrorOutputType::Json);
2263+
v1.search_paths.add_path("native=abc", super::ErrorOutputType::Json(false));
2264+
v1.search_paths.add_path("crate=def", super::ErrorOutputType::Json(false));
2265+
v1.search_paths.add_path("dependency=ghi", super::ErrorOutputType::Json(false));
2266+
v1.search_paths.add_path("framework=jkl", super::ErrorOutputType::Json(false));
2267+
v1.search_paths.add_path("all=mno", super::ErrorOutputType::Json(false));
22622268

22632269
// Native changed
2264-
v2.search_paths.add_path("native=XXX", super::ErrorOutputType::Json);
2265-
v2.search_paths.add_path("crate=def", super::ErrorOutputType::Json);
2266-
v2.search_paths.add_path("dependency=ghi", super::ErrorOutputType::Json);
2267-
v2.search_paths.add_path("framework=jkl", super::ErrorOutputType::Json);
2268-
v2.search_paths.add_path("all=mno", super::ErrorOutputType::Json);
2270+
v2.search_paths.add_path("native=XXX", super::ErrorOutputType::Json(false));
2271+
v2.search_paths.add_path("crate=def", super::ErrorOutputType::Json(false));
2272+
v2.search_paths.add_path("dependency=ghi", super::ErrorOutputType::Json(false));
2273+
v2.search_paths.add_path("framework=jkl", super::ErrorOutputType::Json(false));
2274+
v2.search_paths.add_path("all=mno", super::ErrorOutputType::Json(false));
22692275

22702276
// Crate changed
2271-
v2.search_paths.add_path("native=abc", super::ErrorOutputType::Json);
2272-
v2.search_paths.add_path("crate=XXX", super::ErrorOutputType::Json);
2273-
v2.search_paths.add_path("dependency=ghi", super::ErrorOutputType::Json);
2274-
v2.search_paths.add_path("framework=jkl", super::ErrorOutputType::Json);
2275-
v2.search_paths.add_path("all=mno", super::ErrorOutputType::Json);
2277+
v2.search_paths.add_path("native=abc", super::ErrorOutputType::Json(false));
2278+
v2.search_paths.add_path("crate=XXX", super::ErrorOutputType::Json(false));
2279+
v2.search_paths.add_path("dependency=ghi", super::ErrorOutputType::Json(false));
2280+
v2.search_paths.add_path("framework=jkl", super::ErrorOutputType::Json(false));
2281+
v2.search_paths.add_path("all=mno", super::ErrorOutputType::Json(false));
22762282

22772283
// Dependency changed
2278-
v3.search_paths.add_path("native=abc", super::ErrorOutputType::Json);
2279-
v3.search_paths.add_path("crate=def", super::ErrorOutputType::Json);
2280-
v3.search_paths.add_path("dependency=XXX", super::ErrorOutputType::Json);
2281-
v3.search_paths.add_path("framework=jkl", super::ErrorOutputType::Json);
2282-
v3.search_paths.add_path("all=mno", super::ErrorOutputType::Json);
2284+
v3.search_paths.add_path("native=abc", super::ErrorOutputType::Json(false));
2285+
v3.search_paths.add_path("crate=def", super::ErrorOutputType::Json(false));
2286+
v3.search_paths.add_path("dependency=XXX", super::ErrorOutputType::Json(false));
2287+
v3.search_paths.add_path("framework=jkl", super::ErrorOutputType::Json(false));
2288+
v3.search_paths.add_path("all=mno", super::ErrorOutputType::Json(false));
22832289

22842290
// Framework changed
2285-
v4.search_paths.add_path("native=abc", super::ErrorOutputType::Json);
2286-
v4.search_paths.add_path("crate=def", super::ErrorOutputType::Json);
2287-
v4.search_paths.add_path("dependency=ghi", super::ErrorOutputType::Json);
2288-
v4.search_paths.add_path("framework=XXX", super::ErrorOutputType::Json);
2289-
v4.search_paths.add_path("all=mno", super::ErrorOutputType::Json);
2291+
v4.search_paths.add_path("native=abc", super::ErrorOutputType::Json(false));
2292+
v4.search_paths.add_path("crate=def", super::ErrorOutputType::Json(false));
2293+
v4.search_paths.add_path("dependency=ghi", super::ErrorOutputType::Json(false));
2294+
v4.search_paths.add_path("framework=XXX", super::ErrorOutputType::Json(false));
2295+
v4.search_paths.add_path("all=mno", super::ErrorOutputType::Json(false));
22902296

22912297
// All changed
2292-
v5.search_paths.add_path("native=abc", super::ErrorOutputType::Json);
2293-
v5.search_paths.add_path("crate=def", super::ErrorOutputType::Json);
2294-
v5.search_paths.add_path("dependency=ghi", super::ErrorOutputType::Json);
2295-
v5.search_paths.add_path("framework=jkl", super::ErrorOutputType::Json);
2296-
v5.search_paths.add_path("all=XXX", super::ErrorOutputType::Json);
2298+
v5.search_paths.add_path("native=abc", super::ErrorOutputType::Json(false));
2299+
v5.search_paths.add_path("crate=def", super::ErrorOutputType::Json(false));
2300+
v5.search_paths.add_path("dependency=ghi", super::ErrorOutputType::Json(false));
2301+
v5.search_paths.add_path("framework=jkl", super::ErrorOutputType::Json(false));
2302+
v5.search_paths.add_path("all=XXX", super::ErrorOutputType::Json(false));
22972303

22982304
assert!(v1.dep_tracking_hash() != v2.dep_tracking_hash());
22992305
assert!(v1.dep_tracking_hash() != v3.dep_tracking_hash());
@@ -2316,29 +2322,29 @@ mod tests {
23162322
let mut v4 = super::basic_options();
23172323

23182324
// Reference
2319-
v1.search_paths.add_path("native=abc", super::ErrorOutputType::Json);
2320-
v1.search_paths.add_path("crate=def", super::ErrorOutputType::Json);
2321-
v1.search_paths.add_path("dependency=ghi", super::ErrorOutputType::Json);
2322-
v1.search_paths.add_path("framework=jkl", super::ErrorOutputType::Json);
2323-
v1.search_paths.add_path("all=mno", super::ErrorOutputType::Json);
2324-
2325-
v2.search_paths.add_path("native=abc", super::ErrorOutputType::Json);
2326-
v2.search_paths.add_path("dependency=ghi", super::ErrorOutputType::Json);
2327-
v2.search_paths.add_path("crate=def", super::ErrorOutputType::Json);
2328-
v2.search_paths.add_path("framework=jkl", super::ErrorOutputType::Json);
2329-
v2.search_paths.add_path("all=mno", super::ErrorOutputType::Json);
2330-
2331-
v3.search_paths.add_path("crate=def", super::ErrorOutputType::Json);
2332-
v3.search_paths.add_path("framework=jkl", super::ErrorOutputType::Json);
2333-
v3.search_paths.add_path("native=abc", super::ErrorOutputType::Json);
2334-
v3.search_paths.add_path("dependency=ghi", super::ErrorOutputType::Json);
2335-
v3.search_paths.add_path("all=mno", super::ErrorOutputType::Json);
2336-
2337-
v4.search_paths.add_path("all=mno", super::ErrorOutputType::Json);
2338-
v4.search_paths.add_path("native=abc", super::ErrorOutputType::Json);
2339-
v4.search_paths.add_path("crate=def", super::ErrorOutputType::Json);
2340-
v4.search_paths.add_path("dependency=ghi", super::ErrorOutputType::Json);
2341-
v4.search_paths.add_path("framework=jkl", super::ErrorOutputType::Json);
2325+
v1.search_paths.add_path("native=abc", super::ErrorOutputType::Json(false));
2326+
v1.search_paths.add_path("crate=def", super::ErrorOutputType::Json(false));
2327+
v1.search_paths.add_path("dependency=ghi", super::ErrorOutputType::Json(false));
2328+
v1.search_paths.add_path("framework=jkl", super::ErrorOutputType::Json(false));
2329+
v1.search_paths.add_path("all=mno", super::ErrorOutputType::Json(false));
2330+
2331+
v2.search_paths.add_path("native=abc", super::ErrorOutputType::Json(false));
2332+
v2.search_paths.add_path("dependency=ghi", super::ErrorOutputType::Json(false));
2333+
v2.search_paths.add_path("crate=def", super::ErrorOutputType::Json(false));
2334+
v2.search_paths.add_path("framework=jkl", super::ErrorOutputType::Json(false));
2335+
v2.search_paths.add_path("all=mno", super::ErrorOutputType::Json(false));
2336+
2337+
v3.search_paths.add_path("crate=def", super::ErrorOutputType::Json(false));
2338+
v3.search_paths.add_path("framework=jkl", super::ErrorOutputType::Json(false));
2339+
v3.search_paths.add_path("native=abc", super::ErrorOutputType::Json(false));
2340+
v3.search_paths.add_path("dependency=ghi", super::ErrorOutputType::Json(false));
2341+
v3.search_paths.add_path("all=mno", super::ErrorOutputType::Json(false));
2342+
2343+
v4.search_paths.add_path("all=mno", super::ErrorOutputType::Json(false));
2344+
v4.search_paths.add_path("native=abc", super::ErrorOutputType::Json(false));
2345+
v4.search_paths.add_path("crate=def", super::ErrorOutputType::Json(false));
2346+
v4.search_paths.add_path("dependency=ghi", super::ErrorOutputType::Json(false));
2347+
v4.search_paths.add_path("framework=jkl", super::ErrorOutputType::Json(false));
23422348

23432349
assert!(v1.dep_tracking_hash() == v2.dep_tracking_hash());
23442350
assert!(v1.dep_tracking_hash() == v3.dep_tracking_hash());

src/librustc/session/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ impl Session {
372372
match self.opts.error_format {
373373
// when outputting JSON for tool consumption, the tool might want
374374
// the duplicates
375-
config::ErrorOutputType::Json => {
375+
config::ErrorOutputType::Json(_) => {
376376
do_method()
377377
},
378378
_ => {
@@ -736,11 +736,11 @@ pub fn build_session_with_codemap(sopts: config::Options,
736736
(config::ErrorOutputType::HumanReadable(_), Some(dst)) => {
737737
Box::new(EmitterWriter::new(dst, Some(codemap.clone()), false))
738738
}
739-
(config::ErrorOutputType::Json, None) => {
740-
Box::new(JsonEmitter::stderr(Some(registry), codemap.clone()))
739+
(config::ErrorOutputType::Json(pretty), None) => {
740+
Box::new(JsonEmitter::stderr(Some(registry), codemap.clone(), pretty))
741741
}
742-
(config::ErrorOutputType::Json, Some(dst)) => {
743-
Box::new(JsonEmitter::new(dst, Some(registry), codemap.clone()))
742+
(config::ErrorOutputType::Json(pretty), Some(dst)) => {
743+
Box::new(JsonEmitter::new(dst, Some(registry), codemap.clone(), pretty))
744744
}
745745
(config::ErrorOutputType::Short(color_config), None) => {
746746
Box::new(EmitterWriter::stderr(color_config, Some(codemap.clone()), true))
@@ -918,7 +918,7 @@ pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
918918
config::ErrorOutputType::HumanReadable(color_config) => {
919919
Box::new(EmitterWriter::stderr(color_config, None, false))
920920
}
921-
config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()),
921+
config::ErrorOutputType::Json(pretty) => Box::new(JsonEmitter::basic(pretty)),
922922
config::ErrorOutputType::Short(color_config) => {
923923
Box::new(EmitterWriter::stderr(color_config, None, true))
924924
}
@@ -933,7 +933,7 @@ pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
933933
config::ErrorOutputType::HumanReadable(color_config) => {
934934
Box::new(EmitterWriter::stderr(color_config, None, false))
935935
}
936-
config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()),
936+
config::ErrorOutputType::Json(pretty) => Box::new(JsonEmitter::basic(pretty)),
937937
config::ErrorOutputType::Short(color_config) => {
938938
Box::new(EmitterWriter::stderr(color_config, None, true))
939939
}

src/libsyntax/json.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -30,44 +30,54 @@ use std::rc::Rc;
3030
use std::io::{self, Write};
3131
use std::vec;
3232

33-
use rustc_serialize::json::as_json;
33+
use rustc_serialize::json::{as_json, as_pretty_json};
3434

3535
pub struct JsonEmitter {
3636
dst: Box<Write + Send>,
3737
registry: Option<Registry>,
3838
cm: Rc<CodeMapper + 'static>,
39+
pretty: bool,
3940
}
4041

4142
impl JsonEmitter {
4243
pub fn stderr(registry: Option<Registry>,
43-
code_map: Rc<CodeMap>) -> JsonEmitter {
44+
code_map: Rc<CodeMap>,
45+
pretty: bool) -> JsonEmitter {
4446
JsonEmitter {
4547
dst: Box::new(io::stderr()),
4648
registry,
4749
cm: code_map,
50+
pretty,
4851
}
4952
}
5053

51-
pub fn basic() -> JsonEmitter {
54+
pub fn basic(pretty: bool) -> JsonEmitter {
5255
let file_path_mapping = FilePathMapping::empty();
53-
JsonEmitter::stderr(None, Rc::new(CodeMap::new(file_path_mapping)))
56+
JsonEmitter::stderr(None, Rc::new(CodeMap::new(file_path_mapping)), pretty)
5457
}
5558

5659
pub fn new(dst: Box<Write + Send>,
5760
registry: Option<Registry>,
58-
code_map: Rc<CodeMap>) -> JsonEmitter {
61+
code_map: Rc<CodeMap>,
62+
pretty: bool) -> JsonEmitter {
5963
JsonEmitter {
6064
dst,
6165
registry,
6266
cm: code_map,
67+
pretty,
6368
}
6469
}
6570
}
6671

6772
impl Emitter for JsonEmitter {
6873
fn emit(&mut self, db: &DiagnosticBuilder) {
6974
let data = Diagnostic::from_diagnostic_builder(db, self);
70-
if let Err(e) = writeln!(&mut self.dst, "{}", as_json(&data)) {
75+
let result = if self.pretty {
76+
writeln!(&mut self.dst, "{}", as_pretty_json(&data))
77+
} else {
78+
writeln!(&mut self.dst, "{}", as_json(&data))
79+
};
80+
if let Err(e) = result {
7181
panic!("failed to print diagnostics: {:?}", e);
7282
}
7383
}
@@ -85,9 +95,7 @@ struct Diagnostic {
8595
spans: Vec<DiagnosticSpan>,
8696
/// Associated diagnostic messages.
8797
children: Vec<Diagnostic>,
88-
/// The message as rustc would render it. Currently this is only
89-
/// `Some` for "suggestions", but eventually it will include all
90-
/// snippets.
98+
/// The message as rustc would render it. Currently this is always `None`
9199
rendered: Option<String>,
92100
}
93101

@@ -110,9 +118,7 @@ struct DiagnosticSpan {
110118
/// Label that should be placed at this location (if any)
111119
label: Option<String>,
112120
/// If we are suggesting a replacement, this will contain text
113-
/// that should be sliced in atop this span. You may prefer to
114-
/// load the fully rendered version from the parent `Diagnostic`,
115-
/// however.
121+
/// that should be sliced in atop this span.
116122
suggested_replacement: Option<String>,
117123
/// Macro invocations that created the code at this span, if any.
118124
expansion: Option<Box<DiagnosticSpanMacroExpansion>>,

src/test/ui/lint/unused_parens_json_suggestion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: --error-format json
11+
// compile-flags: --error-format pretty-json -Zunstable-options
1212

1313
// The output for humans should just highlight the whole span without showing
1414
// the suggested replacement, but we also want to test that suggested
Original file line numberDiff line numberDiff line change
@@ -1 +1,91 @@
1-
{"message":"unnecessary parentheses around assigned value","code":{"code":"unused_parens","explanation":null},"level":"warning","spans":[{"file_name":"$DIR/unused_parens_json_suggestion.rs","byte_start":1001,"byte_end":1014,"line_start":24,"line_end":24,"column_start":14,"column_end":27,"is_primary":true,"text":[{"text":" let _a = (1 / (2 + 3));","highlight_start":14,"highlight_end":27}],"label":null,"suggested_replacement":null,"expansion":null}],"children":[{"message":"lint level defined here","code":null,"level":"note","spans":[{"file_name":"$DIR/unused_parens_json_suggestion.rs","byte_start":847,"byte_end":860,"line_start":19,"line_end":19,"column_start":9,"column_end":22,"is_primary":true,"text":[{"text":"#![warn(unused_parens)]","highlight_start":9,"highlight_end":22}],"label":null,"suggested_replacement":null,"expansion":null}],"children":[],"rendered":null},{"message":"remove these parentheses","code":null,"level":"help","spans":[{"file_name":"$DIR/unused_parens_json_suggestion.rs","byte_start":1001,"byte_end":1014,"line_start":24,"line_end":24,"column_start":14,"column_end":27,"is_primary":true,"text":[{"text":" let _a = (1 / (2 + 3));","highlight_start":14,"highlight_end":27}],"label":null,"suggested_replacement":"1 / (2 + 3)","expansion":null}],"children":[],"rendered":null}],"rendered":null}
1+
{
2+
"message": "unnecessary parentheses around assigned value",
3+
"code": {
4+
"code": "unused_parens",
5+
"explanation": null
6+
},
7+
"level": "warning",
8+
"spans": [
9+
{
10+
"file_name": "$DIR/unused_parens_json_suggestion.rs",
11+
"byte_start": 1027,
12+
"byte_end": 1040,
13+
"line_start": 24,
14+
"line_end": 24,
15+
"column_start": 14,
16+
"column_end": 27,
17+
"is_primary": true,
18+
"text": [
19+
{
20+
"text": " let _a = (1 / (2 + 3));",
21+
"highlight_start": 14,
22+
"highlight_end": 27
23+
}
24+
],
25+
"label": null,
26+
"suggested_replacement": null,
27+
"expansion": null
28+
}
29+
],
30+
"children": [
31+
{
32+
"message": "lint level defined here",
33+
"code": null,
34+
"level": "note",
35+
"spans": [
36+
{
37+
"file_name": "$DIR/unused_parens_json_suggestion.rs",
38+
"byte_start": 873,
39+
"byte_end": 886,
40+
"line_start": 19,
41+
"line_end": 19,
42+
"column_start": 9,
43+
"column_end": 22,
44+
"is_primary": true,
45+
"text": [
46+
{
47+
"text": "#![warn(unused_parens)]",
48+
"highlight_start": 9,
49+
"highlight_end": 22
50+
}
51+
],
52+
"label": null,
53+
"suggested_replacement": null,
54+
"expansion": null
55+
}
56+
],
57+
"children": [],
58+
"rendered": null
59+
},
60+
{
61+
"message": "remove these parentheses",
62+
"code": null,
63+
"level": "help",
64+
"spans": [
65+
{
66+
"file_name": "$DIR/unused_parens_json_suggestion.rs",
67+
"byte_start": 1027,
68+
"byte_end": 1040,
69+
"line_start": 24,
70+
"line_end": 24,
71+
"column_start": 14,
72+
"column_end": 27,
73+
"is_primary": true,
74+
"text": [
75+
{
76+
"text": " let _a = (1 / (2 + 3));",
77+
"highlight_start": 14,
78+
"highlight_end": 27
79+
}
80+
],
81+
"label": null,
82+
"suggested_replacement": "1 / (2 + 3)",
83+
"expansion": null
84+
}
85+
],
86+
"children": [],
87+
"rendered": null
88+
}
89+
],
90+
"rendered": null
91+
}

src/test/ui/lint/use_suggestion_json.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: --error-format json
11+
// compile-flags: --error-format pretty-json -Zunstable-options
1212

1313
// The output for humans should just highlight the whole span without showing
1414
// the suggested replacement, but we also want to test that suggested

0 commit comments

Comments
 (0)