diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index ebbf973d334..9a0620caf10 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -35,8 +35,11 @@ use tracing::debug; pub use crate::config::cfg::{Cfg, CheckCfg, ExpectedValues}; use crate::config::native_libs::parse_native_libs; -pub use crate::config::print_request::{PrintKind, PrintRequest, collect_print_requests}; +pub use crate::config::print_request::{ + PrintCategory, PrintKind, PrintRequest, collect_print_requests, +}; use crate::diagnostics::FileWriteFail; +use crate::macros::AllVariants; pub use crate::options::*; use crate::search_paths::SearchPath; use crate::utils::CanonicalizedPath; @@ -2897,7 +2900,13 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M )); } - let prints = print_request::collect_print_requests(early_dcx, &mut cg, &unstable_opts, matches); + let prints = print_request::collect_print_requests( + early_dcx, + &mut cg, + &unstable_opts, + matches, + PrintCategory::ALL_VARIANTS, + ); // -Zretpoline-external-thunk also requires -Zretpoline if unstable_opts.retpoline_external_thunk { diff --git a/compiler/rustc_session/src/config/print_request.rs b/compiler/rustc_session/src/config/print_request.rs index dee63856c2e..b41ceb1699e 100644 --- a/compiler/rustc_session/src/config/print_request.rs +++ b/compiler/rustc_session/src/config/print_request.rs @@ -105,6 +105,15 @@ pub enum PrintKind { // tidy-alphabetical-end } +#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(AllVariants)] +pub enum PrintCategory { + Target, + Codegen, + Linker, + Crate, +} + impl PrintKind { fn name(self) -> &'static str { use PrintKind::*; @@ -141,6 +150,28 @@ impl PrintKind { } } + fn category(self) -> PrintCategory { + use PrintKind::*; + match self { + TargetList | TargetSpecJsonSchema | AllTargetSpecsJson | TargetSpecJson + | TargetCPUs | TargetFeatures | DeploymentTarget | HostTuple | SupportedCrateTypes + | Sysroot | TargetLibdir | Cfg | CheckCfg | WasmProcMacroTuple => PrintCategory::Target, + + BackendHasMnemonic + | BackendHasZstd + | CallingConventions + | CodeModels + | SplitDebuginfo + | StackProtectorStrategies + | TlsModels + | RelocationModels => PrintCategory::Codegen, + + LinkArgs | NativeStaticLibs => PrintCategory::Linker, + + CrateName | CrateRootLintLevels | FileNames => PrintCategory::Crate, + } + } + fn is_stable(self) -> bool { use PrintKind::*; match self { @@ -202,6 +233,7 @@ pub fn collect_print_requests( cg: &mut CodegenOptions, unstable_opts: &UnstableOptions, matches: &getopts::Matches, + allowed: &[PrintCategory], ) -> Vec { let mut prints = Vec::::new(); if cg.target_cpu.as_deref() == Some("help") { @@ -243,12 +275,14 @@ pub fn collect_print_requests( for example: `--print=backend-has-mnemonic:RET`", ); } - } else if let Some(print_kind) = PrintKind::from_str(req) { + } else if let Some(print_kind) = PrintKind::from_str(req) + && allowed.contains(&print_kind.category()) + { check_print_request_stability(early_dcx, unstable_opts, print_kind); (print_kind, None) } else { let is_nightly = nightly_options::match_is_nightly_build(matches); - emit_unknown_print_request_help(early_dcx, req, is_nightly) + emit_unknown_print_request_help(early_dcx, req, is_nightly, allowed) }; let out = out.unwrap_or(OutFileName::Stdout); @@ -279,11 +313,17 @@ fn check_print_request_stability( } } -fn emit_unknown_print_request_help(early_dcx: &EarlyDiagCtxt, req: &str, is_nightly: bool) -> ! { +fn emit_unknown_print_request_help( + early_dcx: &EarlyDiagCtxt, + req: &str, + is_nightly: bool, + allowed: &[PrintCategory], +) -> ! { let prints = PrintKind::ALL_VARIANTS .iter() // If we're not on nightly, we don't want to print unstable options .filter(|kind| is_nightly || kind.is_stable()) + .filter(|kind| allowed.contains(&kind.category())) .map(|kind| format!("`{kind}`")) .collect::>() .join(", "); @@ -292,9 +332,12 @@ fn emit_unknown_print_request_help(early_dcx: &EarlyDiagCtxt, req: &str, is_nigh diag.help(format!("valid print requests are: {prints}")); if req == "lints" { - diag.help(format!("use `-Whelp` to print a list of lints")); + diag.help("use `-Whelp` to print a list of lints"); + } + + if allowed == PrintCategory::ALL_VARIANTS { + diag.help("for more information, see the rustc book: https://doc.rust-lang.org/rustc/command-line-arguments.html#--print-print-compiler-information"); } - diag.help(format!("for more information, see the rustc book: https://doc.rust-lang.org/rustc/command-line-arguments.html#--print-print-compiler-information")); diag.emit() } diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index 886275ee294..c2c58a345fa 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -10,7 +10,7 @@ use rustc_errors::DiagCtxtHandle; use rustc_lint::Level; use rustc_session::config::{ self, CodegenOptions, ErrorOutputType, Externs, Input, JsonUnusedExterns, - OptionsTargetModifiers, OutFileName, PrintRequest, Sysroot, UnstableOptions, + OptionsTargetModifiers, OutFileName, PrintCategory, PrintRequest, Sysroot, UnstableOptions, collect_print_requests, get_cmd_lint_options, nightly_options, parse_crate_types_from_list, parse_externs, parse_target_triple, }; @@ -574,8 +574,13 @@ impl Options { Err(err) => dcx.fatal(err), }; - let prints = - collect_print_requests(early_dcx, &mut codegen_options, &unstable_opts, matches); + let prints = collect_print_requests( + early_dcx, + &mut codegen_options, + &unstable_opts, + matches, + &[PrintCategory::Target, PrintCategory::Crate], + ); let mut parts_out_dir = match matches.opt_str("write-doc-meta-dir").map(PathToParts::from_flag).transpose() { diff --git a/tests/run-make/rustdoc/print-request-help/invalid-print-request-help.err b/tests/run-make/rustdoc/print-request-help/invalid-print-request-help.err index 06842577618..02d14190030 100644 --- a/tests/run-make/rustdoc/print-request-help/invalid-print-request-help.err +++ b/tests/run-make/rustdoc/print-request-help/invalid-print-request-help.err @@ -1,5 +1,4 @@ error: unknown print request: `xxx` | - = help: valid print requests are: `all-target-specs-json`, `backend-has-mnemonic`, `backend-has-zstd`, `calling-conventions`, `cfg`, `check-cfg`, `code-models`, `crate-name`, `crate-root-lint-levels`, `deployment-target`, `file-names`, `host-tuple`, `link-args`, `native-static-libs`, `relocation-models`, `split-debuginfo`, `stack-protector-strategies`, `supported-crate-types`, `sysroot`, `target-cpus`, `target-features`, `target-libdir`, `target-list`, `target-spec-json`, `target-spec-json-schema`, `tls-models`, `wasm-proc-macro-tuple` - = help: for more information, see the rustc book: https://doc.rust-lang.org/rustc/command-line-arguments.html#--print-print-compiler-information + = help: valid print requests are: `all-target-specs-json`, `cfg`, `check-cfg`, `crate-name`, `crate-root-lint-levels`, `deployment-target`, `file-names`, `host-tuple`, `supported-crate-types`, `sysroot`, `target-cpus`, `target-features`, `target-libdir`, `target-list`, `target-spec-json`, `target-spec-json-schema`, `wasm-proc-macro-tuple`