Skip to content

Commit 86f9fd1

Browse files
authored
Rollup merge of rust-lang#85067 - Stupremee:minimize-amount-of-fake-defids, r=jyn514,GuillaumeGomez
Minimize amount of fake `DefId`s used in rustdoc Follow up from rust-lang#84707, which minimizes the amount of fake defids to the smallest amount possible. Every `FakeDefId` that is now used in the rustdoc library must be preserved and can not be replaced with a normal `DefId`.
2 parents 28ce404 + a8b49ff commit 86f9fd1

12 files changed

+55
-90
lines changed

src/librustdoc/clean/mod.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,7 @@ impl Clean<Generics> for hir::Generics<'_> {
533533
match param.kind {
534534
GenericParamDefKind::Lifetime => unreachable!(),
535535
GenericParamDefKind::Type { did, ref bounds, .. } => {
536-
cx.impl_trait_bounds
537-
.insert(FakeDefId::new_real(did).into(), bounds.clone());
536+
cx.impl_trait_bounds.insert(did.into(), bounds.clone());
538537
}
539538
GenericParamDefKind::Const { .. } => unreachable!(),
540539
}
@@ -615,7 +614,7 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx
615614
.collect::<Vec<GenericParamDef>>();
616615

617616
// param index -> [(DefId of trait, associated type name, type)]
618-
let mut impl_trait_proj = FxHashMap::<u32, Vec<(FakeDefId, Symbol, Ty<'tcx>)>>::default();
617+
let mut impl_trait_proj = FxHashMap::<u32, Vec<(DefId, Symbol, Ty<'tcx>)>>::default();
619618

620619
let where_predicates = preds
621620
.predicates
@@ -687,13 +686,7 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx
687686
if let Some(proj) = impl_trait_proj.remove(&idx) {
688687
for (trait_did, name, rhs) in proj {
689688
let rhs = rhs.clean(cx);
690-
simplify::merge_bounds(
691-
cx,
692-
&mut bounds,
693-
trait_did.expect_real(),
694-
name,
695-
&rhs,
696-
);
689+
simplify::merge_bounds(cx, &mut bounds, trait_did, name, &rhs);
697690
}
698691
}
699692
} else {
@@ -1183,8 +1176,7 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
11831176
if let Some(new_ty) = cx.ty_substs.get(&did).cloned() {
11841177
return new_ty;
11851178
}
1186-
if let Some(bounds) = cx.impl_trait_bounds.remove(&FakeDefId::new_real(did).into())
1187-
{
1179+
if let Some(bounds) = cx.impl_trait_bounds.remove(&did.into()) {
11881180
return ImplTrait(bounds);
11891181
}
11901182
}

src/librustdoc/clean/types.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ impl FakeDefId {
7373
Self::Fake(DefIndex::from(id), krate)
7474
}
7575

76-
crate fn new_real(id: DefId) -> Self {
77-
Self::Real(id)
78-
}
79-
8076
#[inline]
8177
crate fn is_local(self) -> bool {
8278
match self {
@@ -470,7 +466,7 @@ impl Item {
470466
.filter_map(|ItemLink { link: s, link_text, did, ref fragment }| {
471467
match did {
472468
Some(did) => {
473-
if let Some((mut href, ..)) = href(did.expect_real(), cx) {
469+
if let Some((mut href, ..)) = href(did.clone(), cx) {
474470
if let Some(ref fragment) = *fragment {
475471
href.push('#');
476472
href.push_str(fragment);
@@ -972,7 +968,7 @@ crate struct ItemLink {
972968
/// This may not be the same as `link` if there was a disambiguator
973969
/// in an intra-doc link (e.g. \[`fn@f`\])
974970
pub(crate) link_text: String,
975-
pub(crate) did: Option<FakeDefId>,
971+
pub(crate) did: Option<DefId>,
976972
/// The url fragment to append to the link
977973
pub(crate) fragment: Option<String>,
978974
}

src/librustdoc/core.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -576,12 +576,12 @@ impl<'tcx> Visitor<'tcx> for EmitIgnoredResolutionErrors<'tcx> {
576576
/// for `impl Trait` in argument position.
577577
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
578578
crate enum ImplTraitParam {
579-
DefId(FakeDefId),
579+
DefId(DefId),
580580
ParamIndex(u32),
581581
}
582582

583-
impl From<FakeDefId> for ImplTraitParam {
584-
fn from(did: FakeDefId) -> Self {
583+
impl From<DefId> for ImplTraitParam {
584+
fn from(did: DefId) -> Self {
585585
ImplTraitParam::DefId(did)
586586
}
587587
}

src/librustdoc/formats/cache.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ crate struct Cache {
6767
/// When rendering traits, it's often useful to be able to list all
6868
/// implementors of the trait, and this mapping is exactly, that: a mapping
6969
/// of trait ids to the list of known implementors of the trait
70-
crate implementors: FxHashMap<FakeDefId, Vec<Impl>>,
70+
crate implementors: FxHashMap<DefId, Vec<Impl>>,
7171

7272
/// Cache of where external crate documentation can be found.
7373
crate extern_locations: FxHashMap<CrateNum, ExternalLocation>,
@@ -299,7 +299,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
299299
desc: item
300300
.doc_value()
301301
.map_or_else(String::new, |x| short_markdown_summary(&x.as_str())),
302-
parent: parent.map(FakeDefId::new_real),
302+
parent,
303303
parent_idx: None,
304304
search_type: get_index_search_type(&item, &self.empty_cache, self.tcx),
305305
aliases: item.attrs.get_doc_aliases(),

src/librustdoc/html/render/cache.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use serde::ser::{Serialize, SerializeStruct, Serializer};
77

88
use crate::clean;
99
use crate::clean::types::{
10-
FakeDefId, FnDecl, FnRetTy, GenericBound, Generics, GetDefId, Type, WherePredicate,
10+
FnDecl, FnRetTy, GenericBound, Generics, GetDefId, Type, WherePredicate,
1111
};
1212
use crate::formats::cache::Cache;
1313
use crate::formats::item_type::ItemType;
@@ -82,7 +82,7 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
8282
defid_to_pathid.insert(defid, pathid);
8383
lastpathid += 1;
8484

85-
if let Some(&(ref fqp, short)) = paths.get(&defid.expect_real()) {
85+
if let Some(&(ref fqp, short)) = paths.get(&defid) {
8686
crate_paths.push((short, fqp.last().unwrap().clone()));
8787
Some(pathid)
8888
} else {
@@ -214,7 +214,7 @@ crate fn get_index_search_type<'tcx>(
214214

215215
fn get_index_type(clean_type: &clean::Type, cache: &Cache) -> RenderType {
216216
RenderType {
217-
ty: clean_type.def_id_full(cache).map(FakeDefId::new_real),
217+
ty: clean_type.def_id_full(cache),
218218
idx: None,
219219
name: get_index_type_name(clean_type, true).map(|s| s.as_str().to_ascii_lowercase()),
220220
generics: get_generics(clean_type, cache),
@@ -256,7 +256,7 @@ fn get_generics(clean_type: &clean::Type, cache: &Cache) -> Option<Vec<Generic>>
256256
.filter_map(|t| {
257257
get_index_type_name(t, false).map(|name| Generic {
258258
name: name.as_str().to_ascii_lowercase(),
259-
defid: t.def_id_full(cache).map(FakeDefId::new_real),
259+
defid: t.def_id_full(cache),
260260
idx: None,
261261
})
262262
})

src/librustdoc/html/render/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ crate struct IndexItem {
8787
crate name: String,
8888
crate path: String,
8989
crate desc: String,
90-
crate parent: Option<FakeDefId>,
90+
crate parent: Option<DefId>,
9191
crate parent_idx: Option<usize>,
9292
crate search_type: Option<IndexItemFunctionType>,
9393
crate aliases: Box<[String]>,
@@ -96,7 +96,7 @@ crate struct IndexItem {
9696
/// A type used for the search index.
9797
#[derive(Debug)]
9898
crate struct RenderType {
99-
ty: Option<FakeDefId>,
99+
ty: Option<DefId>,
100100
idx: Option<usize>,
101101
name: Option<String>,
102102
generics: Option<Vec<Generic>>,
@@ -128,7 +128,7 @@ impl Serialize for RenderType {
128128
#[derive(Debug)]
129129
crate struct Generic {
130130
name: String,
131-
defid: Option<FakeDefId>,
131+
defid: Option<DefId>,
132132
idx: Option<usize>,
133133
}
134134

@@ -2118,7 +2118,7 @@ fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean
21182118
"</div>",
21192119
);
21202120

2121-
if let Some(implementors) = cx.cache.implementors.get(&it.def_id) {
2121+
if let Some(implementors) = cx.cache.implementors.get(&it.def_id.expect_real()) {
21222122
let cache = cx.cache();
21232123
let mut res = implementors
21242124
.iter()

src/librustdoc/html/render/print_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
635635
// If there are methods directly on this trait object, render them here.
636636
render_assoc_items(w, cx, it, it.def_id.expect_real(), AssocItemRender::All);
637637

638-
if let Some(implementors) = cx.cache.implementors.get(&it.def_id) {
638+
if let Some(implementors) = cx.cache.implementors.get(&it.def_id.expect_real()) {
639639
// The DefId is for the first Type found with that name. The bool is
640640
// if any Types with the same name but different DefId have been found.
641641
let mut implementor_dups: FxHashMap<Symbol, (DefId, bool)> = FxHashMap::default();

src/librustdoc/html/render/write_shared.rs

-2
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,6 @@ pub(super) fn write_shared(
464464
// Update the list of all implementors for traits
465465
let dst = cx.dst.join("implementors");
466466
for (&did, imps) in &cx.cache.implementors {
467-
let did = did.expect_real();
468-
469467
// Private modules can leak through to this phase of rustdoc, which
470468
// could contain implementations for otherwise private types. In some
471469
// rare cases we could find an implementation for an item which wasn't

src/librustdoc/json/conversions.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ use rustc_span::Pos;
1414

1515
use rustdoc_json_types::*;
1616

17-
use crate::clean;
1817
use crate::clean::utils::print_const_expr;
19-
use crate::clean::FakeDefId;
18+
use crate::clean::{self, FakeDefId};
2019
use crate::formats::item_type::ItemType;
2120
use crate::json::JsonRenderer;
2221
use std::collections::HashSet;
@@ -31,7 +30,7 @@ impl JsonRenderer<'_> {
3130
.into_iter()
3231
.flatten()
3332
.filter_map(|clean::ItemLink { link, did, .. }| {
34-
did.map(|did| (link.clone(), from_def_id(did)))
33+
did.map(|did| (link.clone(), from_def_id(did.into())))
3534
})
3635
.collect();
3736
let docs = item.attrs.collapsed_doc_value();

src/librustdoc/json/mod.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ use std::path::PathBuf;
1212
use std::rc::Rc;
1313

1414
use rustc_data_structures::fx::FxHashMap;
15+
use rustc_hir::def_id::DefId;
1516
use rustc_middle::ty::TyCtxt;
1617
use rustc_session::Session;
1718

1819
use rustdoc_json_types as types;
1920

2021
use crate::clean;
21-
use crate::clean::{ExternalCrate, FakeDefId};
22+
use crate::clean::ExternalCrate;
2223
use crate::config::RenderOptions;
2324
use crate::error::Error;
2425
use crate::formats::cache::Cache;
@@ -42,7 +43,7 @@ impl JsonRenderer<'tcx> {
4243
self.tcx.sess
4344
}
4445

45-
fn get_trait_implementors(&mut self, id: FakeDefId) -> Vec<types::Id> {
46+
fn get_trait_implementors(&mut self, id: DefId) -> Vec<types::Id> {
4647
Rc::clone(&self.cache)
4748
.implementors
4849
.get(&id)
@@ -59,10 +60,10 @@ impl JsonRenderer<'tcx> {
5960
.unwrap_or_default()
6061
}
6162

62-
fn get_impls(&mut self, id: FakeDefId) -> Vec<types::Id> {
63+
fn get_impls(&mut self, id: DefId) -> Vec<types::Id> {
6364
Rc::clone(&self.cache)
6465
.impls
65-
.get(&id.expect_real())
66+
.get(&id)
6667
.map(|impls| {
6768
impls
6869
.iter()
@@ -163,11 +164,11 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
163164
let id = item.def_id;
164165
if let Some(mut new_item) = self.convert_item(item) {
165166
if let types::ItemEnum::Trait(ref mut t) = new_item.inner {
166-
t.implementors = self.get_trait_implementors(id)
167+
t.implementors = self.get_trait_implementors(id.expect_real())
167168
} else if let types::ItemEnum::Struct(ref mut s) = new_item.inner {
168-
s.impls = self.get_impls(id)
169+
s.impls = self.get_impls(id.expect_real())
169170
} else if let types::ItemEnum::Enum(ref mut e) = new_item.inner {
170-
e.impls = self.get_impls(id)
171+
e.impls = self.get_impls(id.expect_real())
171172
}
172173
let removed = self.index.borrow_mut().insert(from_def_id(id), new_item.clone());
173174

0 commit comments

Comments
 (0)