Skip to content

Commit 6360287

Browse files
committed
Fix #128930: Print documentation of CLI options missing their arg
1 parent b389b0a commit 6360287

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

compiler/rustc_driver_impl/src/lib.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -1221,17 +1221,30 @@ pub fn handle_options(early_dcx: &EarlyDiagCtxt, args: &[String]) -> Option<geto
12211221
// Parse with *all* options defined in the compiler, we don't worry about
12221222
// option stability here we just want to parse as much as possible.
12231223
let mut options = getopts::Options::new();
1224-
for option in config::rustc_optgroups() {
1224+
let optgroups = config::rustc_optgroups();
1225+
for option in &optgroups {
12251226
(option.apply)(&mut options);
12261227
}
12271228
let matches = options.parse(args).unwrap_or_else(|e| {
1228-
let msg = match e {
1229+
let msg: Option<String> = match e {
12291230
getopts::Fail::UnrecognizedOption(ref opt) => CG_OPTIONS
12301231
.iter()
12311232
.map(|&(name, ..)| ('C', name))
12321233
.chain(Z_OPTIONS.iter().map(|&(name, ..)| ('Z', name)))
12331234
.find(|&(_, name)| *opt == name.replace('_', "-"))
12341235
.map(|(flag, _)| format!("{e}. Did you mean `-{flag} {opt}`?")),
1236+
getopts::Fail::ArgumentMissing(ref opt) => {
1237+
optgroups.iter().find(|option| option.name == opt).map(|option| {
1238+
// Print the help just for the option in question.
1239+
let mut options = getopts::Options::new();
1240+
(option.apply)(&mut options);
1241+
// getopt requires us to pass a function for joining an iterator of
1242+
// strings, even though in this case we expect exactly one string.
1243+
options.usage_with_format(|it| {
1244+
it.fold(format!("{e}\nUsage:"), |a, b| a + "\n" + &b)
1245+
})
1246+
})
1247+
}
12351248
_ => None,
12361249
};
12371250
early_dcx.early_fatal(msg.unwrap_or_else(|| e.to_string()));

compiler/rustc_session/src/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,7 @@ enum OptionStability {
13491349

13501350
pub struct RustcOptGroup {
13511351
pub apply: Box<dyn Fn(&mut getopts::Options) -> &mut getopts::Options>,
1352-
name: &'static str,
1352+
pub name: &'static str,
13531353
stability: OptionStability,
13541354
}
13551355

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//@ compile-flags: --print
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: Argument to option 'print' missing
2+
Usage:
3+
--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]
4+
Compiler information to print on stdout
5+

0 commit comments

Comments
 (0)