From 6360287fd91c5d02597b45ef65c34388af348638 Mon Sep 17 00:00:00 2001 From: George Bateman Date: Sun, 11 Aug 2024 09:07:52 +0100 Subject: [PATCH 1/2] Fix #128930: Print documentation of CLI options missing their arg --- compiler/rustc_driver_impl/src/lib.rs | 17 +++++++++++++++-- compiler/rustc_session/src/config.rs | 2 +- .../invalid-compile-flags/print-without-arg.rs | 1 + .../print-without-arg.stderr | 5 +++++ 4 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 tests/ui/invalid-compile-flags/print-without-arg.rs create mode 100644 tests/ui/invalid-compile-flags/print-without-arg.stderr diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index 627a0ebb4e5..fa1b8e7c834 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -1221,17 +1221,30 @@ pub fn handle_options(early_dcx: &EarlyDiagCtxt, args: &[String]) -> Option = match e { getopts::Fail::UnrecognizedOption(ref opt) => CG_OPTIONS .iter() .map(|&(name, ..)| ('C', name)) .chain(Z_OPTIONS.iter().map(|&(name, ..)| ('Z', name))) .find(|&(_, name)| *opt == name.replace('_', "-")) .map(|(flag, _)| format!("{e}. Did you mean `-{flag} {opt}`?")), + getopts::Fail::ArgumentMissing(ref opt) => { + optgroups.iter().find(|option| option.name == opt).map(|option| { + // Print the help just for the option in question. + let mut options = getopts::Options::new(); + (option.apply)(&mut options); + // getopt requires us to pass a function for joining an iterator of + // strings, even though in this case we expect exactly one string. + options.usage_with_format(|it| { + it.fold(format!("{e}\nUsage:"), |a, b| a + "\n" + &b) + }) + }) + } _ => None, }; early_dcx.early_fatal(msg.unwrap_or_else(|| e.to_string())); diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index f58a991a616..6699fd8332b 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -1349,7 +1349,7 @@ enum OptionStability { pub struct RustcOptGroup { pub apply: Box &mut getopts::Options>, - name: &'static str, + pub name: &'static str, stability: OptionStability, } diff --git a/tests/ui/invalid-compile-flags/print-without-arg.rs b/tests/ui/invalid-compile-flags/print-without-arg.rs new file mode 100644 index 00000000000..a762cb22275 --- /dev/null +++ b/tests/ui/invalid-compile-flags/print-without-arg.rs @@ -0,0 +1 @@ +//@ compile-flags: --print diff --git a/tests/ui/invalid-compile-flags/print-without-arg.stderr b/tests/ui/invalid-compile-flags/print-without-arg.stderr new file mode 100644 index 00000000000..a18d2779cad --- /dev/null +++ b/tests/ui/invalid-compile-flags/print-without-arg.stderr @@ -0,0 +1,5 @@ +error: Argument to option 'print' missing + Usage: + --print [crate-name|file-names|sysroot|target-libdir|cfg|check-cfg|calling-conventions|target-list|target-cpus|target-features|relocation-models|code-models|tls-models|target-spec-json|all-target-specs-json|native-static-libs|stack-protector-strategies|link-args|deployment-target] + Compiler information to print on stdout + From ae75a9b33aef4bdd03f33bc75bdf44fe3a48356d Mon Sep 17 00:00:00 2001 From: George Bateman Date: Sun, 11 Aug 2024 09:58:11 +0100 Subject: [PATCH 2/2] Update expectations --- tests/ui/compiletest-self-test/compile-flags-last.stderr | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/ui/compiletest-self-test/compile-flags-last.stderr b/tests/ui/compiletest-self-test/compile-flags-last.stderr index d8d40a7d9f1..72d92206e2b 100644 --- a/tests/ui/compiletest-self-test/compile-flags-last.stderr +++ b/tests/ui/compiletest-self-test/compile-flags-last.stderr @@ -1,2 +1,5 @@ error: Argument to option 'cap-lints' missing + Usage: + --cap-lints LEVEL Set the most restrictive lint level. More restrictive + lints are capped at this level