Skip to content

Commit

Permalink
Rollup merge of rust-lang#85067 - Stupremee:minimize-amount-of-fake-d…
Browse files Browse the repository at this point in the history
…efids, 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`.
  • Loading branch information
GuillaumeGomez authored May 15, 2021
2 parents 9682fa9 + 67d8d18 commit b68d543
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 90 deletions.
16 changes: 4 additions & 12 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,7 @@ impl Clean<Generics> for hir::Generics<'_> {
match param.kind {
GenericParamDefKind::Lifetime => unreachable!(),
GenericParamDefKind::Type { did, ref bounds, .. } => {
cx.impl_trait_bounds
.insert(FakeDefId::new_real(did).into(), bounds.clone());
cx.impl_trait_bounds.insert(did.into(), bounds.clone());
}
GenericParamDefKind::Const { .. } => unreachable!(),
}
Expand Down Expand Up @@ -615,7 +614,7 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx
.collect::<Vec<GenericParamDef>>();

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

let where_predicates = preds
.predicates
Expand Down Expand Up @@ -687,13 +686,7 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx
if let Some(proj) = impl_trait_proj.remove(&idx) {
for (trait_did, name, rhs) in proj {
let rhs = rhs.clean(cx);
simplify::merge_bounds(
cx,
&mut bounds,
trait_did.expect_real(),
name,
&rhs,
);
simplify::merge_bounds(cx, &mut bounds, trait_did, name, &rhs);
}
}
} else {
Expand Down Expand Up @@ -1183,8 +1176,7 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
if let Some(new_ty) = cx.ty_substs.get(&did).cloned() {
return new_ty;
}
if let Some(bounds) = cx.impl_trait_bounds.remove(&FakeDefId::new_real(did).into())
{
if let Some(bounds) = cx.impl_trait_bounds.remove(&did.into()) {
return ImplTrait(bounds);
}
}
Expand Down
8 changes: 2 additions & 6 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ impl FakeDefId {
Self::Fake(DefIndex::from(id), krate)
}

crate fn new_real(id: DefId) -> Self {
Self::Real(id)
}

#[inline]
crate fn is_local(self) -> bool {
match self {
Expand Down Expand Up @@ -470,7 +466,7 @@ impl Item {
.filter_map(|ItemLink { link: s, link_text, did, ref fragment }| {
match did {
Some(did) => {
if let Some((mut href, ..)) = href(did.expect_real(), cx) {
if let Some((mut href, ..)) = href(did.clone(), cx) {
if let Some(ref fragment) = *fragment {
href.push('#');
href.push_str(fragment);
Expand Down Expand Up @@ -972,7 +968,7 @@ crate struct ItemLink {
/// This may not be the same as `link` if there was a disambiguator
/// in an intra-doc link (e.g. \[`fn@f`\])
pub(crate) link_text: String,
pub(crate) did: Option<FakeDefId>,
pub(crate) did: Option<DefId>,
/// The url fragment to append to the link
pub(crate) fragment: Option<String>,
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,12 +576,12 @@ impl<'tcx> Visitor<'tcx> for EmitIgnoredResolutionErrors<'tcx> {
/// for `impl Trait` in argument position.
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
crate enum ImplTraitParam {
DefId(FakeDefId),
DefId(DefId),
ParamIndex(u32),
}

impl From<FakeDefId> for ImplTraitParam {
fn from(did: FakeDefId) -> Self {
impl From<DefId> for ImplTraitParam {
fn from(did: DefId) -> Self {
ImplTraitParam::DefId(did)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ crate struct Cache {
/// When rendering traits, it's often useful to be able to list all
/// implementors of the trait, and this mapping is exactly, that: a mapping
/// of trait ids to the list of known implementors of the trait
crate implementors: FxHashMap<FakeDefId, Vec<Impl>>,
crate implementors: FxHashMap<DefId, Vec<Impl>>,

/// Cache of where external crate documentation can be found.
crate extern_locations: FxHashMap<CrateNum, ExternalLocation>,
Expand Down Expand Up @@ -299,7 +299,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
desc: item
.doc_value()
.map_or_else(String::new, |x| short_markdown_summary(&x.as_str())),
parent: parent.map(FakeDefId::new_real),
parent,
parent_idx: None,
search_type: get_index_search_type(&item, &self.empty_cache, self.tcx),
aliases: item.attrs.get_doc_aliases(),
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/html/render/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::ser::{Serialize, SerializeStruct, Serializer};

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

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

fn get_index_type(clean_type: &clean::Type, cache: &Cache) -> RenderType {
RenderType {
ty: clean_type.def_id_full(cache).map(FakeDefId::new_real),
ty: clean_type.def_id_full(cache),
idx: None,
name: get_index_type_name(clean_type, true).map(|s| s.as_str().to_ascii_lowercase()),
generics: get_generics(clean_type, cache),
Expand Down Expand Up @@ -256,7 +256,7 @@ fn get_generics(clean_type: &clean::Type, cache: &Cache) -> Option<Vec<Generic>>
.filter_map(|t| {
get_index_type_name(t, false).map(|name| Generic {
name: name.as_str().to_ascii_lowercase(),
defid: t.def_id_full(cache).map(FakeDefId::new_real),
defid: t.def_id_full(cache),
idx: None,
})
})
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ crate struct IndexItem {
crate name: String,
crate path: String,
crate desc: String,
crate parent: Option<FakeDefId>,
crate parent: Option<DefId>,
crate parent_idx: Option<usize>,
crate search_type: Option<IndexItemFunctionType>,
crate aliases: Box<[String]>,
Expand All @@ -96,7 +96,7 @@ crate struct IndexItem {
/// A type used for the search index.
#[derive(Debug)]
crate struct RenderType {
ty: Option<FakeDefId>,
ty: Option<DefId>,
idx: Option<usize>,
name: Option<String>,
generics: Option<Vec<Generic>>,
Expand Down Expand Up @@ -128,7 +128,7 @@ impl Serialize for RenderType {
#[derive(Debug)]
crate struct Generic {
name: String,
defid: Option<FakeDefId>,
defid: Option<DefId>,
idx: Option<usize>,
}

Expand Down Expand Up @@ -2137,7 +2137,7 @@ fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean
"</div>",
);

if let Some(implementors) = cx.cache.implementors.get(&it.def_id) {
if let Some(implementors) = cx.cache.implementors.get(&it.def_id.expect_real()) {
let cache = cx.cache();
let mut res = implementors
.iter()
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
// If there are methods directly on this trait object, render them here.
render_assoc_items(w, cx, it, it.def_id.expect_real(), AssocItemRender::All);

if let Some(implementors) = cx.cache.implementors.get(&it.def_id) {
if let Some(implementors) = cx.cache.implementors.get(&it.def_id.expect_real()) {
// 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<Symbol, (DefId, bool)> = FxHashMap::default();
Expand Down
2 changes: 0 additions & 2 deletions src/librustdoc/html/render/write_shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,6 @@ pub(super) fn write_shared(
// Update the list of all implementors for traits
let dst = cx.dst.join("implementors");
for (&did, imps) in &cx.cache.implementors {
let did = did.expect_real();

// Private modules can leak through to this phase of rustdoc, which
// could contain implementations for otherwise private types. In some
// rare cases we could find an implementation for an item which wasn't
Expand Down
5 changes: 2 additions & 3 deletions src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ use rustc_span::Pos;

use rustdoc_json_types::*;

use crate::clean;
use crate::clean::utils::print_const_expr;
use crate::clean::FakeDefId;
use crate::clean::{self, FakeDefId};
use crate::formats::item_type::ItemType;
use crate::json::JsonRenderer;
use std::collections::HashSet;
Expand All @@ -31,7 +30,7 @@ impl JsonRenderer<'_> {
.into_iter()
.flatten()
.filter_map(|clean::ItemLink { link, did, .. }| {
did.map(|did| (link.clone(), from_def_id(did)))
did.map(|did| (link.clone(), from_def_id(did.into())))
})
.collect();
let docs = item.attrs.collapsed_doc_value();
Expand Down
15 changes: 8 additions & 7 deletions src/librustdoc/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ use std::path::PathBuf;
use std::rc::Rc;

use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def_id::DefId;
use rustc_middle::ty::TyCtxt;
use rustc_session::Session;

use rustdoc_json_types as types;

use crate::clean;
use crate::clean::{ExternalCrate, FakeDefId};
use crate::clean::ExternalCrate;
use crate::config::RenderOptions;
use crate::error::Error;
use crate::formats::cache::Cache;
Expand All @@ -42,7 +43,7 @@ impl JsonRenderer<'tcx> {
self.tcx.sess
}

fn get_trait_implementors(&mut self, id: FakeDefId) -> Vec<types::Id> {
fn get_trait_implementors(&mut self, id: DefId) -> Vec<types::Id> {
Rc::clone(&self.cache)
.implementors
.get(&id)
Expand All @@ -59,10 +60,10 @@ impl JsonRenderer<'tcx> {
.unwrap_or_default()
}

fn get_impls(&mut self, id: FakeDefId) -> Vec<types::Id> {
fn get_impls(&mut self, id: DefId) -> Vec<types::Id> {
Rc::clone(&self.cache)
.impls
.get(&id.expect_real())
.get(&id)
.map(|impls| {
impls
.iter()
Expand Down Expand Up @@ -163,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)
t.implementors = self.get_trait_implementors(id.expect_real())
} else if let types::ItemEnum::Struct(ref mut s) = new_item.inner {
s.impls = self.get_impls(id)
s.impls = self.get_impls(id.expect_real())
} else if let types::ItemEnum::Enum(ref mut e) = new_item.inner {
e.impls = self.get_impls(id)
e.impls = self.get_impls(id.expect_real())
}
let removed = self.index.borrow_mut().insert(from_def_id(id), new_item.clone());

Expand Down
Loading

0 comments on commit b68d543

Please sign in to comment.