Skip to content

Commit e528884

Browse files
committed
sess: stabilize --terminal-width
Formerly `-Zterminal-width`, `--terminal-width` allows the user or build tool to inform rustc of the width of the terminal so that diagnostics can be truncated. Signed-off-by: David Wood <[email protected]>
1 parent 5b8cf49 commit e528884

File tree

13 files changed

+66
-11
lines changed

13 files changed

+66
-11
lines changed

compiler/rustc_interface/src/tests.rs

-1
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,6 @@ fn test_debugging_options_tracking_hash() {
689689
untracked!(span_debug, true);
690690
untracked!(span_free_formats, true);
691691
untracked!(temps_dir, Some(String::from("abc")));
692-
untracked!(terminal_width, Some(80));
693692
untracked!(threads, 99);
694693
untracked!(time, true);
695694
untracked!(time_llvm_passes, true);

compiler/rustc_session/src/config.rs

+12
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,7 @@ impl Default for Options {
726726
prints: Vec::new(),
727727
cg: Default::default(),
728728
error_format: ErrorOutputType::default(),
729+
terminal_width: None,
729730
externs: Externs(BTreeMap::new()),
730731
crate_name: None,
731732
libs: Vec::new(),
@@ -1427,6 +1428,12 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
14271428
never = never colorize output",
14281429
"auto|always|never",
14291430
),
1431+
opt::opt_s(
1432+
"",
1433+
"terminal-width",
1434+
"Inform rustc of the width of the terminal so that errors can be truncated",
1435+
"WIDTH",
1436+
),
14301437
opt::multi_s(
14311438
"",
14321439
"remap-path-prefix",
@@ -2202,6 +2209,10 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
22022209

22032210
let error_format = parse_error_format(matches, color, json_rendered);
22042211

2212+
let terminal_width = matches.opt_get("terminal-width").unwrap_or_else(|_| {
2213+
early_error(error_format, "`--terminal-width` must be an positive integer");
2214+
});
2215+
22052216
let unparsed_crate_types = matches.opt_strs("crate-type");
22062217
let crate_types = parse_crate_types_from_list(unparsed_crate_types)
22072218
.unwrap_or_else(|e| early_error(error_format, &e));
@@ -2474,6 +2485,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
24742485
prints,
24752486
cg,
24762487
error_format,
2488+
terminal_width,
24772489
externs,
24782490
unstable_features: UnstableFeatures::from_environment(crate_name.as_deref()),
24792491
crate_name,

compiler/rustc_session/src/options.rs

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ top_level_options!(
170170

171171
test: bool [TRACKED],
172172
error_format: ErrorOutputType [UNTRACKED],
173+
terminal_width: Option<usize> [UNTRACKED],
173174

174175
/// If `Some`, enable incremental compilation, using the given
175176
/// directory to store intermediate results.

compiler/rustc_session/src/session.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ fn default_emitter(
11621162
fallback_bundle,
11631163
short,
11641164
sopts.debugging_opts.teach,
1165-
sopts.debugging_opts.terminal_width,
1165+
sopts.terminal_width,
11661166
macro_backtrace,
11671167
),
11681168
Some(dst) => EmitterWriter::new(
@@ -1188,7 +1188,7 @@ fn default_emitter(
11881188
fallback_bundle,
11891189
pretty,
11901190
json_rendered,
1191-
sopts.debugging_opts.terminal_width,
1191+
sopts.terminal_width,
11921192
macro_backtrace,
11931193
)
11941194
.ui_testing(sopts.debugging_opts.ui_testing),
@@ -1202,7 +1202,7 @@ fn default_emitter(
12021202
fallback_bundle,
12031203
pretty,
12041204
json_rendered,
1205-
sopts.debugging_opts.terminal_width,
1205+
sopts.terminal_width,
12061206
macro_backtrace,
12071207
)
12081208
.ui_testing(sopts.debugging_opts.ui_testing),

src/librustdoc/config.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ pub(crate) struct Options {
7373
pub(crate) proc_macro_crate: bool,
7474
/// How to format errors and warnings.
7575
pub(crate) error_format: ErrorOutputType,
76+
/// Width of terminal to truncate errors appropriately.
77+
pub(crate) terminal_width: Option<usize>,
7678
/// Library search paths to hand to the compiler.
7779
pub(crate) libs: Vec<SearchPath>,
7880
/// Library search paths strings to hand to the compiler.
@@ -334,11 +336,12 @@ impl Options {
334336
let config::JsonConfig { json_rendered, json_unused_externs, .. } =
335337
config::parse_json(matches);
336338
let error_format = config::parse_error_format(matches, color, json_rendered);
339+
let terminal_width = matches.opt_get("terminal-width").unwrap_or_default();
337340

338341
let codegen_options = CodegenOptions::build(matches, error_format);
339342
let debugging_opts = DebuggingOptions::build(matches, error_format);
340343

341-
let diag = new_handler(error_format, None, &debugging_opts);
344+
let diag = new_handler(error_format, None, terminal_width, &debugging_opts);
342345

343346
// check for deprecated options
344347
check_deprecated_options(matches, &diag);
@@ -702,6 +705,7 @@ impl Options {
702705
input,
703706
proc_macro_crate,
704707
error_format,
708+
terminal_width,
705709
libs,
706710
lib_strs,
707711
externs,

src/librustdoc/core.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ impl<'tcx> DocContext<'tcx> {
154154
pub(crate) fn new_handler(
155155
error_format: ErrorOutputType,
156156
source_map: Option<Lrc<source_map::SourceMap>>,
157+
terminal_width: Option<usize>,
157158
debugging_opts: &DebuggingOptions,
158159
) -> rustc_errors::Handler {
159160
let fallback_bundle =
@@ -169,7 +170,7 @@ pub(crate) fn new_handler(
169170
fallback_bundle,
170171
short,
171172
debugging_opts.teach,
172-
debugging_opts.terminal_width,
173+
terminal_width,
173174
false,
174175
)
175176
.ui_testing(debugging_opts.ui_testing),
@@ -187,7 +188,7 @@ pub(crate) fn new_handler(
187188
fallback_bundle,
188189
pretty,
189190
json_rendered,
190-
debugging_opts.terminal_width,
191+
terminal_width,
191192
false,
192193
)
193194
.ui_testing(debugging_opts.ui_testing),
@@ -208,6 +209,7 @@ pub(crate) fn create_config(
208209
crate_name,
209210
proc_macro_crate,
210211
error_format,
212+
terminal_width,
211213
libs,
212214
externs,
213215
mut cfgs,
@@ -266,6 +268,7 @@ pub(crate) fn create_config(
266268
actually_rustdoc: true,
267269
debugging_opts,
268270
error_format,
271+
terminal_width,
269272
edition,
270273
describe_lints,
271274
crate_name,

src/librustdoc/lib.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,14 @@ fn opts() -> Vec<RustcOptGroup> {
462462
"human|json|short",
463463
)
464464
}),
465+
unstable("terminal-width", |o| {
466+
o.optopt(
467+
"",
468+
"terminal-width",
469+
"Provide width of the terminal for truncated error messages",
470+
"WIDTH",
471+
)
472+
}),
465473
stable("json", |o| {
466474
o.optopt("", "json", "Configure the structure of JSON diagnostics", "CONFIG")
467475
}),
@@ -733,7 +741,12 @@ fn run_renderer<'tcx, T: formats::FormatRenderer<'tcx>>(
733741
}
734742

735743
fn main_options(options: config::Options) -> MainResult {
736-
let diag = core::new_handler(options.error_format, None, &options.debugging_opts);
744+
let diag = core::new_handler(
745+
options.error_format,
746+
None,
747+
options.terminal_width,
748+
&options.debugging_opts,
749+
);
737750

738751
match (options.should_test, options.markdown_input()) {
739752
(true, true) => return wrap_return(&diag, markdown::test(options)),

src/test/run-make/issue-88756-default-output/output-default.stdout

+3
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ Options:
110110
never = never colorize output
111111
--error-format human|json|short
112112
How errors and other messages are produced
113+
--terminal-width WIDTH
114+
Provide width of the terminal for truncated error
115+
messages
113116
--json CONFIG Configure the structure of JSON diagnostics
114117
--disable-minification
115118
Disable minification applied on JS files

src/test/rustdoc-ui/terminal-width.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// compile-flags: -Zunstable-options --terminal-width=10
2+
#![deny(rustdoc::bare_urls)]
3+
4+
/// This is a long line that contains a http://link.com
5+
pub struct Foo; //~^ ERROR
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: this URL is not a hyperlink
2+
--> $DIR/terminal-width.rs:4:41
3+
|
4+
LL | ... a http://link.com
5+
| ^^^^^^^^^^^^^^^ help: use an automatic link instead: `<http://link.com>`
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/terminal-width.rs:2:9
9+
|
10+
LL | ...ny(rustdoc::bare_url...
11+
| ^^^^^^^^^^^^^^^^^^
12+
= note: bare URLs are not automatically turned into clickable links
13+
14+
error: aborting due to previous error
15+

src/test/ui/terminal-width/flag-human.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -Z terminal-width=20
1+
// compile-flags: --terminal-width=20
22

33
// This test checks that `-Z terminal-width` effects the human error output by restricting it to an
44
// arbitrarily low value so that the effect is visible.

src/test/ui/terminal-width/flag-json.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -Z terminal-width=20 --error-format=json
1+
// compile-flags: --terminal-width=20 --error-format=json
22

33
// This test checks that `-Z terminal-width` effects the JSON error output by restricting it to an
44
// arbitrarily low value so that the effect is visible.

src/test/ui/terminal-width/flag-json.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ This error occurs when an expression was used in a place where the compiler
2424
expected an expression of a different type. It can occur in several cases, the
2525
most common being when calling a function and passing an argument which has a
2626
different type than the matching type in the function declaration.
27-
"},"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
27+
"},"level":"error","spans":[{"file_name":"$DIR/flag-json.rs","byte_start":243,"byte_end":245,"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":238,"byte_end":240,"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
2828
--> $DIR/flag-json.rs:7:17
2929
|
3030
LL | ..._: () = 42;

0 commit comments

Comments
 (0)