Skip to content

Commit 2603ead

Browse files
authored
Unrolled build for #148990
Rollup merge of #148990 - Zalathar:print-kind, r=WaffleLapkin Exhaustively specify names and stability of `--print` values While trying to add a new unstable `--print` kind for use by compiletest, I found that the relevant code is quite awkward to work with, for a few reasons: - It's spread across various parts of a multi-thousand-line source file. - All newly-added `PrintKind` values are automatically treated as *stable*, unless they are explicitly marked as unstable in a helper function far away. - Parsing `--print` values relies on a separate table of name/value mappings, but there's no exhaustiveness check for that table. This PR therefore: - Extracts the relevant code into its own `print_request` submodule. - Uses a macro-rules derive to obtain an exhaustive list of values. - Uses exhaustive matches to associate a name and stability status with each `PrintKind` value. --- The first commit moves code to a separate module; the second commit contains actual changes. There should be no change to compiler output.
2 parents 5f7653d + 9c7e7cc commit 2603ead

File tree

4 files changed

+255
-177
lines changed

4 files changed

+255
-177
lines changed

compiler/rustc_session/src/config.rs

Lines changed: 4 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use tracing::debug;
3636

3737
pub use crate::config::cfg::{Cfg, CheckCfg, ExpectedValues};
3838
use crate::config::native_libs::parse_native_libs;
39+
pub use crate::config::print_request::{PrintKind, PrintRequest};
3940
use crate::errors::FileWriteFail;
4041
pub use crate::options::*;
4142
use crate::search_paths::SearchPath;
@@ -45,37 +46,9 @@ use crate::{EarlyDiagCtxt, HashStableContext, Session, filesearch, lint};
4546
mod cfg;
4647
mod externs;
4748
mod native_libs;
49+
mod print_request;
4850
pub mod sigpipe;
4951

50-
pub const PRINT_KINDS: &[(&str, PrintKind)] = &[
51-
// tidy-alphabetical-start
52-
("all-target-specs-json", PrintKind::AllTargetSpecsJson),
53-
("calling-conventions", PrintKind::CallingConventions),
54-
("cfg", PrintKind::Cfg),
55-
("check-cfg", PrintKind::CheckCfg),
56-
("code-models", PrintKind::CodeModels),
57-
("crate-name", PrintKind::CrateName),
58-
("crate-root-lint-levels", PrintKind::CrateRootLintLevels),
59-
("deployment-target", PrintKind::DeploymentTarget),
60-
("file-names", PrintKind::FileNames),
61-
("host-tuple", PrintKind::HostTuple),
62-
("link-args", PrintKind::LinkArgs),
63-
("native-static-libs", PrintKind::NativeStaticLibs),
64-
("relocation-models", PrintKind::RelocationModels),
65-
("split-debuginfo", PrintKind::SplitDebuginfo),
66-
("stack-protector-strategies", PrintKind::StackProtectorStrategies),
67-
("supported-crate-types", PrintKind::SupportedCrateTypes),
68-
("sysroot", PrintKind::Sysroot),
69-
("target-cpus", PrintKind::TargetCPUs),
70-
("target-features", PrintKind::TargetFeatures),
71-
("target-libdir", PrintKind::TargetLibdir),
72-
("target-list", PrintKind::TargetList),
73-
("target-spec-json", PrintKind::TargetSpecJson),
74-
("target-spec-json-schema", PrintKind::TargetSpecJsonSchema),
75-
("tls-models", PrintKind::TlsModels),
76-
// tidy-alphabetical-end
77-
];
78-
7952
/// The different settings that the `-C strip` flag can have.
8053
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
8154
pub enum Strip {
@@ -1015,42 +988,6 @@ impl ExternEntry {
1015988
}
1016989
}
1017990

1018-
#[derive(Clone, PartialEq, Debug)]
1019-
pub struct PrintRequest {
1020-
pub kind: PrintKind,
1021-
pub out: OutFileName,
1022-
}
1023-
1024-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
1025-
pub enum PrintKind {
1026-
// tidy-alphabetical-start
1027-
AllTargetSpecsJson,
1028-
CallingConventions,
1029-
Cfg,
1030-
CheckCfg,
1031-
CodeModels,
1032-
CrateName,
1033-
CrateRootLintLevels,
1034-
DeploymentTarget,
1035-
FileNames,
1036-
HostTuple,
1037-
LinkArgs,
1038-
NativeStaticLibs,
1039-
RelocationModels,
1040-
SplitDebuginfo,
1041-
StackProtectorStrategies,
1042-
SupportedCrateTypes,
1043-
Sysroot,
1044-
TargetCPUs,
1045-
TargetFeatures,
1046-
TargetLibdir,
1047-
TargetList,
1048-
TargetSpecJson,
1049-
TargetSpecJsonSchema,
1050-
TlsModels,
1051-
// tidy-alphabetical-end
1052-
}
1053-
1054991
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Default)]
1055992
pub struct NextSolverConfig {
1056993
/// Whether the new trait solver should be enabled in coherence.
@@ -1798,14 +1735,6 @@ The default is {DEFAULT_EDITION} and the latest stable edition is {LATEST_STABLE
17981735
)
17991736
});
18001737

1801-
static PRINT_HELP: LazyLock<String> = LazyLock::new(|| {
1802-
format!(
1803-
"Compiler information to print on stdout (or to a file)\n\
1804-
INFO may be one of <{}>.",
1805-
PRINT_KINDS.iter().map(|(name, _)| format!("{name}")).collect::<Vec<_>>().join("|")
1806-
)
1807-
});
1808-
18091738
static EMIT_HELP: LazyLock<String> = LazyLock::new(|| {
18101739
let mut result =
18111740
String::from("Comma separated list of types of output for the compiler to emit.\n");
@@ -1872,7 +1801,7 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
18721801
opt(Stable, Opt, "", "crate-name", "Specify the name of the crate being built", "<NAME>"),
18731802
opt(Stable, Opt, "", "edition", &EDITION_STRING, EDITION_NAME_LIST),
18741803
opt(Stable, Multi, "", "emit", &EMIT_HELP, "<TYPE>[=<FILE>]"),
1875-
opt(Stable, Multi, "", "print", &PRINT_HELP, "<INFO>[=<FILE>]"),
1804+
opt(Stable, Multi, "", "print", &print_request::PRINT_HELP, "<INFO>[=<FILE>]"),
18761805
opt(Stable, FlagMulti, "g", "", "Equivalent to -C debuginfo=2", ""),
18771806
opt(Stable, FlagMulti, "O", "", "Equivalent to -C opt-level=3", ""),
18781807
opt(Stable, Opt, "o", "", "Write output to FILENAME", "<FILENAME>"),
@@ -2320,108 +2249,6 @@ fn should_override_cgus_and_disable_thinlto(
23202249
(disable_local_thinlto, codegen_units)
23212250
}
23222251

2323-
fn collect_print_requests(
2324-
early_dcx: &EarlyDiagCtxt,
2325-
cg: &mut CodegenOptions,
2326-
unstable_opts: &UnstableOptions,
2327-
matches: &getopts::Matches,
2328-
) -> Vec<PrintRequest> {
2329-
let mut prints = Vec::<PrintRequest>::new();
2330-
if cg.target_cpu.as_deref() == Some("help") {
2331-
prints.push(PrintRequest { kind: PrintKind::TargetCPUs, out: OutFileName::Stdout });
2332-
cg.target_cpu = None;
2333-
};
2334-
if cg.target_feature == "help" {
2335-
prints.push(PrintRequest { kind: PrintKind::TargetFeatures, out: OutFileName::Stdout });
2336-
cg.target_feature = String::new();
2337-
}
2338-
2339-
// We disallow reusing the same path in multiple prints, such as `--print
2340-
// cfg=output.txt --print link-args=output.txt`, because outputs are printed
2341-
// by disparate pieces of the compiler, and keeping track of which files
2342-
// need to be overwritten vs appended to is annoying.
2343-
let mut printed_paths = FxHashSet::default();
2344-
2345-
prints.extend(matches.opt_strs("print").into_iter().map(|req| {
2346-
let (req, out) = split_out_file_name(&req);
2347-
2348-
let kind = if let Some((print_name, print_kind)) =
2349-
PRINT_KINDS.iter().find(|&&(name, _)| name == req)
2350-
{
2351-
check_print_request_stability(early_dcx, unstable_opts, (print_name, *print_kind));
2352-
*print_kind
2353-
} else {
2354-
let is_nightly = nightly_options::match_is_nightly_build(matches);
2355-
emit_unknown_print_request_help(early_dcx, req, is_nightly)
2356-
};
2357-
2358-
let out = out.unwrap_or(OutFileName::Stdout);
2359-
if let OutFileName::Real(path) = &out {
2360-
if !printed_paths.insert(path.clone()) {
2361-
early_dcx.early_fatal(format!(
2362-
"cannot print multiple outputs to the same path: {}",
2363-
path.display(),
2364-
));
2365-
}
2366-
}
2367-
2368-
PrintRequest { kind, out }
2369-
}));
2370-
2371-
prints
2372-
}
2373-
2374-
fn check_print_request_stability(
2375-
early_dcx: &EarlyDiagCtxt,
2376-
unstable_opts: &UnstableOptions,
2377-
(print_name, print_kind): (&str, PrintKind),
2378-
) {
2379-
if !is_print_request_stable(print_kind) && !unstable_opts.unstable_options {
2380-
early_dcx.early_fatal(format!(
2381-
"the `-Z unstable-options` flag must also be passed to enable the `{print_name}` \
2382-
print option"
2383-
));
2384-
}
2385-
}
2386-
2387-
fn is_print_request_stable(print_kind: PrintKind) -> bool {
2388-
match print_kind {
2389-
PrintKind::AllTargetSpecsJson
2390-
| PrintKind::CheckCfg
2391-
| PrintKind::CrateRootLintLevels
2392-
| PrintKind::SupportedCrateTypes
2393-
| PrintKind::TargetSpecJson
2394-
| PrintKind::TargetSpecJsonSchema => false,
2395-
_ => true,
2396-
}
2397-
}
2398-
2399-
fn emit_unknown_print_request_help(early_dcx: &EarlyDiagCtxt, req: &str, is_nightly: bool) -> ! {
2400-
let prints = PRINT_KINDS
2401-
.iter()
2402-
.filter_map(|(name, kind)| {
2403-
// If we're not on nightly, we don't want to print unstable options
2404-
if !is_nightly && !is_print_request_stable(*kind) {
2405-
None
2406-
} else {
2407-
Some(format!("`{name}`"))
2408-
}
2409-
})
2410-
.collect::<Vec<_>>();
2411-
let prints = prints.join(", ");
2412-
2413-
let mut diag = early_dcx.early_struct_fatal(format!("unknown print request: `{req}`"));
2414-
#[allow(rustc::diagnostic_outside_of_impl)]
2415-
diag.help(format!("valid print requests are: {prints}"));
2416-
2417-
if req == "lints" {
2418-
diag.help(format!("use `-Whelp` to print a list of lints"));
2419-
}
2420-
2421-
diag.help(format!("for more information, see the rustc book: https://doc.rust-lang.org/rustc/command-line-arguments.html#--print-print-compiler-information"));
2422-
diag.emit()
2423-
}
2424-
24252252
pub fn parse_target_triple(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> TargetTuple {
24262253
match matches.opt_str("target") {
24272254
Some(target) if target.ends_with(".json") => {
@@ -2846,7 +2673,7 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
28462673
));
28472674
}
28482675

2849-
let prints = collect_print_requests(early_dcx, &mut cg, &unstable_opts, matches);
2676+
let prints = print_request::collect_print_requests(early_dcx, &mut cg, &unstable_opts, matches);
28502677

28512678
// -Zretpoline-external-thunk also requires -Zretpoline
28522679
if unstable_opts.retpoline_external_thunk {

0 commit comments

Comments
 (0)