From 43e1cdbaf9230cc36b18f9ab41079449c5cac375 Mon Sep 17 00:00:00 2001 From: Justus K Date: Sat, 26 Jun 2021 13:52:31 +0200 Subject: [PATCH 1/7] rustdoc: Replace `FakeDefId` with new `ItemId` type --- src/librustdoc/clean/auto_trait.rs | 2 +- src/librustdoc/clean/blanket_impl.rs | 2 +- src/librustdoc/clean/inline.rs | 4 +- src/librustdoc/clean/types.rs | 74 +++++++++----------- src/librustdoc/core.rs | 11 +-- src/librustdoc/formats/cache.rs | 4 +- src/librustdoc/html/format.rs | 4 +- src/librustdoc/html/render/mod.rs | 4 +- src/librustdoc/json/conversions.rs | 26 ++++--- src/librustdoc/json/mod.rs | 14 ++-- src/librustdoc/passes/collect_trait_impls.rs | 6 +- src/librustdoc/passes/strip_hidden.rs | 6 +- src/librustdoc/passes/strip_private.rs | 4 +- src/librustdoc/passes/stripper.rs | 6 +- 14 files changed, 81 insertions(+), 86 deletions(-) diff --git a/src/librustdoc/clean/auto_trait.rs b/src/librustdoc/clean/auto_trait.rs index ebab3add6c55d..e479d162b8ffb 100644 --- a/src/librustdoc/clean/auto_trait.rs +++ b/src/librustdoc/clean/auto_trait.rs @@ -113,7 +113,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> { name: None, attrs: Default::default(), visibility: Inherited, - def_id: FakeDefId::new_fake(item_def_id.krate), + def_id: ItemId::Auto { trait_: trait_def_id, for_: item_def_id }, kind: box ImplItem(Impl { span: Span::dummy(), unsafety: hir::Unsafety::Normal, diff --git a/src/librustdoc/clean/blanket_impl.rs b/src/librustdoc/clean/blanket_impl.rs index 3f2fae1aca33b..59fefee5ee4ed 100644 --- a/src/librustdoc/clean/blanket_impl.rs +++ b/src/librustdoc/clean/blanket_impl.rs @@ -96,7 +96,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> { name: None, attrs: Default::default(), visibility: Inherited, - def_id: FakeDefId::new_fake(item_def_id.krate), + def_id: ItemId::Blanket { trait_: trait_def_id, for_: item_def_id }, kind: box ImplItem(Impl { span: self.cx.tcx.def_span(impl_def_id).clean(self.cx), unsafety: hir::Unsafety::Normal, diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 7b1b23ff8e6d3..ce4c1a76b5b80 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -15,7 +15,7 @@ use rustc_span::hygiene::MacroKind; use rustc_span::symbol::{kw, sym, Symbol}; use crate::clean::{ - self, utils, Attributes, AttributesExt, FakeDefId, GetDefId, NestedAttributesExt, Type, + self, utils, Attributes, AttributesExt, GetDefId, ItemId, NestedAttributesExt, Type, }; use crate::core::DocContext; use crate::formats::item_type::ItemType; @@ -486,7 +486,7 @@ fn build_module( items.push(clean::Item { name: None, attrs: box clean::Attributes::default(), - def_id: FakeDefId::new_fake(did.krate), + def_id: ItemId::Primitive(did.krate), visibility: clean::Public, kind: box clean::ImportItem(clean::Import::new_simple( item.ident.name, diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 4da2f14ce8a8d..fe78548bc3189 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1,4 +1,4 @@ -use std::cell::{Cell, RefCell}; +use std::cell::RefCell; use std::default::Default; use std::hash::{Hash, Hasher}; use std::iter::FromIterator; @@ -18,7 +18,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::thin_vec::ThinVec; use rustc_hir as hir; use rustc_hir::def::{CtorKind, DefKind, Res}; -use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE}; +use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX}; use rustc_hir::lang_items::LangItem; use rustc_hir::{BodyId, Mutability}; use rustc_index::vec::IndexVec; @@ -48,73 +48,68 @@ use self::ItemKind::*; use self::SelfTy::*; use self::Type::*; -crate type FakeDefIdSet = FxHashSet; +crate type ItemIdSet = FxHashSet; #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] -crate enum FakeDefId { - Real(DefId), - Fake(DefIndex, CrateNum), -} - -impl FakeDefId { - #[cfg(parallel_compiler)] - crate fn new_fake(crate: CrateNum) -> Self { - unimplemented!("") - } - - #[cfg(not(parallel_compiler))] - crate fn new_fake(krate: CrateNum) -> Self { - thread_local!(static FAKE_DEF_ID_COUNTER: Cell = Cell::new(0)); - let id = FAKE_DEF_ID_COUNTER.with(|id| { - let tmp = id.get(); - id.set(tmp + 1); - tmp - }); - Self::Fake(DefIndex::from(id), krate) - } - +crate enum ItemId { + /// A "normal" item that uses a [`DefId`] for identification. + DefId(DefId), + /// Identifier that is used for auto traits. + Auto { trait_: DefId, for_: DefId }, + /// Identifier that is used for blanket implementations. + Blanket { trait_: DefId, for_: DefId }, + /// Identifier for primitive types. + Primitive(CrateNum), +} + +impl ItemId { #[inline] crate fn is_local(self) -> bool { match self { - FakeDefId::Real(id) => id.is_local(), - FakeDefId::Fake(_, krate) => krate == LOCAL_CRATE, + ItemId::DefId(id) => id.is_local(), + _ => false, } } #[inline] #[track_caller] crate fn expect_real(self) -> rustc_hir::def_id::DefId { - self.as_real().unwrap_or_else(|| panic!("FakeDefId::expect_real: `{:?}` isn't real", self)) + self.as_real() + .unwrap_or_else(|| panic!("ItemId::expect_real: `{:?}` isn't a real ItemId", self)) } #[inline] crate fn as_real(self) -> Option { match self { - FakeDefId::Real(id) => Some(id), - FakeDefId::Fake(_, _) => None, + ItemId::DefId(id) => Some(id), + _ => None, } } #[inline] crate fn krate(self) -> CrateNum { match self { - FakeDefId::Real(id) => id.krate, - FakeDefId::Fake(_, krate) => krate, + ItemId::DefId(id) => id.krate, + ItemId::Auto { trait_, .. } => trait_.krate, + ItemId::Blanket { trait_, .. } => trait_.krate, + ItemId::Primitive(krate) => krate, } } #[inline] crate fn index(self) -> Option { match self { - FakeDefId::Real(id) => Some(id.index), - FakeDefId::Fake(_, _) => None, + ItemId::DefId(id) => Some(id.index), + ItemId::Auto { trait_, .. } => Some(trait_.index), + ItemId::Blanket { trait_, .. } => Some(trait_.index), + ItemId::Primitive(..) => None, } } } -impl From for FakeDefId { +impl From for ItemId { fn from(id: DefId) -> Self { - Self::Real(id) + Self::DefId(id) } } @@ -338,14 +333,14 @@ crate struct Item { /// Information about this item that is specific to what kind of item it is. /// E.g., struct vs enum vs function. crate kind: Box, - crate def_id: FakeDefId, + crate def_id: ItemId, crate cfg: Option>, } // `Item` is used a lot. Make sure it doesn't unintentionally get bigger. #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(Item, 48); +rustc_data_structures::static_assert_size!(Item, 56); crate fn rustc_span(def_id: DefId, tcx: TyCtxt<'_>) -> Span { Span::from_rustc_span(def_id.as_local().map_or_else( @@ -664,7 +659,8 @@ impl Item { } crate fn is_fake(&self) -> bool { - matches!(self.def_id, FakeDefId::Fake(_, _)) + // FIXME: Find a better way to handle this + !matches!(self.def_id, ItemId::DefId(..)) } } diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 66f4f48129269..0689d72e4e0cf 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -30,7 +30,7 @@ use std::mem; use std::rc::Rc; use crate::clean::inline::build_external_trait; -use crate::clean::{self, FakeDefId, TraitWithExtraInfo}; +use crate::clean::{self, ItemId, TraitWithExtraInfo}; use crate::config::{Options as RustdocOptions, OutputFormat, RenderOptions}; use crate::formats::cache::Cache; use crate::passes::{self, Condition::*, ConditionalPass}; @@ -78,7 +78,7 @@ crate struct DocContext<'tcx> { /// This same cache is used throughout rustdoc, including in [`crate::html::render`]. crate cache: Cache, /// Used by [`clean::inline`] to tell if an item has already been inlined. - crate inlined: FxHashSet, + crate inlined: FxHashSet, /// Used by `calculate_doc_coverage`. crate output_format: OutputFormat, } @@ -128,12 +128,13 @@ impl<'tcx> DocContext<'tcx> { /// Like `hir().local_def_id_to_hir_id()`, but skips calling it on fake DefIds. /// (This avoids a slice-index-out-of-bounds panic.) - crate fn as_local_hir_id(tcx: TyCtxt<'_>, def_id: FakeDefId) -> Option { + crate fn as_local_hir_id(tcx: TyCtxt<'_>, def_id: ItemId) -> Option { match def_id { - FakeDefId::Real(real_id) => { + ItemId::DefId(real_id) => { real_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)) } - FakeDefId::Fake(_, _) => None, + // FIXME: Can this be `Some` for `Auto` or `Blanket`? + _ => None, } } } diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index 1e1fc2436aac1..913566657a9aa 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -8,7 +8,7 @@ use rustc_middle::middle::privacy::AccessLevels; use rustc_middle::ty::TyCtxt; use rustc_span::symbol::sym; -use crate::clean::{self, FakeDefId, GetDefId}; +use crate::clean::{self, GetDefId, ItemId}; use crate::fold::DocFolder; use crate::formats::item_type::ItemType; use crate::formats::Impl; @@ -122,7 +122,7 @@ crate struct Cache { /// All intra-doc links resolved so far. /// /// Links are indexed by the DefId of the item they document. - crate intra_doc_links: BTreeMap>, + crate intra_doc_links: BTreeMap>, } /// This struct is used to wrap the `cache` and `tcx` in order to run `DocFolder`. diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 1e08aeb379a2b..4465c9c95cc03 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -19,7 +19,7 @@ use rustc_span::def_id::CRATE_DEF_INDEX; use rustc_target::spec::abi::Abi; use crate::clean::{ - self, utils::find_nearest_parent_module, ExternalCrate, FakeDefId, GetDefId, PrimitiveType, + self, utils::find_nearest_parent_module, ExternalCrate, GetDefId, ItemId, PrimitiveType, }; use crate::formats::item_type::ItemType; use crate::html::escape::Escape; @@ -1181,7 +1181,7 @@ impl clean::FnDecl { impl clean::Visibility { crate fn print_with_space<'a, 'tcx: 'a>( self, - item_did: FakeDefId, + item_did: ItemId, cx: &'a Context<'tcx>, ) -> impl fmt::Display + 'a + Captures<'tcx> { let to_print = match self { diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 0583efa92ffad..5beb75d2e9e00 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -53,7 +53,7 @@ use rustc_span::symbol::{kw, sym, Symbol}; use serde::ser::SerializeSeq; use serde::{Serialize, Serializer}; -use crate::clean::{self, FakeDefId, GetDefId, RenderedLink, SelfTy}; +use crate::clean::{self, GetDefId, ItemId, RenderedLink, SelfTy}; use crate::docfs::PathError; use crate::error::Error; use crate::formats::cache::Cache; @@ -987,7 +987,7 @@ fn render_attributes_in_code(w: &mut Buffer, it: &clean::Item) { #[derive(Copy, Clone)] enum AssocItemLink<'a> { Anchor(Option<&'a str>), - GotoSource(FakeDefId, &'a FxHashSet), + GotoSource(ItemId, &'a FxHashSet), } impl<'a> AssocItemLink<'a> { diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index b89a266a695e9..d4549a78f215e 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -15,7 +15,7 @@ use rustc_span::Pos; use rustdoc_json_types::*; use crate::clean::utils::print_const_expr; -use crate::clean::{self, FakeDefId}; +use crate::clean::{self, ItemId}; use crate::formats::item_type::ItemType; use crate::json::JsonRenderer; use std::collections::HashSet; @@ -30,7 +30,7 @@ impl JsonRenderer<'_> { .into_iter() .flatten() .filter_map(|clean::ItemLink { link, did, .. }| { - did.map(|did| (link.clone(), from_def_id(did.into()))) + did.map(|did| (link.clone(), from_item_id(did.into()))) }) .collect(); let docs = item.attrs.collapsed_doc_value(); @@ -47,7 +47,7 @@ impl JsonRenderer<'_> { _ => from_clean_item(item, self.tcx), }; Some(Item { - id: from_def_id(def_id), + id: from_item_id(def_id), crate_id: def_id.krate().as_u32(), name: name.map(|sym| sym.to_string()), span: self.convert_span(span), @@ -86,7 +86,7 @@ impl JsonRenderer<'_> { Inherited => Visibility::Default, Restricted(did) if did.index == CRATE_DEF_INDEX => Visibility::Crate, Restricted(did) => Visibility::Restricted { - parent: from_def_id(did.into()), + parent: from_item_id(did.into()), path: self.tcx.def_path(did).to_string_no_crate_verbose(), }, } @@ -170,12 +170,10 @@ impl FromWithTcx for TypeBindingKind { } } -crate fn from_def_id(did: FakeDefId) -> Id { +crate fn from_item_id(did: ItemId) -> Id { match did { - FakeDefId::Real(did) => Id(format!("{}:{}", did.krate.as_u32(), u32::from(did.index))), - // We need to differentiate real and fake ids, because the indices might overlap for fake - // and real DefId's, which would cause two different Id's treated as they were the same. - FakeDefId::Fake(idx, krate) => Id(format!("F{}:{}", krate.as_u32(), u32::from(idx))), + ItemId::DefId(did) => Id(format!("{}:{}", did.krate.as_u32(), u32::from(did.index))), + _ => todo!("how should json ItemId's be represented?"), } } @@ -375,7 +373,7 @@ impl FromWithTcx for Type { match ty { ResolvedPath { path, did, is_generic: _ } => Type::ResolvedPath { name: path.whole_name(), - id: from_def_id(did.into()), + id: from_item_id(did.into()), args: path.segments.last().map(|args| Box::new(args.clone().args.into_tcx(tcx))), param_names: Vec::new(), }, @@ -387,7 +385,7 @@ impl FromWithTcx for Type { Type::ResolvedPath { name: path.whole_name(), - id: from_def_id(id.into()), + id: from_item_id(id.into()), args: path .segments .last() @@ -568,13 +566,13 @@ impl FromWithTcx for Import { Simple(s) => Import { source: import.source.path.whole_name(), name: s.to_string(), - id: import.source.did.map(FakeDefId::from).map(from_def_id), + id: import.source.did.map(ItemId::from).map(from_item_id), glob: false, }, Glob => Import { source: import.source.path.whole_name(), name: import.source.path.last_name().to_string(), - id: import.source.did.map(FakeDefId::from).map(from_def_id), + id: import.source.did.map(ItemId::from).map(from_item_id), glob: true, }, } @@ -668,5 +666,5 @@ impl FromWithTcx for ItemKind { } fn ids(items: impl IntoIterator) -> Vec { - items.into_iter().filter(|x| !x.is_stripped()).map(|i| from_def_id(i.def_id)).collect() + items.into_iter().filter(|x| !x.is_stripped()).map(|i| from_item_id(i.def_id)).collect() } diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs index 0d84bf250c9e3..c7d910093ce4f 100644 --- a/src/librustdoc/json/mod.rs +++ b/src/librustdoc/json/mod.rs @@ -25,7 +25,7 @@ use crate::error::Error; use crate::formats::cache::Cache; use crate::formats::FormatRenderer; use crate::html::render::cache::ExternalLocation; -use crate::json::conversions::{from_def_id, IntoWithTcx}; +use crate::json::conversions::{from_item_id, IntoWithTcx}; #[derive(Clone)] crate struct JsonRenderer<'tcx> { @@ -53,7 +53,7 @@ impl JsonRenderer<'tcx> { .map(|i| { let item = &i.impl_item; self.item(item.clone()).unwrap(); - from_def_id(item.def_id) + from_item_id(item.def_id) }) .collect() }) @@ -71,7 +71,7 @@ impl JsonRenderer<'tcx> { let item = &i.impl_item; if item.def_id.is_local() { self.item(item.clone()).unwrap(); - Some(from_def_id(item.def_id)) + Some(from_item_id(item.def_id)) } else { None } @@ -91,9 +91,9 @@ impl JsonRenderer<'tcx> { let trait_item = &trait_item.trait_; trait_item.items.clone().into_iter().for_each(|i| self.item(i).unwrap()); Some(( - from_def_id(id.into()), + from_item_id(id.into()), types::Item { - id: from_def_id(id.into()), + id: from_item_id(id.into()), crate_id: id.krate.as_u32(), name: self .cache @@ -170,7 +170,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { } else if let types::ItemEnum::Enum(ref mut e) = new_item.inner { e.impls = self.get_impls(id.expect_real()) } - let removed = self.index.borrow_mut().insert(from_def_id(id), 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. The main place this happens is when an item, is @@ -207,7 +207,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { .chain(self.cache.external_paths.clone().into_iter()) .map(|(k, (path, kind))| { ( - from_def_id(k.into()), + from_item_id(k.into()), types::ItemSummary { crate_id: k.krate.as_u32(), path, diff --git a/src/librustdoc/passes/collect_trait_impls.rs b/src/librustdoc/passes/collect_trait_impls.rs index 8280bbf0858b7..85b883f118819 100644 --- a/src/librustdoc/passes/collect_trait_impls.rs +++ b/src/librustdoc/passes/collect_trait_impls.rs @@ -149,7 +149,7 @@ impl<'a, 'tcx> DocFolder for SyntheticImplCollector<'a, 'tcx> { #[derive(Default)] struct ItemCollector { - items: FxHashSet, + items: FxHashSet, } impl ItemCollector { @@ -168,7 +168,7 @@ impl DocFolder for ItemCollector { struct BadImplStripper { prims: FxHashSet, - items: FxHashSet, + items: FxHashSet, } impl BadImplStripper { @@ -185,7 +185,7 @@ impl BadImplStripper { } } - fn keep_impl_with_def_id(&self, did: FakeDefId) -> bool { + fn keep_impl_with_def_id(&self, did: ItemId) -> bool { self.items.contains(&did) } } diff --git a/src/librustdoc/passes/strip_hidden.rs b/src/librustdoc/passes/strip_hidden.rs index 87573e8e037a9..0aedbda35e9a8 100644 --- a/src/librustdoc/passes/strip_hidden.rs +++ b/src/librustdoc/passes/strip_hidden.rs @@ -2,7 +2,7 @@ use rustc_span::symbol::sym; use std::mem; use crate::clean; -use crate::clean::{FakeDefIdSet, Item, NestedAttributesExt}; +use crate::clean::{Item, ItemIdSet, NestedAttributesExt}; use crate::core::DocContext; use crate::fold::{strip_item, DocFolder}; use crate::passes::{ImplStripper, Pass}; @@ -15,7 +15,7 @@ crate const STRIP_HIDDEN: Pass = Pass { /// Strip items marked `#[doc(hidden)]` crate fn strip_hidden(krate: clean::Crate, _: &mut DocContext<'_>) -> clean::Crate { - let mut retained = FakeDefIdSet::default(); + let mut retained = ItemIdSet::default(); // strip all #[doc(hidden)] items let krate = { @@ -29,7 +29,7 @@ crate fn strip_hidden(krate: clean::Crate, _: &mut DocContext<'_>) -> clean::Cra } struct Stripper<'a> { - retained: &'a mut FakeDefIdSet, + retained: &'a mut ItemIdSet, update_retained: bool, } diff --git a/src/librustdoc/passes/strip_private.rs b/src/librustdoc/passes/strip_private.rs index 18abeb607a17f..dfdba2a4b3666 100644 --- a/src/librustdoc/passes/strip_private.rs +++ b/src/librustdoc/passes/strip_private.rs @@ -1,4 +1,4 @@ -use crate::clean::{self, FakeDefIdSet}; +use crate::clean::{self, ItemIdSet}; use crate::core::DocContext; use crate::fold::DocFolder; use crate::passes::{ImplStripper, ImportStripper, Pass, Stripper}; @@ -14,7 +14,7 @@ crate const STRIP_PRIVATE: Pass = Pass { /// crate, specified by the `xcrate` flag. crate fn strip_private(mut krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate { // This stripper collects all *retained* nodes. - let mut retained = FakeDefIdSet::default(); + let mut retained = ItemIdSet::default(); // strip all private items { diff --git a/src/librustdoc/passes/stripper.rs b/src/librustdoc/passes/stripper.rs index 528518410aa06..d7717001cebf5 100644 --- a/src/librustdoc/passes/stripper.rs +++ b/src/librustdoc/passes/stripper.rs @@ -2,11 +2,11 @@ use rustc_hir::def_id::DefId; use rustc_middle::middle::privacy::AccessLevels; use std::mem; -use crate::clean::{self, FakeDefIdSet, GetDefId, Item}; +use crate::clean::{self, GetDefId, Item, ItemIdSet}; use crate::fold::{strip_item, DocFolder}; crate struct Stripper<'a> { - crate retained: &'a mut FakeDefIdSet, + crate retained: &'a mut ItemIdSet, crate access_levels: &'a AccessLevels, crate update_retained: bool, } @@ -116,7 +116,7 @@ impl<'a> DocFolder for Stripper<'a> { /// This stripper discards all impls which reference stripped items crate struct ImplStripper<'a> { - crate retained: &'a FakeDefIdSet, + crate retained: &'a ItemIdSet, } impl<'a> DocFolder for ImplStripper<'a> { From acd4dc2d0ca8676fbf105507504e24d44e5dd1f6 Mon Sep 17 00:00:00 2001 From: Justus K Date: Sat, 26 Jun 2021 17:10:52 +0200 Subject: [PATCH 2/7] rustdoc: Rename `expect_real` to `expect_def_id`, remove `Item::is_fake` --- src/librustdoc/clean/types.rs | 43 +++++----- src/librustdoc/formats/cache.rs | 12 +-- src/librustdoc/html/format.rs | 2 +- src/librustdoc/html/render/context.rs | 2 +- src/librustdoc/html/render/mod.rs | 8 +- src/librustdoc/html/render/print_item.rs | 26 +++--- src/librustdoc/json/mod.rs | 6 +- .../passes/calculate_doc_coverage.rs | 4 +- .../passes/check_code_block_syntax.rs | 4 +- .../passes/collect_intra_doc_links.rs | 81 +++++++++---------- src/librustdoc/passes/collect_trait_impls.rs | 9 ++- src/librustdoc/passes/doc_test_lints.rs | 9 ++- src/librustdoc/passes/stripper.rs | 2 +- 13 files changed, 103 insertions(+), 105 deletions(-) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index fe78548bc3189..12c9e561f04be 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -66,20 +66,22 @@ impl ItemId { #[inline] crate fn is_local(self) -> bool { match self { - ItemId::DefId(id) => id.is_local(), - _ => false, + ItemId::Auto { for_: id, .. } + | ItemId::Blanket { for_: id, .. } + | ItemId::DefId(id) => id.is_local(), + ItemId::Primitive(krate) => krate == LOCAL_CRATE, } } #[inline] #[track_caller] - crate fn expect_real(self) -> rustc_hir::def_id::DefId { - self.as_real() - .unwrap_or_else(|| panic!("ItemId::expect_real: `{:?}` isn't a real ItemId", self)) + crate fn expect_def_id(self) -> DefId { + self.as_def_id() + .unwrap_or_else(|| panic!("ItemId::expect_def_id: `{:?}` isn't a DefId", self)) } #[inline] - crate fn as_real(self) -> Option { + crate fn as_def_id(self) -> Option { match self { ItemId::DefId(id) => Some(id), _ => None, @@ -89,9 +91,9 @@ impl ItemId { #[inline] crate fn krate(self) -> CrateNum { match self { - ItemId::DefId(id) => id.krate, - ItemId::Auto { trait_, .. } => trait_.krate, - ItemId::Blanket { trait_, .. } => trait_.krate, + ItemId::Auto { for_: id, .. } + | ItemId::Blanket { for_: id, .. } + | ItemId::DefId(id) => id.krate, ItemId::Primitive(krate) => krate, } } @@ -100,9 +102,7 @@ impl ItemId { crate fn index(self) -> Option { match self { ItemId::DefId(id) => Some(id.index), - ItemId::Auto { trait_, .. } => Some(trait_.index), - ItemId::Blanket { trait_, .. } => Some(trait_.index), - ItemId::Primitive(..) => None, + _ => None, } } } @@ -354,19 +354,19 @@ crate fn rustc_span(def_id: DefId, tcx: TyCtxt<'_>) -> Span { impl Item { crate fn stability<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option<&'tcx Stability> { - if self.is_fake() { None } else { tcx.lookup_stability(self.def_id.expect_real()) } + self.def_id.as_def_id().and_then(|did| tcx.lookup_stability(did)) } crate fn const_stability<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option<&'tcx ConstStability> { - if self.is_fake() { None } else { tcx.lookup_const_stability(self.def_id.expect_real()) } + self.def_id.as_def_id().and_then(|did| tcx.lookup_const_stability(did)) } crate fn deprecation(&self, tcx: TyCtxt<'_>) -> Option { - if self.is_fake() { None } else { tcx.lookup_deprecation(self.def_id.expect_real()) } + self.def_id.as_def_id().and_then(|did| tcx.lookup_deprecation(did)) } crate fn inner_docs(&self, tcx: TyCtxt<'_>) -> bool { - if self.is_fake() { false } else { tcx.get_attrs(self.def_id.expect_real()).inner_docs() } + self.def_id.as_def_id().map(|did| tcx.get_attrs(did).inner_docs()).unwrap_or(false) } crate fn span(&self, tcx: TyCtxt<'_>) -> Span { @@ -378,10 +378,8 @@ impl Item { kind { *span - } else if self.is_fake() { - Span::dummy() } else { - rustc_span(self.def_id.expect_real(), tcx) + self.def_id.as_def_id().map(|did| rustc_span(did, tcx)).unwrap_or_else(|| Span::dummy()) } } @@ -546,7 +544,7 @@ impl Item { } crate fn is_crate(&self) -> bool { - self.is_mod() && self.def_id.as_real().map_or(false, |did| did.index == CRATE_DEF_INDEX) + self.is_mod() && self.def_id.as_def_id().map_or(false, |did| did.index == CRATE_DEF_INDEX) } crate fn is_mod(&self) -> bool { self.type_() == ItemType::Module @@ -657,11 +655,6 @@ impl Item { _ => false, } } - - crate fn is_fake(&self) -> bool { - // FIXME: Find a better way to handle this - !matches!(self.def_id, ItemId::DefId(..)) - } } #[derive(Clone, Debug)] diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index 913566657a9aa..671e9e5c382b2 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -215,7 +215,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { // Propagate a trait method's documentation to all implementors of the // trait. if let clean::TraitItem(ref t) = *item.kind { - self.cache.traits.entry(item.def_id.expect_real()).or_insert_with(|| { + self.cache.traits.entry(item.def_id.expect_def_id()).or_insert_with(|| { clean::TraitWithExtraInfo { trait_: t.clone(), is_notable: item.attrs.has_doc_flag(sym::notable_trait), @@ -348,11 +348,11 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { // `public_items` map, so we can skip inserting into the // paths map if there was already an entry present and we're // not a public item. - if !self.cache.paths.contains_key(&item.def_id.expect_real()) - || self.cache.access_levels.is_public(item.def_id.expect_real()) + if !self.cache.paths.contains_key(&item.def_id.expect_def_id()) + || self.cache.access_levels.is_public(item.def_id.expect_def_id()) { self.cache.paths.insert( - item.def_id.expect_real(), + item.def_id.expect_def_id(), (self.cache.stack.clone(), item.type_()), ); } @@ -361,7 +361,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { clean::PrimitiveItem(..) => { self.cache .paths - .insert(item.def_id.expect_real(), (self.cache.stack.clone(), item.type_())); + .insert(item.def_id.expect_def_id(), (self.cache.stack.clone(), item.type_())); } clean::ExternCrateItem { .. } @@ -391,7 +391,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { | clean::StructItem(..) | clean::UnionItem(..) | clean::VariantItem(..) => { - self.cache.parent_stack.push(item.def_id.expect_real()); + self.cache.parent_stack.push(item.def_id.expect_def_id()); self.cache.parent_is_trait_impl = false; true } diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 4465c9c95cc03..c08fe47696bf1 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -1191,7 +1191,7 @@ impl clean::Visibility { // FIXME(camelid): This may not work correctly if `item_did` is a module. // However, rustdoc currently never displays a module's // visibility, so it shouldn't matter. - let parent_module = find_nearest_parent_module(cx.tcx(), item_did.expect_real()); + let parent_module = find_nearest_parent_module(cx.tcx(), item_did.expect_def_id()); if vis_did.index == CRATE_DEF_INDEX { "pub(crate) ".to_owned() diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 2085739fc46ec..61057ff515b16 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -230,7 +230,7 @@ impl<'tcx> Context<'tcx> { &self.shared.style_files, ) } else { - if let Some(&(ref names, ty)) = self.cache.paths.get(&it.def_id.expect_real()) { + if let Some(&(ref names, ty)) = self.cache.paths.get(&it.def_id.expect_def_id()) { 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 5beb75d2e9e00..97ee682c11c4d 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -735,7 +735,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.expect_real(), cx).map(|p| format!("{}{}", p.0, anchor)).unwrap_or(anchor) + href(did.expect_def_id(), cx).map(|p| format!("{}{}", p.0, anchor)).unwrap_or(anchor) } } } @@ -867,7 +867,7 @@ fn render_assoc_item( ItemType::TyMethod }; - href(did.expect_real(), cx) + href(did.expect_def_id(), cx) .map(|p| format!("{}#{}.{}", p.0, ty, name)) .unwrap_or_else(|| format!("#{}.{}", ty, name)) } @@ -1819,7 +1819,7 @@ fn small_url_encode(s: String) -> String { } fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) { - let did = it.def_id.expect_real(); + let did = it.def_id.expect_def_id(); if let Some(v) = cx.cache.impls.get(&did) { let mut used_links = FxHashSet::default(); let cache = cx.cache(); @@ -2109,7 +2109,7 @@ fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean "", ); - if let Some(implementors) = cx.cache.implementors.get(&it.def_id.expect_real()) { + if let Some(implementors) = cx.cache.implementors.get(&it.def_id.expect_def_id()) { let cache = cx.cache(); let mut res = implementors .iter() diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index ea141c5104824..eeac9d1a9db76 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -289,7 +289,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl w, "
{}extern crate {} as {};", myitem.visibility.print_with_space(myitem.def_id, cx), - anchor(myitem.def_id.expect_real(), &*src.as_str(), cx), + anchor(myitem.def_id.expect_def_id(), &*src.as_str(), cx), myitem.name.as_ref().unwrap(), ), None => write!( @@ -297,7 +297,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl "
{}extern crate {};", myitem.visibility.print_with_space(myitem.def_id, cx), anchor( - myitem.def_id.expect_real(), + myitem.def_id.expect_def_id(), &*myitem.name.as_ref().unwrap().as_str(), cx ), @@ -669,9 +669,9 @@ 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.expect_real(), AssocItemRender::All); + render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All); - if let Some(implementors) = cx.cache.implementors.get(&it.def_id.expect_real()) { + if let Some(implementors) = cx.cache.implementors.get(&it.def_id.expect_def_id()) { // The DefId is for the first Type found with that name. The bool is // if any Types with the same name but different DefId have been found. let mut implementor_dups: FxHashMap = FxHashMap::default(); @@ -787,7 +787,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.expect_real()]; + let (ref path, _) = cx.cache.external_paths[&it.def_id.expect_def_id()]; path[..path.len() - 1].join("/") }, ty = it.type_(), @@ -813,7 +813,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.expect_real(), AssocItemRender::All) + render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All) } fn item_opaque_ty(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::OpaqueTy) { @@ -834,7 +834,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.expect_real(), AssocItemRender::All) + render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All) } fn item_typedef(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Typedef) { @@ -851,7 +851,7 @@ fn item_typedef(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::T document(w, cx, it, None); - let def_id = it.def_id.expect_real(); + let def_id = it.def_id.expect_def_id(); // Render any items associated directly to this alias, as otherwise they // 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 @@ -903,7 +903,7 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni document(w, cx, field, Some(it)); } } - let def_id = it.def_id.expect_real(); + let def_id = it.def_id.expect_def_id(); render_assoc_items(w, cx, it, def_id, AssocItemRender::All); document_type_layout(w, cx, def_id); } @@ -1041,7 +1041,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum } } } - let def_id = it.def_id.expect_real(); + let def_id = it.def_id.expect_def_id(); render_assoc_items(w, cx, it, def_id, AssocItemRender::All); document_type_layout(w, cx, def_id); } @@ -1093,7 +1093,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.expect_real(), AssocItemRender::All) + render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All) } fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean::Constant) { @@ -1182,7 +1182,7 @@ fn item_struct(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St } } } - let def_id = it.def_id.expect_real(); + let def_id = it.def_id.expect_def_id(); render_assoc_items(w, cx, it, def_id, AssocItemRender::All); document_type_layout(w, cx, def_id); } @@ -1213,7 +1213,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.expect_real(), AssocItemRender::All) + render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All) } fn item_keyword(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) { diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs index c7d910093ce4f..8bdf1a5981230 100644 --- a/src/librustdoc/json/mod.rs +++ b/src/librustdoc/json/mod.rs @@ -164,11 +164,11 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { let id = item.def_id; if let Some(mut new_item) = self.convert_item(item) { if let types::ItemEnum::Trait(ref mut t) = new_item.inner { - t.implementors = self.get_trait_implementors(id.expect_real()) + t.implementors = 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_real()) + 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_real()) + e.impls = self.get_impls(id.expect_def_id()) } let removed = self.index.borrow_mut().insert(from_item_id(id), new_item.clone()); diff --git a/src/librustdoc/passes/calculate_doc_coverage.rs b/src/librustdoc/passes/calculate_doc_coverage.rs index 26a22f5b30494..92e678e4c024b 100644 --- a/src/librustdoc/passes/calculate_doc_coverage.rs +++ b/src/librustdoc/passes/calculate_doc_coverage.rs @@ -213,13 +213,13 @@ impl<'a, 'b> fold::DocFolder for CoverageCalculator<'a, 'b> { let filename = i.span(self.ctx.tcx).filename(self.ctx.sess()); let has_doc_example = tests.found_tests != 0; - // The `expect_real()` should be okay because `local_def_id_to_hir_id` + // The `expect_def_id()` should be okay because `local_def_id_to_hir_id` // would presumably panic if a fake `DefIndex` were passed. let hir_id = self .ctx .tcx .hir() - .local_def_id_to_hir_id(i.def_id.expect_real().expect_local()); + .local_def_id_to_hir_id(i.def_id.expect_def_id().expect_local()); let (level, source) = self.ctx.tcx.lint_level_at_node(MISSING_DOCS, hir_id); // `missing_docs` is allow-by-default, so don't treat this as ignoring the item // unless the user had an explicit `allow` diff --git a/src/librustdoc/passes/check_code_block_syntax.rs b/src/librustdoc/passes/check_code_block_syntax.rs index 7ccfdf29041d2..d961340f1f8d3 100644 --- a/src/librustdoc/passes/check_code_block_syntax.rs +++ b/src/librustdoc/passes/check_code_block_syntax.rs @@ -53,7 +53,7 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> { return; } - let local_id = match item.def_id.as_real().and_then(|x| x.as_local()) { + let local_id = match item.def_id.as_def_id().and_then(|x| x.as_local()) { Some(id) => id, // We don't need to check the syntax for other crates so returning // without doing anything should not be a problem. @@ -137,7 +137,7 @@ impl<'a, 'tcx> DocFolder for SyntaxChecker<'a, 'tcx> { let sp = item.attr_span(self.cx.tcx); let extra = crate::html::markdown::ExtraInfo::new_did( self.cx.tcx, - item.def_id.expect_real(), + item.def_id.expect_def_id(), sp, ); for code_block in markdown::rust_code_blocks(&dox, &extra) { diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 1113d61012852..44a3faf6f7be2 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -830,49 +830,48 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> { fn fold_item(&mut self, item: Item) -> Option { use rustc_middle::ty::DefIdTree; - let parent_node = if item.is_fake() { - None - } else { - find_nearest_parent_module(self.cx.tcx, item.def_id.expect_real()) - }; - + let parent_node = + item.def_id.as_def_id().and_then(|did| find_nearest_parent_module(self.cx.tcx, did)); if parent_node.is_some() { trace!("got parent node for {:?} {:?}, id {:?}", item.type_(), item.name, item.def_id); } // find item's parent to resolve `Self` in item's docs below debug!("looking for the `Self` type"); - let self_id = if item.is_fake() { - None - // Checking if the item is a field in an enum variant - } else if (matches!(self.cx.tcx.def_kind(item.def_id.expect_real()), DefKind::Field) - && matches!( - self.cx.tcx.def_kind(self.cx.tcx.parent(item.def_id.expect_real()).unwrap()), - DefKind::Variant - )) - { - self.cx - .tcx - .parent(item.def_id.expect_real()) - .and_then(|item_id| self.cx.tcx.parent(item_id)) - } else if matches!( - self.cx.tcx.def_kind(item.def_id.expect_real()), - DefKind::AssocConst - | DefKind::AssocFn - | DefKind::AssocTy - | DefKind::Variant - | DefKind::Field - ) { - self.cx.tcx.parent(item.def_id.expect_real()) - // HACK(jynelson): `clean` marks associated types as `TypedefItem`, not as `AssocTypeItem`. - // Fixing this breaks `fn render_deref_methods`. - // As a workaround, see if the parent of the item is an `impl`; if so this must be an associated item, - // regardless of what rustdoc wants to call it. - } else if let Some(parent) = self.cx.tcx.parent(item.def_id.expect_real()) { - let parent_kind = self.cx.tcx.def_kind(parent); - Some(if parent_kind == DefKind::Impl { parent } else { item.def_id.expect_real() }) - } else { - Some(item.def_id.expect_real()) + let self_id = match item.def_id.as_def_id() { + None => None, + Some(did) + if (matches!(self.cx.tcx.def_kind(did), DefKind::Field) + && matches!( + self.cx.tcx.def_kind(self.cx.tcx.parent(did).unwrap()), + DefKind::Variant + )) => + { + self.cx.tcx.parent(did).and_then(|item_id| self.cx.tcx.parent(item_id)) + } + Some(did) + if matches!( + self.cx.tcx.def_kind(did), + DefKind::AssocConst + | DefKind::AssocFn + | DefKind::AssocTy + | DefKind::Variant + | DefKind::Field + ) => + { + self.cx.tcx.parent(did) + } + Some(did) => match self.cx.tcx.parent(did) { + // HACK(jynelson): `clean` marks associated types as `TypedefItem`, not as `AssocTypeItem`. + // Fixing this breaks `fn render_deref_methods`. + // As a workaround, see if the parent of the item is an `impl`; if so this must be an associated item, + // regardless of what rustdoc wants to call it. + Some(parent) => { + let parent_kind = self.cx.tcx.def_kind(parent); + Some(if parent_kind == DefKind::Impl { parent } else { did }) + } + None => Some(did), + }, }; // FIXME(jynelson): this shouldn't go through stringification, rustdoc should just use the DefId directly @@ -897,7 +896,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> { let inner_docs = item.inner_docs(self.cx.tcx); if item.is_mod() && inner_docs { - self.mod_ids.push(item.def_id.expect_real()); + self.mod_ids.push(item.def_id.expect_def_id()); } // We want to resolve in the lexical scope of the documentation. @@ -924,7 +923,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> { Some(if item.is_mod() { if !inner_docs { - self.mod_ids.push(item.def_id.expect_real()); + self.mod_ids.push(item.def_id.expect_def_id()); } let ret = self.fold_item_recur(item); @@ -1235,10 +1234,10 @@ impl LinkCollector<'_, '_> { // item can be non-local e.g. when using #[doc(primitive = "pointer")] if let Some((src_id, dst_id)) = id .as_local() - // The `expect_real()` should be okay because `local_def_id_to_hir_id` + // The `expect_def_id()` should be okay because `local_def_id_to_hir_id` // would presumably panic if a fake `DefIndex` were passed. .and_then(|dst_id| { - item.def_id.expect_real().as_local().map(|src_id| (src_id, dst_id)) + item.def_id.expect_def_id().as_local().map(|src_id| (src_id, dst_id)) }) { let hir_src = self.cx.tcx.hir().local_def_id_to_hir_id(src_id); diff --git a/src/librustdoc/passes/collect_trait_impls.rs b/src/librustdoc/passes/collect_trait_impls.rs index 85b883f118819..91c495a2bbc06 100644 --- a/src/librustdoc/passes/collect_trait_impls.rs +++ b/src/librustdoc/passes/collect_trait_impls.rs @@ -136,10 +136,15 @@ impl<'a, 'tcx> DocFolder for SyntheticImplCollector<'a, 'tcx> { fn fold_item(&mut self, i: Item) -> Option { if i.is_struct() || i.is_enum() || i.is_union() { // FIXME(eddyb) is this `doc(hidden)` check needed? - if !self.cx.tcx.get_attrs(i.def_id.expect_real()).lists(sym::doc).has_word(sym::hidden) + if !self + .cx + .tcx + .get_attrs(i.def_id.expect_def_id()) + .lists(sym::doc) + .has_word(sym::hidden) { self.impls - .extend(get_auto_trait_and_blanket_impls(self.cx, i.def_id.expect_real())); + .extend(get_auto_trait_and_blanket_impls(self.cx, i.def_id.expect_def_id())); } } diff --git a/src/librustdoc/passes/doc_test_lints.rs b/src/librustdoc/passes/doc_test_lints.rs index ba698474f1629..03bc2b52f178f 100644 --- a/src/librustdoc/passes/doc_test_lints.rs +++ b/src/librustdoc/passes/doc_test_lints.rs @@ -53,7 +53,7 @@ impl crate::doctest::Tester for Tests { } crate fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> bool { - if !cx.cache.access_levels.is_public(item.def_id.expect_real()) + if !cx.cache.access_levels.is_public(item.def_id.expect_def_id()) || matches!( *item.kind, clean::StructFieldItem(_) @@ -71,9 +71,9 @@ crate fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> boo { return false; } - // The `expect_real()` should be okay because `local_def_id_to_hir_id` + // The `expect_def_id()` should be okay because `local_def_id_to_hir_id` // would presumably panic if a fake `DefIndex` were passed. - let hir_id = cx.tcx.hir().local_def_id_to_hir_id(item.def_id.expect_real().expect_local()); + let hir_id = cx.tcx.hir().local_def_id_to_hir_id(item.def_id.expect_def_id().expect_local()); if cx.tcx.hir().attrs(hir_id).lists(sym::doc).has_word(sym::hidden) || inherits_doc_hidden(cx.tcx, hir_id) { @@ -107,7 +107,8 @@ crate fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) { |lint| lint.build("missing code example in this documentation").emit(), ); } - } else if tests.found_tests > 0 && !cx.cache.access_levels.is_public(item.def_id.expect_real()) + } else if tests.found_tests > 0 + && !cx.cache.access_levels.is_public(item.def_id.expect_def_id()) { cx.tcx.struct_span_lint_hir( crate::lint::PRIVATE_DOC_TESTS, diff --git a/src/librustdoc/passes/stripper.rs b/src/librustdoc/passes/stripper.rs index d7717001cebf5..4305268c9aab0 100644 --- a/src/librustdoc/passes/stripper.rs +++ b/src/librustdoc/passes/stripper.rs @@ -42,7 +42,7 @@ impl<'a> DocFolder for Stripper<'a> { | clean::TraitAliasItem(..) | clean::ForeignTypeItem => { if i.def_id.is_local() { - if !self.access_levels.is_exported(i.def_id.expect_real()) { + if !self.access_levels.is_exported(i.def_id.expect_def_id()) { debug!("Stripper: stripping {:?} {:?}", i.type_(), i.name); return None; } From 45d3daece364e4cf6b6bc4668dcfd7670197b1e8 Mon Sep 17 00:00:00 2001 From: Justus K Date: Sat, 26 Jun 2021 20:47:33 +0200 Subject: [PATCH 3/7] rustdoc: Store DefId's in ItemId on heap for decreasing Item's size --- src/librustdoc/clean/auto_trait.rs | 2 +- src/librustdoc/clean/blanket_impl.rs | 2 +- src/librustdoc/clean/types.rs | 46 +++++++-------- src/librustdoc/core.rs | 6 +- src/librustdoc/formats/cache.rs | 2 +- src/librustdoc/html/render/mod.rs | 21 ++++--- src/librustdoc/html/render/print_item.rs | 36 ++++++------ src/librustdoc/json/conversions.rs | 21 +++---- src/librustdoc/json/mod.rs | 14 ++--- src/librustdoc/passes/bare_urls.rs | 2 +- .../passes/collect_intra_doc_links.rs | 58 +++++++++---------- src/librustdoc/passes/collect_trait_impls.rs | 4 +- src/librustdoc/passes/doc_test_lints.rs | 2 +- src/librustdoc/passes/html_tags.rs | 2 +- src/librustdoc/passes/strip_hidden.rs | 2 +- src/librustdoc/passes/stripper.rs | 4 +- 16 files changed, 115 insertions(+), 109 deletions(-) diff --git a/src/librustdoc/clean/auto_trait.rs b/src/librustdoc/clean/auto_trait.rs index e479d162b8ffb..5564261492e92 100644 --- a/src/librustdoc/clean/auto_trait.rs +++ b/src/librustdoc/clean/auto_trait.rs @@ -113,7 +113,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> { name: None, attrs: Default::default(), visibility: Inherited, - def_id: ItemId::Auto { trait_: trait_def_id, for_: item_def_id }, + def_id: ItemId::Auto(box ImplId { trait_: trait_def_id, for_: item_def_id }), kind: box ImplItem(Impl { span: Span::dummy(), unsafety: hir::Unsafety::Normal, diff --git a/src/librustdoc/clean/blanket_impl.rs b/src/librustdoc/clean/blanket_impl.rs index 59fefee5ee4ed..af8680fb40c3c 100644 --- a/src/librustdoc/clean/blanket_impl.rs +++ b/src/librustdoc/clean/blanket_impl.rs @@ -96,7 +96,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> { name: None, attrs: Default::default(), visibility: Inherited, - def_id: ItemId::Blanket { trait_: trait_def_id, for_: item_def_id }, + def_id: ItemId::Blanket(box ImplId { trait_: trait_def_id, for_: item_def_id }), kind: box ImplItem(Impl { span: self.cx.tcx.def_span(impl_def_id).clean(self.cx), unsafety: hir::Unsafety::Normal, diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 12c9e561f04be..4fa1fde5f0b18 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -18,7 +18,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::thin_vec::ThinVec; use rustc_hir as hir; use rustc_hir::def::{CtorKind, DefKind, Res}; -use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX}; +use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE}; use rustc_hir::lang_items::LangItem; use rustc_hir::{BodyId, Mutability}; use rustc_index::vec::IndexVec; @@ -50,61 +50,59 @@ use self::Type::*; crate type ItemIdSet = FxHashSet; -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +crate struct ImplId { + crate trait_: DefId, + crate for_: DefId, +} + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] crate enum ItemId { /// A "normal" item that uses a [`DefId`] for identification. DefId(DefId), /// Identifier that is used for auto traits. - Auto { trait_: DefId, for_: DefId }, + Auto(Box), /// Identifier that is used for blanket implementations. - Blanket { trait_: DefId, for_: DefId }, + Blanket(Box), /// Identifier for primitive types. Primitive(CrateNum), } impl ItemId { #[inline] - crate fn is_local(self) -> bool { + crate fn is_local(&self) -> bool { match self { - ItemId::Auto { for_: id, .. } - | ItemId::Blanket { for_: id, .. } + ItemId::Auto(box ImplId { for_: id, .. }) + | ItemId::Blanket(box ImplId { for_: id, .. }) | ItemId::DefId(id) => id.is_local(), - ItemId::Primitive(krate) => krate == LOCAL_CRATE, + ItemId::Primitive(krate) => *krate == LOCAL_CRATE, } } #[inline] #[track_caller] - crate fn expect_def_id(self) -> DefId { + crate fn expect_def_id(&self) -> DefId { self.as_def_id() .unwrap_or_else(|| panic!("ItemId::expect_def_id: `{:?}` isn't a DefId", self)) } #[inline] - crate fn as_def_id(self) -> Option { + crate fn as_def_id(&self) -> Option { match self { - ItemId::DefId(id) => Some(id), + ItemId::DefId(id) => Some(*id), _ => None, } } #[inline] - crate fn krate(self) -> CrateNum { - match self { - ItemId::Auto { for_: id, .. } - | ItemId::Blanket { for_: id, .. } + crate fn krate(&self) -> CrateNum { + match *self { + ItemId::Auto(box ImplId { for_: id, .. }) + | ItemId::Blanket(box ImplId { for_: id, .. }) | ItemId::DefId(id) => id.krate, ItemId::Primitive(krate) => krate, } } - - #[inline] - crate fn index(self) -> Option { - match self { - ItemId::DefId(id) => Some(id.index), - _ => None, - } - } } impl From for ItemId { @@ -379,7 +377,7 @@ impl Item { { *span } else { - self.def_id.as_def_id().map(|did| rustc_span(did, tcx)).unwrap_or_else(|| Span::dummy()) + self.def_id.as_def_id().map(|did| rustc_span(did, tcx)).unwrap_or_else(Span::dummy) } } diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 0689d72e4e0cf..a9f84e747b18a 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -128,8 +128,8 @@ impl<'tcx> DocContext<'tcx> { /// Like `hir().local_def_id_to_hir_id()`, but skips calling it on fake DefIds. /// (This avoids a slice-index-out-of-bounds panic.) - crate fn as_local_hir_id(tcx: TyCtxt<'_>, def_id: ItemId) -> Option { - match def_id { + crate fn as_local_hir_id(tcx: TyCtxt<'_>, def_id: &ItemId) -> Option { + match *def_id { ItemId::DefId(real_id) => { real_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)) } @@ -432,7 +432,7 @@ crate fn run_global_ctxt( ); tcx.struct_lint_node( crate::lint::MISSING_CRATE_LEVEL_DOCS, - DocContext::as_local_hir_id(tcx, krate.module.def_id).unwrap(), + DocContext::as_local_hir_id(tcx, &krate.module.def_id).unwrap(), |lint| { let mut diag = lint.build("no documentation found for this crate's top-level module"); diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index 671e9e5c382b2..978ba501fd0ba 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -290,7 +290,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { // A crate has a module at its root, containing all items, // which should not be indexed. The crate-item itself is // inserted later on when serializing the search-index. - if item.def_id.index().map_or(false, |idx| idx != CRATE_DEF_INDEX) { + if item.def_id.as_def_id().map_or(false, |did| did.index != CRATE_DEF_INDEX) { let desc = item.doc_value().map_or_else(String::new, |x| { short_markdown_summary(&x.as_str(), &item.link_names(&self.cache)) }); diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 97ee682c11c4d..1cee68baff4db 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -753,7 +753,7 @@ fn assoc_const( w, "{}{}const {}: {}", extra, - it.visibility.print_with_space(it.def_id, cx), + it.visibility.print_with_space(it.def_id.clone(), cx), naive_assoc_href(it, link, cx), it.name.as_ref().unwrap(), ty.print(cx) @@ -872,7 +872,7 @@ fn render_assoc_item( .unwrap_or_else(|| format!("#{}.{}", ty, name)) } }; - let vis = meth.visibility.print_with_space(meth.def_id, cx).to_string(); + let vis = meth.visibility.print_with_space(meth.def_id.clone(), cx).to_string(); let constness = print_constness_with_space(&header.constness, meth.const_stability(cx.tcx())); let asyncness = header.asyncness.print_with_space(); @@ -984,7 +984,7 @@ fn render_attributes_in_code(w: &mut Buffer, it: &clean::Item) { } } -#[derive(Copy, Clone)] +#[derive(Clone)] enum AssocItemLink<'a> { Anchor(Option<&'a str>), GotoSource(ItemId, &'a FxHashSet), @@ -994,7 +994,7 @@ impl<'a> AssocItemLink<'a> { fn anchor(&self, id: &'a str) -> Self { match *self { AssocItemLink::Anchor(_) => AssocItemLink::Anchor(Some(&id)), - ref other => *other, + ref other => other.clone(), } } } @@ -1306,7 +1306,14 @@ fn render_impl( } else { // In case the item isn't documented, // provide short documentation from the trait. - document_short(&mut doc_buffer, it, cx, link, parent, show_def_docs); + document_short( + &mut doc_buffer, + it, + cx, + link.clone(), + parent, + show_def_docs, + ); } } } else { @@ -1317,7 +1324,7 @@ fn render_impl( } } } else { - document_short(&mut doc_buffer, item, cx, link, parent, show_def_docs); + document_short(&mut doc_buffer, item, cx, link.clone(), parent, show_def_docs); } } let w = if short_documented && trait_.is_some() { interesting } else { boring }; @@ -1445,7 +1452,7 @@ fn render_impl( trait_item, if trait_.is_some() { &i.impl_item } else { parent }, parent, - link, + link.clone(), render_mode, false, trait_.map(|t| &t.trait_), diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index eeac9d1a9db76..70e80b20b2a5c 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -245,7 +245,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl // (which is the position in the vector). indices.dedup_by_key(|i| { ( - items[*i].def_id, + items[*i].def_id.clone(), if items[*i].name.as_ref().is_some() { Some(full_path(cx, &items[*i])) } else { None }, items[*i].type_(), if items[*i].is_import() { *i } else { 0 }, @@ -288,14 +288,14 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl Some(ref src) => write!( w, "
{}extern crate {} as {};", - myitem.visibility.print_with_space(myitem.def_id, cx), + myitem.visibility.print_with_space(myitem.def_id.clone(), cx), anchor(myitem.def_id.expect_def_id(), &*src.as_str(), cx), myitem.name.as_ref().unwrap(), ), None => write!( w, "
{}extern crate {};", - myitem.visibility.print_with_space(myitem.def_id, cx), + myitem.visibility.print_with_space(myitem.def_id.clone(), cx), anchor( myitem.def_id.expect_def_id(), &*myitem.name.as_ref().unwrap().as_str(), @@ -336,7 +336,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
{stab_tags}
", stab = stab.unwrap_or_default(), add = add, - vis = myitem.visibility.print_with_space(myitem.def_id, cx), + vis = myitem.visibility.print_with_space(myitem.def_id.clone(), cx), imp = import.print(cx), stab_tags = stab_tags.unwrap_or_default(), ); @@ -437,7 +437,7 @@ fn extra_info_tags(item: &clean::Item, parent: &clean::Item, tcx: TyCtxt<'_>) -> } fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::Function) { - let vis = it.visibility.print_with_space(it.def_id, cx).to_string(); + let vis = it.visibility.print_with_space(it.def_id.clone(), cx).to_string(); let constness = print_constness_with_space(&f.header.constness, it.const_stability(cx.tcx())); let asyncness = f.header.asyncness.print_with_space(); let unsafety = f.header.unsafety.print_with_space(); @@ -489,7 +489,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra write!( w, "{}{}{}trait {}{}{}", - it.visibility.print_with_space(it.def_id, cx), + it.visibility.print_with_space(it.def_id.clone(), cx), t.unsafety.print_with_space(), if t.is_auto { "auto " } else { "" }, it.name.as_ref().unwrap(), @@ -710,8 +710,10 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra for implementor in foreign { let provided_methods = implementor.inner_impl().provided_trait_methods(cx.tcx()); - let assoc_link = - AssocItemLink::GotoSource(implementor.impl_item.def_id, &provided_methods); + let assoc_link = AssocItemLink::GotoSource( + implementor.impl_item.def_id.clone(), + &provided_methods, + ); render_impl( w, cx, @@ -915,7 +917,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum write!( w, "{}enum {}{}{}", - it.visibility.print_with_space(it.def_id, cx), + it.visibility.print_with_space(it.def_id.clone(), cx), it.name.as_ref().unwrap(), e.generics.print(cx), print_where_clause(&e.generics, cx, 0, true), @@ -1103,7 +1105,7 @@ fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean:: write!( w, "{vis}const {name}: {typ}", - vis = it.visibility.print_with_space(it.def_id, cx), + vis = it.visibility.print_with_space(it.def_id.clone(), cx), name = it.name.as_ref().unwrap(), typ = c.type_.print(cx), ); @@ -1193,7 +1195,7 @@ fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St write!( w, "{vis}static {mutability}{name}: {typ}", - vis = it.visibility.print_with_space(it.def_id, cx), + vis = it.visibility.print_with_space(it.def_id.clone(), cx), mutability = s.mutability.print_with_space(), name = it.name.as_ref().unwrap(), typ = s.type_.print(cx) @@ -1207,7 +1209,7 @@ fn item_foreign_type(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) { write!( w, " {}type {};\n}}", - it.visibility.print_with_space(it.def_id, cx), + it.visibility.print_with_space(it.def_id.clone(), cx), it.name.as_ref().unwrap(), ); @@ -1362,7 +1364,7 @@ fn render_union( write!( w, "{}{}{}", - it.visibility.print_with_space(it.def_id, cx), + it.visibility.print_with_space(it.def_id.clone(), cx), if structhead { "union " } else { "" }, it.name.as_ref().unwrap() ); @@ -1384,7 +1386,7 @@ fn render_union( write!( w, " {}{}: {},\n{}", - field.visibility.print_with_space(field.def_id, cx), + field.visibility.print_with_space(field.def_id.clone(), cx), field.name.as_ref().unwrap(), ty.print(cx), tab @@ -1414,7 +1416,7 @@ fn render_struct( write!( w, "{}{}{}", - it.visibility.print_with_space(it.def_id, cx), + it.visibility.print_with_space(it.def_id.clone(), cx), if structhead { "struct " } else { "" }, it.name.as_ref().unwrap() ); @@ -1440,7 +1442,7 @@ fn render_struct( w, "\n{} {}{}: {},", tab, - field.visibility.print_with_space(field.def_id, cx), + field.visibility.print_with_space(field.def_id.clone(), cx), field.name.as_ref().unwrap(), ty.print(cx), ); @@ -1474,7 +1476,7 @@ fn render_struct( write!( w, "{}{}", - field.visibility.print_with_space(field.def_id, cx), + field.visibility.print_with_space(field.def_id.clone(), cx), ty.print(cx), ) } diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index d4549a78f215e..98c34ee8be7c4 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -30,7 +30,7 @@ impl JsonRenderer<'_> { .into_iter() .flatten() .filter_map(|clean::ItemLink { link, did, .. }| { - did.map(|did| (link.clone(), from_item_id(did.into()))) + did.map(|did| (link.clone(), from_item_id(&did.into()))) }) .collect(); let docs = item.attrs.collapsed_doc_value(); @@ -41,14 +41,15 @@ impl JsonRenderer<'_> { .map(rustc_ast_pretty::pprust::attribute_to_string) .collect(); let span = item.span(self.tcx); - let clean::Item { name, attrs: _, kind: _, visibility, def_id, cfg: _ } = item; + let clean::Item { name, attrs: _, kind: _, visibility, ref def_id, cfg: _ } = item; + let def_id = def_id.clone(); let inner = match *item.kind { clean::StrippedItem(_) => return None, _ => from_clean_item(item, self.tcx), }; Some(Item { - id: from_item_id(def_id), crate_id: def_id.krate().as_u32(), + id: from_item_id(&def_id), name: name.map(|sym| sym.to_string()), span: self.convert_span(span), visibility: self.convert_visibility(visibility), @@ -86,7 +87,7 @@ impl JsonRenderer<'_> { Inherited => Visibility::Default, Restricted(did) if did.index == CRATE_DEF_INDEX => Visibility::Crate, Restricted(did) => Visibility::Restricted { - parent: from_item_id(did.into()), + parent: from_item_id(&did.into()), path: self.tcx.def_path(did).to_string_no_crate_verbose(), }, } @@ -170,7 +171,7 @@ impl FromWithTcx for TypeBindingKind { } } -crate fn from_item_id(did: ItemId) -> Id { +crate fn from_item_id(did: &ItemId) -> Id { match did { ItemId::DefId(did) => Id(format!("{}:{}", did.krate.as_u32(), u32::from(did.index))), _ => todo!("how should json ItemId's be represented?"), @@ -373,7 +374,7 @@ impl FromWithTcx for Type { match ty { ResolvedPath { path, did, is_generic: _ } => Type::ResolvedPath { name: path.whole_name(), - id: from_item_id(did.into()), + id: from_item_id(&did.into()), args: path.segments.last().map(|args| Box::new(args.clone().args.into_tcx(tcx))), param_names: Vec::new(), }, @@ -385,7 +386,7 @@ impl FromWithTcx for Type { Type::ResolvedPath { name: path.whole_name(), - id: from_item_id(id.into()), + id: from_item_id(&id.into()), args: path .segments .last() @@ -566,13 +567,13 @@ impl FromWithTcx for Import { Simple(s) => Import { source: import.source.path.whole_name(), name: s.to_string(), - id: import.source.did.map(ItemId::from).map(from_item_id), + id: import.source.did.map(ItemId::from).as_ref().map(from_item_id), glob: false, }, Glob => Import { source: import.source.path.whole_name(), name: import.source.path.last_name().to_string(), - id: import.source.did.map(ItemId::from).map(from_item_id), + id: import.source.did.map(ItemId::from).as_ref().map(from_item_id), glob: true, }, } @@ -666,5 +667,5 @@ impl FromWithTcx for ItemKind { } fn ids(items: impl IntoIterator) -> Vec { - items.into_iter().filter(|x| !x.is_stripped()).map(|i| from_item_id(i.def_id)).collect() + items.into_iter().filter(|x| !x.is_stripped()).map(|i| from_item_id(&i.def_id)).collect() } diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs index 8bdf1a5981230..06c66ce55406e 100644 --- a/src/librustdoc/json/mod.rs +++ b/src/librustdoc/json/mod.rs @@ -53,7 +53,7 @@ impl JsonRenderer<'tcx> { .map(|i| { let item = &i.impl_item; self.item(item.clone()).unwrap(); - from_item_id(item.def_id) + from_item_id(&item.def_id) }) .collect() }) @@ -71,7 +71,7 @@ impl JsonRenderer<'tcx> { let item = &i.impl_item; if item.def_id.is_local() { self.item(item.clone()).unwrap(); - Some(from_item_id(item.def_id)) + Some(from_item_id(&item.def_id)) } else { None } @@ -91,9 +91,9 @@ impl JsonRenderer<'tcx> { let trait_item = &trait_item.trait_; trait_item.items.clone().into_iter().for_each(|i| self.item(i).unwrap()); Some(( - from_item_id(id.into()), + from_item_id(&id.into()), types::Item { - id: from_item_id(id.into()), + id: from_item_id(&id.into()), crate_id: id.krate.as_u32(), name: self .cache @@ -161,7 +161,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { // Flatten items that recursively store other items item.kind.inner_items().for_each(|i| self.item(i.clone()).unwrap()); - let id = item.def_id; + let id = item.def_id.clone(); if let Some(mut new_item) = self.convert_item(item) { if let types::ItemEnum::Trait(ref mut t) = new_item.inner { t.implementors = self.get_trait_implementors(id.expect_def_id()) @@ -170,7 +170,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { } else if let types::ItemEnum::Enum(ref mut e) = new_item.inner { e.impls = self.get_impls(id.expect_def_id()) } - let removed = self.index.borrow_mut().insert(from_item_id(id), 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. The main place this happens is when an item, is @@ -207,7 +207,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { .chain(self.cache.external_paths.clone().into_iter()) .map(|(k, (path, kind))| { ( - from_item_id(k.into()), + from_item_id(&k.into()), types::ItemSummary { crate_id: k.krate.as_u32(), path, diff --git a/src/librustdoc/passes/bare_urls.rs b/src/librustdoc/passes/bare_urls.rs index 56ef15eb88420..9d02ae2507290 100644 --- a/src/librustdoc/passes/bare_urls.rs +++ b/src/librustdoc/passes/bare_urls.rs @@ -58,7 +58,7 @@ crate fn check_bare_urls(krate: Crate, cx: &mut DocContext<'_>) -> Crate { impl<'a, 'tcx> DocFolder for BareUrlsLinter<'a, 'tcx> { fn fold_item(&mut self, item: Item) -> Option { - let hir_id = match DocContext::as_local_hir_id(self.cx.tcx, item.def_id) { + let hir_id = match DocContext::as_local_hir_id(self.cx.tcx, &item.def_id) { Some(hir_id) => hir_id, None => { // If non-local, no need to check anything. diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 44a3faf6f7be2..61623bac27e73 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -838,41 +838,34 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> { // find item's parent to resolve `Self` in item's docs below debug!("looking for the `Self` type"); - let self_id = match item.def_id.as_def_id() { - None => None, - Some(did) - if (matches!(self.cx.tcx.def_kind(did), DefKind::Field) - && matches!( - self.cx.tcx.def_kind(self.cx.tcx.parent(did).unwrap()), - DefKind::Variant - )) => + let self_id = item.def_id.as_def_id().and_then(|did| { + if (matches!(self.cx.tcx.def_kind(did), DefKind::Field) + && matches!( + self.cx.tcx.def_kind(self.cx.tcx.parent(did).unwrap()), + DefKind::Variant + )) { self.cx.tcx.parent(did).and_then(|item_id| self.cx.tcx.parent(item_id)) - } - Some(did) - if matches!( - self.cx.tcx.def_kind(did), - DefKind::AssocConst - | DefKind::AssocFn - | DefKind::AssocTy - | DefKind::Variant - | DefKind::Field - ) => - { + } else if matches!( + self.cx.tcx.def_kind(did), + DefKind::AssocConst + | DefKind::AssocFn + | DefKind::AssocTy + | DefKind::Variant + | DefKind::Field + ) { self.cx.tcx.parent(did) - } - Some(did) => match self.cx.tcx.parent(did) { + } else if let Some(parent) = self.cx.tcx.parent(did) { // HACK(jynelson): `clean` marks associated types as `TypedefItem`, not as `AssocTypeItem`. // Fixing this breaks `fn render_deref_methods`. // As a workaround, see if the parent of the item is an `impl`; if so this must be an associated item, // regardless of what rustdoc wants to call it. - Some(parent) => { - let parent_kind = self.cx.tcx.def_kind(parent); - Some(if parent_kind == DefKind::Impl { parent } else { did }) - } - None => Some(did), - }, - }; + let parent_kind = self.cx.tcx.def_kind(parent); + Some(if parent_kind == DefKind::Impl { parent } else { did }) + } else { + Some(did) + } + }); // FIXME(jynelson): this shouldn't go through stringification, rustdoc should just use the DefId directly let self_name = self_id.and_then(|self_id| { @@ -916,7 +909,12 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> { for md_link in markdown_links(&doc) { let link = self.resolve_link(&item, &doc, &self_name, parent_node, krate, md_link); if let Some(link) = link { - self.cx.cache.intra_doc_links.entry(item.def_id).or_default().push(link); + self.cx + .cache + .intra_doc_links + .entry(item.def_id.clone()) + .or_default() + .push(link); } } } @@ -1712,7 +1710,7 @@ fn report_diagnostic( DiagnosticInfo { item, ori_link: _, dox, link_range }: &DiagnosticInfo<'_>, decorate: impl FnOnce(&mut DiagnosticBuilder<'_>, Option), ) { - let hir_id = match DocContext::as_local_hir_id(tcx, item.def_id) { + let hir_id = match DocContext::as_local_hir_id(tcx, &item.def_id) { Some(hir_id) => hir_id, None => { // If non-local, no need to check anything. diff --git a/src/librustdoc/passes/collect_trait_impls.rs b/src/librustdoc/passes/collect_trait_impls.rs index 91c495a2bbc06..d589c697fa6a5 100644 --- a/src/librustdoc/passes/collect_trait_impls.rs +++ b/src/librustdoc/passes/collect_trait_impls.rs @@ -46,7 +46,7 @@ crate fn collect_trait_impls(krate: Crate, cx: &mut DocContext<'_>) -> Crate { // FIXME(eddyb) is this `doc(hidden)` check needed? if !cx.tcx.get_attrs(def_id).lists(sym::doc).has_word(sym::hidden) { let impls = get_auto_trait_and_blanket_impls(cx, def_id.into()); - new_items.extend(impls.filter(|i| cx.inlined.insert(i.def_id))); + new_items.extend(impls.filter(|i| cx.inlined.insert(i.def_id.clone()))); } }); } @@ -165,7 +165,7 @@ impl ItemCollector { impl DocFolder for ItemCollector { fn fold_item(&mut self, i: Item) -> Option { - self.items.insert(i.def_id); + self.items.insert(i.def_id.clone()); Some(self.fold_item_recur(i)) } diff --git a/src/librustdoc/passes/doc_test_lints.rs b/src/librustdoc/passes/doc_test_lints.rs index 03bc2b52f178f..c3a30661e5046 100644 --- a/src/librustdoc/passes/doc_test_lints.rs +++ b/src/librustdoc/passes/doc_test_lints.rs @@ -84,7 +84,7 @@ crate fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> boo } crate fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) { - let hir_id = match DocContext::as_local_hir_id(cx.tcx, item.def_id) { + let hir_id = match DocContext::as_local_hir_id(cx.tcx, &item.def_id) { Some(hir_id) => hir_id, None => { // If non-local, no need to check anything. diff --git a/src/librustdoc/passes/html_tags.rs b/src/librustdoc/passes/html_tags.rs index f29d38e3e078a..ad9f80c2e6c13 100644 --- a/src/librustdoc/passes/html_tags.rs +++ b/src/librustdoc/passes/html_tags.rs @@ -168,7 +168,7 @@ fn extract_tags( impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> { fn fold_item(&mut self, item: Item) -> Option { let tcx = self.cx.tcx; - let hir_id = match DocContext::as_local_hir_id(tcx, item.def_id) { + let hir_id = match DocContext::as_local_hir_id(tcx, &item.def_id) { Some(hir_id) => hir_id, None => { // If non-local, no need to check anything. diff --git a/src/librustdoc/passes/strip_hidden.rs b/src/librustdoc/passes/strip_hidden.rs index 0aedbda35e9a8..b0c745edbba5b 100644 --- a/src/librustdoc/passes/strip_hidden.rs +++ b/src/librustdoc/passes/strip_hidden.rs @@ -52,7 +52,7 @@ impl<'a> DocFolder for Stripper<'a> { } } else { if self.update_retained { - self.retained.insert(i.def_id); + self.retained.insert(i.def_id.clone()); } } Some(self.fold_item_recur(i)) diff --git a/src/librustdoc/passes/stripper.rs b/src/librustdoc/passes/stripper.rs index 4305268c9aab0..6ee7179c5db84 100644 --- a/src/librustdoc/passes/stripper.rs +++ b/src/librustdoc/passes/stripper.rs @@ -100,7 +100,7 @@ impl<'a> DocFolder for Stripper<'a> { let i = if fastreturn { if self.update_retained { - self.retained.insert(i.def_id); + self.retained.insert(i.def_id.clone()); } return Some(i); } else { @@ -108,7 +108,7 @@ impl<'a> DocFolder for Stripper<'a> { }; if self.update_retained { - self.retained.insert(i.def_id); + self.retained.insert(i.def_id.clone()); } Some(i) } From 97c82d8731784866dfc12f7e79e592a9e191c28f Mon Sep 17 00:00:00 2001 From: Justus K Date: Sun, 27 Jun 2021 09:28:17 +0200 Subject: [PATCH 4/7] Revert "rustdoc: Store DefId's in ItemId on heap for decreasing Item's size" This reverts commit 41a345d4c46dad1a98c9993bc78513415994e8ba. --- src/librustdoc/clean/auto_trait.rs | 2 +- src/librustdoc/clean/blanket_impl.rs | 2 +- src/librustdoc/clean/types.rs | 46 ++++++++------- src/librustdoc/core.rs | 6 +- src/librustdoc/formats/cache.rs | 2 +- src/librustdoc/html/render/mod.rs | 21 +++---- src/librustdoc/html/render/print_item.rs | 36 ++++++------ src/librustdoc/json/conversions.rs | 21 ++++--- src/librustdoc/json/mod.rs | 14 ++--- src/librustdoc/passes/bare_urls.rs | 2 +- .../passes/collect_intra_doc_links.rs | 58 ++++++++++--------- src/librustdoc/passes/collect_trait_impls.rs | 4 +- src/librustdoc/passes/doc_test_lints.rs | 2 +- src/librustdoc/passes/html_tags.rs | 2 +- src/librustdoc/passes/strip_hidden.rs | 2 +- src/librustdoc/passes/stripper.rs | 4 +- 16 files changed, 109 insertions(+), 115 deletions(-) diff --git a/src/librustdoc/clean/auto_trait.rs b/src/librustdoc/clean/auto_trait.rs index 5564261492e92..e479d162b8ffb 100644 --- a/src/librustdoc/clean/auto_trait.rs +++ b/src/librustdoc/clean/auto_trait.rs @@ -113,7 +113,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> { name: None, attrs: Default::default(), visibility: Inherited, - def_id: ItemId::Auto(box ImplId { trait_: trait_def_id, for_: item_def_id }), + def_id: ItemId::Auto { trait_: trait_def_id, for_: item_def_id }, kind: box ImplItem(Impl { span: Span::dummy(), unsafety: hir::Unsafety::Normal, diff --git a/src/librustdoc/clean/blanket_impl.rs b/src/librustdoc/clean/blanket_impl.rs index af8680fb40c3c..59fefee5ee4ed 100644 --- a/src/librustdoc/clean/blanket_impl.rs +++ b/src/librustdoc/clean/blanket_impl.rs @@ -96,7 +96,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> { name: None, attrs: Default::default(), visibility: Inherited, - def_id: ItemId::Blanket(box ImplId { trait_: trait_def_id, for_: item_def_id }), + def_id: ItemId::Blanket { trait_: trait_def_id, for_: item_def_id }, kind: box ImplItem(Impl { span: self.cx.tcx.def_span(impl_def_id).clean(self.cx), unsafety: hir::Unsafety::Normal, diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 4fa1fde5f0b18..67f7a10e2d072 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -18,7 +18,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::thin_vec::ThinVec; use rustc_hir as hir; use rustc_hir::def::{CtorKind, DefKind, Res}; -use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE}; +use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE}; use rustc_hir::lang_items::LangItem; use rustc_hir::{BodyId, Mutability}; use rustc_index::vec::IndexVec; @@ -50,59 +50,61 @@ use self::Type::*; crate type ItemIdSet = FxHashSet; -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -crate struct ImplId { - crate trait_: DefId, - crate for_: DefId, -} - -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] crate enum ItemId { /// A "normal" item that uses a [`DefId`] for identification. DefId(DefId), /// Identifier that is used for auto traits. - Auto(Box), + Auto { trait_: DefId, for_: DefId }, /// Identifier that is used for blanket implementations. - Blanket(Box), + Blanket { trait_: DefId, for_: DefId }, /// Identifier for primitive types. Primitive(CrateNum), } impl ItemId { #[inline] - crate fn is_local(&self) -> bool { + crate fn is_local(self) -> bool { match self { - ItemId::Auto(box ImplId { for_: id, .. }) - | ItemId::Blanket(box ImplId { for_: id, .. }) + ItemId::Auto { for_: id, .. } + | ItemId::Blanket { for_: id, .. } | ItemId::DefId(id) => id.is_local(), - ItemId::Primitive(krate) => *krate == LOCAL_CRATE, + ItemId::Primitive(krate) => krate == LOCAL_CRATE, } } #[inline] #[track_caller] - crate fn expect_def_id(&self) -> DefId { + crate fn expect_def_id(self) -> DefId { self.as_def_id() .unwrap_or_else(|| panic!("ItemId::expect_def_id: `{:?}` isn't a DefId", self)) } #[inline] - crate fn as_def_id(&self) -> Option { + crate fn as_def_id(self) -> Option { match self { - ItemId::DefId(id) => Some(*id), + ItemId::DefId(id) => Some(id), _ => None, } } #[inline] - crate fn krate(&self) -> CrateNum { - match *self { - ItemId::Auto(box ImplId { for_: id, .. }) - | ItemId::Blanket(box ImplId { for_: id, .. }) + crate fn krate(self) -> CrateNum { + match self { + ItemId::Auto { for_: id, .. } + | ItemId::Blanket { for_: id, .. } | ItemId::DefId(id) => id.krate, ItemId::Primitive(krate) => krate, } } + + #[inline] + crate fn index(self) -> Option { + match self { + ItemId::DefId(id) => Some(id.index), + _ => None, + } + } } impl From for ItemId { @@ -377,7 +379,7 @@ impl Item { { *span } else { - self.def_id.as_def_id().map(|did| rustc_span(did, tcx)).unwrap_or_else(Span::dummy) + self.def_id.as_def_id().map(|did| rustc_span(did, tcx)).unwrap_or_else(|| Span::dummy()) } } diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index a9f84e747b18a..0689d72e4e0cf 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -128,8 +128,8 @@ impl<'tcx> DocContext<'tcx> { /// Like `hir().local_def_id_to_hir_id()`, but skips calling it on fake DefIds. /// (This avoids a slice-index-out-of-bounds panic.) - crate fn as_local_hir_id(tcx: TyCtxt<'_>, def_id: &ItemId) -> Option { - match *def_id { + crate fn as_local_hir_id(tcx: TyCtxt<'_>, def_id: ItemId) -> Option { + match def_id { ItemId::DefId(real_id) => { real_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)) } @@ -432,7 +432,7 @@ crate fn run_global_ctxt( ); tcx.struct_lint_node( crate::lint::MISSING_CRATE_LEVEL_DOCS, - DocContext::as_local_hir_id(tcx, &krate.module.def_id).unwrap(), + DocContext::as_local_hir_id(tcx, krate.module.def_id).unwrap(), |lint| { let mut diag = lint.build("no documentation found for this crate's top-level module"); diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index 978ba501fd0ba..671e9e5c382b2 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -290,7 +290,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { // A crate has a module at its root, containing all items, // which should not be indexed. The crate-item itself is // inserted later on when serializing the search-index. - if item.def_id.as_def_id().map_or(false, |did| did.index != CRATE_DEF_INDEX) { + if item.def_id.index().map_or(false, |idx| idx != CRATE_DEF_INDEX) { let desc = item.doc_value().map_or_else(String::new, |x| { short_markdown_summary(&x.as_str(), &item.link_names(&self.cache)) }); diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 1cee68baff4db..97ee682c11c4d 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -753,7 +753,7 @@ fn assoc_const( w, "{}{}const {}: {}", extra, - it.visibility.print_with_space(it.def_id.clone(), cx), + it.visibility.print_with_space(it.def_id, cx), naive_assoc_href(it, link, cx), it.name.as_ref().unwrap(), ty.print(cx) @@ -872,7 +872,7 @@ fn render_assoc_item( .unwrap_or_else(|| format!("#{}.{}", ty, name)) } }; - let vis = meth.visibility.print_with_space(meth.def_id.clone(), cx).to_string(); + let vis = meth.visibility.print_with_space(meth.def_id, cx).to_string(); let constness = print_constness_with_space(&header.constness, meth.const_stability(cx.tcx())); let asyncness = header.asyncness.print_with_space(); @@ -984,7 +984,7 @@ fn render_attributes_in_code(w: &mut Buffer, it: &clean::Item) { } } -#[derive(Clone)] +#[derive(Copy, Clone)] enum AssocItemLink<'a> { Anchor(Option<&'a str>), GotoSource(ItemId, &'a FxHashSet), @@ -994,7 +994,7 @@ impl<'a> AssocItemLink<'a> { fn anchor(&self, id: &'a str) -> Self { match *self { AssocItemLink::Anchor(_) => AssocItemLink::Anchor(Some(&id)), - ref other => other.clone(), + ref other => *other, } } } @@ -1306,14 +1306,7 @@ fn render_impl( } else { // In case the item isn't documented, // provide short documentation from the trait. - document_short( - &mut doc_buffer, - it, - cx, - link.clone(), - parent, - show_def_docs, - ); + document_short(&mut doc_buffer, it, cx, link, parent, show_def_docs); } } } else { @@ -1324,7 +1317,7 @@ fn render_impl( } } } else { - document_short(&mut doc_buffer, item, cx, link.clone(), parent, show_def_docs); + document_short(&mut doc_buffer, item, cx, link, parent, show_def_docs); } } let w = if short_documented && trait_.is_some() { interesting } else { boring }; @@ -1452,7 +1445,7 @@ fn render_impl( trait_item, if trait_.is_some() { &i.impl_item } else { parent }, parent, - link.clone(), + link, render_mode, false, trait_.map(|t| &t.trait_), diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 70e80b20b2a5c..eeac9d1a9db76 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -245,7 +245,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl // (which is the position in the vector). indices.dedup_by_key(|i| { ( - items[*i].def_id.clone(), + items[*i].def_id, if items[*i].name.as_ref().is_some() { Some(full_path(cx, &items[*i])) } else { None }, items[*i].type_(), if items[*i].is_import() { *i } else { 0 }, @@ -288,14 +288,14 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl Some(ref src) => write!( w, "
{}extern crate {} as {};", - myitem.visibility.print_with_space(myitem.def_id.clone(), cx), + myitem.visibility.print_with_space(myitem.def_id, cx), anchor(myitem.def_id.expect_def_id(), &*src.as_str(), cx), myitem.name.as_ref().unwrap(), ), None => write!( w, "
{}extern crate {};", - myitem.visibility.print_with_space(myitem.def_id.clone(), cx), + myitem.visibility.print_with_space(myitem.def_id, cx), anchor( myitem.def_id.expect_def_id(), &*myitem.name.as_ref().unwrap().as_str(), @@ -336,7 +336,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
{stab_tags}
", stab = stab.unwrap_or_default(), add = add, - vis = myitem.visibility.print_with_space(myitem.def_id.clone(), cx), + vis = myitem.visibility.print_with_space(myitem.def_id, cx), imp = import.print(cx), stab_tags = stab_tags.unwrap_or_default(), ); @@ -437,7 +437,7 @@ fn extra_info_tags(item: &clean::Item, parent: &clean::Item, tcx: TyCtxt<'_>) -> } fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::Function) { - let vis = it.visibility.print_with_space(it.def_id.clone(), cx).to_string(); + let vis = it.visibility.print_with_space(it.def_id, cx).to_string(); let constness = print_constness_with_space(&f.header.constness, it.const_stability(cx.tcx())); let asyncness = f.header.asyncness.print_with_space(); let unsafety = f.header.unsafety.print_with_space(); @@ -489,7 +489,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra write!( w, "{}{}{}trait {}{}{}", - it.visibility.print_with_space(it.def_id.clone(), cx), + it.visibility.print_with_space(it.def_id, cx), t.unsafety.print_with_space(), if t.is_auto { "auto " } else { "" }, it.name.as_ref().unwrap(), @@ -710,10 +710,8 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra for implementor in foreign { let provided_methods = implementor.inner_impl().provided_trait_methods(cx.tcx()); - let assoc_link = AssocItemLink::GotoSource( - implementor.impl_item.def_id.clone(), - &provided_methods, - ); + let assoc_link = + AssocItemLink::GotoSource(implementor.impl_item.def_id, &provided_methods); render_impl( w, cx, @@ -917,7 +915,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum write!( w, "{}enum {}{}{}", - it.visibility.print_with_space(it.def_id.clone(), cx), + it.visibility.print_with_space(it.def_id, cx), it.name.as_ref().unwrap(), e.generics.print(cx), print_where_clause(&e.generics, cx, 0, true), @@ -1105,7 +1103,7 @@ fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean:: write!( w, "{vis}const {name}: {typ}", - vis = it.visibility.print_with_space(it.def_id.clone(), cx), + vis = it.visibility.print_with_space(it.def_id, cx), name = it.name.as_ref().unwrap(), typ = c.type_.print(cx), ); @@ -1195,7 +1193,7 @@ fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St write!( w, "{vis}static {mutability}{name}: {typ}", - vis = it.visibility.print_with_space(it.def_id.clone(), cx), + vis = it.visibility.print_with_space(it.def_id, cx), mutability = s.mutability.print_with_space(), name = it.name.as_ref().unwrap(), typ = s.type_.print(cx) @@ -1209,7 +1207,7 @@ fn item_foreign_type(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) { write!( w, " {}type {};\n}}", - it.visibility.print_with_space(it.def_id.clone(), cx), + it.visibility.print_with_space(it.def_id, cx), it.name.as_ref().unwrap(), ); @@ -1364,7 +1362,7 @@ fn render_union( write!( w, "{}{}{}", - it.visibility.print_with_space(it.def_id.clone(), cx), + it.visibility.print_with_space(it.def_id, cx), if structhead { "union " } else { "" }, it.name.as_ref().unwrap() ); @@ -1386,7 +1384,7 @@ fn render_union( write!( w, " {}{}: {},\n{}", - field.visibility.print_with_space(field.def_id.clone(), cx), + field.visibility.print_with_space(field.def_id, cx), field.name.as_ref().unwrap(), ty.print(cx), tab @@ -1416,7 +1414,7 @@ fn render_struct( write!( w, "{}{}{}", - it.visibility.print_with_space(it.def_id.clone(), cx), + it.visibility.print_with_space(it.def_id, cx), if structhead { "struct " } else { "" }, it.name.as_ref().unwrap() ); @@ -1442,7 +1440,7 @@ fn render_struct( w, "\n{} {}{}: {},", tab, - field.visibility.print_with_space(field.def_id.clone(), cx), + field.visibility.print_with_space(field.def_id, cx), field.name.as_ref().unwrap(), ty.print(cx), ); @@ -1476,7 +1474,7 @@ fn render_struct( write!( w, "{}{}", - field.visibility.print_with_space(field.def_id.clone(), cx), + field.visibility.print_with_space(field.def_id, cx), ty.print(cx), ) } diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index 98c34ee8be7c4..d4549a78f215e 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -30,7 +30,7 @@ impl JsonRenderer<'_> { .into_iter() .flatten() .filter_map(|clean::ItemLink { link, did, .. }| { - did.map(|did| (link.clone(), from_item_id(&did.into()))) + did.map(|did| (link.clone(), from_item_id(did.into()))) }) .collect(); let docs = item.attrs.collapsed_doc_value(); @@ -41,15 +41,14 @@ impl JsonRenderer<'_> { .map(rustc_ast_pretty::pprust::attribute_to_string) .collect(); let span = item.span(self.tcx); - let clean::Item { name, attrs: _, kind: _, visibility, ref def_id, cfg: _ } = item; - let def_id = def_id.clone(); + let clean::Item { name, attrs: _, kind: _, visibility, def_id, cfg: _ } = item; let inner = match *item.kind { clean::StrippedItem(_) => return None, _ => from_clean_item(item, self.tcx), }; Some(Item { + id: from_item_id(def_id), crate_id: def_id.krate().as_u32(), - id: from_item_id(&def_id), name: name.map(|sym| sym.to_string()), span: self.convert_span(span), visibility: self.convert_visibility(visibility), @@ -87,7 +86,7 @@ impl JsonRenderer<'_> { Inherited => Visibility::Default, Restricted(did) if did.index == CRATE_DEF_INDEX => Visibility::Crate, Restricted(did) => Visibility::Restricted { - parent: from_item_id(&did.into()), + parent: from_item_id(did.into()), path: self.tcx.def_path(did).to_string_no_crate_verbose(), }, } @@ -171,7 +170,7 @@ impl FromWithTcx for TypeBindingKind { } } -crate fn from_item_id(did: &ItemId) -> Id { +crate fn from_item_id(did: ItemId) -> Id { match did { ItemId::DefId(did) => Id(format!("{}:{}", did.krate.as_u32(), u32::from(did.index))), _ => todo!("how should json ItemId's be represented?"), @@ -374,7 +373,7 @@ impl FromWithTcx for Type { match ty { ResolvedPath { path, did, is_generic: _ } => Type::ResolvedPath { name: path.whole_name(), - id: from_item_id(&did.into()), + id: from_item_id(did.into()), args: path.segments.last().map(|args| Box::new(args.clone().args.into_tcx(tcx))), param_names: Vec::new(), }, @@ -386,7 +385,7 @@ impl FromWithTcx for Type { Type::ResolvedPath { name: path.whole_name(), - id: from_item_id(&id.into()), + id: from_item_id(id.into()), args: path .segments .last() @@ -567,13 +566,13 @@ impl FromWithTcx for Import { Simple(s) => Import { source: import.source.path.whole_name(), name: s.to_string(), - id: import.source.did.map(ItemId::from).as_ref().map(from_item_id), + id: import.source.did.map(ItemId::from).map(from_item_id), glob: false, }, Glob => Import { source: import.source.path.whole_name(), name: import.source.path.last_name().to_string(), - id: import.source.did.map(ItemId::from).as_ref().map(from_item_id), + id: import.source.did.map(ItemId::from).map(from_item_id), glob: true, }, } @@ -667,5 +666,5 @@ impl FromWithTcx for ItemKind { } fn ids(items: impl IntoIterator) -> Vec { - items.into_iter().filter(|x| !x.is_stripped()).map(|i| from_item_id(&i.def_id)).collect() + items.into_iter().filter(|x| !x.is_stripped()).map(|i| from_item_id(i.def_id)).collect() } diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs index 06c66ce55406e..8bdf1a5981230 100644 --- a/src/librustdoc/json/mod.rs +++ b/src/librustdoc/json/mod.rs @@ -53,7 +53,7 @@ impl JsonRenderer<'tcx> { .map(|i| { let item = &i.impl_item; self.item(item.clone()).unwrap(); - from_item_id(&item.def_id) + from_item_id(item.def_id) }) .collect() }) @@ -71,7 +71,7 @@ impl JsonRenderer<'tcx> { let item = &i.impl_item; if item.def_id.is_local() { self.item(item.clone()).unwrap(); - Some(from_item_id(&item.def_id)) + Some(from_item_id(item.def_id)) } else { None } @@ -91,9 +91,9 @@ impl JsonRenderer<'tcx> { let trait_item = &trait_item.trait_; trait_item.items.clone().into_iter().for_each(|i| self.item(i).unwrap()); Some(( - from_item_id(&id.into()), + from_item_id(id.into()), types::Item { - id: from_item_id(&id.into()), + id: from_item_id(id.into()), crate_id: id.krate.as_u32(), name: self .cache @@ -161,7 +161,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { // Flatten items that recursively store other items item.kind.inner_items().for_each(|i| self.item(i.clone()).unwrap()); - let id = item.def_id.clone(); + let id = item.def_id; if let Some(mut new_item) = self.convert_item(item) { if let types::ItemEnum::Trait(ref mut t) = new_item.inner { t.implementors = self.get_trait_implementors(id.expect_def_id()) @@ -170,7 +170,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { } else if let types::ItemEnum::Enum(ref mut e) = new_item.inner { e.impls = self.get_impls(id.expect_def_id()) } - let removed = self.index.borrow_mut().insert(from_item_id(&id), 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. The main place this happens is when an item, is @@ -207,7 +207,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { .chain(self.cache.external_paths.clone().into_iter()) .map(|(k, (path, kind))| { ( - from_item_id(&k.into()), + from_item_id(k.into()), types::ItemSummary { crate_id: k.krate.as_u32(), path, diff --git a/src/librustdoc/passes/bare_urls.rs b/src/librustdoc/passes/bare_urls.rs index 9d02ae2507290..56ef15eb88420 100644 --- a/src/librustdoc/passes/bare_urls.rs +++ b/src/librustdoc/passes/bare_urls.rs @@ -58,7 +58,7 @@ crate fn check_bare_urls(krate: Crate, cx: &mut DocContext<'_>) -> Crate { impl<'a, 'tcx> DocFolder for BareUrlsLinter<'a, 'tcx> { fn fold_item(&mut self, item: Item) -> Option { - let hir_id = match DocContext::as_local_hir_id(self.cx.tcx, &item.def_id) { + let hir_id = match DocContext::as_local_hir_id(self.cx.tcx, item.def_id) { Some(hir_id) => hir_id, None => { // If non-local, no need to check anything. diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 61623bac27e73..44a3faf6f7be2 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -838,34 +838,41 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> { // find item's parent to resolve `Self` in item's docs below debug!("looking for the `Self` type"); - let self_id = item.def_id.as_def_id().and_then(|did| { - if (matches!(self.cx.tcx.def_kind(did), DefKind::Field) - && matches!( - self.cx.tcx.def_kind(self.cx.tcx.parent(did).unwrap()), - DefKind::Variant - )) + let self_id = match item.def_id.as_def_id() { + None => None, + Some(did) + if (matches!(self.cx.tcx.def_kind(did), DefKind::Field) + && matches!( + self.cx.tcx.def_kind(self.cx.tcx.parent(did).unwrap()), + DefKind::Variant + )) => { self.cx.tcx.parent(did).and_then(|item_id| self.cx.tcx.parent(item_id)) - } else if matches!( - self.cx.tcx.def_kind(did), - DefKind::AssocConst - | DefKind::AssocFn - | DefKind::AssocTy - | DefKind::Variant - | DefKind::Field - ) { + } + Some(did) + if matches!( + self.cx.tcx.def_kind(did), + DefKind::AssocConst + | DefKind::AssocFn + | DefKind::AssocTy + | DefKind::Variant + | DefKind::Field + ) => + { self.cx.tcx.parent(did) - } else if let Some(parent) = self.cx.tcx.parent(did) { + } + Some(did) => match self.cx.tcx.parent(did) { // HACK(jynelson): `clean` marks associated types as `TypedefItem`, not as `AssocTypeItem`. // Fixing this breaks `fn render_deref_methods`. // As a workaround, see if the parent of the item is an `impl`; if so this must be an associated item, // regardless of what rustdoc wants to call it. - let parent_kind = self.cx.tcx.def_kind(parent); - Some(if parent_kind == DefKind::Impl { parent } else { did }) - } else { - Some(did) - } - }); + Some(parent) => { + let parent_kind = self.cx.tcx.def_kind(parent); + Some(if parent_kind == DefKind::Impl { parent } else { did }) + } + None => Some(did), + }, + }; // FIXME(jynelson): this shouldn't go through stringification, rustdoc should just use the DefId directly let self_name = self_id.and_then(|self_id| { @@ -909,12 +916,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> { for md_link in markdown_links(&doc) { let link = self.resolve_link(&item, &doc, &self_name, parent_node, krate, md_link); if let Some(link) = link { - self.cx - .cache - .intra_doc_links - .entry(item.def_id.clone()) - .or_default() - .push(link); + self.cx.cache.intra_doc_links.entry(item.def_id).or_default().push(link); } } } @@ -1710,7 +1712,7 @@ fn report_diagnostic( DiagnosticInfo { item, ori_link: _, dox, link_range }: &DiagnosticInfo<'_>, decorate: impl FnOnce(&mut DiagnosticBuilder<'_>, Option), ) { - let hir_id = match DocContext::as_local_hir_id(tcx, &item.def_id) { + let hir_id = match DocContext::as_local_hir_id(tcx, item.def_id) { Some(hir_id) => hir_id, None => { // If non-local, no need to check anything. diff --git a/src/librustdoc/passes/collect_trait_impls.rs b/src/librustdoc/passes/collect_trait_impls.rs index d589c697fa6a5..91c495a2bbc06 100644 --- a/src/librustdoc/passes/collect_trait_impls.rs +++ b/src/librustdoc/passes/collect_trait_impls.rs @@ -46,7 +46,7 @@ crate fn collect_trait_impls(krate: Crate, cx: &mut DocContext<'_>) -> Crate { // FIXME(eddyb) is this `doc(hidden)` check needed? if !cx.tcx.get_attrs(def_id).lists(sym::doc).has_word(sym::hidden) { let impls = get_auto_trait_and_blanket_impls(cx, def_id.into()); - new_items.extend(impls.filter(|i| cx.inlined.insert(i.def_id.clone()))); + new_items.extend(impls.filter(|i| cx.inlined.insert(i.def_id))); } }); } @@ -165,7 +165,7 @@ impl ItemCollector { impl DocFolder for ItemCollector { fn fold_item(&mut self, i: Item) -> Option { - self.items.insert(i.def_id.clone()); + self.items.insert(i.def_id); Some(self.fold_item_recur(i)) } diff --git a/src/librustdoc/passes/doc_test_lints.rs b/src/librustdoc/passes/doc_test_lints.rs index c3a30661e5046..03bc2b52f178f 100644 --- a/src/librustdoc/passes/doc_test_lints.rs +++ b/src/librustdoc/passes/doc_test_lints.rs @@ -84,7 +84,7 @@ crate fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> boo } crate fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) { - let hir_id = match DocContext::as_local_hir_id(cx.tcx, &item.def_id) { + let hir_id = match DocContext::as_local_hir_id(cx.tcx, item.def_id) { Some(hir_id) => hir_id, None => { // If non-local, no need to check anything. diff --git a/src/librustdoc/passes/html_tags.rs b/src/librustdoc/passes/html_tags.rs index ad9f80c2e6c13..f29d38e3e078a 100644 --- a/src/librustdoc/passes/html_tags.rs +++ b/src/librustdoc/passes/html_tags.rs @@ -168,7 +168,7 @@ fn extract_tags( impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> { fn fold_item(&mut self, item: Item) -> Option { let tcx = self.cx.tcx; - let hir_id = match DocContext::as_local_hir_id(tcx, &item.def_id) { + let hir_id = match DocContext::as_local_hir_id(tcx, item.def_id) { Some(hir_id) => hir_id, None => { // If non-local, no need to check anything. diff --git a/src/librustdoc/passes/strip_hidden.rs b/src/librustdoc/passes/strip_hidden.rs index b0c745edbba5b..0aedbda35e9a8 100644 --- a/src/librustdoc/passes/strip_hidden.rs +++ b/src/librustdoc/passes/strip_hidden.rs @@ -52,7 +52,7 @@ impl<'a> DocFolder for Stripper<'a> { } } else { if self.update_retained { - self.retained.insert(i.def_id.clone()); + self.retained.insert(i.def_id); } } Some(self.fold_item_recur(i)) diff --git a/src/librustdoc/passes/stripper.rs b/src/librustdoc/passes/stripper.rs index 6ee7179c5db84..4305268c9aab0 100644 --- a/src/librustdoc/passes/stripper.rs +++ b/src/librustdoc/passes/stripper.rs @@ -100,7 +100,7 @@ impl<'a> DocFolder for Stripper<'a> { let i = if fastreturn { if self.update_retained { - self.retained.insert(i.def_id.clone()); + self.retained.insert(i.def_id); } return Some(i); } else { @@ -108,7 +108,7 @@ impl<'a> DocFolder for Stripper<'a> { }; if self.update_retained { - self.retained.insert(i.def_id.clone()); + self.retained.insert(i.def_id); } Some(i) } From 4b1027a317d6ca533064a11319d35db75baff141 Mon Sep 17 00:00:00 2001 From: Justus K Date: Sat, 3 Jul 2021 15:29:26 +0200 Subject: [PATCH 5/7] rustdoc: Convert new ItemId's to real Json Ids --- src/librustdoc/json/conversions.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index d4549a78f215e..bf4c7e7ff376e 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -5,9 +5,10 @@ #![allow(rustc::default_hash_types)] use std::convert::From; +use std::fmt; use rustc_ast::ast; -use rustc_hir::def::CtorKind; +use rustc_hir::{def::CtorKind, def_id::DefId}; use rustc_middle::ty::TyCtxt; use rustc_span::def_id::CRATE_DEF_INDEX; use rustc_span::Pos; @@ -171,9 +172,23 @@ impl FromWithTcx for TypeBindingKind { } crate fn from_item_id(did: ItemId) -> Id { + struct DisplayDefId(DefId); + + impl fmt::Display for DisplayDefId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}:{}", self.0.krate.as_u32(), u32::from(self.0.index)) + } + } + match did { - ItemId::DefId(did) => Id(format!("{}:{}", did.krate.as_u32(), u32::from(did.index))), - _ => todo!("how should json ItemId's be represented?"), + ItemId::DefId(did) => Id(format!("{}", DisplayDefId(did))), + ItemId::Blanket { for_, trait_ } => { + Id(format!("b:{}-{}", DisplayDefId(trait_), DisplayDefId(for_))) + } + ItemId::Auto { for_, trait_ } => { + Id(format!("a:{}-{}", DisplayDefId(trait_), DisplayDefId(for_))) + } + ItemId::Primitive(krate) => Id(format!("p:{}", krate.as_u32())), } } From 21424d2966cabd8e6edb1efbe09bf3b2a6120065 Mon Sep 17 00:00:00 2001 From: Justus K Date: Sat, 3 Jul 2021 17:21:39 +0200 Subject: [PATCH 6/7] rustdoc: Add `PrimitiveType` to `ItemId::Primitive` --- src/librustdoc/clean/inline.rs | 5 +++-- src/librustdoc/clean/types.rs | 8 ++++---- src/librustdoc/formats/cache.rs | 2 +- src/librustdoc/json/conversions.rs | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index ce4c1a76b5b80..6f2085e0d43dc 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -483,10 +483,11 @@ fn build_module( } if let Res::PrimTy(p) = item.res { // Primitive types can't be inlined so generate an import instead. + let prim_ty = clean::PrimitiveType::from(p); items.push(clean::Item { name: None, attrs: box clean::Attributes::default(), - def_id: ItemId::Primitive(did.krate), + def_id: ItemId::Primitive(prim_ty, did.krate), visibility: clean::Public, kind: box clean::ImportItem(clean::Import::new_simple( item.ident.name, @@ -495,7 +496,7 @@ fn build_module( global: false, res: item.res, segments: vec![clean::PathSegment { - name: clean::PrimitiveType::from(p).as_sym(), + name: prim_ty.as_sym(), args: clean::GenericArgs::AngleBracketed { args: Vec::new(), bindings: Vec::new(), diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 67f7a10e2d072..4ffce6c7656f0 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -50,7 +50,7 @@ use self::Type::*; crate type ItemIdSet = FxHashSet; -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy)] crate enum ItemId { /// A "normal" item that uses a [`DefId`] for identification. DefId(DefId), @@ -59,7 +59,7 @@ crate enum ItemId { /// Identifier that is used for blanket implementations. Blanket { trait_: DefId, for_: DefId }, /// Identifier for primitive types. - Primitive(CrateNum), + Primitive(PrimitiveType, CrateNum), } impl ItemId { @@ -69,7 +69,7 @@ impl ItemId { ItemId::Auto { for_: id, .. } | ItemId::Blanket { for_: id, .. } | ItemId::DefId(id) => id.is_local(), - ItemId::Primitive(krate) => krate == LOCAL_CRATE, + ItemId::Primitive(_, krate) => krate == LOCAL_CRATE, } } @@ -94,7 +94,7 @@ impl ItemId { ItemId::Auto { for_: id, .. } | ItemId::Blanket { for_: id, .. } | ItemId::DefId(id) => id.krate, - ItemId::Primitive(krate) => krate, + ItemId::Primitive(_, krate) => krate, } } diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index 671e9e5c382b2..e7d6e5ac2c24b 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -122,7 +122,7 @@ crate struct Cache { /// All intra-doc links resolved so far. /// /// Links are indexed by the DefId of the item they document. - crate intra_doc_links: BTreeMap>, + crate intra_doc_links: FxHashMap>, } /// This struct is used to wrap the `cache` and `tcx` in order to run `DocFolder`. diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index bf4c7e7ff376e..bd9f54d2e80d7 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -188,7 +188,7 @@ crate fn from_item_id(did: ItemId) -> Id { ItemId::Auto { for_, trait_ } => { Id(format!("a:{}-{}", DisplayDefId(trait_), DisplayDefId(for_))) } - ItemId::Primitive(krate) => Id(format!("p:{}", krate.as_u32())), + ItemId::Primitive(ty, krate) => Id(format!("p:{}:{}", krate.as_u32(), ty.as_sym())), } } From a89912c8c3a97e5d3b50833984d8dbe23a2fdf52 Mon Sep 17 00:00:00 2001 From: Justus K Date: Sat, 3 Jul 2021 21:34:17 +0200 Subject: [PATCH 7/7] rustdoc: Use `impl_id` and `for_` DefId's for Blanket item id --- src/librustdoc/clean/blanket_impl.rs | 2 +- src/librustdoc/clean/types.rs | 2 +- src/librustdoc/json/conversions.rs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/librustdoc/clean/blanket_impl.rs b/src/librustdoc/clean/blanket_impl.rs index 59fefee5ee4ed..c257d362694fe 100644 --- a/src/librustdoc/clean/blanket_impl.rs +++ b/src/librustdoc/clean/blanket_impl.rs @@ -96,7 +96,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> { name: None, attrs: Default::default(), visibility: Inherited, - def_id: ItemId::Blanket { trait_: trait_def_id, for_: item_def_id }, + def_id: ItemId::Blanket { impl_id: impl_def_id, for_: item_def_id }, kind: box ImplItem(Impl { span: self.cx.tcx.def_span(impl_def_id).clean(self.cx), unsafety: hir::Unsafety::Normal, diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 4ffce6c7656f0..e716e09b8b3f0 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -57,7 +57,7 @@ crate enum ItemId { /// Identifier that is used for auto traits. Auto { trait_: DefId, for_: DefId }, /// Identifier that is used for blanket implementations. - Blanket { trait_: DefId, for_: DefId }, + Blanket { impl_id: DefId, for_: DefId }, /// Identifier for primitive types. Primitive(PrimitiveType, CrateNum), } diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index bd9f54d2e80d7..bf8db79416b34 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -182,8 +182,8 @@ crate fn from_item_id(did: ItemId) -> Id { match did { ItemId::DefId(did) => Id(format!("{}", DisplayDefId(did))), - ItemId::Blanket { for_, trait_ } => { - Id(format!("b:{}-{}", DisplayDefId(trait_), DisplayDefId(for_))) + ItemId::Blanket { for_, impl_id } => { + Id(format!("b:{}-{}", DisplayDefId(impl_id), DisplayDefId(for_))) } ItemId::Auto { for_, trait_ } => { Id(format!("a:{}-{}", DisplayDefId(trait_), DisplayDefId(for_)))