Skip to content

Commit 8562110

Browse files
committed
Hide unstable print kinds within emit_unknown_print_request_help in stable channel
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
1 parent 6e83046 commit 8562110

File tree

5 files changed

+75
-12
lines changed

5 files changed

+75
-12
lines changed

compiler/rustc_session/src/config.rs

+25-12
Original file line numberDiff line numberDiff line change
@@ -2069,7 +2069,8 @@ fn collect_print_requests(
20692069
check_print_request_stability(early_dcx, unstable_opts, (print_name, *print_kind));
20702070
*print_kind
20712071
} else {
2072-
emit_unknown_print_request_help(early_dcx, req)
2072+
let is_nightly = nightly_options::match_is_nightly_build(matches);
2073+
emit_unknown_print_request_help(early_dcx, req, is_nightly)
20732074
};
20742075

20752076
let out = out.unwrap_or(OutFileName::Stdout);
@@ -2093,25 +2094,37 @@ fn check_print_request_stability(
20932094
unstable_opts: &UnstableOptions,
20942095
(print_name, print_kind): (&str, PrintKind),
20952096
) {
2097+
if !is_print_request_stable(print_kind) && !unstable_opts.unstable_options {
2098+
early_dcx.early_fatal(format!(
2099+
"the `-Z unstable-options` flag must also be passed to enable the `{print_name}` \
2100+
print option"
2101+
));
2102+
}
2103+
}
2104+
2105+
fn is_print_request_stable(print_kind: PrintKind) -> bool {
20962106
match print_kind {
20972107
PrintKind::AllTargetSpecsJson
20982108
| PrintKind::CheckCfg
20992109
| PrintKind::CrateRootLintLevels
21002110
| PrintKind::SupportedCrateTypes
2101-
| PrintKind::TargetSpecJson
2102-
if !unstable_opts.unstable_options =>
2103-
{
2104-
early_dcx.early_fatal(format!(
2105-
"the `-Z unstable-options` flag must also be passed to enable the `{print_name}` \
2106-
print option"
2107-
));
2108-
}
2109-
_ => {}
2111+
| PrintKind::TargetSpecJson => false,
2112+
_ => true,
21102113
}
21112114
}
21122115

2113-
fn emit_unknown_print_request_help(early_dcx: &EarlyDiagCtxt, req: &str) -> ! {
2114-
let prints = PRINT_KINDS.iter().map(|(name, _)| format!("`{name}`")).collect::<Vec<_>>();
2116+
fn emit_unknown_print_request_help(early_dcx: &EarlyDiagCtxt, req: &str, is_nightly: bool) -> ! {
2117+
let prints = PRINT_KINDS
2118+
.iter()
2119+
.filter_map(|(name, kind)| {
2120+
// If we're not on nightly, we don't want to print unstable options
2121+
if !is_nightly && !is_print_request_stable(*kind) {
2122+
None
2123+
} else {
2124+
Some(format!("`{name}`"))
2125+
}
2126+
})
2127+
.collect::<Vec<_>>();
21152128
let prints = prints.join(", ");
21162129

21172130
let mut diag = early_dcx.early_struct_fatal(format!("unknown print request: `{req}`"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@@ -1,5 +1,5 @@
2+
error: unknown print request: `xxx`
3+
|
4+
- = help: valid print requests are: `calling-conventions`, `cfg`, `code-models`, `crate-name`, `deployment-target`, `file-names`, `host-tuple`, `link-args`, `native-static-libs`, `relocation-models`, `split-debuginfo`, `stack-protector-strategies`, `sysroot`, `target-cpus`, `target-features`, `target-libdir`, `target-list`, `tls-models`
5+
+ = help: valid print requests are: `all-target-specs-json`, `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`, `tls-models`
6+
= help: for more information, see the rustc book: https://doc.rust-lang.org/rustc/command-line-arguments.html#--print-print-compiler-information
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//! Check that unstable print requests are omitted from help if compiler is in stable channel.
2+
//!
3+
//! Issue: <https://github.com/rust-lang/rust/issues/138698>
4+
use run_make_support::{diff, rustc, similar};
5+
6+
fn main() {
7+
let stable_invalid_print_request_help = rustc()
8+
.env("RUSTC_BOOTSTRAP", "-1")
9+
.cfg("force_stable")
10+
.print("xxx")
11+
.run_fail()
12+
.stderr_utf8();
13+
assert!(!stable_invalid_print_request_help.contains("all-target-specs-json"));
14+
diff()
15+
.expected_file("stable-invalid-print-request-help.err")
16+
.actual_text("stable_invalid_print_request_help", &stable_invalid_print_request_help)
17+
.run();
18+
19+
let unstable_invalid_print_request_help = rustc().print("xxx").run_fail().stderr_utf8();
20+
assert!(unstable_invalid_print_request_help.contains("all-target-specs-json"));
21+
diff()
22+
.expected_file("unstable-invalid-print-request-help.err")
23+
.actual_text("unstable_invalid_print_request_help", &unstable_invalid_print_request_help)
24+
.run();
25+
26+
let help_diff = similar::TextDiff::from_lines(
27+
&stable_invalid_print_request_help,
28+
&unstable_invalid_print_request_help,
29+
)
30+
.unified_diff()
31+
.to_string();
32+
diff().expected_file("help-diff.diff").actual_text("help_diff", help_diff).run();
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unknown print request: `xxx`
2+
|
3+
= help: valid print requests are: `calling-conventions`, `cfg`, `code-models`, `crate-name`, `deployment-target`, `file-names`, `host-tuple`, `link-args`, `native-static-libs`, `relocation-models`, `split-debuginfo`, `stack-protector-strategies`, `sysroot`, `target-cpus`, `target-features`, `target-libdir`, `target-list`, `tls-models`
4+
= help: for more information, see the rustc book: https://doc.rust-lang.org/rustc/command-line-arguments.html#--print-print-compiler-information
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: unknown print request: `xxx`
2+
|
3+
= help: valid print requests are: `all-target-specs-json`, `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`, `tls-models`
4+
= help: for more information, see the rustc book: https://doc.rust-lang.org/rustc/command-line-arguments.html#--print-print-compiler-information
5+

0 commit comments

Comments
 (0)