Skip to content

Commit 36c9b49

Browse files
Remove unneeded ItemId::Primitive variant
1 parent c54c8cb commit 36c9b49

File tree

5 files changed

+61
-37
lines changed

5 files changed

+61
-37
lines changed

src/librustdoc/clean/inline.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,9 @@ fn build_module_items(
600600
items.push(clean::Item {
601601
name: None,
602602
attrs: Box::new(clean::Attributes::default()),
603-
item_id: ItemId::Primitive(prim_ty, did.krate),
603+
// We can use the item's `DefId` directly since the only information ever used
604+
// from it is `DefId.krate`.
605+
item_id: ItemId::DefId(did),
604606
kind: Box::new(clean::ImportItem(clean::Import::new_simple(
605607
item.ident.name,
606608
clean::ImportSource {

src/librustdoc/clean/types.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ pub(crate) enum ItemId {
6262
Auto { trait_: DefId, for_: DefId },
6363
/// Identifier that is used for blanket implementations.
6464
Blanket { impl_id: DefId, for_: DefId },
65-
/// Identifier for primitive types.
66-
Primitive(PrimitiveType, CrateNum),
6765
}
6866

6967
impl ItemId {
@@ -73,7 +71,6 @@ impl ItemId {
7371
ItemId::Auto { for_: id, .. }
7472
| ItemId::Blanket { for_: id, .. }
7573
| ItemId::DefId(id) => id.is_local(),
76-
ItemId::Primitive(_, krate) => krate == LOCAL_CRATE,
7774
}
7875
}
7976

@@ -98,7 +95,6 @@ impl ItemId {
9895
ItemId::Auto { for_: id, .. }
9996
| ItemId::Blanket { for_: id, .. }
10097
| ItemId::DefId(id) => id.krate,
101-
ItemId::Primitive(_, krate) => krate,
10298
}
10399
}
104100
}
@@ -707,15 +703,13 @@ impl Item {
707703
let def_id = match self.item_id {
708704
// Anything but DefId *shouldn't* matter, but return a reasonable value anyway.
709705
ItemId::Auto { .. } | ItemId::Blanket { .. } => return None,
710-
// Primitives and Keywords are written in the source code as private modules.
711-
// The modules need to be private so that nobody actually uses them, but the
712-
// keywords and primitives that they are documenting are public.
713-
ItemId::Primitive(..) => return Some(Visibility::Public),
714706
ItemId::DefId(def_id) => def_id,
715707
};
716708

717709
match *self.kind {
718-
// Explication on `ItemId::Primitive` just above.
710+
// Primitives and Keywords are written in the source code as private modules.
711+
// The modules need to be private so that nobody actually uses them, but the
712+
// keywords and primitives that they are documenting are public.
719713
ItemKind::KeywordItem | ItemKind::PrimitiveItem(_) => return Some(Visibility::Public),
720714
// Variant fields inherit their enum's visibility.
721715
StructFieldItem(..) if is_field_vis_inherited(tcx, def_id) => {

src/librustdoc/formats/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@ impl Impl {
5353
ItemId::Blanket { impl_id, .. } => impl_id,
5454
ItemId::Auto { trait_, .. } => trait_,
5555
ItemId::DefId(def_id) => def_id,
56-
ItemId::Primitive(_, _) => {
57-
panic!(
58-
"Unexpected ItemId::Primitive in expect_def_id: {:?}",
59-
self.impl_item.item_id
60-
)
61-
}
6256
}
6357
}
6458

src/librustdoc/html/render/search_index.rs

+55-20
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::collections::BTreeMap;
33

44
use rustc_data_structures::fx::FxHashMap;
55
use rustc_middle::ty::TyCtxt;
6-
use rustc_span::def_id::LOCAL_CRATE;
76
use rustc_span::symbol::Symbol;
87
use serde::ser::{Serialize, SerializeStruct, Serializer};
98

@@ -24,6 +23,7 @@ pub(crate) fn build_index<'tcx>(
2423
tcx: TyCtxt<'tcx>,
2524
) -> String {
2625
let mut itemid_to_pathid = FxHashMap::default();
26+
let mut primitives = FxHashMap::default();
2727
let mut crate_paths = vec![];
2828

2929
// Attach all orphan items to the type's definition if the type
@@ -78,50 +78,83 @@ pub(crate) fn build_index<'tcx>(
7878
// First, on function signatures
7979
let mut search_index = std::mem::replace(&mut cache.search_index, Vec::new());
8080
for item in search_index.iter_mut() {
81+
fn insert_into_map<F: std::hash::Hash + Eq>(
82+
ty: &mut RenderType,
83+
map: &mut FxHashMap<F, usize>,
84+
itemid: F,
85+
lastpathid: &mut usize,
86+
crate_paths: &mut Vec<(ItemType, Symbol)>,
87+
item_type: ItemType,
88+
path: Symbol,
89+
) {
90+
match map.entry(itemid) {
91+
Entry::Occupied(entry) => ty.id = Some(RenderTypeId::Index(*entry.get())),
92+
Entry::Vacant(entry) => {
93+
let pathid = *lastpathid;
94+
entry.insert(pathid);
95+
*lastpathid += 1;
96+
crate_paths.push((item_type, path));
97+
ty.id = Some(RenderTypeId::Index(pathid));
98+
}
99+
}
100+
}
101+
81102
fn convert_render_type(
82103
ty: &mut RenderType,
83104
cache: &mut Cache,
84105
itemid_to_pathid: &mut FxHashMap<ItemId, usize>,
106+
primitives: &mut FxHashMap<Symbol, usize>,
85107
lastpathid: &mut usize,
86108
crate_paths: &mut Vec<(ItemType, Symbol)>,
87109
) {
88110
if let Some(generics) = &mut ty.generics {
89111
for item in generics {
90-
convert_render_type(item, cache, itemid_to_pathid, lastpathid, crate_paths);
112+
convert_render_type(
113+
item,
114+
cache,
115+
itemid_to_pathid,
116+
primitives,
117+
lastpathid,
118+
crate_paths,
119+
);
91120
}
92121
}
93122
let Cache { ref paths, ref external_paths, .. } = *cache;
94123
let Some(id) = ty.id.clone() else {
95124
assert!(ty.generics.is_some());
96125
return;
97126
};
98-
let (itemid, path, item_type) = match id {
127+
match id {
99128
RenderTypeId::DefId(defid) => {
100129
if let Some(&(ref fqp, item_type)) =
101130
paths.get(&defid).or_else(|| external_paths.get(&defid))
102131
{
103-
(ItemId::DefId(defid), *fqp.last().unwrap(), item_type)
132+
insert_into_map(
133+
ty,
134+
itemid_to_pathid,
135+
ItemId::DefId(defid),
136+
lastpathid,
137+
crate_paths,
138+
item_type,
139+
*fqp.last().unwrap(),
140+
);
104141
} else {
105142
ty.id = None;
106-
return;
107143
}
108144
}
109-
RenderTypeId::Primitive(primitive) => (
110-
ItemId::Primitive(primitive, LOCAL_CRATE),
111-
primitive.as_sym(),
112-
ItemType::Primitive,
113-
),
114-
RenderTypeId::Index(_) => return,
115-
};
116-
match itemid_to_pathid.entry(itemid) {
117-
Entry::Occupied(entry) => ty.id = Some(RenderTypeId::Index(*entry.get())),
118-
Entry::Vacant(entry) => {
119-
let pathid = *lastpathid;
120-
entry.insert(pathid);
121-
*lastpathid += 1;
122-
crate_paths.push((item_type, path));
123-
ty.id = Some(RenderTypeId::Index(pathid));
145+
RenderTypeId::Primitive(primitive) => {
146+
let sym = primitive.as_sym();
147+
insert_into_map(
148+
ty,
149+
primitives,
150+
sym,
151+
lastpathid,
152+
crate_paths,
153+
ItemType::Primitive,
154+
sym,
155+
);
124156
}
157+
RenderTypeId::Index(_) => {}
125158
}
126159
}
127160
if let Some(search_type) = &mut item.search_type {
@@ -130,6 +163,7 @@ pub(crate) fn build_index<'tcx>(
130163
item,
131164
cache,
132165
&mut itemid_to_pathid,
166+
&mut primitives,
133167
&mut lastpathid,
134168
&mut crate_paths,
135169
);
@@ -139,6 +173,7 @@ pub(crate) fn build_index<'tcx>(
139173
item,
140174
cache,
141175
&mut itemid_to_pathid,
176+
&mut primitives,
142177
&mut lastpathid,
143178
&mut crate_paths,
144179
);

src/librustdoc/json/conversions.rs

-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ pub(crate) fn from_item_id_with_name(item_id: ItemId, tcx: TyCtxt<'_>, name: Opt
252252
ItemId::Auto { for_, trait_ } => {
253253
Id(format!("a:{}-{}", DisplayDefId(trait_, tcx, None), DisplayDefId(for_, tcx, name)))
254254
}
255-
ItemId::Primitive(_, _) => unreachable!(),
256255
}
257256
}
258257

0 commit comments

Comments
 (0)