Rollup merge of #139850 - xizheyin:issue-138698, r=jieyouxu

Hide unstable print kinds within emit_unknown_print_request_help in stable channel

Fixes #138698

We need to get the channel from `matches`. However, since `matches`(Line 1169) is constructed after `rustc_optgroups` (Line1165, where `RustcOptGroup::value_hint` is generated, i.e. what `rustc --print print` prints), I've left it unchanged here for now.

2da29dbe8f/compiler/rustc_driver_impl/src/lib.rs (L1161-L1169)

There is actually a way to manually parse the `--crate-name` parameter, but I'm afraid that's an unorthodox practice. So I conservatively just modified `emit_unknown_print_request_help` to print different parameters depending on whether they are nightly or not when passing the error parameter.

r? ```@jieyouxu```
This commit is contained in:
Matthias Krüger 2025-04-17 17:40:27 +02:00 committed by GitHub
commit d2db1c1df9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 75 additions and 12 deletions

View file

@ -2065,7 +2065,8 @@ fn collect_print_requests(
check_print_request_stability(early_dcx, unstable_opts, (print_name, *print_kind));
*print_kind
} else {
emit_unknown_print_request_help(early_dcx, req)
let is_nightly = nightly_options::match_is_nightly_build(matches);
emit_unknown_print_request_help(early_dcx, req, is_nightly)
};
let out = out.unwrap_or(OutFileName::Stdout);
@ -2089,25 +2090,37 @@ fn check_print_request_stability(
unstable_opts: &UnstableOptions,
(print_name, print_kind): (&str, PrintKind),
) {
if !is_print_request_stable(print_kind) && !unstable_opts.unstable_options {
early_dcx.early_fatal(format!(
"the `-Z unstable-options` flag must also be passed to enable the `{print_name}` \
print option"
));
}
}
fn is_print_request_stable(print_kind: PrintKind) -> bool {
match print_kind {
PrintKind::AllTargetSpecsJson
| PrintKind::CheckCfg
| PrintKind::CrateRootLintLevels
| PrintKind::SupportedCrateTypes
| PrintKind::TargetSpecJson
if !unstable_opts.unstable_options =>
{
early_dcx.early_fatal(format!(
"the `-Z unstable-options` flag must also be passed to enable the `{print_name}` \
print option"
));
}
_ => {}
| PrintKind::TargetSpecJson => false,
_ => true,
}
}
fn emit_unknown_print_request_help(early_dcx: &EarlyDiagCtxt, req: &str) -> ! {
let prints = PRINT_KINDS.iter().map(|(name, _)| format!("`{name}`")).collect::<Vec<_>>();
fn emit_unknown_print_request_help(early_dcx: &EarlyDiagCtxt, req: &str, is_nightly: bool) -> ! {
let prints = PRINT_KINDS
.iter()
.filter_map(|(name, kind)| {
// If we're not on nightly, we don't want to print unstable options
if !is_nightly && !is_print_request_stable(*kind) {
None
} else {
Some(format!("`{name}`"))
}
})
.collect::<Vec<_>>();
let prints = prints.join(", ");
let mut diag = early_dcx.early_struct_fatal(format!("unknown print request: `{req}`"));