]> git.proxmox.com Git - rustc.git/blobdiff - src/librustdoc/json/mod.rs
New upstream version 1.61.0+dfsg1
[rustc.git] / src / librustdoc / json / mod.rs
index dc2bc14e7ceb9074a73b54b6ecf9c42bf72509e8..126c5d89ca97c03896c61f9d512fc76488cc763e 100644 (file)
@@ -5,46 +5,47 @@
 //! docs for usage and details.
 
 mod conversions;
-pub mod types;
 
 use std::cell::RefCell;
-use std::fs::File;
+use std::fs::{create_dir_all, File};
+use std::io::{BufWriter, Write};
 use std::path::PathBuf;
 use std::rc::Rc;
 
 use rustc_data_structures::fx::FxHashMap;
-use rustc_middle::ty;
+use rustc_hir::def_id::DefId;
+use rustc_middle::ty::TyCtxt;
 use rustc_session::Session;
-use rustc_span::edition::Edition;
 
-use crate::clean;
-use crate::config::{RenderInfo, RenderOptions};
+use rustdoc_json_types as types;
+
+use crate::clean::types::{ExternalCrate, ExternalLocation};
+use crate::config::RenderOptions;
+use crate::docfs::PathError;
 use crate::error::Error;
 use crate::formats::cache::Cache;
 use crate::formats::FormatRenderer;
-use crate::html::render::cache::ExternalLocation;
+use crate::json::conversions::{from_item_id, IntoWithTcx};
+use crate::{clean, try_err};
 
 #[derive(Clone)]
 crate struct JsonRenderer<'tcx> {
-    tcx: ty::TyCtxt<'tcx>,
+    tcx: TyCtxt<'tcx>,
     /// A mapping of IDs that contains all local items for this crate which gets output as a top
     /// level field of the JSON blob.
     index: Rc<RefCell<FxHashMap<types::Id, types::Item>>>,
     /// The directory where the blob will be written to.
     out_path: PathBuf,
+    cache: Rc<Cache>,
 }
 
-impl JsonRenderer<'_> {
-    fn sess(&self) -> &Session {
+impl<'tcx> JsonRenderer<'tcx> {
+    fn sess(&self) -> &'tcx Session {
         self.tcx.sess
     }
 
-    fn get_trait_implementors(
-        &mut self,
-        id: rustc_span::def_id::DefId,
-        cache: &Cache,
-    ) -> Vec<types::Id> {
-        cache
+    fn get_trait_implementors(&mut self, id: DefId) -> Vec<types::Id> {
+        Rc::clone(&self.cache)
             .implementors
             .get(&id)
             .map(|implementors| {
@@ -52,16 +53,16 @@ impl JsonRenderer<'_> {
                     .iter()
                     .map(|i| {
                         let item = &i.impl_item;
-                        self.item(item.clone(), cache).unwrap();
-                        item.def_id.into()
+                        self.item(item.clone()).unwrap();
+                        from_item_id(item.def_id)
                     })
                     .collect()
             })
             .unwrap_or_default()
     }
 
-    fn get_impls(&mut self, id: rustc_span::def_id::DefId, cache: &Cache) -> Vec<types::Id> {
-        cache
+    fn get_impls(&mut self, id: DefId) -> Vec<types::Id> {
+        Rc::clone(&self.cache)
             .impls
             .get(&id)
             .map(|impls| {
@@ -69,9 +70,23 @@ impl JsonRenderer<'_> {
                     .iter()
                     .filter_map(|i| {
                         let item = &i.impl_item;
-                        if item.def_id.is_local() {
-                            self.item(item.clone(), cache).unwrap();
-                            Some(item.def_id.into())
+
+                        // HACK(hkmatsumoto): For impls of primitive types, we index them
+                        // regardless of whether they're local. This is because users can
+                        // document primitive items in an arbitrary crate by using
+                        // `doc(primitive)`.
+                        let mut is_primitive_impl = false;
+                        if let clean::types::ItemKind::ImplItem(ref impl_) = *item.kind {
+                            if impl_.trait_.is_none() {
+                                if let clean::types::Type::Primitive(_) = impl_.for_ {
+                                    is_primitive_impl = true;
+                                }
+                            }
+                        }
+
+                        if item.def_id.is_local() || is_primitive_impl {
+                            self.item(item.clone()).unwrap();
+                            Some(from_item_id(item.def_id))
                         } else {
                             None
                         }
@@ -81,35 +96,36 @@ impl JsonRenderer<'_> {
             .unwrap_or_default()
     }
 
-    fn get_trait_items(&mut self, cache: &Cache) -> Vec<(types::Id, types::Item)> {
-        cache
+    fn get_trait_items(&mut self) -> Vec<(types::Id, types::Item)> {
+        Rc::clone(&self.cache)
             .traits
             .iter()
             .filter_map(|(&id, trait_item)| {
                 // only need to synthesize items for external traits
                 if !id.is_local() {
-                    trait_item.items.clone().into_iter().for_each(|i| self.item(i, cache).unwrap());
+                    let trait_item = &trait_item.trait_;
+                    trait_item.items.clone().into_iter().for_each(|i| self.item(i).unwrap());
                     Some((
-                        id.into(),
+                        from_item_id(id.into()),
                         types::Item {
-                            id: id.into(),
+                            id: from_item_id(id.into()),
                             crate_id: id.krate.as_u32(),
-                            name: cache
+                            name: self
+                                .cache
                                 .paths
                                 .get(&id)
                                 .unwrap_or_else(|| {
-                                    cache
+                                    self.cache
                                         .external_paths
                                         .get(&id)
                                         .expect("Trait should either be in local or external paths")
                                 })
                                 .0
                                 .last()
-                                .map(Clone::clone),
+                                .map(|s| s.to_string()),
                             visibility: types::Visibility::Public,
-                            kind: types::ItemKind::Trait,
-                            inner: types::ItemEnum::TraitItem(trait_item.clone().into()),
-                            source: None,
+                            inner: types::ItemEnum::Trait(trait_item.clone().into_tcx(self.tcx)),
+                            span: None,
                             docs: Default::default(),
                             links: Default::default(),
                             attrs: Default::default(),
@@ -125,13 +141,17 @@ impl JsonRenderer<'_> {
 }
 
 impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
+    fn descr() -> &'static str {
+        "json"
+    }
+
+    const RUN_ON_MODULE: bool = false;
+
     fn init(
         krate: clean::Crate,
         options: RenderOptions,
-        _render_info: RenderInfo,
-        _edition: Edition,
-        _cache: &mut Cache,
-        tcx: ty::TyCtxt<'tcx>,
+        cache: Cache,
+        tcx: TyCtxt<'tcx>,
     ) -> Result<(Self, clean::Crate), Error> {
         debug!("Initializing json renderer");
         Ok((
@@ -139,30 +159,39 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
                 tcx,
                 index: Rc::new(RefCell::new(FxHashMap::default())),
                 out_path: options.output,
+                cache: Rc::new(cache),
             },
             krate,
         ))
     }
 
+    fn make_child_renderer(&self) -> Self {
+        self.clone()
+    }
+
     /// Inserts an item into the index. This should be used rather than directly calling insert on
     /// the hashmap because certain items (traits and types) need to have their mappings for trait
     /// implementations filled out before they're inserted.
-    fn item(&mut self, item: clean::Item, cache: &Cache) -> Result<(), Error> {
+    fn item(&mut self, item: clean::Item) -> Result<(), Error> {
         // Flatten items that recursively store other items
-        item.kind.inner_items().for_each(|i| self.item(i.clone(), cache).unwrap());
+        item.kind.inner_items().for_each(|i| self.item(i.clone()).unwrap());
 
         let id = item.def_id;
         if let Some(mut new_item) = self.convert_item(item) {
-            if let types::ItemEnum::TraitItem(ref mut t) = new_item.inner {
-                t.implementors = self.get_trait_implementors(id, cache)
-            } else if let types::ItemEnum::StructItem(ref mut s) = new_item.inner {
-                s.impls = self.get_impls(id, cache)
-            } else if let types::ItemEnum::EnumItem(ref mut e) = new_item.inner {
-                e.impls = self.get_impls(id, cache)
+            if let types::ItemEnum::Trait(ref mut t) = new_item.inner {
+                t.implementations = self.get_trait_implementors(id.expect_def_id())
+            } else if let types::ItemEnum::Struct(ref mut s) = new_item.inner {
+                s.impls = self.get_impls(id.expect_def_id())
+            } else if let types::ItemEnum::Enum(ref mut e) = new_item.inner {
+                e.impls = self.get_impls(id.expect_def_id())
+            } else if let types::ItemEnum::Union(ref mut u) = new_item.inner {
+                u.impls = self.get_impls(id.expect_def_id())
             }
-            let removed = self.index.borrow_mut().insert(id.into(), new_item.clone());
+            let removed = self.index.borrow_mut().insert(from_item_id(id), new_item.clone());
+
             // FIXME(adotinthevoid): Currently, the index is duplicated. This is a sanity check
-            // to make sure the items are unique.
+            // to make sure the items are unique. The main place this happens is when an item, is
+            // reexported in more than one place. See `rustdoc-json/reexport/in_root_and_mod`
             if let Some(old_item) = removed {
                 assert_eq!(old_item, new_item);
             }
@@ -171,64 +200,55 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
         Ok(())
     }
 
-    fn mod_item_in(
-        &mut self,
-        item: &clean::Item,
-        _module_name: &str,
-        cache: &Cache,
-    ) -> Result<(), Error> {
-        use clean::types::ItemKind::*;
-        if let ModuleItem(m) = &item.kind {
-            for item in &m.items {
-                match &item.kind {
-                    // These don't have names so they don't get added to the output by default
-                    ImportItem(_) => self.item(item.clone(), cache).unwrap(),
-                    ExternCrateItem(_, _) => self.item(item.clone(), cache).unwrap(),
-                    ImplItem(i) => {
-                        i.items.iter().for_each(|i| self.item(i.clone(), cache).unwrap())
-                    }
-                    _ => {}
-                }
-            }
-        }
-        self.item(item.clone(), cache).unwrap();
-        Ok(())
-    }
-
-    fn mod_item_out(&mut self, _item_name: &str) -> Result<(), Error> {
-        Ok(())
+    fn mod_item_in(&mut self, _item: &clean::Item) -> Result<(), Error> {
+        unreachable!("RUN_ON_MODULE = false should never call mod_item_in")
     }
 
-    fn after_krate(&mut self, krate: &clean::Crate, cache: &Cache) -> Result<(), Error> {
+    fn after_krate(&mut self) -> Result<(), Error> {
         debug!("Done with crate");
+
+        for primitive in Rc::clone(&self.cache).primitive_locations.values() {
+            self.get_impls(*primitive);
+        }
+
         let mut index = (*self.index).clone().into_inner();
-        index.extend(self.get_trait_items(cache));
+        index.extend(self.get_trait_items());
+        // This needs to be the default HashMap for compatibility with the public interface for
+        // rustdoc-json-types
+        #[allow(rustc::default_hash_types)]
         let output = types::Crate {
             root: types::Id(String::from("0:0")),
-            crate_version: krate.version.clone(),
-            includes_private: cache.document_private,
-            index,
-            paths: cache
+            crate_version: self.cache.crate_version.clone(),
+            includes_private: self.cache.document_private,
+            index: index.into_iter().collect(),
+            paths: self
+                .cache
                 .paths
                 .clone()
                 .into_iter()
-                .chain(cache.external_paths.clone().into_iter())
+                .chain(self.cache.external_paths.clone().into_iter())
                 .map(|(k, (path, kind))| {
                     (
-                        k.into(),
-                        types::ItemSummary { crate_id: k.krate.as_u32(), path, kind: kind.into() },
+                        from_item_id(k.into()),
+                        types::ItemSummary {
+                            crate_id: k.krate.as_u32(),
+                            path: path.iter().map(|s| s.to_string()).collect(),
+                            kind: kind.into_tcx(self.tcx),
+                        },
                     )
                 })
                 .collect(),
-            external_crates: cache
+            external_crates: self
+                .cache
                 .extern_locations
                 .iter()
-                .map(|(k, v)| {
+                .map(|(crate_num, external_location)| {
+                    let e = ExternalCrate { crate_num: *crate_num };
                     (
-                        k.as_u32(),
+                        crate_num.as_u32(),
                         types::ExternalCrate {
-                            name: v.0.to_string(),
-                            html_root_url: match &v.2 {
+                            name: e.name(self.tcx).to_string(),
+                            html_root_url: match external_location {
                                 ExternalLocation::Remote(s) => Some(s.clone()),
                                 _ => None,
                             },
@@ -236,17 +256,22 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
                     )
                 })
                 .collect(),
-            format_version: 1,
+            format_version: types::FORMAT_VERSION,
         };
-        let mut p = self.out_path.clone();
+        let out_dir = self.out_path.clone();
+        try_err!(create_dir_all(&out_dir), out_dir);
+
+        let mut p = out_dir;
         p.push(output.index.get(&output.root).unwrap().name.clone().unwrap());
         p.set_extension("json");
-        let file = File::create(&p).map_err(|error| Error { error: error.to_string(), file: p })?;
-        serde_json::ser::to_writer(&file, &output).unwrap();
+        let mut file = BufWriter::new(try_err!(File::create(&p), p));
+        serde_json::ser::to_writer(&mut file, &output).unwrap();
+        try_err!(file.flush(), p);
+
         Ok(())
     }
 
-    fn after_run(&mut self, _diag: &rustc_errors::Handler) -> Result<(), Error> {
-        Ok(())
+    fn cache(&self) -> &Cache {
+        &self.cache
     }
 }