Skip to content

Commit b20d969

Browse files
committed
Print valid --print requests if request is invalid
When someone makes a typo, it can be useful to see the valid options. This is also useful if someone wants to find out about all the options.
1 parent c493bae commit b20d969

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed

compiler/rustc_session/src/config.rs

+38-23
Original file line numberDiff line numberDiff line change
@@ -1788,34 +1788,49 @@ fn collect_print_requests(
17881788
cg.target_feature = String::new();
17891789
}
17901790

1791-
prints.extend(matches.opt_strs("print").into_iter().map(|s| match &*s {
1792-
"crate-name" => PrintRequest::CrateName,
1793-
"file-names" => PrintRequest::FileNames,
1794-
"sysroot" => PrintRequest::Sysroot,
1795-
"target-libdir" => PrintRequest::TargetLibdir,
1796-
"cfg" => PrintRequest::Cfg,
1797-
"calling-conventions" => PrintRequest::CallingConventions,
1798-
"target-list" => PrintRequest::TargetList,
1799-
"target-cpus" => PrintRequest::TargetCPUs,
1800-
"target-features" => PrintRequest::TargetFeatures,
1801-
"relocation-models" => PrintRequest::RelocationModels,
1802-
"code-models" => PrintRequest::CodeModels,
1803-
"tls-models" => PrintRequest::TlsModels,
1804-
"native-static-libs" => PrintRequest::NativeStaticLibs,
1805-
"stack-protector-strategies" => PrintRequest::StackProtectorStrategies,
1806-
"target-spec-json" => {
1807-
if unstable_opts.unstable_options {
1808-
PrintRequest::TargetSpec
1809-
} else {
1791+
const PRINT_REQUESTS: &[(&str, PrintRequest)] = &[
1792+
("crate-name", PrintRequest::CrateName),
1793+
("file-names", PrintRequest::FileNames),
1794+
("sysroot", PrintRequest::Sysroot),
1795+
("target-libdir", PrintRequest::TargetLibdir),
1796+
("cfg", PrintRequest::Cfg),
1797+
("calling-conventions", PrintRequest::CallingConventions),
1798+
("target-list", PrintRequest::TargetList),
1799+
("target-cpus", PrintRequest::TargetCPUs),
1800+
("target-features", PrintRequest::TargetFeatures),
1801+
("relocation-models", PrintRequest::RelocationModels),
1802+
("code-models", PrintRequest::CodeModels),
1803+
("tls-models", PrintRequest::TlsModels),
1804+
("native-static-libs", PrintRequest::NativeStaticLibs),
1805+
("stack-protector-strategies", PrintRequest::StackProtectorStrategies),
1806+
("target-spec-json", PrintRequest::TargetSpec),
1807+
("link-args", PrintRequest::LinkArgs),
1808+
];
1809+
1810+
prints.extend(matches.opt_strs("print").into_iter().map(|req| {
1811+
match PRINT_REQUESTS.iter().find(|&&(name, _)| name == req) {
1812+
Some((_, PrintRequest::TargetSpec)) => {
1813+
if unstable_opts.unstable_options {
1814+
PrintRequest::TargetSpec
1815+
} else {
1816+
early_error(
1817+
error_format,
1818+
"the `-Z unstable-options` flag must also be passed to \
1819+
enable the target-spec-json print option",
1820+
);
1821+
}
1822+
}
1823+
Some(&(_, print_request)) => print_request,
1824+
None => {
1825+
let prints =
1826+
PRINT_REQUESTS.iter().map(|(name, _)| format!("`{name}`")).collect::<Vec<_>>();
1827+
let prints = prints.join(", ");
18101828
early_error(
18111829
error_format,
1812-
"the `-Z unstable-options` flag must also be passed to \
1813-
enable the target-spec-json print option",
1830+
&format!("unknown print request `{req}`. Valid print requests are: {prints}"),
18141831
);
18151832
}
18161833
}
1817-
"link-args" => PrintRequest::LinkArgs,
1818-
req => early_error(error_format, &format!("unknown print request `{req}`")),
18191834
}));
18201835

18211836
prints
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include ../../run-make-fulldeps/tools.mk
2+
3+
all:
4+
$(RUSTC) --print uwu 2>&1 | diff - valid-print-requests.stderr
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: unknown print request `uwu`. Valid print requests are: `crate-name`, `file-names`, `sysroot`, `target-libdir`, `cfg`, `calling-conventions`, `target-list`, `target-cpus`, `target-features`, `relocation-models`, `code-models`, `tls-models`, `native-static-libs`, `stack-protector-strategies`, `target-spec-json`, `link-args`
2+

0 commit comments

Comments
 (0)