From dd46789e6f9363aa4efd7ce5c5f4b40be22e88a1 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Fri, 23 Sep 2022 11:50:03 -0700 Subject: [PATCH] Stabilize terminal-width --- src/cargo/core/compiler/mod.rs | 18 ++----------- src/cargo/core/features.rs | 16 +++-------- src/cargo/core/shell.rs | 3 +++ src/doc/src/reference/unstable.md | 44 +++++-------------------------- tests/testsuite/build.rs | 15 +++++++---- 5 files changed, 25 insertions(+), 71 deletions(-) diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index ed2c6d9bc568..56347d527737 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -899,22 +899,8 @@ fn add_error_format_and_color(cx: &Context<'_, '_>, cmd: &mut ProcessBuilder) { cmd.arg(json); let config = cx.bcx.config; - if config.nightly_features_allowed { - match ( - config.cli_unstable().terminal_width, - config.shell().err_width().diagnostic_terminal_width(), - ) { - // Terminal width explicitly provided - only useful for testing. - (Some(Some(width)), _) => { - cmd.arg(format!("--diagnostic-width={}", width)); - } - // Terminal width was not explicitly provided but flag was provided - common case. - (Some(None), Some(width)) => { - cmd.arg(format!("--diagnostic-width={}", width)); - } - // User didn't opt-in. - _ => (), - } + if let Some(width) = config.shell().err_width().diagnostic_terminal_width() { + cmd.arg(format!("--diagnostic-width={}", width)); } } diff --git a/src/cargo/core/features.rs b/src/cargo/core/features.rs index f295f063be10..28abfea49bac 100644 --- a/src/cargo/core/features.rs +++ b/src/cargo/core/features.rs @@ -686,7 +686,6 @@ unstable_cli_options!( target_applies_to_host: bool = ("Enable the `target-applies-to-host` key in the .cargo/config.toml file"), rustdoc_map: bool = ("Allow passing external documentation mappings to rustdoc"), separate_nightlies: bool = (HIDDEN), - terminal_width: Option> = ("Provide a terminal width to rustc for error truncation"), publish_timeout: bool = ("Enable the `publish.timeout` key in .cargo/config.toml file"), unstable_options: bool = ("Allow the usage of unstable options"), skip_rustdoc_fingerprint: bool = (HIDDEN), @@ -749,6 +748,9 @@ const STABILIZED_TIMINGS: &str = "The -Ztimings option has been stabilized as -- const STABILISED_MULTITARGET: &str = "Multiple `--target` options are now always available."; +const STABILIZED_TERMINAL_WIDTH: &str = + "The -Zterminal-width option is now always enabled for terminal output."; + fn deserialize_build_std<'de, D>(deserializer: D) -> Result>, D::Error> where D: serde::Deserializer<'de>, @@ -862,16 +864,6 @@ impl CliUnstable { Ok(true) } - fn parse_usize_opt(value: Option<&str>) -> CargoResult> { - Ok(match value { - Some(value) => match value.parse::() { - Ok(value) => Some(value), - Err(e) => bail!("expected a number, found: {}", e), - }, - None => None, - }) - } - let mut stabilized_warn = |key: &str, version: &str, message: &str| { warnings.push(format!( "flag `-Z {}` has been stabilized in the {} release, \ @@ -955,7 +947,7 @@ impl CliUnstable { "separate-nightlies" => self.separate_nightlies = parse_empty(k, v)?, "multitarget" => stabilized_warn(k, "1.64", STABILISED_MULTITARGET), "rustdoc-map" => self.rustdoc_map = parse_empty(k, v)?, - "terminal-width" => self.terminal_width = Some(parse_usize_opt(v)?), + "terminal-width" => stabilized_warn(k, "1.68", STABILIZED_TERMINAL_WIDTH), "sparse-registry" => self.sparse_registry = parse_empty(k, v)?, "registry-auth" => self.registry_auth = parse_empty(k, v)?, "namespaced-features" => stabilized_warn(k, "1.60", STABILISED_NAMESPACED_FEATURES), diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index 473c574fb53a..d7ef145ae9fe 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -17,6 +17,9 @@ impl TtyWidth { /// Returns the width provided with `-Z terminal-width` to rustc to truncate diagnostics with /// long lines. pub fn diagnostic_terminal_width(&self) -> Option { + if let Ok(width) = std::env::var("__CARGO_TEST_TTY_WIDTH_DO_NOT_USE_THIS") { + return Some(width.parse().unwrap()); + } match *self { TtyWidth::NoTty | TtyWidth::Guess(_) => None, TtyWidth::Known(width) => Some(width), diff --git a/src/doc/src/reference/unstable.md b/src/doc/src/reference/unstable.md index 64fef581dd63..d232865cfcfd 100644 --- a/src/doc/src/reference/unstable.md +++ b/src/doc/src/reference/unstable.md @@ -70,7 +70,6 @@ Each new feature described below should explain how to use it. * [public-dependency](#public-dependency) — Allows dependencies to be classified as either public or private. * Output behavior * [out-dir](#out-dir) — Adds a directory where artifacts are copied to. - * [terminal-width](#terminal-width) — Tells rustc the width of the terminal so that long diagnostic messages can be truncated to be more readable. * [Different binary name](#different-binary-name) — Assign a name to the built binary that is separate from the crate name. * Compile behavior * [mtime-on-use](#mtime-on-use) — Updates the last-modified timestamp on every dependency every time it is used, to provide a mechanism to delete unused artifacts. @@ -723,43 +722,6 @@ The default value is `"remote"`. The value may also take a URL for a custom location. -### terminal-width - -* Tracking Issue: [#84673](https://github.com/rust-lang/rust/issues/84673) - -This feature provides a new flag, `-Z terminal-width`, which is used to pass -a terminal width to `rustc` so that error messages containing long lines -can be intelligently truncated. - -For example, passing `-Z terminal-width=20` (an arbitrarily low value) might -produce the following error: - -```text -error[E0308]: mismatched types - --> src/main.rs:2:17 - | -2 | ..._: () = 42; - | -- ^^ expected `()`, found integer - | | - | expected due to this - -error: aborting due to previous error -``` - -In contrast, without `-Z terminal-width`, the error would look as shown below: - -```text -error[E0308]: mismatched types - --> src/main.rs:2:17 - | -2 | let _: () = 42; - | -- ^^ expected `()`, found integer - | | - | expected due to this - -error: aborting due to previous error -``` - ### per-package-target * Tracking Issue: [#9406](https://github.com/rust-lang/cargo/pull/9406) * Original Pull Request: [#9030](https://github.com/rust-lang/cargo/pull/9030) @@ -1447,3 +1409,9 @@ See [workspace.package](workspaces.md#the-package-table), [workspace.dependencies](workspaces.md#the-dependencies-table), and [inheriting-a-dependency-from-a-workspace](specifying-dependencies.md#inheriting-a-dependency-from-a-workspace) for more information. + +### terminal-width + +The `-Z terminal-width` option has been stabilized in the 1.68 release. +The terminal width is always passed to the compiler when running from a +terminal where Cargo can automatically detect the width. diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 62aea1bc6520..73aa439ceb34 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -6130,23 +6130,28 @@ fn target_directory_backup_exclusion() { assert!(!&cachedir_tag.is_file()); } -#[cargo_test(>=1.64, reason = "--diagnostic-width is stabilized in 1.64")] +#[cargo_test] fn simple_terminal_width() { let p = project() .file( "src/lib.rs", r#" - fn main() { + pub fn foo() { let _: () = 42; } "#, ) .build(); - p.cargo("build -Zterminal-width=20") - .masquerade_as_nightly_cargo(&["terminal-width"]) + p.cargo("build -v") + .env("__CARGO_TEST_TTY_WIDTH_DO_NOT_USE_THIS", "20") .with_status(101) - .with_stderr_contains("3 | ..._: () = 42;") + .with_stderr_contains("[RUNNING] `rustc [..]--diagnostic-width=20[..]") + .run(); + + p.cargo("doc -v") + .env("__CARGO_TEST_TTY_WIDTH_DO_NOT_USE_THIS", "20") + .with_stderr_contains("[RUNNING] `rustdoc [..]--diagnostic-width=20[..]") .run(); }