rustdoc/json/
mod.rs

1//! Rustdoc's JSON backend
2//!
3//! This module contains the logic for rendering a crate as JSON rather than the normal static HTML
4//! output. See [the RFC](https://github.com/rust-lang/rfcs/pull/2963) and the [`types`] module
5//! docs for usage and details.
6
7mod conversions;
8mod import_finder;
9
10use std::cell::RefCell;
11use std::fs::{File, create_dir_all};
12use std::io::{BufWriter, Write, stdout};
13use std::path::PathBuf;
14use std::rc::Rc;
15
16use rustc_hir::def_id::{DefId, DefIdSet};
17use rustc_middle::ty::TyCtxt;
18use rustc_session::Session;
19use rustc_span::Symbol;
20use rustc_span::def_id::LOCAL_CRATE;
21use rustdoc_json_types as types;
22// It's important to use the FxHashMap from rustdoc_json_types here, instead of
23// the one from rustc_data_structures, as they're different types due to sysroots.
24// See #110051 and #127456 for details
25use rustdoc_json_types::FxHashMap;
26use tracing::{debug, trace};
27
28use crate::clean::ItemKind;
29use crate::clean::types::{ExternalCrate, ExternalLocation};
30use crate::config::RenderOptions;
31use crate::docfs::PathError;
32use crate::error::Error;
33use crate::formats::FormatRenderer;
34use crate::formats::cache::Cache;
35use crate::json::conversions::IntoJson;
36use crate::{clean, try_err};
37
38#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
39struct FullItemId {
40    def_id: DefId,
41    name: Option<Symbol>,
42    /// Used to distinguish imports of different items with the same name
43    extra: Option<types::Id>,
44}
45
46#[derive(Clone)]
47pub(crate) struct JsonRenderer<'tcx> {
48    tcx: TyCtxt<'tcx>,
49    /// A mapping of IDs that contains all local items for this crate which gets output as a top
50    /// level field of the JSON blob.
51    index: Rc<RefCell<FxHashMap<types::Id, types::Item>>>,
52    /// The directory where the JSON blob should be written to.
53    ///
54    /// If this is `None`, the blob will be printed to `stdout` instead.
55    out_dir: Option<PathBuf>,
56    cache: Rc<Cache>,
57    imported_items: DefIdSet,
58    id_interner: Rc<RefCell<FxHashMap<(FullItemId, Option<FullItemId>), types::Id>>>,
59}
60
61impl<'tcx> JsonRenderer<'tcx> {
62    fn sess(&self) -> &'tcx Session {
63        self.tcx.sess
64    }
65
66    fn get_trait_implementors(&mut self, id: DefId) -> Vec<types::Id> {
67        Rc::clone(&self.cache)
68            .implementors
69            .get(&id)
70            .map(|implementors| {
71                implementors
72                    .iter()
73                    .map(|i| {
74                        let item = &i.impl_item;
75                        self.item(item.clone()).unwrap();
76                        self.id_from_item(item)
77                    })
78                    .collect()
79            })
80            .unwrap_or_default()
81    }
82
83    fn get_impls(&mut self, id: DefId) -> Vec<types::Id> {
84        Rc::clone(&self.cache)
85            .impls
86            .get(&id)
87            .map(|impls| {
88                impls
89                    .iter()
90                    .filter_map(|i| {
91                        let item = &i.impl_item;
92
93                        // HACK(hkmatsumoto): For impls of primitive types, we index them
94                        // regardless of whether they're local. This is because users can
95                        // document primitive items in an arbitrary crate by using
96                        // `rustc_doc_primitive`.
97                        let mut is_primitive_impl = false;
98                        if let clean::types::ItemKind::ImplItem(ref impl_) = item.kind
99                            && impl_.trait_.is_none()
100                            && let clean::types::Type::Primitive(_) = impl_.for_
101                        {
102                            is_primitive_impl = true;
103                        }
104
105                        if item.item_id.is_local() || is_primitive_impl {
106                            self.item(item.clone()).unwrap();
107                            Some(self.id_from_item(item))
108                        } else {
109                            None
110                        }
111                    })
112                    .collect()
113            })
114            .unwrap_or_default()
115    }
116
117    fn serialize_and_write<T: Write>(
118        &self,
119        output_crate: types::Crate,
120        mut writer: BufWriter<T>,
121        path: &str,
122    ) -> Result<(), Error> {
123        self.sess().time("rustdoc_json_serialize_and_write", || {
124            try_err!(
125                serde_json::ser::to_writer(&mut writer, &output_crate).map_err(|e| e.to_string()),
126                path
127            );
128            try_err!(writer.flush(), path);
129            Ok(())
130        })
131    }
132}
133
134impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
135    fn descr() -> &'static str {
136        "json"
137    }
138
139    const RUN_ON_MODULE: bool = false;
140    type ModuleData = ();
141
142    fn init(
143        krate: clean::Crate,
144        options: RenderOptions,
145        cache: Cache,
146        tcx: TyCtxt<'tcx>,
147    ) -> Result<(Self, clean::Crate), Error> {
148        debug!("Initializing json renderer");
149
150        let (krate, imported_items) = import_finder::get_imports(krate);
151
152        Ok((
153            JsonRenderer {
154                tcx,
155                index: Rc::new(RefCell::new(FxHashMap::default())),
156                out_dir: if options.output_to_stdout { None } else { Some(options.output) },
157                cache: Rc::new(cache),
158                imported_items,
159                id_interner: Default::default(),
160            },
161            krate,
162        ))
163    }
164
165    fn save_module_data(&mut self) -> Self::ModuleData {
166        unreachable!("RUN_ON_MODULE = false, should never call save_module_data")
167    }
168    fn restore_module_data(&mut self, _info: Self::ModuleData) {
169        unreachable!("RUN_ON_MODULE = false, should never call set_back_info")
170    }
171
172    /// Inserts an item into the index. This should be used rather than directly calling insert on
173    /// the hashmap because certain items (traits and types) need to have their mappings for trait
174    /// implementations filled out before they're inserted.
175    fn item(&mut self, item: clean::Item) -> Result<(), Error> {
176        let item_type = item.type_();
177        let item_name = item.name;
178        trace!("rendering {item_type} {item_name:?}");
179
180        // Flatten items that recursively store other items. We include orphaned items from
181        // stripped modules and etc that are otherwise reachable.
182        if let ItemKind::StrippedItem(inner) = &item.kind {
183            inner.inner_items().for_each(|i| self.item(i.clone()).unwrap());
184        }
185
186        // Flatten items that recursively store other items
187        item.kind.inner_items().for_each(|i| self.item(i.clone()).unwrap());
188
189        let item_id = item.item_id;
190        if let Some(mut new_item) = self.convert_item(item) {
191            let can_be_ignored = match new_item.inner {
192                types::ItemEnum::Trait(ref mut t) => {
193                    t.implementations = self.get_trait_implementors(item_id.expect_def_id());
194                    false
195                }
196                types::ItemEnum::Struct(ref mut s) => {
197                    s.impls = self.get_impls(item_id.expect_def_id());
198                    false
199                }
200                types::ItemEnum::Enum(ref mut e) => {
201                    e.impls = self.get_impls(item_id.expect_def_id());
202                    false
203                }
204                types::ItemEnum::Union(ref mut u) => {
205                    u.impls = self.get_impls(item_id.expect_def_id());
206                    false
207                }
208                types::ItemEnum::Primitive(ref mut p) => {
209                    p.impls = self.get_impls(item_id.expect_def_id());
210                    false
211                }
212
213                types::ItemEnum::Function(_)
214                | types::ItemEnum::Module(_)
215                | types::ItemEnum::Use(_)
216                | types::ItemEnum::AssocConst { .. }
217                | types::ItemEnum::AssocType { .. } => true,
218                types::ItemEnum::ExternCrate { .. }
219                | types::ItemEnum::StructField(_)
220                | types::ItemEnum::Variant(_)
221                | types::ItemEnum::TraitAlias(_)
222                | types::ItemEnum::Impl(_)
223                | types::ItemEnum::TypeAlias(_)
224                | types::ItemEnum::Constant { .. }
225                | types::ItemEnum::Static(_)
226                | types::ItemEnum::ExternType
227                | types::ItemEnum::Macro(_)
228                | types::ItemEnum::ProcMacro(_) => false,
229            };
230            let removed = self.index.borrow_mut().insert(new_item.id, new_item.clone());
231
232            // FIXME(adotinthevoid): Currently, the index is duplicated. This is a sanity check
233            // to make sure the items are unique. The main place this happens is when an item, is
234            // reexported in more than one place. See `rustdoc-json/reexport/in_root_and_mod`
235            if let Some(old_item) = removed {
236                // In case of generic implementations (like `impl<T> Trait for T {}`), all the
237                // inner items will be duplicated so we can ignore if they are slightly different.
238                if !can_be_ignored {
239                    assert_eq!(old_item, new_item);
240                }
241                trace!("replaced {old_item:?}\nwith {new_item:?}");
242            }
243        }
244
245        trace!("done rendering {item_type} {item_name:?}");
246        Ok(())
247    }
248
249    fn mod_item_in(&mut self, _item: &clean::Item) -> Result<(), Error> {
250        unreachable!("RUN_ON_MODULE = false, should never call mod_item_in")
251    }
252
253    fn after_krate(&mut self) -> Result<(), Error> {
254        debug!("Done with crate");
255
256        let e = ExternalCrate { crate_num: LOCAL_CRATE };
257        let index = (*self.index).clone().into_inner();
258
259        debug!("Constructing Output");
260        let output_crate = types::Crate {
261            root: self.id_from_item_default(e.def_id().into()),
262            crate_version: self.cache.crate_version.clone(),
263            includes_private: self.cache.document_private,
264            index,
265            paths: self
266                .cache
267                .paths
268                .iter()
269                .chain(&self.cache.external_paths)
270                .map(|(&k, &(ref path, kind))| {
271                    (
272                        self.id_from_item_default(k.into()),
273                        types::ItemSummary {
274                            crate_id: k.krate.as_u32(),
275                            path: path.iter().map(|s| s.to_string()).collect(),
276                            kind: kind.into_json(self),
277                        },
278                    )
279                })
280                .collect(),
281            external_crates: self
282                .cache
283                .extern_locations
284                .iter()
285                .map(|(crate_num, external_location)| {
286                    let e = ExternalCrate { crate_num: *crate_num };
287                    (
288                        crate_num.as_u32(),
289                        types::ExternalCrate {
290                            name: e.name(self.tcx).to_string(),
291                            html_root_url: match external_location {
292                                ExternalLocation::Remote(s) => Some(s.clone()),
293                                _ => None,
294                            },
295                        },
296                    )
297                })
298                .collect(),
299            format_version: types::FORMAT_VERSION,
300        };
301        if let Some(ref out_dir) = self.out_dir {
302            try_err!(create_dir_all(out_dir), out_dir);
303
304            let mut p = out_dir.clone();
305            p.push(output_crate.index.get(&output_crate.root).unwrap().name.clone().unwrap());
306            p.set_extension("json");
307
308            self.serialize_and_write(
309                output_crate,
310                try_err!(File::create_buffered(&p), p),
311                &p.display().to_string(),
312            )
313        } else {
314            self.serialize_and_write(output_crate, BufWriter::new(stdout().lock()), "<stdout>")
315        }
316    }
317
318    fn cache(&self) -> &Cache {
319        &self.cache
320    }
321}