1
Fork 0

Rollup merge of #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 #84673.

r? ```@oli-obk```
This commit is contained in:
Matthias Krüger 2022-07-08 08:00:36 +02:00 committed by GitHub
commit b36e58a458
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 101 additions and 46 deletions

View file

@ -63,7 +63,7 @@ impl HumanReadableErrorType {
bundle: Option<Lrc<FluentBundle>>, bundle: Option<Lrc<FluentBundle>>,
fallback_bundle: LazyFallbackBundle, fallback_bundle: LazyFallbackBundle,
teach: bool, teach: bool,
terminal_width: Option<usize>, diagnostic_width: Option<usize>,
macro_backtrace: bool, macro_backtrace: bool,
) -> EmitterWriter { ) -> EmitterWriter {
let (short, color_config) = self.unzip(); let (short, color_config) = self.unzip();
@ -76,7 +76,7 @@ impl HumanReadableErrorType {
short, short,
teach, teach,
color, color,
terminal_width, diagnostic_width,
macro_backtrace, macro_backtrace,
) )
} }
@ -710,7 +710,7 @@ pub struct EmitterWriter {
short_message: bool, short_message: bool,
teach: bool, teach: bool,
ui_testing: bool, ui_testing: bool,
terminal_width: Option<usize>, diagnostic_width: Option<usize>,
macro_backtrace: bool, macro_backtrace: bool,
} }
@ -730,7 +730,7 @@ impl EmitterWriter {
fallback_bundle: LazyFallbackBundle, fallback_bundle: LazyFallbackBundle,
short_message: bool, short_message: bool,
teach: bool, teach: bool,
terminal_width: Option<usize>, diagnostic_width: Option<usize>,
macro_backtrace: bool, macro_backtrace: bool,
) -> EmitterWriter { ) -> EmitterWriter {
let dst = Destination::from_stderr(color_config); let dst = Destination::from_stderr(color_config);
@ -742,7 +742,7 @@ impl EmitterWriter {
short_message, short_message,
teach, teach,
ui_testing: false, ui_testing: false,
terminal_width, diagnostic_width,
macro_backtrace, macro_backtrace,
} }
} }
@ -755,7 +755,7 @@ impl EmitterWriter {
short_message: bool, short_message: bool,
teach: bool, teach: bool,
colored: bool, colored: bool,
terminal_width: Option<usize>, diagnostic_width: Option<usize>,
macro_backtrace: bool, macro_backtrace: bool,
) -> EmitterWriter { ) -> EmitterWriter {
EmitterWriter { EmitterWriter {
@ -766,7 +766,7 @@ impl EmitterWriter {
short_message, short_message,
teach, teach,
ui_testing: false, ui_testing: false,
terminal_width, diagnostic_width,
macro_backtrace, macro_backtrace,
} }
} }
@ -1615,7 +1615,7 @@ impl EmitterWriter {
width_offset + annotated_file.multiline_depth + 1 width_offset + annotated_file.multiline_depth + 1
}; };
let column_width = if let Some(width) = self.terminal_width { let column_width = if let Some(width) = self.diagnostic_width {
width.saturating_sub(code_offset) width.saturating_sub(code_offset)
} else if self.ui_testing { } else if self.ui_testing {
DEFAULT_COLUMN_WIDTH DEFAULT_COLUMN_WIDTH

View file

@ -42,7 +42,7 @@ pub struct JsonEmitter {
pretty: bool, pretty: bool,
ui_testing: bool, ui_testing: bool,
json_rendered: HumanReadableErrorType, json_rendered: HumanReadableErrorType,
terminal_width: Option<usize>, diagnostic_width: Option<usize>,
macro_backtrace: bool, macro_backtrace: bool,
} }
@ -54,7 +54,7 @@ impl JsonEmitter {
fallback_bundle: LazyFallbackBundle, fallback_bundle: LazyFallbackBundle,
pretty: bool, pretty: bool,
json_rendered: HumanReadableErrorType, json_rendered: HumanReadableErrorType,
terminal_width: Option<usize>, diagnostic_width: Option<usize>,
macro_backtrace: bool, macro_backtrace: bool,
) -> JsonEmitter { ) -> JsonEmitter {
JsonEmitter { JsonEmitter {
@ -66,7 +66,7 @@ impl JsonEmitter {
pretty, pretty,
ui_testing: false, ui_testing: false,
json_rendered, json_rendered,
terminal_width, diagnostic_width,
macro_backtrace, macro_backtrace,
} }
} }
@ -76,7 +76,7 @@ impl JsonEmitter {
json_rendered: HumanReadableErrorType, json_rendered: HumanReadableErrorType,
fluent_bundle: Option<Lrc<FluentBundle>>, fluent_bundle: Option<Lrc<FluentBundle>>,
fallback_bundle: LazyFallbackBundle, fallback_bundle: LazyFallbackBundle,
terminal_width: Option<usize>, diagnostic_width: Option<usize>,
macro_backtrace: bool, macro_backtrace: bool,
) -> JsonEmitter { ) -> JsonEmitter {
let file_path_mapping = FilePathMapping::empty(); let file_path_mapping = FilePathMapping::empty();
@ -87,7 +87,7 @@ impl JsonEmitter {
fallback_bundle, fallback_bundle,
pretty, pretty,
json_rendered, json_rendered,
terminal_width, diagnostic_width,
macro_backtrace, macro_backtrace,
) )
} }
@ -100,7 +100,7 @@ impl JsonEmitter {
fallback_bundle: LazyFallbackBundle, fallback_bundle: LazyFallbackBundle,
pretty: bool, pretty: bool,
json_rendered: HumanReadableErrorType, json_rendered: HumanReadableErrorType,
terminal_width: Option<usize>, diagnostic_width: Option<usize>,
macro_backtrace: bool, macro_backtrace: bool,
) -> JsonEmitter { ) -> JsonEmitter {
JsonEmitter { JsonEmitter {
@ -112,7 +112,7 @@ impl JsonEmitter {
pretty, pretty,
ui_testing: false, ui_testing: false,
json_rendered, json_rendered,
terminal_width, diagnostic_width,
macro_backtrace, macro_backtrace,
} }
} }
@ -345,7 +345,7 @@ impl Diagnostic {
je.fluent_bundle.clone(), je.fluent_bundle.clone(),
je.fallback_bundle.clone(), je.fallback_bundle.clone(),
false, false,
je.terminal_width, je.diagnostic_width,
je.macro_backtrace, je.macro_backtrace,
) )
.ui_testing(je.ui_testing) .ui_testing(je.ui_testing)

View file

@ -689,7 +689,6 @@ fn test_debugging_options_tracking_hash() {
untracked!(span_debug, true); untracked!(span_debug, true);
untracked!(span_free_formats, true); untracked!(span_free_formats, true);
untracked!(temps_dir, Some(String::from("abc"))); untracked!(temps_dir, Some(String::from("abc")));
untracked!(terminal_width, Some(80));
untracked!(threads, 99); untracked!(threads, 99);
untracked!(time, true); untracked!(time, true);
untracked!(time_llvm_passes, true); untracked!(time_llvm_passes, true);

View file

@ -726,6 +726,7 @@ impl Default for Options {
prints: Vec::new(), prints: Vec::new(),
cg: Default::default(), cg: Default::default(),
error_format: ErrorOutputType::default(), error_format: ErrorOutputType::default(),
diagnostic_width: None,
externs: Externs(BTreeMap::new()), externs: Externs(BTreeMap::new()),
crate_name: None, crate_name: None,
libs: Vec::new(), libs: Vec::new(),
@ -1427,6 +1428,12 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
never = never colorize output", never = never colorize output",
"auto|always|never", "auto|always|never",
), ),
opt::opt_s(
"",
"diagnostic-width",
"Inform rustc of the width of the output so that diagnostics can be truncated to fit",
"WIDTH",
),
opt::multi_s( opt::multi_s(
"", "",
"remap-path-prefix", "remap-path-prefix",
@ -2202,6 +2209,10 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
let error_format = parse_error_format(matches, color, json_rendered); let error_format = parse_error_format(matches, color, json_rendered);
let diagnostic_width = matches.opt_get("diagnostic-width").unwrap_or_else(|_| {
early_error(error_format, "`--diagnostic-width` must be an positive integer");
});
let unparsed_crate_types = matches.opt_strs("crate-type"); let unparsed_crate_types = matches.opt_strs("crate-type");
let crate_types = parse_crate_types_from_list(unparsed_crate_types) let crate_types = parse_crate_types_from_list(unparsed_crate_types)
.unwrap_or_else(|e| early_error(error_format, &e)); .unwrap_or_else(|e| early_error(error_format, &e));
@ -2474,6 +2485,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
prints, prints,
cg, cg,
error_format, error_format,
diagnostic_width,
externs, externs,
unstable_features: UnstableFeatures::from_environment(crate_name.as_deref()), unstable_features: UnstableFeatures::from_environment(crate_name.as_deref()),
crate_name, crate_name,

View file

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

View file

@ -1162,7 +1162,7 @@ fn default_emitter(
fallback_bundle, fallback_bundle,
short, short,
sopts.debugging_opts.teach, sopts.debugging_opts.teach,
sopts.debugging_opts.terminal_width, sopts.diagnostic_width,
macro_backtrace, macro_backtrace,
), ),
Some(dst) => EmitterWriter::new( Some(dst) => EmitterWriter::new(
@ -1173,7 +1173,7 @@ fn default_emitter(
short, short,
false, // no teach messages when writing to a buffer false, // no teach messages when writing to a buffer
false, // no colors when writing to a buffer false, // no colors when writing to a buffer
None, // no terminal width None, // no diagnostic width
macro_backtrace, macro_backtrace,
), ),
}; };
@ -1188,7 +1188,7 @@ fn default_emitter(
fallback_bundle, fallback_bundle,
pretty, pretty,
json_rendered, json_rendered,
sopts.debugging_opts.terminal_width, sopts.diagnostic_width,
macro_backtrace, macro_backtrace,
) )
.ui_testing(sopts.debugging_opts.ui_testing), .ui_testing(sopts.debugging_opts.ui_testing),
@ -1202,7 +1202,7 @@ fn default_emitter(
fallback_bundle, fallback_bundle,
pretty, pretty,
json_rendered, json_rendered,
sopts.debugging_opts.terminal_width, sopts.diagnostic_width,
macro_backtrace, macro_backtrace,
) )
.ui_testing(sopts.debugging_opts.ui_testing), .ui_testing(sopts.debugging_opts.ui_testing),

View file

@ -73,6 +73,8 @@ pub(crate) struct Options {
pub(crate) proc_macro_crate: bool, pub(crate) proc_macro_crate: bool,
/// How to format errors and warnings. /// How to format errors and warnings.
pub(crate) error_format: ErrorOutputType, pub(crate) error_format: ErrorOutputType,
/// Width of output buffer to truncate errors appropriately.
pub(crate) diagnostic_width: Option<usize>,
/// Library search paths to hand to the compiler. /// Library search paths to hand to the compiler.
pub(crate) libs: Vec<SearchPath>, pub(crate) libs: Vec<SearchPath>,
/// Library search paths strings to hand to the compiler. /// Library search paths strings to hand to the compiler.
@ -331,11 +333,12 @@ impl Options {
let config::JsonConfig { json_rendered, json_unused_externs, .. } = let config::JsonConfig { json_rendered, json_unused_externs, .. } =
config::parse_json(matches); config::parse_json(matches);
let error_format = config::parse_error_format(matches, color, json_rendered); let error_format = config::parse_error_format(matches, color, json_rendered);
let diagnostic_width = matches.opt_get("diagnostic-width").unwrap_or_default();
let codegen_options = CodegenOptions::build(matches, error_format); let codegen_options = CodegenOptions::build(matches, error_format);
let debugging_opts = DebuggingOptions::build(matches, error_format); let debugging_opts = DebuggingOptions::build(matches, error_format);
let diag = new_handler(error_format, None, &debugging_opts); let diag = new_handler(error_format, None, diagnostic_width, &debugging_opts);
// check for deprecated options // check for deprecated options
check_deprecated_options(matches, &diag); check_deprecated_options(matches, &diag);
@ -699,6 +702,7 @@ impl Options {
input, input,
proc_macro_crate, proc_macro_crate,
error_format, error_format,
diagnostic_width,
libs, libs,
lib_strs, lib_strs,
externs, externs,

View file

@ -154,6 +154,7 @@ impl<'tcx> DocContext<'tcx> {
pub(crate) fn new_handler( pub(crate) fn new_handler(
error_format: ErrorOutputType, error_format: ErrorOutputType,
source_map: Option<Lrc<source_map::SourceMap>>, source_map: Option<Lrc<source_map::SourceMap>>,
diagnostic_width: Option<usize>,
debugging_opts: &DebuggingOptions, debugging_opts: &DebuggingOptions,
) -> rustc_errors::Handler { ) -> rustc_errors::Handler {
let fallback_bundle = let fallback_bundle =
@ -169,7 +170,7 @@ pub(crate) fn new_handler(
fallback_bundle, fallback_bundle,
short, short,
debugging_opts.teach, debugging_opts.teach,
debugging_opts.terminal_width, diagnostic_width,
false, false,
) )
.ui_testing(debugging_opts.ui_testing), .ui_testing(debugging_opts.ui_testing),
@ -187,7 +188,7 @@ pub(crate) fn new_handler(
fallback_bundle, fallback_bundle,
pretty, pretty,
json_rendered, json_rendered,
debugging_opts.terminal_width, diagnostic_width,
false, false,
) )
.ui_testing(debugging_opts.ui_testing), .ui_testing(debugging_opts.ui_testing),
@ -208,6 +209,7 @@ pub(crate) fn create_config(
crate_name, crate_name,
proc_macro_crate, proc_macro_crate,
error_format, error_format,
diagnostic_width,
libs, libs,
externs, externs,
mut cfgs, mut cfgs,
@ -266,6 +268,7 @@ pub(crate) fn create_config(
actually_rustdoc: true, actually_rustdoc: true,
debugging_opts, debugging_opts,
error_format, error_format,
diagnostic_width,
edition, edition,
describe_lints, describe_lints,
crate_name, crate_name,

View file

@ -462,6 +462,14 @@ fn opts() -> Vec<RustcOptGroup> {
"human|json|short", "human|json|short",
) )
}), }),
unstable("diagnostic-width", |o| {
o.optopt(
"",
"diagnostic-width",
"Provide width of the output for truncated error messages",
"WIDTH",
)
}),
stable("json", |o| { stable("json", |o| {
o.optopt("", "json", "Configure the structure of JSON diagnostics", "CONFIG") o.optopt("", "json", "Configure the structure of JSON diagnostics", "CONFIG")
}), }),
@ -733,7 +741,12 @@ fn run_renderer<'tcx, T: formats::FormatRenderer<'tcx>>(
} }
fn main_options(options: config::Options) -> MainResult { fn main_options(options: config::Options) -> MainResult {
let diag = core::new_handler(options.error_format, None, &options.debugging_opts); let diag = core::new_handler(
options.error_format,
None,
options.diagnostic_width,
&options.debugging_opts,
);
match (options.should_test, options.markdown_input()) { match (options.should_test, options.markdown_input()) {
(true, true) => return wrap_return(&diag, markdown::test(options)), (true, true) => return wrap_return(&diag, markdown::test(options)),

View file

@ -110,6 +110,9 @@ Options:
never = never colorize output never = never colorize output
--error-format human|json|short --error-format human|json|short
How errors and other messages are produced How errors and other messages are produced
--diagnostic-width WIDTH
Provide width of the output for truncated error
messages
--json CONFIG Configure the structure of JSON diagnostics --json CONFIG Configure the structure of JSON diagnostics
--disable-minification --disable-minification
Disable minification applied on JS files Disable minification applied on JS files

View file

@ -0,0 +1,5 @@
// compile-flags: -Zunstable-options --diagnostic-width=10
#![deny(rustdoc::bare_urls)]
/// This is a long line that contains a http://link.com
pub struct Foo; //~^ ERROR

View file

@ -0,0 +1,15 @@
error: this URL is not a hyperlink
--> $DIR/diagnostic-width.rs:4:41
|
LL | ... a http://link.com
| ^^^^^^^^^^^^^^^ help: use an automatic link instead: `<http://link.com>`
|
note: the lint level is defined here
--> $DIR/diagnostic-width.rs:2:9
|
LL | ...ny(rustdoc::bare_url...
| ^^^^^^^^^^^^^^^^^^
= note: bare URLs are not automatically turned into clickable links
error: aborting due to previous error

View file

@ -0,0 +1,9 @@
// compile-flags: --diagnostic-width=20
// This test checks that `-Z output-width` effects the human error output by restricting it to an
// arbitrarily low value so that the effect is visible.
fn main() {
let _: () = 42;
//~^ ERROR mismatched types
}

View file

@ -0,0 +1,9 @@
// compile-flags: --diagnostic-width=20 --error-format=json
// This test checks that `-Z output-width` effects the JSON error output by restricting it to an
// arbitrarily low value so that the effect is visible.
fn main() {
let _: () = 42;
//~^ ERROR arguments to this function are incorrect
}

View file

@ -24,7 +24,7 @@ This error occurs when an expression was used in a place where the compiler
expected an expression of a different type. It can occur in several cases, the expected an expression of a different type. It can occur in several cases, the
most common being when calling a function and passing an argument which has a most common being when calling a function and passing an argument which has a
different type than the matching type in the function declaration. different type than the matching type in the function declaration.
"},"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 "},"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
--> $DIR/flag-json.rs:7:17 --> $DIR/flag-json.rs:7:17
| |
LL | ..._: () = 42; LL | ..._: () = 42;

View file

@ -1,9 +0,0 @@
// compile-flags: -Z terminal-width=20
// This test checks that `-Z terminal-width` effects the human error output by restricting it to an
// arbitrarily low value so that the effect is visible.
fn main() {
let _: () = 42;
//~^ ERROR mismatched types
}

View file

@ -1,9 +0,0 @@
// compile-flags: -Z terminal-width=20 --error-format=json
// This test checks that `-Z terminal-width` effects the JSON error output by restricting it to an
// arbitrarily low value so that the effect is visible.
fn main() {
let _: () = 42;
//~^ ERROR arguments to this function are incorrect
}