Skip to content

Commit 97011eb

Browse files
committed
Address review comments.
Go back to CRATE_DEF_INDEX Minor niceness improvements Don't output hidden items Remove striped items from fields Add $TEST_BASE_DIR Small catch
1 parent 35cc349 commit 97011eb

File tree

9 files changed

+80
-103
lines changed

9 files changed

+80
-103
lines changed

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2276,7 +2276,7 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
22762276
name: None,
22772277
attrs: self.attrs.clean(cx),
22782278
source: self.span.clean(cx),
2279-
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
2279+
def_id: DefId::local(CRATE_DEF_INDEX),
22802280
visibility: self.vis.clean(cx),
22812281
stability: None,
22822282
deprecation: None,

src/librustdoc/json/conversions.rs

+39-37
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,44 @@ use crate::doctree;
1212
use crate::formats::item_type::ItemType;
1313
use crate::json::types::*;
1414

15-
impl From<clean::Item> for Item {
15+
impl From<clean::Item> for Option<Item> {
1616
fn from(item: clean::Item) -> Self {
1717
let item_type = ItemType::from(&item);
1818
let clean::Item {
1919
source,
2020
name,
2121
attrs,
22-
kind: inner,
22+
kind,
2323
visibility,
2424
def_id,
2525
stability: _,
2626
deprecation,
2727
} = item;
28-
Item {
29-
id: def_id.into(),
30-
crate_id: def_id.krate.as_u32(),
31-
name,
32-
stripped: match inner {
33-
clean::StrippedItem(_) => true,
34-
_ => false,
35-
},
36-
source: source.into(),
37-
visibility: visibility.into(),
38-
docs: attrs.collapsed_doc_value().unwrap_or_default(),
39-
links: attrs
40-
.links
41-
.into_iter()
42-
.filter_map(|clean::ItemLink { link, did, .. }| did.map(|did| (link, did.into())))
43-
.collect(),
44-
attrs: attrs
45-
.other_attrs
46-
.iter()
47-
.map(rustc_ast_pretty::pprust::attribute_to_string)
48-
.collect(),
49-
deprecation: deprecation.map(Into::into),
50-
kind: item_type.into(),
51-
inner: inner.into(),
28+
match kind {
29+
clean::StrippedItem(_) => None,
30+
_ => Some(Item {
31+
id: def_id.into(),
32+
crate_id: def_id.krate.as_u32(),
33+
name,
34+
source: source.into(),
35+
visibility: visibility.into(),
36+
docs: attrs.collapsed_doc_value().unwrap_or_default(),
37+
links: attrs
38+
.links
39+
.into_iter()
40+
.filter_map(|clean::ItemLink { link, did, .. }| {
41+
did.map(|did| (link, did.into()))
42+
})
43+
.collect(),
44+
attrs: attrs
45+
.other_attrs
46+
.iter()
47+
.map(rustc_ast_pretty::pprust::attribute_to_string)
48+
.collect(),
49+
deprecation: deprecation.map(Into::into),
50+
kind: item_type.into(),
51+
inner: kind.into(),
52+
}),
5253
}
5354
}
5455
}
@@ -194,10 +195,7 @@ impl From<clean::ItemKind> for ItemEnum {
194195

195196
impl From<clean::Module> for Module {
196197
fn from(module: clean::Module) -> Self {
197-
Module {
198-
is_crate: module.is_crate,
199-
items: module.items.into_iter().map(|i| i.def_id.into()).collect(),
200-
}
198+
Module { is_crate: module.is_crate, items: ids(module.items) }
201199
}
202200
}
203201

@@ -208,7 +206,7 @@ impl From<clean::Struct> for Struct {
208206
struct_type: struct_type.into(),
209207
generics: generics.into(),
210208
fields_stripped,
211-
fields: fields.into_iter().map(|i| i.def_id.into()).collect(),
209+
fields: ids(fields),
212210
impls: Vec::new(), // Added in JsonRenderer::item
213211
}
214212
}
@@ -221,7 +219,7 @@ impl From<clean::Union> for Struct {
221219
struct_type: struct_type.into(),
222220
generics: generics.into(),
223221
fields_stripped,
224-
fields: fields.into_iter().map(|i| i.def_id.into()).collect(),
222+
fields: ids(fields),
225223
impls: Vec::new(), // Added in JsonRenderer::item
226224
}
227225
}
@@ -407,7 +405,7 @@ impl From<clean::Trait> for Trait {
407405
Trait {
408406
is_auto,
409407
is_unsafe: unsafety == rustc_hir::Unsafety::Unsafe,
410-
items: items.into_iter().map(|i| i.def_id.into()).collect(),
408+
items: ids(items),
411409
generics: generics.into(),
412410
bounds: bounds.into_iter().map(Into::into).collect(),
413411
implementors: Vec::new(), // Added in JsonRenderer::item
@@ -434,7 +432,7 @@ impl From<clean::Impl> for Impl {
434432
provided_trait_methods: provided_trait_methods.into_iter().collect(),
435433
trait_: trait_.map(Into::into),
436434
for_: for_.into(),
437-
items: items.into_iter().map(|i| i.def_id.into()).collect(),
435+
items: ids(items),
438436
negative: polarity == Some(clean::ImplPolarity::Negative),
439437
synthetic,
440438
blanket_impl: blanket_impl.map(Into::into),
@@ -460,7 +458,7 @@ impl From<clean::Enum> for Enum {
460458
Enum {
461459
generics: generics.into(),
462460
variants_stripped,
463-
variants: variants.into_iter().map(|i| i.def_id.into()).collect(),
461+
variants: ids(variants),
464462
impls: Vec::new(), // Added in JsonRenderer::item
465463
}
466464
}
@@ -473,7 +471,7 @@ impl From<clean::VariantStruct> for Struct {
473471
struct_type: struct_type.into(),
474472
generics: Default::default(),
475473
fields_stripped,
476-
fields: fields.into_iter().map(|i| i.def_id.into()).collect(),
474+
fields: ids(fields),
477475
impls: Vec::new(),
478476
}
479477
}
@@ -485,7 +483,7 @@ impl From<clean::Variant> for Variant {
485483
match variant.kind {
486484
CLike => Variant::Plain,
487485
Tuple(t) => Variant::Tuple(t.into_iter().map(Into::into).collect()),
488-
Struct(s) => Variant::Struct(s.fields.into_iter().map(|i| i.def_id.into()).collect()),
486+
Struct(s) => Variant::Struct(ids(s.fields)),
489487
}
490488
}
491489
}
@@ -594,3 +592,7 @@ impl From<ItemType> for ItemKind {
594592
}
595593
}
596594
}
595+
596+
fn ids(items: impl IntoIterator<Item = clean::Item>) -> Vec<Id> {
597+
items.into_iter().filter(|x| !x.is_stripped()).map(|i| i.def_id.into()).collect()
598+
}

src/librustdoc/json/mod.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ impl JsonRenderer {
9999
.0
100100
.last()
101101
.map(Clone::clone),
102-
stripped: false,
103102
visibility: types::Visibility::Public,
104103
kind: types::ItemKind::Trait,
105104
inner: types::ItemEnum::TraitItem(trait_item.clone().into()),
@@ -144,16 +143,17 @@ impl FormatRenderer for JsonRenderer {
144143
item.kind.inner_items().for_each(|i| self.item(i.clone(), cache).unwrap());
145144

146145
let id = item.def_id;
147-
let mut new_item: types::Item = item.into();
148-
if let types::ItemEnum::TraitItem(ref mut t) = new_item.inner {
149-
t.implementors = self.get_trait_implementors(id, cache)
150-
} else if let types::ItemEnum::StructItem(ref mut s) = new_item.inner {
151-
s.impls = self.get_impls(id, cache)
152-
} else if let types::ItemEnum::EnumItem(ref mut e) = new_item.inner {
153-
e.impls = self.get_impls(id, cache)
146+
if let Some(mut new_item) = item.into(): Option<types::Item> {
147+
if let types::ItemEnum::TraitItem(ref mut t) = new_item.inner {
148+
t.implementors = self.get_trait_implementors(id, cache)
149+
} else if let types::ItemEnum::StructItem(ref mut s) = new_item.inner {
150+
s.impls = self.get_impls(id, cache)
151+
} else if let types::ItemEnum::EnumItem(ref mut e) = new_item.inner {
152+
e.impls = self.get_impls(id, cache)
153+
}
154+
self.index.borrow_mut().insert(id.into(), new_item);
154155
}
155156

156-
self.index.borrow_mut().insert(id.into(), new_item);
157157
Ok(())
158158
}
159159

src/librustdoc/json/types.rs

-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ pub struct Item {
6262
pub crate_id: u32,
6363
/// Some items such as impls don't have names.
6464
pub name: Option<String>,
65-
/// Whether this item is meant to be omitted from the generated documentation due to `#doc(hidden)`,
66-
/// because it is private, or because it was inlined.
67-
pub stripped: bool,
6865
/// The source location of this item (absent if it came from a macro expansion or inline
6966
/// assembly).
7067
pub source: Option<Span>,

src/librustdoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![feature(crate_visibility_modifier)]
1515
#![feature(never_type)]
1616
#![feature(once_cell)]
17+
#![feature(type_ascription)]
1718
#![recursion_limit = "256"]
1819

1920
#[macro_use]

src/test/rustdoc-json/Makefile

-6
This file was deleted.

src/test/rustdoc-json/compare.py

+8-13
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,18 @@ def check_subset(expected_main, actual_main, base_dir):
3434
def _check_subset(expected, actual, trace):
3535
expected_type = type(expected)
3636
actual_type = type(actual)
37+
38+
if actual_type is str:
39+
actual = actual.replace(base_dir, "$TEST_BASE_DIR")
40+
3741
if expected_type is not actual_type:
3842
raise SubsetException(
3943
"expected type `{}`, got `{}`".format(expected_type, actual_type), trace
4044
)
41-
if expected_type in (str, int, bool) and expected != actual:
42-
if expected_type == str and actual.startswith(base_dir):
43-
if actual.replace(base_dir + "/", "") != expected:
44-
raise SubsetException(
45-
"expected `{}`, got: `{}`".format(
46-
expected, actual.replace(base_dir + "/", "")
47-
),
48-
trace,
49-
)
50-
else:
51-
raise SubsetException(
52-
"expected `{}`, got: `{}`".format(expected, actual), trace
53-
)
45+
46+
47+
if expected_type in (int, bool, str) and expected != actual:
48+
raise SubsetException("expected `{}`, got: `{}`".format(expected, actual), trace)
5449
if expected_type is dict:
5550
for key in expected:
5651
if key not in actual:

src/test/rustdoc-json/structs.expected

+15-27
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"crate_id": 0,
88
"name": "Unit",
99
"source": {
10-
"filename": "structs.rs",
10+
"filename": "$TEST_BASE_DIR/structs.rs",
1111
"begin": [
1212
7,
1313
0
@@ -37,7 +37,7 @@
3737
"crate_id": 0,
3838
"name": "1",
3939
"source": {
40-
"filename": "structs.rs",
40+
"filename": "$TEST_BASE_DIR/structs.rs",
4141
"begin": [
4242
5,
4343
22
@@ -72,7 +72,7 @@
7272
"crate_id": 0,
7373
"name": "stuff",
7474
"source": {
75-
"filename": "structs.rs",
75+
"filename": "$TEST_BASE_DIR/structs.rs",
7676
"begin": [
7777
15,
7878
4
@@ -114,7 +114,7 @@
114114
"crate_id": 0,
115115
"name": "WithPrimitives",
116116
"source": {
117-
"filename": "structs.rs",
117+
"filename": "$TEST_BASE_DIR/structs.rs",
118118
"begin": [
119119
9,
120120
0
@@ -141,18 +141,14 @@
141141
],
142142
"where_predicates": []
143143
},
144-
"fields_stripped": true,
145-
"fields": [
146-
"0:13",
147-
"0:14"
148-
]
144+
"fields_stripped": true
149145
}
150146
},
151147
"0:14": {
152148
"crate_id": 0,
153149
"name": "s",
154150
"source": {
155-
"filename": "structs.rs",
151+
"filename": "$TEST_BASE_DIR/structs.rs",
156152
"begin": [
157153
11,
158154
4
@@ -184,7 +180,7 @@
184180
"crate_id": 0,
185181
"name": "things",
186182
"source": {
187-
"filename": "structs.rs",
183+
"filename": "$TEST_BASE_DIR/structs.rs",
188184
"begin": [
189185
16,
190186
4
@@ -232,7 +228,7 @@
232228
"crate_id": 0,
233229
"name": "WithGenerics",
234230
"source": {
235-
"filename": "structs.rs",
231+
"filename": "$TEST_BASE_DIR/structs.rs",
236232
"begin": [
237233
14,
238234
0
@@ -273,18 +269,14 @@
273269
],
274270
"where_predicates": []
275271
},
276-
"fields_stripped": true,
277-
"fields": [
278-
"0:18",
279-
"0:19"
280-
]
272+
"fields_stripped": true
281273
}
282274
},
283275
"0:0": {
284276
"crate_id": 0,
285277
"name": "structs",
286278
"source": {
287-
"filename": "structs.rs",
279+
"filename": "$TEST_BASE_DIR/structs.rs",
288280
"begin": [
289281
1,
290282
0
@@ -315,7 +307,7 @@
315307
"crate_id": 0,
316308
"name": "num",
317309
"source": {
318-
"filename": "structs.rs",
310+
"filename": "$TEST_BASE_DIR/structs.rs",
319311
"begin": [
320312
10,
321313
4
@@ -340,7 +332,7 @@
340332
"crate_id": 0,
341333
"name": "Tuple",
342334
"source": {
343-
"filename": "structs.rs",
335+
"filename": "$TEST_BASE_DIR/structs.rs",
344336
"begin": [
345337
5,
346338
0
@@ -362,18 +354,14 @@
362354
"params": [],
363355
"where_predicates": []
364356
},
365-
"fields_stripped": true,
366-
"fields": [
367-
"0:7",
368-
"0:8"
369-
]
357+
"fields_stripped": true
370358
}
371359
},
372360
"0:4": {
373361
"crate_id": 0,
374362
"name": "PlainEmpty",
375363
"source": {
376-
"filename": "structs.rs",
364+
"filename": "$TEST_BASE_DIR/structs.rs",
377365
"begin": [
378366
3,
379367
0
@@ -403,7 +391,7 @@
403391
"crate_id": 0,
404392
"name": "0",
405393
"source": {
406-
"filename": "structs.rs",
394+
"filename": "$TEST_BASE_DIR/structs.rs",
407395
"begin": [
408396
5,
409397
17

0 commit comments

Comments
 (0)