Skip to content

Commit c2affd5

Browse files
authored
Rollup merge of #103839 - Nilstrieb:print-list, r=compiler-errors
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.
2 parents 617f246 + b20d969 commit c2affd5

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
@@ -1789,34 +1789,49 @@ fn collect_print_requests(
17891789
cg.target_feature = String::new();
17901790
}
17911791

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

18221837
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)