Skip to content

Commit 43e1cdb

Browse files
committed
rustdoc: Replace FakeDefId with new ItemId type
1 parent 6e9b369 commit 43e1cdb

14 files changed

+81
-86
lines changed

src/librustdoc/clean/auto_trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
113113
name: None,
114114
attrs: Default::default(),
115115
visibility: Inherited,
116-
def_id: FakeDefId::new_fake(item_def_id.krate),
116+
def_id: ItemId::Auto { trait_: trait_def_id, for_: item_def_id },
117117
kind: box ImplItem(Impl {
118118
span: Span::dummy(),
119119
unsafety: hir::Unsafety::Normal,

src/librustdoc/clean/blanket_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
9696
name: None,
9797
attrs: Default::default(),
9898
visibility: Inherited,
99-
def_id: FakeDefId::new_fake(item_def_id.krate),
99+
def_id: ItemId::Blanket { trait_: trait_def_id, for_: item_def_id },
100100
kind: box ImplItem(Impl {
101101
span: self.cx.tcx.def_span(impl_def_id).clean(self.cx),
102102
unsafety: hir::Unsafety::Normal,

src/librustdoc/clean/inline.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_span::hygiene::MacroKind;
1515
use rustc_span::symbol::{kw, sym, Symbol};
1616

1717
use crate::clean::{
18-
self, utils, Attributes, AttributesExt, FakeDefId, GetDefId, NestedAttributesExt, Type,
18+
self, utils, Attributes, AttributesExt, GetDefId, ItemId, NestedAttributesExt, Type,
1919
};
2020
use crate::core::DocContext;
2121
use crate::formats::item_type::ItemType;
@@ -486,7 +486,7 @@ fn build_module(
486486
items.push(clean::Item {
487487
name: None,
488488
attrs: box clean::Attributes::default(),
489-
def_id: FakeDefId::new_fake(did.krate),
489+
def_id: ItemId::Primitive(did.krate),
490490
visibility: clean::Public,
491491
kind: box clean::ImportItem(clean::Import::new_simple(
492492
item.ident.name,

src/librustdoc/clean/types.rs

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::cell::{Cell, RefCell};
1+
use std::cell::RefCell;
22
use std::default::Default;
33
use std::hash::{Hash, Hasher};
44
use std::iter::FromIterator;
@@ -18,7 +18,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1818
use rustc_data_structures::thin_vec::ThinVec;
1919
use rustc_hir as hir;
2020
use rustc_hir::def::{CtorKind, DefKind, Res};
21-
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
21+
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX};
2222
use rustc_hir::lang_items::LangItem;
2323
use rustc_hir::{BodyId, Mutability};
2424
use rustc_index::vec::IndexVec;
@@ -48,73 +48,68 @@ use self::ItemKind::*;
4848
use self::SelfTy::*;
4949
use self::Type::*;
5050

51-
crate type FakeDefIdSet = FxHashSet<FakeDefId>;
51+
crate type ItemIdSet = FxHashSet<ItemId>;
5252

5353
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
54-
crate enum FakeDefId {
55-
Real(DefId),
56-
Fake(DefIndex, CrateNum),
57-
}
58-
59-
impl FakeDefId {
60-
#[cfg(parallel_compiler)]
61-
crate fn new_fake(crate: CrateNum) -> Self {
62-
unimplemented!("")
63-
}
64-
65-
#[cfg(not(parallel_compiler))]
66-
crate fn new_fake(krate: CrateNum) -> Self {
67-
thread_local!(static FAKE_DEF_ID_COUNTER: Cell<usize> = Cell::new(0));
68-
let id = FAKE_DEF_ID_COUNTER.with(|id| {
69-
let tmp = id.get();
70-
id.set(tmp + 1);
71-
tmp
72-
});
73-
Self::Fake(DefIndex::from(id), krate)
74-
}
75-
54+
crate enum ItemId {
55+
/// A "normal" item that uses a [`DefId`] for identification.
56+
DefId(DefId),
57+
/// Identifier that is used for auto traits.
58+
Auto { trait_: DefId, for_: DefId },
59+
/// Identifier that is used for blanket implementations.
60+
Blanket { trait_: DefId, for_: DefId },
61+
/// Identifier for primitive types.
62+
Primitive(CrateNum),
63+
}
64+
65+
impl ItemId {
7666
#[inline]
7767
crate fn is_local(self) -> bool {
7868
match self {
79-
FakeDefId::Real(id) => id.is_local(),
80-
FakeDefId::Fake(_, krate) => krate == LOCAL_CRATE,
69+
ItemId::DefId(id) => id.is_local(),
70+
_ => false,
8171
}
8272
}
8373

8474
#[inline]
8575
#[track_caller]
8676
crate fn expect_real(self) -> rustc_hir::def_id::DefId {
87-
self.as_real().unwrap_or_else(|| panic!("FakeDefId::expect_real: `{:?}` isn't real", self))
77+
self.as_real()
78+
.unwrap_or_else(|| panic!("ItemId::expect_real: `{:?}` isn't a real ItemId", self))
8879
}
8980

9081
#[inline]
9182
crate fn as_real(self) -> Option<DefId> {
9283
match self {
93-
FakeDefId::Real(id) => Some(id),
94-
FakeDefId::Fake(_, _) => None,
84+
ItemId::DefId(id) => Some(id),
85+
_ => None,
9586
}
9687
}
9788

9889
#[inline]
9990
crate fn krate(self) -> CrateNum {
10091
match self {
101-
FakeDefId::Real(id) => id.krate,
102-
FakeDefId::Fake(_, krate) => krate,
92+
ItemId::DefId(id) => id.krate,
93+
ItemId::Auto { trait_, .. } => trait_.krate,
94+
ItemId::Blanket { trait_, .. } => trait_.krate,
95+
ItemId::Primitive(krate) => krate,
10396
}
10497
}
10598

10699
#[inline]
107100
crate fn index(self) -> Option<DefIndex> {
108101
match self {
109-
FakeDefId::Real(id) => Some(id.index),
110-
FakeDefId::Fake(_, _) => None,
102+
ItemId::DefId(id) => Some(id.index),
103+
ItemId::Auto { trait_, .. } => Some(trait_.index),
104+
ItemId::Blanket { trait_, .. } => Some(trait_.index),
105+
ItemId::Primitive(..) => None,
111106
}
112107
}
113108
}
114109

115-
impl From<DefId> for FakeDefId {
110+
impl From<DefId> for ItemId {
116111
fn from(id: DefId) -> Self {
117-
Self::Real(id)
112+
Self::DefId(id)
118113
}
119114
}
120115

@@ -338,14 +333,14 @@ crate struct Item {
338333
/// Information about this item that is specific to what kind of item it is.
339334
/// E.g., struct vs enum vs function.
340335
crate kind: Box<ItemKind>,
341-
crate def_id: FakeDefId,
336+
crate def_id: ItemId,
342337

343338
crate cfg: Option<Arc<Cfg>>,
344339
}
345340

346341
// `Item` is used a lot. Make sure it doesn't unintentionally get bigger.
347342
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
348-
rustc_data_structures::static_assert_size!(Item, 48);
343+
rustc_data_structures::static_assert_size!(Item, 56);
349344

350345
crate fn rustc_span(def_id: DefId, tcx: TyCtxt<'_>) -> Span {
351346
Span::from_rustc_span(def_id.as_local().map_or_else(
@@ -664,7 +659,8 @@ impl Item {
664659
}
665660

666661
crate fn is_fake(&self) -> bool {
667-
matches!(self.def_id, FakeDefId::Fake(_, _))
662+
// FIXME: Find a better way to handle this
663+
!matches!(self.def_id, ItemId::DefId(..))
668664
}
669665
}
670666

src/librustdoc/core.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use std::mem;
3030
use std::rc::Rc;
3131

3232
use crate::clean::inline::build_external_trait;
33-
use crate::clean::{self, FakeDefId, TraitWithExtraInfo};
33+
use crate::clean::{self, ItemId, TraitWithExtraInfo};
3434
use crate::config::{Options as RustdocOptions, OutputFormat, RenderOptions};
3535
use crate::formats::cache::Cache;
3636
use crate::passes::{self, Condition::*, ConditionalPass};
@@ -78,7 +78,7 @@ crate struct DocContext<'tcx> {
7878
/// This same cache is used throughout rustdoc, including in [`crate::html::render`].
7979
crate cache: Cache,
8080
/// Used by [`clean::inline`] to tell if an item has already been inlined.
81-
crate inlined: FxHashSet<FakeDefId>,
81+
crate inlined: FxHashSet<ItemId>,
8282
/// Used by `calculate_doc_coverage`.
8383
crate output_format: OutputFormat,
8484
}
@@ -128,12 +128,13 @@ impl<'tcx> DocContext<'tcx> {
128128

129129
/// Like `hir().local_def_id_to_hir_id()`, but skips calling it on fake DefIds.
130130
/// (This avoids a slice-index-out-of-bounds panic.)
131-
crate fn as_local_hir_id(tcx: TyCtxt<'_>, def_id: FakeDefId) -> Option<HirId> {
131+
crate fn as_local_hir_id(tcx: TyCtxt<'_>, def_id: ItemId) -> Option<HirId> {
132132
match def_id {
133-
FakeDefId::Real(real_id) => {
133+
ItemId::DefId(real_id) => {
134134
real_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
135135
}
136-
FakeDefId::Fake(_, _) => None,
136+
// FIXME: Can this be `Some` for `Auto` or `Blanket`?
137+
_ => None,
137138
}
138139
}
139140
}

src/librustdoc/formats/cache.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::middle::privacy::AccessLevels;
88
use rustc_middle::ty::TyCtxt;
99
use rustc_span::symbol::sym;
1010

11-
use crate::clean::{self, FakeDefId, GetDefId};
11+
use crate::clean::{self, GetDefId, ItemId};
1212
use crate::fold::DocFolder;
1313
use crate::formats::item_type::ItemType;
1414
use crate::formats::Impl;
@@ -122,7 +122,7 @@ crate struct Cache {
122122
/// All intra-doc links resolved so far.
123123
///
124124
/// Links are indexed by the DefId of the item they document.
125-
crate intra_doc_links: BTreeMap<FakeDefId, Vec<clean::ItemLink>>,
125+
crate intra_doc_links: BTreeMap<ItemId, Vec<clean::ItemLink>>,
126126
}
127127

128128
/// This struct is used to wrap the `cache` and `tcx` in order to run `DocFolder`.

src/librustdoc/html/format.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_span::def_id::CRATE_DEF_INDEX;
1919
use rustc_target::spec::abi::Abi;
2020

2121
use crate::clean::{
22-
self, utils::find_nearest_parent_module, ExternalCrate, FakeDefId, GetDefId, PrimitiveType,
22+
self, utils::find_nearest_parent_module, ExternalCrate, GetDefId, ItemId, PrimitiveType,
2323
};
2424
use crate::formats::item_type::ItemType;
2525
use crate::html::escape::Escape;
@@ -1181,7 +1181,7 @@ impl clean::FnDecl {
11811181
impl clean::Visibility {
11821182
crate fn print_with_space<'a, 'tcx: 'a>(
11831183
self,
1184-
item_did: FakeDefId,
1184+
item_did: ItemId,
11851185
cx: &'a Context<'tcx>,
11861186
) -> impl fmt::Display + 'a + Captures<'tcx> {
11871187
let to_print = match self {

src/librustdoc/html/render/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use rustc_span::symbol::{kw, sym, Symbol};
5353
use serde::ser::SerializeSeq;
5454
use serde::{Serialize, Serializer};
5555

56-
use crate::clean::{self, FakeDefId, GetDefId, RenderedLink, SelfTy};
56+
use crate::clean::{self, GetDefId, ItemId, RenderedLink, SelfTy};
5757
use crate::docfs::PathError;
5858
use crate::error::Error;
5959
use crate::formats::cache::Cache;
@@ -987,7 +987,7 @@ fn render_attributes_in_code(w: &mut Buffer, it: &clean::Item) {
987987
#[derive(Copy, Clone)]
988988
enum AssocItemLink<'a> {
989989
Anchor(Option<&'a str>),
990-
GotoSource(FakeDefId, &'a FxHashSet<Symbol>),
990+
GotoSource(ItemId, &'a FxHashSet<Symbol>),
991991
}
992992

993993
impl<'a> AssocItemLink<'a> {

src/librustdoc/json/conversions.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_span::Pos;
1515
use rustdoc_json_types::*;
1616

1717
use crate::clean::utils::print_const_expr;
18-
use crate::clean::{self, FakeDefId};
18+
use crate::clean::{self, ItemId};
1919
use crate::formats::item_type::ItemType;
2020
use crate::json::JsonRenderer;
2121
use std::collections::HashSet;
@@ -30,7 +30,7 @@ impl JsonRenderer<'_> {
3030
.into_iter()
3131
.flatten()
3232
.filter_map(|clean::ItemLink { link, did, .. }| {
33-
did.map(|did| (link.clone(), from_def_id(did.into())))
33+
did.map(|did| (link.clone(), from_item_id(did.into())))
3434
})
3535
.collect();
3636
let docs = item.attrs.collapsed_doc_value();
@@ -47,7 +47,7 @@ impl JsonRenderer<'_> {
4747
_ => from_clean_item(item, self.tcx),
4848
};
4949
Some(Item {
50-
id: from_def_id(def_id),
50+
id: from_item_id(def_id),
5151
crate_id: def_id.krate().as_u32(),
5252
name: name.map(|sym| sym.to_string()),
5353
span: self.convert_span(span),
@@ -86,7 +86,7 @@ impl JsonRenderer<'_> {
8686
Inherited => Visibility::Default,
8787
Restricted(did) if did.index == CRATE_DEF_INDEX => Visibility::Crate,
8888
Restricted(did) => Visibility::Restricted {
89-
parent: from_def_id(did.into()),
89+
parent: from_item_id(did.into()),
9090
path: self.tcx.def_path(did).to_string_no_crate_verbose(),
9191
},
9292
}
@@ -170,12 +170,10 @@ impl FromWithTcx<clean::TypeBindingKind> for TypeBindingKind {
170170
}
171171
}
172172

173-
crate fn from_def_id(did: FakeDefId) -> Id {
173+
crate fn from_item_id(did: ItemId) -> Id {
174174
match did {
175-
FakeDefId::Real(did) => Id(format!("{}:{}", did.krate.as_u32(), u32::from(did.index))),
176-
// We need to differentiate real and fake ids, because the indices might overlap for fake
177-
// and real DefId's, which would cause two different Id's treated as they were the same.
178-
FakeDefId::Fake(idx, krate) => Id(format!("F{}:{}", krate.as_u32(), u32::from(idx))),
175+
ItemId::DefId(did) => Id(format!("{}:{}", did.krate.as_u32(), u32::from(did.index))),
176+
_ => todo!("how should json ItemId's be represented?"),
179177
}
180178
}
181179

@@ -375,7 +373,7 @@ impl FromWithTcx<clean::Type> for Type {
375373
match ty {
376374
ResolvedPath { path, did, is_generic: _ } => Type::ResolvedPath {
377375
name: path.whole_name(),
378-
id: from_def_id(did.into()),
376+
id: from_item_id(did.into()),
379377
args: path.segments.last().map(|args| Box::new(args.clone().args.into_tcx(tcx))),
380378
param_names: Vec::new(),
381379
},
@@ -387,7 +385,7 @@ impl FromWithTcx<clean::Type> for Type {
387385

388386
Type::ResolvedPath {
389387
name: path.whole_name(),
390-
id: from_def_id(id.into()),
388+
id: from_item_id(id.into()),
391389
args: path
392390
.segments
393391
.last()
@@ -568,13 +566,13 @@ impl FromWithTcx<clean::Import> for Import {
568566
Simple(s) => Import {
569567
source: import.source.path.whole_name(),
570568
name: s.to_string(),
571-
id: import.source.did.map(FakeDefId::from).map(from_def_id),
569+
id: import.source.did.map(ItemId::from).map(from_item_id),
572570
glob: false,
573571
},
574572
Glob => Import {
575573
source: import.source.path.whole_name(),
576574
name: import.source.path.last_name().to_string(),
577-
id: import.source.did.map(FakeDefId::from).map(from_def_id),
575+
id: import.source.did.map(ItemId::from).map(from_item_id),
578576
glob: true,
579577
},
580578
}
@@ -668,5 +666,5 @@ impl FromWithTcx<ItemType> for ItemKind {
668666
}
669667

670668
fn ids(items: impl IntoIterator<Item = clean::Item>) -> Vec<Id> {
671-
items.into_iter().filter(|x| !x.is_stripped()).map(|i| from_def_id(i.def_id)).collect()
669+
items.into_iter().filter(|x| !x.is_stripped()).map(|i| from_item_id(i.def_id)).collect()
672670
}

0 commit comments

Comments
 (0)