about summary refs log tree commit diff
diff options
context:
space:
mode:
authorxizheyin <xizheyin@smail.nju.edu.cn>2025-04-15 15:03:11 +0800
committerxizheyin <xizheyin@smail.nju.edu.cn>2025-04-17 22:02:31 +0800
commit8562110e0d8ef293563200d15d466c012e936ff7 (patch)
tree5ef8f737f77c23dd624a72dfa05ebed9fc006044
parent6e830462330a9e34d8176e86d4580dd0820c6fd5 (diff)
downloadrust-8562110e0d8ef293563200d15d466c012e936ff7.tar.gz
rust-8562110e0d8ef293563200d15d466c012e936ff7.zip
Hide unstable print kinds within emit_unknown_print_request_help in stable channel
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
-rw-r--r--compiler/rustc_session/src/config.rs37
-rw-r--r--tests/run-make/print-request-help-stable-unstable/help-diff.diff7
-rw-r--r--tests/run-make/print-request-help-stable-unstable/rmake.rs33
-rw-r--r--tests/run-make/print-request-help-stable-unstable/stable-invalid-print-request-help.err5
-rw-r--r--tests/run-make/print-request-help-stable-unstable/unstable-invalid-print-request-help.err5
5 files changed, 75 insertions, 12 deletions
diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs
index bdd54a15147..d00c0873961 100644
--- a/compiler/rustc_session/src/config.rs
+++ b/compiler/rustc_session/src/config.rs
@@ -2069,7 +2069,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);
@@ -2093,25 +2094,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}`"));
diff --git a/tests/run-make/print-request-help-stable-unstable/help-diff.diff b/tests/run-make/print-request-help-stable-unstable/help-diff.diff
new file mode 100644
index 00000000000..07eafca3271
--- /dev/null
+++ b/tests/run-make/print-request-help-stable-unstable/help-diff.diff
@@ -0,0 +1,7 @@
+@@ -1,5 +1,5 @@
+ error: unknown print request: `xxx`
+   |
+-  = 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`
++  = 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`
+   = help: for more information, see the rustc book: https://doc.rust-lang.org/rustc/command-line-arguments.html#--print-print-compiler-information
+ 
diff --git a/tests/run-make/print-request-help-stable-unstable/rmake.rs b/tests/run-make/print-request-help-stable-unstable/rmake.rs
new file mode 100644
index 00000000000..a59963da5c4
--- /dev/null
+++ b/tests/run-make/print-request-help-stable-unstable/rmake.rs
@@ -0,0 +1,33 @@
+//! Check that unstable print requests are omitted from help if compiler is in stable channel.
+//!
+//! Issue: <https://github.com/rust-lang/rust/issues/138698>
+use run_make_support::{diff, rustc, similar};
+
+fn main() {
+    let stable_invalid_print_request_help = rustc()
+        .env("RUSTC_BOOTSTRAP", "-1")
+        .cfg("force_stable")
+        .print("xxx")
+        .run_fail()
+        .stderr_utf8();
+    assert!(!stable_invalid_print_request_help.contains("all-target-specs-json"));
+    diff()
+        .expected_file("stable-invalid-print-request-help.err")
+        .actual_text("stable_invalid_print_request_help", &stable_invalid_print_request_help)
+        .run();
+
+    let unstable_invalid_print_request_help = rustc().print("xxx").run_fail().stderr_utf8();
+    assert!(unstable_invalid_print_request_help.contains("all-target-specs-json"));
+    diff()
+        .expected_file("unstable-invalid-print-request-help.err")
+        .actual_text("unstable_invalid_print_request_help", &unstable_invalid_print_request_help)
+        .run();
+
+    let help_diff = similar::TextDiff::from_lines(
+        &stable_invalid_print_request_help,
+        &unstable_invalid_print_request_help,
+    )
+    .unified_diff()
+    .to_string();
+    diff().expected_file("help-diff.diff").actual_text("help_diff", help_diff).run();
+}
diff --git a/tests/run-make/print-request-help-stable-unstable/stable-invalid-print-request-help.err b/tests/run-make/print-request-help-stable-unstable/stable-invalid-print-request-help.err
new file mode 100644
index 00000000000..019a578dad3
--- /dev/null
+++ b/tests/run-make/print-request-help-stable-unstable/stable-invalid-print-request-help.err
@@ -0,0 +1,5 @@
+error: unknown print request: `xxx`
+  |
+  = 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`
+  = help: for more information, see the rustc book: https://doc.rust-lang.org/rustc/command-line-arguments.html#--print-print-compiler-information
+
diff --git a/tests/run-make/print-request-help-stable-unstable/unstable-invalid-print-request-help.err b/tests/run-make/print-request-help-stable-unstable/unstable-invalid-print-request-help.err
new file mode 100644
index 00000000000..50ef340e3dd
--- /dev/null
+++ b/tests/run-make/print-request-help-stable-unstable/unstable-invalid-print-request-help.err
@@ -0,0 +1,5 @@
+error: unknown print request: `xxx`
+  |
+  = 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`
+  = help: for more information, see the rustc book: https://doc.rust-lang.org/rustc/command-line-arguments.html#--print-print-compiler-information
+