Skip to content

Commit 8082a5b

Browse files
authored
Rollup merge of rust-lang#95635 - davidtwco:terminal-width-stabilization, r=oli-obk
sess: stabilize `--terminal-width` as `--diagnostic-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. Pending agreement to stabilize, see tracking issue at rust-lang#84673. r? ``@oli-obk``
2 parents 9b21131 + 44c1fcc commit 8082a5b

32 files changed

+101
-46
lines changed

compiler/rustc_errors/src/emitter.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl HumanReadableErrorType {
6363
bundle: Option<Lrc<FluentBundle>>,
6464
fallback_bundle: LazyFallbackBundle,
6565
teach: bool,
66-
terminal_width: Option<usize>,
66+
diagnostic_width: Option<usize>,
6767
macro_backtrace: bool,
6868
) -> EmitterWriter {
6969
let (short, color_config) = self.unzip();
@@ -76,7 +76,7 @@ impl HumanReadableErrorType {
7676
short,
7777
teach,
7878
color,
79-
terminal_width,
79+
diagnostic_width,
8080
macro_backtrace,
8181
)
8282
}
@@ -710,7 +710,7 @@ pub struct EmitterWriter {
710710
short_message: bool,
711711
teach: bool,
712712
ui_testing: bool,
713-
terminal_width: Option<usize>,
713+
diagnostic_width: Option<usize>,
714714

715715
macro_backtrace: bool,
716716
}
@@ -730,7 +730,7 @@ impl EmitterWriter {
730730
fallback_bundle: LazyFallbackBundle,
731731
short_message: bool,
732732
teach: bool,
733-
terminal_width: Option<usize>,
733+
diagnostic_width: Option<usize>,
734734
macro_backtrace: bool,
735735
) -> EmitterWriter {
736736
let dst = Destination::from_stderr(color_config);
@@ -742,7 +742,7 @@ impl EmitterWriter {
742742
short_message,
743743
teach,
744744
ui_testing: false,
745-
terminal_width,
745+
diagnostic_width,
746746
macro_backtrace,
747747
}
748748
}
@@ -755,7 +755,7 @@ impl EmitterWriter {
755755
short_message: bool,
756756
teach: bool,
757757
colored: bool,
758-
terminal_width: Option<usize>,
758+
diagnostic_width: Option<usize>,
759759
macro_backtrace: bool,
760760
) -> EmitterWriter {
761761
EmitterWriter {
@@ -766,7 +766,7 @@ impl EmitterWriter {
766766
short_message,
767767
teach,
768768
ui_testing: false,
769-
terminal_width,
769+
diagnostic_width,
770770
macro_backtrace,
771771
}
772772
}
@@ -1615,7 +1615,7 @@ impl EmitterWriter {
16151615
width_offset + annotated_file.multiline_depth + 1
16161616
};
16171617

1618-
let column_width = if let Some(width) = self.terminal_width {
1618+
let column_width = if let Some(width) = self.diagnostic_width {
16191619
width.saturating_sub(code_offset)
16201620
} else if self.ui_testing {
16211621
DEFAULT_COLUMN_WIDTH

compiler/rustc_errors/src/json.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub struct JsonEmitter {
4242
pretty: bool,
4343
ui_testing: bool,
4444
json_rendered: HumanReadableErrorType,
45-
terminal_width: Option<usize>,
45+
diagnostic_width: Option<usize>,
4646
macro_backtrace: bool,
4747
}
4848

@@ -54,7 +54,7 @@ impl JsonEmitter {
5454
fallback_bundle: LazyFallbackBundle,
5555
pretty: bool,
5656
json_rendered: HumanReadableErrorType,
57-
terminal_width: Option<usize>,
57+
diagnostic_width: Option<usize>,
5858
macro_backtrace: bool,
5959
) -> JsonEmitter {
6060
JsonEmitter {
@@ -66,7 +66,7 @@ impl JsonEmitter {
6666
pretty,
6767
ui_testing: false,
6868
json_rendered,
69-
terminal_width,
69+
diagnostic_width,
7070
macro_backtrace,
7171
}
7272
}
@@ -76,7 +76,7 @@ impl JsonEmitter {
7676
json_rendered: HumanReadableErrorType,
7777
fluent_bundle: Option<Lrc<FluentBundle>>,
7878
fallback_bundle: LazyFallbackBundle,
79-
terminal_width: Option<usize>,
79+
diagnostic_width: Option<usize>,
8080
macro_backtrace: bool,
8181
) -> JsonEmitter {
8282
let file_path_mapping = FilePathMapping::empty();
@@ -87,7 +87,7 @@ impl JsonEmitter {
8787
fallback_bundle,
8888
pretty,
8989
json_rendered,
90-
terminal_width,
90+
diagnostic_width,
9191
macro_backtrace,
9292
)
9393
}
@@ -100,7 +100,7 @@ impl JsonEmitter {
100100
fallback_bundle: LazyFallbackBundle,
101101
pretty: bool,
102102
json_rendered: HumanReadableErrorType,
103-
terminal_width: Option<usize>,
103+
diagnostic_width: Option<usize>,
104104
macro_backtrace: bool,
105105
) -> JsonEmitter {
106106
JsonEmitter {
@@ -112,7 +112,7 @@ impl JsonEmitter {
112112
pretty,
113113
ui_testing: false,
114114
json_rendered,
115-
terminal_width,
115+
diagnostic_width,
116116
macro_backtrace,
117117
}
118118
}
@@ -345,7 +345,7 @@ impl Diagnostic {
345345
je.fluent_bundle.clone(),
346346
je.fallback_bundle.clone(),
347347
false,
348-
je.terminal_width,
348+
je.diagnostic_width,
349349
je.macro_backtrace,
350350
)
351351
.ui_testing(je.ui_testing)

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+
diagnostic_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+
"diagnostic-width",
1434+
"Inform rustc of the width of the output so that diagnostics can be truncated to fit",
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 diagnostic_width = matches.opt_get("diagnostic-width").unwrap_or_else(|_| {
2213+
early_error(error_format, "`--diagnostic-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+
diagnostic_width,
24772489
externs,
24782490
unstable_features: UnstableFeatures::from_environment(crate_name.as_deref()),
24792491
crate_name,

compiler/rustc_session/src/options.rs

+3-2
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+
diagnostic_width: Option<usize> [UNTRACKED],
173174

174175
/// If `Some`, enable incremental compilation, using the given
175176
/// directory to store intermediate results.
@@ -1388,6 +1389,8 @@ options! {
13881389
"panic strategy for out-of-memory handling"),
13891390
osx_rpath_install_name: bool = (false, parse_bool, [TRACKED],
13901391
"pass `-install_name @rpath/...` to the macOS linker (default: no)"),
1392+
diagnostic_width: Option<usize> = (None, parse_opt_number, [UNTRACKED],
1393+
"set the current output width for diagnostic truncation"),
13911394
panic_abort_tests: bool = (false, parse_bool, [TRACKED],
13921395
"support compiling tests with panic=abort (default: no)"),
13931396
panic_in_drop: PanicStrategy = (PanicStrategy::Unwind, parse_panic_strategy, [TRACKED],
@@ -1514,8 +1517,6 @@ options! {
15141517
"show extended diagnostic help (default: no)"),
15151518
temps_dir: Option<String> = (None, parse_opt_string, [UNTRACKED],
15161519
"the directory the intermediate files are written to"),
1517-
terminal_width: Option<usize> = (None, parse_opt_number, [UNTRACKED],
1518-
"set the current terminal width"),
15191520
// Diagnostics are considered side-effects of a query (see `QuerySideEffects`) and are saved
15201521
// alongside query results and changes to translation options can affect diagnostics - so
15211522
// translation options should be tracked.

compiler/rustc_session/src/session.rs

+4-4
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.diagnostic_width,
11661166
macro_backtrace,
11671167
),
11681168
Some(dst) => EmitterWriter::new(
@@ -1173,7 +1173,7 @@ fn default_emitter(
11731173
short,
11741174
false, // no teach messages when writing to a buffer
11751175
false, // no colors when writing to a buffer
1176-
None, // no terminal width
1176+
None, // no diagnostic width
11771177
macro_backtrace,
11781178
),
11791179
};
@@ -1188,7 +1188,7 @@ fn default_emitter(
11881188
fallback_bundle,
11891189
pretty,
11901190
json_rendered,
1191-
sopts.debugging_opts.terminal_width,
1191+
sopts.diagnostic_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.diagnostic_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 output buffer to truncate errors appropriately.
77+
pub(crate) diagnostic_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.
@@ -331,11 +333,12 @@ impl Options {
331333
let config::JsonConfig { json_rendered, json_unused_externs, .. } =
332334
config::parse_json(matches);
333335
let error_format = config::parse_error_format(matches, color, json_rendered);
336+
let diagnostic_width = matches.opt_get("diagnostic-width").unwrap_or_default();
334337

335338
let codegen_options = CodegenOptions::build(matches, error_format);
336339
let debugging_opts = DebuggingOptions::build(matches, error_format);
337340

338-
let diag = new_handler(error_format, None, &debugging_opts);
341+
let diag = new_handler(error_format, None, diagnostic_width, &debugging_opts);
339342

340343
// check for deprecated options
341344
check_deprecated_options(matches, &diag);
@@ -699,6 +702,7 @@ impl Options {
699702
input,
700703
proc_macro_crate,
701704
error_format,
705+
diagnostic_width,
702706
libs,
703707
lib_strs,
704708
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+
diagnostic_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+
diagnostic_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+
diagnostic_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+
diagnostic_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+
diagnostic_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("diagnostic-width", |o| {
466+
o.optopt(
467+
"",
468+
"diagnostic-width",
469+
"Provide width of the output 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.diagnostic_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+
--diagnostic-width WIDTH
114+
Provide width of the output for truncated error
115+
messages
113116
--json CONFIG Configure the structure of JSON diagnostics
114117
--disable-minification
115118
Disable minification applied on JS files
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// compile-flags: -Zunstable-options --diagnostic-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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: this URL is not a hyperlink
2+
--> $DIR/diagnostic-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/diagnostic-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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// compile-flags: --diagnostic-width=20
2+
3+
// This test checks that `-Z output-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+
}

0 commit comments

Comments
 (0)