about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2025-01-29 15:05:21 +0100
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2025-01-29 15:18:14 +0100
commitb7951380ca9e8eddcd67bf947b592d10e21bdea5 (patch)
tree3611d5b8ff2f2478ffc097b0ec9cabcf51d3a9b0
parenta5d66e07e1775e3d5a9a8bb073fc50bf03101198 (diff)
downloadrust-b7951380ca9e8eddcd67bf947b592d10e21bdea5.tar.gz
rust-b7951380ca9e8eddcd67bf947b592d10e21bdea5.zip
Improve check for `--output-format` combinations and add ui regression test
-rw-r--r--src/librustdoc/config.rs66
-rw-r--r--tests/rustdoc-ui/coverage/html.stderr2
-rw-r--r--tests/rustdoc-ui/doctest-output.rs1
-rw-r--r--tests/rustdoc-ui/doctest-output.stderr2
4 files changed, 39 insertions, 32 deletions
diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs
index 1ba9dcaac1d..cfba7895208 100644
--- a/src/librustdoc/config.rs
+++ b/src/librustdoc/config.rs
@@ -447,22 +447,42 @@ impl Options {
             }
         }
 
+        let show_coverage = matches.opt_present("show-coverage");
+        let output_format_s = matches.opt_str("output-format");
+        let output_format = match output_format_s {
+            Some(ref s) => match OutputFormat::try_from(s.as_str()) {
+                Ok(out_fmt) => out_fmt,
+                Err(e) => dcx.fatal(e),
+            },
+            None => OutputFormat::default(),
+        };
+
         // check for `--output-format=json`
-        if let Some(format) = matches.opt_str("output-format").as_deref()
-            && format != "html"
-            && !matches.opt_present("show-coverage")
-            && !nightly_options::is_unstable_enabled(matches)
-        {
-            let extra = match format {
-                "json" => " (see https://github.com/rust-lang/rust/issues/76578)",
-                "doctest" => " (see https://github.com/rust-lang/rust/issues/134529)",
-                _ => "",
-            };
-            dcx.fatal(
-                format!(
-                    "the -Z unstable-options flag must be passed to enable --output-format for documentation generation{extra}",
-                ),
-            );
+        match (
+            output_format_s.as_ref().map(|_| output_format),
+            show_coverage,
+            nightly_options::is_unstable_enabled(matches),
+        ) {
+            (None | Some(OutputFormat::Json), true, _) => {}
+            (_, true, _) => {
+                dcx.fatal(format!(
+                    "`--output-format={}` is not supported for the `--show-coverage` option",
+                    output_format_s.unwrap_or_default(),
+                ));
+            }
+            // If `-Zunstable-options` is used, nothing to check after this point.
+            (_, false, true) => {}
+            (None | Some(OutputFormat::Html), false, _) => {}
+            (Some(OutputFormat::Json), false, false) => {
+                dcx.fatal(
+                    "the -Z unstable-options flag must be passed to enable --output-format for documentation generation (see https://github.com/rust-lang/rust/issues/76578)",
+                );
+            }
+            (Some(OutputFormat::Doctest), false, false) => {
+                dcx.fatal(
+                    "the -Z unstable-options flag must be passed to enable --output-format for documentation generation (see https://github.com/rust-lang/rust/issues/134529)",
+                );
+            }
         }
 
         let to_check = matches.opt_strs("check-theme");
@@ -714,8 +734,6 @@ impl Options {
             })
             .collect();
 
-        let show_coverage = matches.opt_present("show-coverage");
-
         let crate_types = match parse_crate_types_from_list(matches.opt_strs("crate-type")) {
             Ok(types) => types,
             Err(e) => {
@@ -723,20 +741,6 @@ impl Options {
             }
         };
 
-        let output_format = match matches.opt_str("output-format") {
-            Some(s) => match OutputFormat::try_from(s.as_str()) {
-                Ok(out_fmt) => {
-                    if !out_fmt.is_json() && show_coverage {
-                        dcx.fatal(
-                            "html output format isn't supported for the --show-coverage option",
-                        );
-                    }
-                    out_fmt
-                }
-                Err(e) => dcx.fatal(e),
-            },
-            None => OutputFormat::default(),
-        };
         let crate_name = matches.opt_str("crate-name");
         let bin_crate = crate_types.contains(&CrateType::Executable);
         let proc_macro_crate = crate_types.contains(&CrateType::ProcMacro);
diff --git a/tests/rustdoc-ui/coverage/html.stderr b/tests/rustdoc-ui/coverage/html.stderr
index adca375d4bc..764179820c5 100644
--- a/tests/rustdoc-ui/coverage/html.stderr
+++ b/tests/rustdoc-ui/coverage/html.stderr
@@ -1,2 +1,2 @@
-error: html output format isn't supported for the --show-coverage option
+error: `--output-format=html` is not supported for the `--show-coverage` option
 
diff --git a/tests/rustdoc-ui/doctest-output.rs b/tests/rustdoc-ui/doctest-output.rs
new file mode 100644
index 00000000000..720f2952980
--- /dev/null
+++ b/tests/rustdoc-ui/doctest-output.rs
@@ -0,0 +1 @@
+//@ compile-flags:-Z unstable-options --show-coverage --output-format=doctest
diff --git a/tests/rustdoc-ui/doctest-output.stderr b/tests/rustdoc-ui/doctest-output.stderr
new file mode 100644
index 00000000000..20c618dc61b
--- /dev/null
+++ b/tests/rustdoc-ui/doctest-output.stderr
@@ -0,0 +1,2 @@
+error: `--output-format=doctest` is not supported for the `--show-coverage` option
+