diff options
| author | Justus K <justus.k@protonmail.com> | 2021-04-29 21:36:54 +0200 |
|---|---|---|
| committer | Justus K <justus.k@protonmail.com> | 2021-05-04 19:34:12 +0200 |
| commit | b6120bfb354c1c1c9fdfe7ff03daa5550a8706fc (patch) | |
| tree | 4a59bc13db3194abacf0c3a34329b7886c11e8ce /src/librustdoc/html/render | |
| parent | 7a0f1781d04662041db5deaef89598a8edd53717 (diff) | |
| download | rust-b6120bfb354c1c1c9fdfe7ff03daa5550a8706fc.tar.gz rust-b6120bfb354c1c1c9fdfe7ff03daa5550a8706fc.zip | |
Add type to differentiate between fake and real DefId's
Diffstat (limited to 'src/librustdoc/html/render')
| -rw-r--r-- | src/librustdoc/html/render/cache.rs | 10 | ||||
| -rw-r--r-- | src/librustdoc/html/render/context.rs | 5 | ||||
| -rw-r--r-- | src/librustdoc/html/render/mod.rs | 23 | ||||
| -rw-r--r-- | src/librustdoc/html/render/print_item.rs | 30 | ||||
| -rw-r--r-- | src/librustdoc/html/render/write_shared.rs | 4 |
5 files changed, 40 insertions, 32 deletions
diff --git a/src/librustdoc/html/render/cache.rs b/src/librustdoc/html/render/cache.rs index 27a8065afb6..57520a1a1fb 100644 --- a/src/librustdoc/html/render/cache.rs +++ b/src/librustdoc/html/render/cache.rs @@ -7,7 +7,7 @@ use serde::ser::{Serialize, SerializeStruct, Serializer}; use crate::clean; use crate::clean::types::{ - FnDecl, FnRetTy, GenericBound, Generics, GetDefId, Type, WherePredicate, + FakeDefId, FnDecl, FnRetTy, GenericBound, Generics, GetDefId, Type, WherePredicate, }; use crate::formats::cache::Cache; use crate::formats::item_type::ItemType; @@ -39,7 +39,7 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt< name: item.name.unwrap().to_string(), path: fqp[..fqp.len() - 1].join("::"), desc: item.doc_value().map_or_else(String::new, |s| short_markdown_summary(&s)), - parent: Some(did), + parent: Some(did.into()), parent_idx: None, search_type: get_index_search_type(&item, cache, tcx), aliases: item.attrs.get_doc_aliases(), @@ -82,7 +82,7 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt< defid_to_pathid.insert(defid, pathid); lastpathid += 1; - if let Some(&(ref fqp, short)) = paths.get(&defid) { + if let Some(&(ref fqp, short)) = paths.get(&defid.expect_real()) { crate_paths.push((short, fqp.last().unwrap().clone())); Some(pathid) } else { @@ -214,7 +214,7 @@ crate fn get_index_search_type<'tcx>( fn get_index_type(clean_type: &clean::Type, cache: &Cache) -> RenderType { RenderType { - ty: clean_type.def_id_full(cache), + ty: clean_type.def_id_full(cache).map(FakeDefId::new_real), idx: None, name: get_index_type_name(clean_type, true).map(|s| s.as_str().to_ascii_lowercase()), generics: get_generics(clean_type, cache), @@ -256,7 +256,7 @@ fn get_generics(clean_type: &clean::Type, cache: &Cache) -> Option<Vec<Generic>> .filter_map(|t| { get_index_type_name(t, false).map(|name| Generic { name: name.as_str().to_ascii_lowercase(), - defid: t.def_id_full(cache), + defid: t.def_id_full(cache).map(FakeDefId::new_real), idx: None, }) }) diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 293c0a40fa7..2f3f87215c3 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -18,7 +18,8 @@ use super::print_item::{full_path, item_path, print_item}; use super::write_shared::write_shared; use super::{print_sidebar, settings, AllTypes, NameDoc, StylePath, BASIC_KEYWORDS}; -use crate::clean::{self, ExternalCrate}; +use crate::clean; +use crate::clean::ExternalCrate; use crate::config::RenderOptions; use crate::docfs::{DocFS, PathError}; use crate::error::Error; @@ -218,7 +219,7 @@ impl<'tcx> Context<'tcx> { &self.shared.style_files, ) } else { - if let Some(&(ref names, ty)) = self.cache.paths.get(&it.def_id) { + if let Some(&(ref names, ty)) = self.cache.paths.get(&it.def_id.expect_real()) { let mut path = String::new(); for name in &names[..names.len() - 1] { path.push_str(name); diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 1271b54debd..ea57831c0e5 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -54,7 +54,7 @@ use rustc_span::symbol::{kw, sym, Symbol}; use serde::ser::SerializeSeq; use serde::{Serialize, Serializer}; -use crate::clean::{self, GetDefId, RenderedLink, SelfTy}; +use crate::clean::{self, FakeDefId, GetDefId, RenderedLink, SelfTy}; use crate::docfs::PathError; use crate::error::Error; use crate::formats::cache::Cache; @@ -87,7 +87,7 @@ crate struct IndexItem { crate name: String, crate path: String, crate desc: String, - crate parent: Option<DefId>, + crate parent: Option<FakeDefId>, crate parent_idx: Option<usize>, crate search_type: Option<IndexItemFunctionType>, crate aliases: Box<[String]>, @@ -96,7 +96,7 @@ crate struct IndexItem { /// A type used for the search index. #[derive(Debug)] crate struct RenderType { - ty: Option<DefId>, + ty: Option<FakeDefId>, idx: Option<usize>, name: Option<String>, generics: Option<Vec<Generic>>, @@ -128,7 +128,7 @@ impl Serialize for RenderType { #[derive(Debug)] crate struct Generic { name: String, - defid: Option<DefId>, + defid: Option<FakeDefId>, idx: Option<usize>, } @@ -709,7 +709,7 @@ fn render_impls( .map(|i| { let did = i.trait_did_full(cache).unwrap(); let provided_trait_methods = i.inner_impl().provided_trait_methods(tcx); - let assoc_link = AssocItemLink::GotoSource(did, &provided_trait_methods); + let assoc_link = AssocItemLink::GotoSource(did.into(), &provided_trait_methods); let mut buffer = if w.is_for_html() { Buffer::html() } else { Buffer::new() }; render_impl( &mut buffer, @@ -747,7 +747,7 @@ fn naive_assoc_href(it: &clean::Item, link: AssocItemLink<'_>, cx: &Context<'_>) AssocItemLink::Anchor(Some(ref id)) => format!("#{}", id), AssocItemLink::Anchor(None) => anchor, AssocItemLink::GotoSource(did, _) => { - href(did, cx).map(|p| format!("{}{}", p.0, anchor)).unwrap_or(anchor) + href(did.expect_real(), cx).map(|p| format!("{}{}", p.0, anchor)).unwrap_or(anchor) } } } @@ -855,7 +855,7 @@ fn render_assoc_item( ItemType::TyMethod }; - href(did, cx) + href(did.expect_real(), cx) .map(|p| format!("{}#{}.{}", p.0, ty, name)) .unwrap_or_else(|| format!("#{}.{}", ty, name)) } @@ -979,7 +979,7 @@ fn render_attributes_in_code(w: &mut Buffer, it: &clean::Item) { #[derive(Copy, Clone)] enum AssocItemLink<'a> { Anchor(Option<&'a str>), - GotoSource(DefId, &'a FxHashSet<Symbol>), + GotoSource(FakeDefId, &'a FxHashSet<Symbol>), } impl<'a> AssocItemLink<'a> { @@ -1217,7 +1217,7 @@ fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String { it, &[], Some(&tydef.type_), - AssocItemLink::GotoSource(t_did, &FxHashSet::default()), + AssocItemLink::GotoSource(t_did.into(), &FxHashSet::default()), "", cx, ); @@ -1476,7 +1476,7 @@ fn render_impl( } let did = i.trait_.as_ref().unwrap().def_id_full(cx.cache()).unwrap(); let provided_methods = i.provided_trait_methods(cx.tcx()); - let assoc_link = AssocItemLink::GotoSource(did, &provided_methods); + let assoc_link = AssocItemLink::GotoSource(did.into(), &provided_methods); doc_impl_item( boring, @@ -1810,7 +1810,8 @@ fn small_url_encode(s: String) -> String { } fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) { - if let Some(v) = cx.cache.impls.get(&it.def_id) { + let did = it.def_id.expect_real(); + if let Some(v) = cx.cache.impls.get(&did) { let mut used_links = FxHashSet::default(); let cache = cx.cache(); diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 1bf726dd31a..70b5458ece8 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -270,14 +270,18 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl w, "<tr><td><code>{}extern crate {} as {};", myitem.visibility.print_with_space(myitem.def_id, cx), - anchor(myitem.def_id, &*src.as_str(), cx), + anchor(myitem.def_id.expect_real(), &*src.as_str(), cx), myitem.name.as_ref().unwrap(), ), None => write!( w, "<tr><td><code>{}extern crate {};", myitem.visibility.print_with_space(myitem.def_id, cx), - anchor(myitem.def_id, &*myitem.name.as_ref().unwrap().as_str(), cx), + anchor( + myitem.def_id.expect_real(), + &*myitem.name.as_ref().unwrap().as_str(), + cx + ), ), } w.write_str("</code></td></tr>"); @@ -290,7 +294,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl // Just need an item with the correct def_id and attrs let import_item = clean::Item { - def_id: import_def_id, + def_id: import_def_id.into(), attrs: import_attrs, cfg: ast_attrs.cfg(cx.tcx().sess.diagnostic()), ..myitem.clone() @@ -629,7 +633,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra } // If there are methods directly on this trait object, render them here. - render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All); + render_assoc_items(w, cx, it, it.def_id.expect_real(), AssocItemRender::All); if let Some(implementors) = cx.cache.implementors.get(&it.def_id) { // The DefId is for the first Type found with that name. The bool is @@ -752,7 +756,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra path = if it.def_id.is_local() { cx.current.join("/") } else { - let (ref path, _) = cx.cache.external_paths[&it.def_id]; + let (ref path, _) = cx.cache.external_paths[&it.def_id.expect_real()]; path[..path.len() - 1].join("/") }, ty = it.type_(), @@ -778,7 +782,7 @@ fn item_trait_alias(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clea // won't be visible anywhere in the docs. It would be nice to also show // associated items from the aliased type (see discussion in #32077), but // we need #14072 to make sense of the generics. - render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All) + render_assoc_items(w, cx, it, it.def_id.expect_real(), AssocItemRender::All) } fn item_opaque_ty(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::OpaqueTy) { @@ -799,7 +803,7 @@ fn item_opaque_ty(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean: // won't be visible anywhere in the docs. It would be nice to also show // associated items from the aliased type (see discussion in #32077), but // we need #14072 to make sense of the generics. - render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All) + render_assoc_items(w, cx, it, it.def_id.expect_real(), AssocItemRender::All) } fn item_typedef(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Typedef) { @@ -820,7 +824,7 @@ fn item_typedef(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::T // won't be visible anywhere in the docs. It would be nice to also show // associated items from the aliased type (see discussion in #32077), but // we need #14072 to make sense of the generics. - render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All) + render_assoc_items(w, cx, it, it.def_id.expect_real(), AssocItemRender::All) } fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Union) { @@ -866,7 +870,7 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni document(w, cx, field, Some(it)); } } - render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All) + render_assoc_items(w, cx, it, it.def_id.expect_real(), AssocItemRender::All) } fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum) { @@ -1000,7 +1004,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum render_stability_since(w, variant, it, cx.tcx()); } } - render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All) + render_assoc_items(w, cx, it, it.def_id.expect_real(), AssocItemRender::All) } fn item_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Macro) { @@ -1049,7 +1053,7 @@ fn item_proc_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, m: &clean fn item_primitive(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) { document(w, cx, it, None); - render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All) + render_assoc_items(w, cx, it, it.def_id.expect_real(), AssocItemRender::All) } fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean::Constant) { @@ -1137,7 +1141,7 @@ fn item_struct(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St } } } - render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All) + render_assoc_items(w, cx, it, it.def_id.expect_real(), AssocItemRender::All) } fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Static) { @@ -1166,7 +1170,7 @@ fn item_foreign_type(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) { document(w, cx, it, None); - render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All) + render_assoc_items(w, cx, it, it.def_id.expect_real(), AssocItemRender::All) } fn item_keyword(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) { diff --git a/src/librustdoc/html/render/write_shared.rs b/src/librustdoc/html/render/write_shared.rs index 8e10c696df0..c493801d990 100644 --- a/src/librustdoc/html/render/write_shared.rs +++ b/src/librustdoc/html/render/write_shared.rs @@ -464,6 +464,8 @@ pub(super) fn write_shared( // Update the list of all implementors for traits let dst = cx.dst.join("implementors"); for (&did, imps) in &cx.cache.implementors { + let did = did.expect_real(); + // Private modules can leak through to this phase of rustdoc, which // could contain implementations for otherwise private types. In some // rare cases we could find an implementation for an item which wasn't @@ -496,7 +498,7 @@ pub(super) fn write_shared( // // If the implementation is from another crate then that crate // should add it. - if imp.impl_item.def_id.krate == did.krate || !imp.impl_item.def_id.is_local() { + if imp.impl_item.def_id.krate() == did.krate || !imp.impl_item.def_id.is_local() { None } else { Some(Implementor { |
