Skip to content

Commit 60b7fab

Browse files
rustdoc-json-types: Id(String) -> Id(u32)
rustdoc: Add a `TyCtxt` handle to `Cache`
1 parent d571ae8 commit 60b7fab

23 files changed

+506
-480
lines changed

src/librustdoc/clean/inline.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ pub(crate) fn try_inline(
151151
_ => return None,
152152
};
153153

154-
cx.inlined.insert(did.into());
155154
let mut item = crate::clean::generate_item_with_correct_attrs(
156155
cx,
157156
kind,
@@ -160,6 +159,7 @@ pub(crate) fn try_inline(
160159
import_def_id.and_then(|def_id| def_id.as_local()),
161160
None,
162161
);
162+
cx.inlined.insert(item.item_id);
163163
// The visibility needs to reflect the one from the reexport and not from the "source" DefId.
164164
item.inline_stmt_id = import_def_id;
165165
ret.push(item);
@@ -434,7 +434,7 @@ pub(crate) fn build_impl(
434434
attrs: Option<(&[ast::Attribute], Option<DefId>)>,
435435
ret: &mut Vec<clean::Item>,
436436
) {
437-
if !cx.inlined.insert(did.into()) {
437+
if !cx.inlined.insert(ItemId::DefId(did, None)) {
438438
return;
439439
}
440440

@@ -676,7 +676,7 @@ fn build_module_items(
676676
attrs: Box::default(),
677677
// We can use the item's `DefId` directly since the only information ever used
678678
// from it is `DefId.krate`.
679-
item_id: ItemId::DefId(did),
679+
item_id: ItemId::DefId(did, None),
680680
kind: Box::new(clean::ImportItem(clean::Import::new_simple(
681681
item.ident.name,
682682
clean::ImportSource {

src/librustdoc/clean/types.rs

+49-23
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_ast_pretty::pprust;
1111
use rustc_attr::{ConstStability, Deprecation, Stability, StabilityLevel, StableSince};
1212
use rustc_const_eval::const_eval::is_unstable_const_fn;
1313
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
14-
use rustc_hir::def::{CtorKind, DefKind, Res};
14+
use rustc_hir::def::{CtorKind, DefKind, Namespace, Res};
1515
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
1616
use rustc_hir::lang_items::LangItem;
1717
use rustc_hir::{BodyId, Mutability};
@@ -57,20 +57,34 @@ pub(crate) type ItemIdSet = FxHashSet<ItemId>;
5757
#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy)]
5858
pub(crate) enum ItemId {
5959
/// A "normal" item that uses a [`DefId`] for identification.
60-
DefId(DefId),
60+
DefId(DefId, Option<Namespace>),
6161
/// Identifier that is used for auto traits.
6262
Auto { trait_: DefId, for_: DefId },
6363
/// Identifier that is used for blanket implementations.
6464
Blanket { impl_id: DefId, for_: DefId },
6565
}
6666

6767
impl ItemId {
68+
pub(crate) fn type_ns(did: DefId) -> Self {
69+
Self::DefId(did, Some(Namespace::TypeNS))
70+
}
71+
72+
pub(crate) fn opt_from_res<Id>(res: &Res<Id>) -> Option<Self> {
73+
res.opt_def_id().map(|did| Self::DefId(did, res.ns()))
74+
}
75+
76+
#[track_caller]
77+
pub(crate) fn from_res<Id: std::fmt::Debug>(res: &Res<Id>) -> Self {
78+
Self::opt_from_res(res)
79+
.unwrap_or_else(|| panic!("attempted to get the ItemId of a wrong Res: `{res:?}`"))
80+
}
81+
6882
#[inline]
6983
pub(crate) fn is_local(self) -> bool {
7084
match self {
7185
ItemId::Auto { for_: id, .. }
7286
| ItemId::Blanket { for_: id, .. }
73-
| ItemId::DefId(id) => id.is_local(),
87+
| ItemId::DefId(id, _) => id.is_local(),
7488
}
7589
}
7690

@@ -84,7 +98,7 @@ impl ItemId {
8498
#[inline]
8599
pub(crate) fn as_def_id(self) -> Option<DefId> {
86100
match self {
87-
ItemId::DefId(id) => Some(id),
101+
ItemId::DefId(id, _) => Some(id),
88102
_ => None,
89103
}
90104
}
@@ -99,17 +113,11 @@ impl ItemId {
99113
match self {
100114
ItemId::Auto { for_: id, .. }
101115
| ItemId::Blanket { for_: id, .. }
102-
| ItemId::DefId(id) => id.krate,
116+
| ItemId::DefId(id, _) => id.krate,
103117
}
104118
}
105119
}
106120

107-
impl From<DefId> for ItemId {
108-
fn from(id: DefId) -> Self {
109-
Self::DefId(id)
110-
}
111-
}
112-
113121
/// The crate currently being documented.
114122
#[derive(Clone, Debug)]
115123
pub(crate) struct Crate {
@@ -136,6 +144,11 @@ pub(crate) struct ExternalCrate {
136144
impl ExternalCrate {
137145
const LOCAL: Self = Self { crate_num: LOCAL_CRATE };
138146

147+
#[inline]
148+
pub(crate) fn item_id(&self) -> ItemId {
149+
ItemId::type_ns(self.crate_num.as_def_id())
150+
}
151+
139152
#[inline]
140153
pub(crate) fn def_id(&self) -> DefId {
141154
self.crate_num.as_def_id()
@@ -450,8 +463,15 @@ impl Item {
450463
) -> Item {
451464
trace!("name={name:?}, def_id={def_id:?} cfg={cfg:?}");
452465

466+
let ns = if let ItemKind::ImportItem(i) = &kind
467+
&& let ImportKind::Simple(_) = i.kind
468+
{
469+
i.source.path.res.ns()
470+
} else {
471+
ItemType::from(&kind).ns()
472+
};
453473
Item {
454-
item_id: def_id.into(),
474+
item_id: ItemId::DefId(def_id, ns),
455475
kind: Box::new(kind),
456476
name,
457477
attrs,
@@ -466,7 +486,7 @@ impl Item {
466486
let Some(links) = cx.cache().intra_doc_links.get(&self.item_id) else { return vec![] };
467487
links
468488
.iter()
469-
.filter_map(|ItemLink { link: s, link_text, page_id: id, ref fragment }| {
489+
.filter_map(|ItemLink { link: s, link_text, page_id: id, ref fragment, .. }| {
470490
debug!(?id);
471491
if let Ok((mut href, ..)) = href(*id, cx) {
472492
debug!(?href);
@@ -491,7 +511,7 @@ impl Item {
491511
/// This is used for generating summary text, which does not include
492512
/// the link text, but does need to know which `[]`-bracketed names
493513
/// are actually links.
494-
pub(crate) fn link_names(&self, cache: &Cache) -> Vec<RenderedLink> {
514+
pub(crate) fn link_names(&self, cache: &Cache<'_>) -> Vec<RenderedLink> {
495515
let Some(links) = cache.intra_doc_links.get(&self.item_id) else {
496516
return vec![];
497517
};
@@ -675,7 +695,7 @@ impl Item {
675695
let def_id = match self.item_id {
676696
// Anything but DefId *shouldn't* matter, but return a reasonable value anyway.
677697
ItemId::Auto { .. } | ItemId::Blanket { .. } => return None,
678-
ItemId::DefId(def_id) => def_id,
698+
ItemId::DefId(def_id, _) => def_id,
679699
};
680700

681701
match *self.kind {
@@ -714,17 +734,13 @@ impl Item {
714734
Some(tcx.visibility(def_id))
715735
}
716736

717-
pub(crate) fn attributes(
718-
&self,
719-
tcx: TyCtxt<'_>,
720-
cache: &Cache,
721-
keep_as_is: bool,
722-
) -> Vec<String> {
737+
pub(crate) fn attributes(&self, cache: &Cache<'_>, keep_as_is: bool) -> Vec<String> {
723738
const ALLOWED_ATTRIBUTES: &[Symbol] =
724739
&[sym::export_name, sym::link_section, sym::no_mangle, sym::non_exhaustive];
725740

726741
use rustc_abi::IntegerType;
727742

743+
let tcx = cache.tcx;
728744
let mut attrs: Vec<String> = self
729745
.attrs
730746
.other_attrs
@@ -1113,6 +1129,8 @@ pub(crate) struct ItemLink {
11131129
/// linked to. This will be different to `item_id` on item's that don't
11141130
/// have their own page, such as struct fields and enum variants.
11151131
pub(crate) page_id: DefId,
1132+
/// The type of the item pointed to by this link
1133+
pub(crate) item_type: ItemType,
11161134
/// The url fragment to append to the link
11171135
pub(crate) fragment: Option<UrlFragment>,
11181136
}
@@ -1532,7 +1550,7 @@ impl Type {
15321550
///
15331551
/// An owned type is also the same as its borrowed variants (this is commutative),
15341552
/// but `&T` is not the same as `&mut T`.
1535-
pub(crate) fn is_doc_subtype_of(&self, other: &Self, cache: &Cache) -> bool {
1553+
pub(crate) fn is_doc_subtype_of(&self, other: &Self, cache: &Cache<'_>) -> bool {
15361554
// Strip the references so that it can compare the actual types, unless both are references.
15371555
// If both are references, leave them alone and compare the mutabilities later.
15381556
let (self_cleared, other_cleared) = if !self.is_borrowed_ref() || !other.is_borrowed_ref() {
@@ -1668,7 +1686,7 @@ impl Type {
16681686
/// Use this method to get the [DefId] of a [clean] AST node, including [PrimitiveType]s.
16691687
///
16701688
/// [clean]: crate::clean
1671-
pub(crate) fn def_id(&self, cache: &Cache) -> Option<DefId> {
1689+
pub(crate) fn def_id(&self, cache: &Cache<'_>) -> Option<DefId> {
16721690
let t: PrimitiveType = match *self {
16731691
Type::Path { ref path } => return Some(path.def_id()),
16741692
DynTrait(ref bounds, _) => return bounds.first().map(|b| b.trait_.def_id()),
@@ -1692,6 +1710,10 @@ impl Type {
16921710
};
16931711
Primitive(t).def_id(cache)
16941712
}
1713+
1714+
pub(crate) fn item_id(&self, cache: &Cache<'_>) -> Option<ItemId> {
1715+
self.def_id(cache).map(ItemId::type_ns)
1716+
}
16951717
}
16961718

16971719
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
@@ -2164,6 +2186,10 @@ pub(crate) struct Path {
21642186
}
21652187

21662188
impl Path {
2189+
pub(crate) fn item_id(&self) -> ItemId {
2190+
ItemId::from_res(&self.res)
2191+
}
2192+
21672193
pub(crate) fn def_id(&self) -> DefId {
21682194
self.res.def_id()
21692195
}

src/librustdoc/core.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ use crate::passes::Condition::*;
3535
use crate::passes::{self};
3636

3737
pub(crate) struct DocContext<'tcx> {
38+
// FIXME: remove me from here and access me via `self.cache` instead
39+
// And anywhere a Cache & a TyCtxt are expected, remove the TyCtxt param
3840
pub(crate) tcx: TyCtxt<'tcx>,
3941
/// Used for normalization.
4042
///
@@ -62,7 +64,7 @@ pub(crate) struct DocContext<'tcx> {
6264
/// The options given to rustdoc that could be relevant to a pass.
6365
pub(crate) render_options: RenderOptions,
6466
/// This same cache is used throughout rustdoc, including in [`crate::html::render`].
65-
pub(crate) cache: Cache,
67+
pub(crate) cache: Cache<'tcx>,
6668
/// Used by [`clean::inline`] to tell if an item has already been inlined.
6769
pub(crate) inlined: FxHashSet<ItemId>,
6870
/// Used by `calculate_doc_coverage`.
@@ -115,7 +117,7 @@ impl<'tcx> DocContext<'tcx> {
115117
/// (This avoids a slice-index-out-of-bounds panic.)
116118
pub(crate) fn as_local_hir_id(tcx: TyCtxt<'_>, item_id: ItemId) -> Option<HirId> {
117119
match item_id {
118-
ItemId::DefId(real_id) => {
120+
ItemId::DefId(real_id, _) => {
119121
real_id.as_local().map(|def_id| tcx.local_def_id_to_hir_id(def_id))
120122
}
121123
// FIXME: Can this be `Some` for `Auto` or `Blanket`?
@@ -301,12 +303,12 @@ pub(crate) fn create_config(
301303
}
302304
}
303305

304-
pub(crate) fn run_global_ctxt(
305-
tcx: TyCtxt<'_>,
306+
pub(crate) fn run_global_ctxt<'tcx>(
307+
tcx: TyCtxt<'tcx>,
306308
show_coverage: bool,
307309
render_options: RenderOptions,
308310
output_format: OutputFormat,
309-
) -> Result<(clean::Crate, RenderOptions, Cache), ErrorGuaranteed> {
311+
) -> Result<(clean::Crate, RenderOptions, Cache<'tcx>), ErrorGuaranteed> {
310312
// Certain queries assume that some checks were run elsewhere
311313
// (see https://github.com/rust-lang/rust/pull/73566#issuecomment-656954425),
312314
// so type-check everything other than function bodies in this crate before running lints.
@@ -343,7 +345,7 @@ pub(crate) fn run_global_ctxt(
343345
impl_trait_bounds: Default::default(),
344346
generated_synthetics: Default::default(),
345347
auto_traits,
346-
cache: Cache::new(render_options.document_private, render_options.document_hidden),
348+
cache: Cache::new(tcx, render_options.document_private, render_options.document_hidden),
347349
inlined: FxHashSet::default(),
348350
output_format,
349351
render_options,

0 commit comments

Comments
 (0)