Skip to content

Commit 1c1c888

Browse files
committed
rustdoc-json: Restructure and document FullItemId
1 parent a5de4dc commit 1c1c888

File tree

1 file changed

+50
-11
lines changed

1 file changed

+50
-11
lines changed

src/librustdoc/json/ids.rs

+50-11
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,67 @@ use rustdoc_json_types::{self as types, Id}; // FIXME: Consistant.
77
use super::JsonRenderer;
88
use crate::clean::{self, ItemId};
99

10+
pub(super) type IdInterner = FxHashMap<FullItemId, types::Id>;
11+
1012
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
13+
/// An uninterned id.
14+
///
15+
/// One of these coresponds to every:
16+
/// 1. [`rustdoc_json_types::Item`].
17+
/// 2. [`rustdoc_json_types::Id`] transitivly (as each `Item` has an `Id`).
18+
///
19+
/// It's *broadly* equivalent to a [`DefId`], but needs slightly more information
20+
/// to fully disambiguate items, because sometimes we choose to split a single HIR
21+
/// item into multiple JSON items, or have items with no coresponding HIR item.
1122
pub(super) struct FullItemId {
23+
id: SingleItemId,
24+
/// An extra DefId for auto-trait-impls or blanket-impls. These don't have DefId's
25+
/// as they're synthesized by rustdoc.
26+
extra: Option<DefId>,
27+
}
28+
29+
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
30+
struct SingleItemId {
31+
/// Almost all of the "identification" comes from the `DefId`, other fields
32+
/// are only needed in weird cases.
1233
def_id: DefId,
34+
/// Needed for `rustc_doc_primitive` modules.
35+
///
36+
/// For these, 1 DefId is used for both the primitive and the fake-module
37+
/// that holds it's docs.
38+
///
39+
/// N.B. This only matters when documenting the standard library with
40+
/// `--document-private-items`. Maybe we should delete that module, and
41+
/// remove this.
1342
name: Option<Symbol>,
14-
/// Used to distinguish imports of different items with the same name
15-
extra: Option<types::Id>,
43+
/// Used to distinguish imports of different items with the same name.
44+
///
45+
/// ```rust
46+
/// mod module {
47+
/// pub struct Foo {}; // Exists in type namespace
48+
/// pub fn Foo(){} // Exists in value namespace
49+
/// }
50+
///
51+
/// pub use module::Foo; // Imports both items
52+
/// ```
53+
///
54+
/// In HIR, the `pub use` is just 1 item, but in rustdoc-json it's 2, so
55+
/// we need to disambiguate.
56+
imported_id: Option<types::Id>,
1657
}
1758

18-
pub(super) type IdInterner = FxHashMap<(FullItemId, Option<FullItemId>), types::Id>;
19-
2059
impl JsonRenderer<'_> {
2160
pub(crate) fn id_from_item_default(&self, item_id: ItemId) -> Id {
2261
self.id_from_item_inner(item_id, None, None)
2362
}
2463

25-
pub(crate) fn id_from_item_inner(
64+
fn id_from_item_inner(
2665
&self,
2766
item_id: ItemId,
2867
name: Option<Symbol>,
29-
extra: Option<Id>,
68+
imported_id: Option<Id>,
3069
) -> Id {
31-
let make_part = |def_id: DefId, name: Option<Symbol>, extra: Option<Id>| {
70+
let make_part = |def_id: DefId| {
3271
let name = match name {
3372
Some(name) => Some(name),
3473
None => {
@@ -48,16 +87,16 @@ impl JsonRenderer<'_> {
4887
}
4988
};
5089

51-
FullItemId { def_id, name, extra }
90+
SingleItemId { def_id, name, imported_id }
5291
};
5392

5493
let key = match item_id {
55-
ItemId::DefId(did) => (make_part(did, name, extra), None),
94+
ItemId::DefId(did) => FullItemId { id: make_part(did), extra: None },
5695
ItemId::Blanket { for_, impl_id } => {
57-
(make_part(impl_id, None, None), Some(make_part(for_, name, extra)))
96+
FullItemId { id: make_part(for_), extra: Some(impl_id) }
5897
}
5998
ItemId::Auto { for_, trait_ } => {
60-
(make_part(trait_, None, None), Some(make_part(for_, name, extra)))
99+
FullItemId { id: make_part(for_), extra: Some(trait_) }
61100
}
62101
};
63102

0 commit comments

Comments
 (0)