Skip to content

Commit 3678e5c

Browse files
committed
errors: use -Z terminal-width in JSON emitter
This commit makes the JSON emitter use `-Z terminal-width` in the "rendered" field of the JSON output. Signed-off-by: David Wood <david@davidtw.co>
1 parent 14e65d5 commit 3678e5c

File tree

7 files changed

+96
-7
lines changed

7 files changed

+96
-7
lines changed

src/librustc_errors/json.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub struct JsonEmitter {
3636
pretty: bool,
3737
ui_testing: bool,
3838
json_rendered: HumanReadableErrorType,
39+
terminal_width: Option<usize>,
3940
macro_backtrace: bool,
4041
}
4142

@@ -45,6 +46,7 @@ impl JsonEmitter {
4546
source_map: Lrc<SourceMap>,
4647
pretty: bool,
4748
json_rendered: HumanReadableErrorType,
49+
terminal_width: Option<usize>,
4850
macro_backtrace: bool,
4951
) -> JsonEmitter {
5052
JsonEmitter {
@@ -54,13 +56,15 @@ impl JsonEmitter {
5456
pretty,
5557
ui_testing: false,
5658
json_rendered,
59+
terminal_width,
5760
macro_backtrace,
5861
}
5962
}
6063

6164
pub fn basic(
6265
pretty: bool,
6366
json_rendered: HumanReadableErrorType,
67+
terminal_width: Option<usize>,
6468
macro_backtrace: bool,
6569
) -> JsonEmitter {
6670
let file_path_mapping = FilePathMapping::empty();
@@ -69,6 +73,7 @@ impl JsonEmitter {
6973
Lrc::new(SourceMap::new(file_path_mapping)),
7074
pretty,
7175
json_rendered,
76+
terminal_width,
7277
macro_backtrace,
7378
)
7479
}
@@ -79,6 +84,7 @@ impl JsonEmitter {
7984
source_map: Lrc<SourceMap>,
8085
pretty: bool,
8186
json_rendered: HumanReadableErrorType,
87+
terminal_width: Option<usize>,
8288
macro_backtrace: bool,
8389
) -> JsonEmitter {
8490
JsonEmitter {
@@ -88,6 +94,7 @@ impl JsonEmitter {
8894
pretty,
8995
ui_testing: false,
9096
json_rendered,
97+
terminal_width,
9198
macro_backtrace,
9299
}
93100
}
@@ -247,7 +254,13 @@ impl Diagnostic {
247254
let buf = BufWriter::default();
248255
let output = buf.clone();
249256
je.json_rendered
250-
.new_emitter(Box::new(buf), Some(je.sm.clone()), false, None, je.macro_backtrace)
257+
.new_emitter(
258+
Box::new(buf),
259+
Some(je.sm.clone()),
260+
false,
261+
je.terminal_width,
262+
je.macro_backtrace,
263+
)
251264
.ui_testing(je.ui_testing)
252265
.emit_diagnostic(diag);
253266
let output = Arc::try_unwrap(output.0).unwrap().into_inner().unwrap();

src/librustc_session/session.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -1061,8 +1061,15 @@ fn default_emitter(
10611061
}
10621062
}
10631063
(config::ErrorOutputType::Json { pretty, json_rendered }, None) => Box::new(
1064-
JsonEmitter::stderr(Some(registry), source_map, pretty, json_rendered, macro_backtrace)
1065-
.ui_testing(sopts.debugging_opts.ui_testing),
1064+
JsonEmitter::stderr(
1065+
Some(registry),
1066+
source_map,
1067+
pretty,
1068+
json_rendered,
1069+
sopts.debugging_opts.terminal_width,
1070+
macro_backtrace,
1071+
)
1072+
.ui_testing(sopts.debugging_opts.ui_testing),
10661073
),
10671074
(config::ErrorOutputType::Json { pretty, json_rendered }, Some(dst)) => Box::new(
10681075
JsonEmitter::new(
@@ -1071,6 +1078,7 @@ fn default_emitter(
10711078
source_map,
10721079
pretty,
10731080
json_rendered,
1081+
sopts.debugging_opts.terminal_width,
10741082
macro_backtrace,
10751083
)
10761084
.ui_testing(sopts.debugging_opts.ui_testing),
@@ -1416,7 +1424,7 @@ pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
14161424
Box::new(EmitterWriter::stderr(color_config, None, short, false, None, false))
14171425
}
14181426
config::ErrorOutputType::Json { pretty, json_rendered } => {
1419-
Box::new(JsonEmitter::basic(pretty, json_rendered, false))
1427+
Box::new(JsonEmitter::basic(pretty, json_rendered, None, false))
14201428
}
14211429
};
14221430
let handler = rustc_errors::Handler::with_emitter(true, None, emitter);
@@ -1431,7 +1439,7 @@ pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
14311439
Box::new(EmitterWriter::stderr(color_config, None, short, false, None, false))
14321440
}
14331441
config::ErrorOutputType::Json { pretty, json_rendered } => {
1434-
Box::new(JsonEmitter::basic(pretty, json_rendered, false))
1442+
Box::new(JsonEmitter::basic(pretty, json_rendered, None, false))
14351443
}
14361444
};
14371445
let handler = rustc_errors::Handler::with_emitter(true, None, emitter);

src/librustdoc/core.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,15 @@ pub fn new_handler(
191191
Lrc::new(source_map::SourceMap::new(source_map::FilePathMapping::empty()))
192192
});
193193
Box::new(
194-
JsonEmitter::stderr(None, source_map, pretty, json_rendered, false)
195-
.ui_testing(debugging_opts.ui_testing),
194+
JsonEmitter::stderr(
195+
None,
196+
source_map,
197+
pretty,
198+
json_rendered,
199+
debugging_opts.terminal_width,
200+
false,
201+
)
202+
.ui_testing(debugging_opts.ui_testing),
196203
)
197204
}
198205
};
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// compile-flags: -Z terminal-width=20
2+
3+
// This test checks that `-Z terminal-width` effects the human error output by restricting it to an
4+
// arbitrarily low value so that the effect is visible.
5+
6+
fn main() {
7+
let _: () = 42;
8+
//~^ ERROR mismatched types
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/flag-human.rs:7:17
3+
|
4+
LL | ..._: () = 42;
5+
| -- ^^ expected `()`, found integer
6+
| |
7+
| expected due to this
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0308`.
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// compile-flags: -Z terminal-width=20 --error-format=json
2+
3+
// This test checks that `-Z terminal-width` effects the JSON error output by restricting it to an
4+
// arbitrarily low value so that the effect is visible.
5+
6+
fn main() {
7+
let _: () = 42;
8+
//~^ ERROR mismatched types
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{"message":"mismatched types","code":{"code":"E0308","explanation":"Expected type did not match the received type.
2+
3+
Erroneous code example:
4+
5+
```compile_fail,E0308
6+
let x: i32 = \"I am not a number!\";
7+
// ~~~ ~~~~~~~~~~~~~~~~~~~~
8+
// | |
9+
// | initializing expression;
10+
// | compiler infers type `&str`
11+
// |
12+
// type `i32` assigned to variable `x`
13+
```
14+
15+
This error occurs when the compiler is unable to infer the concrete type of a
16+
variable. It can occur in several cases, the most common being a mismatch
17+
between two types: the type the author explicitly assigned, and the type the
18+
compiler inferred.
19+
"},"level":"error","spans":[{"file_name":"$DIR/flag-json.rs","byte_start":244,"byte_end":246,"line_start":7,"line_end":7,"column_start":17,"column_end":19,"is_primary":true,"text":[{"text":" let _: () = 42;","highlight_start":17,"highlight_end":19}],"label":"expected `()`, found integer","suggested_replacement":null,"suggestion_applicability":null,"expansion":null},{"file_name":"$DIR/flag-json.rs","byte_start":239,"byte_end":241,"line_start":7,"line_end":7,"column_start":12,"column_end":14,"is_primary":false,"text":[{"text":" let _: () = 42;","highlight_start":12,"highlight_end":14}],"label":"expected due to this","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[],"rendered":"error[E0308]: mismatched types
20+
--> $DIR/flag-json.rs:7:17
21+
|
22+
LL | ..._: () = 42;
23+
| -- ^^ expected `()`, found integer
24+
| |
25+
| expected due to this
26+
27+
"}
28+
{"message":"aborting due to previous error","code":null,"level":"error","spans":[],"children":[],"rendered":"error: aborting due to previous error
29+
30+
"}
31+
{"message":"For more information about this error, try `rustc --explain E0308`.","code":null,"level":"failure-note","spans":[],"children":[],"rendered":"For more information about this error, try `rustc --explain E0308`.
32+
"}

0 commit comments

Comments
 (0)