about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-07-31 04:37:14 +0000
committerbors <bors@rust-lang.org>2020-07-31 04:37:14 +0000
commit66b97dca3c8ab51f8af7b2db7ae4c8061fbf5e9b (patch)
tree3cce7d7cb31b85fb7e781cb4e74cca206964c6f8
parentffa80f01d800528b2cfbe5b5a8a218d20c7d8e95 (diff)
parent48c6f05662b0bf58cce0e867e60c2d86b5e6fcfb (diff)
downloadrust-66b97dca3c8ab51f8af7b2db7ae4c8061fbf5e9b.tar.gz
rust-66b97dca3c8ab51f8af7b2db7ae4c8061fbf5e9b.zip
Auto merge of #74955 - P1n3appl3:rustdoc-formats, r=GuillaumeGomez
Add `--output-format json` for Rustdoc on nightly

This enables the previously deprecated `--output-format` flag so it can be used on nightly to host the experimental implementation of [rfc/2963](https://github.com/rust-lang/rfcs/pull/2963). The actual implementation will come in later PRs so for now there's just a stub that gives you an ICE.

I'm _pretty_ sure that the logic I added makes it inaccessible from stable, but someone should double check that. @tmandry @jyn514
-rw-r--r--src/librustdoc/config.rs6
-rw-r--r--src/librustdoc/json/mod.rs47
-rw-r--r--src/librustdoc/lib.rs44
3 files changed, 81 insertions, 16 deletions
diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs
index 3547b45dfa7..4f751decc80 100644
--- a/src/librustdoc/config.rs
+++ b/src/librustdoc/config.rs
@@ -508,7 +508,7 @@ impl Options {
         let output_format = match matches.opt_str("output-format") {
             Some(s) => match OutputFormat::try_from(s.as_str()) {
                 Ok(o) => {
-                    if o.is_json() && !show_coverage {
+                    if o.is_json() && !(show_coverage || nightly_options::is_nightly_build()) {
                         diag.struct_err("json output format isn't supported for doc generation")
                             .emit();
                         return Err(1);
@@ -626,7 +626,9 @@ fn check_deprecated_options(matches: &getopts::Matches, diag: &rustc_errors::Han
 
     for flag in deprecated_flags.iter() {
         if matches.opt_present(flag) {
-            if *flag == "output-format" && matches.opt_present("show-coverage") {
+            if *flag == "output-format"
+                && (matches.opt_present("show-coverage") || nightly_options::is_nightly_build())
+            {
                 continue;
             }
             let mut err =
diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs
new file mode 100644
index 00000000000..14f87ec2aa9
--- /dev/null
+++ b/src/librustdoc/json/mod.rs
@@ -0,0 +1,47 @@
+use crate::clean;
+use crate::config::{RenderInfo, RenderOptions};
+use crate::error::Error;
+use crate::formats::cache::Cache;
+use crate::formats::FormatRenderer;
+
+use rustc_span::edition::Edition;
+
+#[derive(Clone)]
+pub struct JsonRenderer {}
+
+impl FormatRenderer for JsonRenderer {
+    fn init(
+        _krate: clean::Crate,
+        _options: RenderOptions,
+        _render_info: RenderInfo,
+        _edition: Edition,
+        _cache: &mut Cache,
+    ) -> Result<(Self, clean::Crate), Error> {
+        unimplemented!()
+    }
+
+    fn item(&mut self, _item: clean::Item, _cache: &Cache) -> Result<(), Error> {
+        unimplemented!()
+    }
+
+    fn mod_item_in(
+        &mut self,
+        _item: &clean::Item,
+        _item_name: &str,
+        _cache: &Cache,
+    ) -> Result<(), Error> {
+        unimplemented!()
+    }
+
+    fn mod_item_out(&mut self, _item_name: &str) -> Result<(), Error> {
+        unimplemented!()
+    }
+
+    fn after_krate(&mut self, _krate: &clean::Crate, _cache: &Cache) -> Result<(), Error> {
+        unimplemented!()
+    }
+
+    fn after_run(&mut self, _diag: &rustc_errors::Handler) -> Result<(), Error> {
+        unimplemented!()
+    }
+}
diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs
index 65bc089faf4..62780878fd5 100644
--- a/src/librustdoc/lib.rs
+++ b/src/librustdoc/lib.rs
@@ -68,6 +68,7 @@ mod error;
 mod fold;
 crate mod formats;
 pub mod html;
+mod json;
 mod markdown;
 mod passes;
 mod test;
@@ -450,6 +451,28 @@ fn wrap_return(diag: &rustc_errors::Handler, res: Result<(), String>) -> i32 {
     }
 }
 
+fn run_renderer<T: formats::FormatRenderer>(
+    krate: clean::Crate,
+    renderopts: config::RenderOptions,
+    render_info: config::RenderInfo,
+    diag: &rustc_errors::Handler,
+    edition: rustc_span::edition::Edition,
+) -> i32 {
+    match formats::run_format::<T>(krate, renderopts, render_info, &diag, edition) {
+        Ok(_) => rustc_driver::EXIT_SUCCESS,
+        Err(e) => {
+            let mut msg = diag.struct_err(&format!("couldn't generate documentation: {}", e.error));
+            let file = e.file.display().to_string();
+            if file.is_empty() {
+                msg.emit()
+            } else {
+                msg.note(&format!("failed to create or modify \"{}\"", file)).emit()
+            }
+            rustc_driver::EXIT_FAILURE
+        }
+    }
+}
+
 fn main_options(options: config::Options) -> i32 {
     let diag = core::new_handler(options.error_format, None, &options.debugging_options);
 
@@ -480,6 +503,7 @@ fn main_options(options: config::Options) -> i32 {
     let result = rustc_driver::catch_fatal_errors(move || {
         let crate_name = options.crate_name.clone();
         let crate_version = options.crate_version.clone();
+        let output_format = options.output_format;
         let (mut krate, renderinfo, renderopts) = core::run_core(options);
 
         info!("finished with rustc");
@@ -502,20 +526,12 @@ fn main_options(options: config::Options) -> i32 {
         info!("going to format");
         let (error_format, edition, debugging_options) = diag_opts;
         let diag = core::new_handler(error_format, None, &debugging_options);
-        match formats::run_format::<html::render::Context>(
-            krate, renderopts, renderinfo, &diag, edition,
-        ) {
-            Ok(_) => rustc_driver::EXIT_SUCCESS,
-            Err(e) => {
-                let mut msg =
-                    diag.struct_err(&format!("couldn't generate documentation: {}", e.error));
-                let file = e.file.display().to_string();
-                if file.is_empty() {
-                    msg.emit()
-                } else {
-                    msg.note(&format!("failed to create or modify \"{}\"", file)).emit()
-                }
-                rustc_driver::EXIT_FAILURE
+        match output_format {
+            None | Some(config::OutputFormat::Html) => {
+                run_renderer::<html::render::Context>(krate, renderopts, renderinfo, &diag, edition)
+            }
+            Some(config::OutputFormat::Json) => {
+                run_renderer::<json::JsonRenderer>(krate, renderopts, renderinfo, &diag, edition)
             }
         }
     });