Skip to content

Commit

Permalink
Address review comments.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
aDotInTheVoid committed Nov 30, 2020
1 parent 35cc349 commit 97011eb
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 103 deletions.
2 changes: 1 addition & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2276,7 +2276,7 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
name: None,
attrs: self.attrs.clean(cx),
source: self.span.clean(cx),
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
def_id: DefId::local(CRATE_DEF_INDEX),
visibility: self.vis.clean(cx),
stability: None,
deprecation: None,
Expand Down
76 changes: 39 additions & 37 deletions src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,44 @@ use crate::doctree;
use crate::formats::item_type::ItemType;
use crate::json::types::*;

impl From<clean::Item> for Item {
impl From<clean::Item> for Option<Item> {
fn from(item: clean::Item) -> Self {
let item_type = ItemType::from(&item);
let clean::Item {
source,
name,
attrs,
kind: inner,
kind,
visibility,
def_id,
stability: _,
deprecation,
} = item;
Item {
id: def_id.into(),
crate_id: def_id.krate.as_u32(),
name,
stripped: match inner {
clean::StrippedItem(_) => true,
_ => false,
},
source: source.into(),
visibility: visibility.into(),
docs: attrs.collapsed_doc_value().unwrap_or_default(),
links: attrs
.links
.into_iter()
.filter_map(|clean::ItemLink { link, did, .. }| did.map(|did| (link, did.into())))
.collect(),
attrs: attrs
.other_attrs
.iter()
.map(rustc_ast_pretty::pprust::attribute_to_string)
.collect(),
deprecation: deprecation.map(Into::into),
kind: item_type.into(),
inner: inner.into(),
match kind {
clean::StrippedItem(_) => None,
_ => Some(Item {
id: def_id.into(),
crate_id: def_id.krate.as_u32(),
name,
source: source.into(),
visibility: visibility.into(),
docs: attrs.collapsed_doc_value().unwrap_or_default(),
links: attrs
.links
.into_iter()
.filter_map(|clean::ItemLink { link, did, .. }| {
did.map(|did| (link, did.into()))
})
.collect(),
attrs: attrs
.other_attrs
.iter()
.map(rustc_ast_pretty::pprust::attribute_to_string)
.collect(),
deprecation: deprecation.map(Into::into),
kind: item_type.into(),
inner: kind.into(),
}),
}
}
}
Expand Down Expand Up @@ -194,10 +195,7 @@ impl From<clean::ItemKind> for ItemEnum {

impl From<clean::Module> for Module {
fn from(module: clean::Module) -> Self {
Module {
is_crate: module.is_crate,
items: module.items.into_iter().map(|i| i.def_id.into()).collect(),
}
Module { is_crate: module.is_crate, items: ids(module.items) }
}
}

Expand All @@ -208,7 +206,7 @@ impl From<clean::Struct> for Struct {
struct_type: struct_type.into(),
generics: generics.into(),
fields_stripped,
fields: fields.into_iter().map(|i| i.def_id.into()).collect(),
fields: ids(fields),
impls: Vec::new(), // Added in JsonRenderer::item
}
}
Expand All @@ -221,7 +219,7 @@ impl From<clean::Union> for Struct {
struct_type: struct_type.into(),
generics: generics.into(),
fields_stripped,
fields: fields.into_iter().map(|i| i.def_id.into()).collect(),
fields: ids(fields),
impls: Vec::new(), // Added in JsonRenderer::item
}
}
Expand Down Expand Up @@ -407,7 +405,7 @@ impl From<clean::Trait> for Trait {
Trait {
is_auto,
is_unsafe: unsafety == rustc_hir::Unsafety::Unsafe,
items: items.into_iter().map(|i| i.def_id.into()).collect(),
items: ids(items),
generics: generics.into(),
bounds: bounds.into_iter().map(Into::into).collect(),
implementors: Vec::new(), // Added in JsonRenderer::item
Expand All @@ -434,7 +432,7 @@ impl From<clean::Impl> for Impl {
provided_trait_methods: provided_trait_methods.into_iter().collect(),
trait_: trait_.map(Into::into),
for_: for_.into(),
items: items.into_iter().map(|i| i.def_id.into()).collect(),
items: ids(items),
negative: polarity == Some(clean::ImplPolarity::Negative),
synthetic,
blanket_impl: blanket_impl.map(Into::into),
Expand All @@ -460,7 +458,7 @@ impl From<clean::Enum> for Enum {
Enum {
generics: generics.into(),
variants_stripped,
variants: variants.into_iter().map(|i| i.def_id.into()).collect(),
variants: ids(variants),
impls: Vec::new(), // Added in JsonRenderer::item
}
}
Expand All @@ -473,7 +471,7 @@ impl From<clean::VariantStruct> for Struct {
struct_type: struct_type.into(),
generics: Default::default(),
fields_stripped,
fields: fields.into_iter().map(|i| i.def_id.into()).collect(),
fields: ids(fields),
impls: Vec::new(),
}
}
Expand All @@ -485,7 +483,7 @@ impl From<clean::Variant> for Variant {
match variant.kind {
CLike => Variant::Plain,
Tuple(t) => Variant::Tuple(t.into_iter().map(Into::into).collect()),
Struct(s) => Variant::Struct(s.fields.into_iter().map(|i| i.def_id.into()).collect()),
Struct(s) => Variant::Struct(ids(s.fields)),
}
}
}
Expand Down Expand Up @@ -594,3 +592,7 @@ impl From<ItemType> for ItemKind {
}
}
}

fn ids(items: impl IntoIterator<Item = clean::Item>) -> Vec<Id> {
items.into_iter().filter(|x| !x.is_stripped()).map(|i| i.def_id.into()).collect()
}
18 changes: 9 additions & 9 deletions src/librustdoc/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ impl JsonRenderer {
.0
.last()
.map(Clone::clone),
stripped: false,
visibility: types::Visibility::Public,
kind: types::ItemKind::Trait,
inner: types::ItemEnum::TraitItem(trait_item.clone().into()),
Expand Down Expand Up @@ -144,16 +143,17 @@ impl FormatRenderer for JsonRenderer {
item.kind.inner_items().for_each(|i| self.item(i.clone(), cache).unwrap());

let id = item.def_id;
let mut new_item: types::Item = item.into();
if let types::ItemEnum::TraitItem(ref mut t) = new_item.inner {
t.implementors = self.get_trait_implementors(id, cache)
} else if let types::ItemEnum::StructItem(ref mut s) = new_item.inner {
s.impls = self.get_impls(id, cache)
} else if let types::ItemEnum::EnumItem(ref mut e) = new_item.inner {
e.impls = self.get_impls(id, cache)
if let Some(mut new_item) = item.into(): Option<types::Item> {
if let types::ItemEnum::TraitItem(ref mut t) = new_item.inner {
t.implementors = self.get_trait_implementors(id, cache)
} else if let types::ItemEnum::StructItem(ref mut s) = new_item.inner {
s.impls = self.get_impls(id, cache)
} else if let types::ItemEnum::EnumItem(ref mut e) = new_item.inner {
e.impls = self.get_impls(id, cache)
}
self.index.borrow_mut().insert(id.into(), new_item);
}

self.index.borrow_mut().insert(id.into(), new_item);
Ok(())
}

Expand Down
3 changes: 0 additions & 3 deletions src/librustdoc/json/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ pub struct Item {
pub crate_id: u32,
/// Some items such as impls don't have names.
pub name: Option<String>,
/// Whether this item is meant to be omitted from the generated documentation due to `#doc(hidden)`,
/// because it is private, or because it was inlined.
pub stripped: bool,
/// The source location of this item (absent if it came from a macro expansion or inline
/// assembly).
pub source: Option<Span>,
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#![feature(crate_visibility_modifier)]
#![feature(never_type)]
#![feature(once_cell)]
#![feature(type_ascription)]
#![recursion_limit = "256"]

#[macro_use]
Expand Down
6 changes: 0 additions & 6 deletions src/test/rustdoc-json/Makefile

This file was deleted.

21 changes: 8 additions & 13 deletions src/test/rustdoc-json/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,18 @@ def check_subset(expected_main, actual_main, base_dir):
def _check_subset(expected, actual, trace):
expected_type = type(expected)
actual_type = type(actual)

if actual_type is str:
actual = actual.replace(base_dir, "$TEST_BASE_DIR")

if expected_type is not actual_type:
raise SubsetException(
"expected type `{}`, got `{}`".format(expected_type, actual_type), trace
)
if expected_type in (str, int, bool) and expected != actual:
if expected_type == str and actual.startswith(base_dir):
if actual.replace(base_dir + "/", "") != expected:
raise SubsetException(
"expected `{}`, got: `{}`".format(
expected, actual.replace(base_dir + "/", "")
),
trace,
)
else:
raise SubsetException(
"expected `{}`, got: `{}`".format(expected, actual), trace
)


if expected_type in (int, bool, str) and expected != actual:
raise SubsetException("expected `{}`, got: `{}`".format(expected, actual), trace)
if expected_type is dict:
for key in expected:
if key not in actual:
Expand Down
42 changes: 15 additions & 27 deletions src/test/rustdoc-json/structs.expected
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"crate_id": 0,
"name": "Unit",
"source": {
"filename": "structs.rs",
"filename": "$TEST_BASE_DIR/structs.rs",
"begin": [
7,
0
Expand Down Expand Up @@ -37,7 +37,7 @@
"crate_id": 0,
"name": "1",
"source": {
"filename": "structs.rs",
"filename": "$TEST_BASE_DIR/structs.rs",
"begin": [
5,
22
Expand Down Expand Up @@ -72,7 +72,7 @@
"crate_id": 0,
"name": "stuff",
"source": {
"filename": "structs.rs",
"filename": "$TEST_BASE_DIR/structs.rs",
"begin": [
15,
4
Expand Down Expand Up @@ -114,7 +114,7 @@
"crate_id": 0,
"name": "WithPrimitives",
"source": {
"filename": "structs.rs",
"filename": "$TEST_BASE_DIR/structs.rs",
"begin": [
9,
0
Expand All @@ -141,18 +141,14 @@
],
"where_predicates": []
},
"fields_stripped": true,
"fields": [
"0:13",
"0:14"
]
"fields_stripped": true
}
},
"0:14": {
"crate_id": 0,
"name": "s",
"source": {
"filename": "structs.rs",
"filename": "$TEST_BASE_DIR/structs.rs",
"begin": [
11,
4
Expand Down Expand Up @@ -184,7 +180,7 @@
"crate_id": 0,
"name": "things",
"source": {
"filename": "structs.rs",
"filename": "$TEST_BASE_DIR/structs.rs",
"begin": [
16,
4
Expand Down Expand Up @@ -232,7 +228,7 @@
"crate_id": 0,
"name": "WithGenerics",
"source": {
"filename": "structs.rs",
"filename": "$TEST_BASE_DIR/structs.rs",
"begin": [
14,
0
Expand Down Expand Up @@ -273,18 +269,14 @@
],
"where_predicates": []
},
"fields_stripped": true,
"fields": [
"0:18",
"0:19"
]
"fields_stripped": true
}
},
"0:0": {
"crate_id": 0,
"name": "structs",
"source": {
"filename": "structs.rs",
"filename": "$TEST_BASE_DIR/structs.rs",
"begin": [
1,
0
Expand Down Expand Up @@ -315,7 +307,7 @@
"crate_id": 0,
"name": "num",
"source": {
"filename": "structs.rs",
"filename": "$TEST_BASE_DIR/structs.rs",
"begin": [
10,
4
Expand All @@ -340,7 +332,7 @@
"crate_id": 0,
"name": "Tuple",
"source": {
"filename": "structs.rs",
"filename": "$TEST_BASE_DIR/structs.rs",
"begin": [
5,
0
Expand All @@ -362,18 +354,14 @@
"params": [],
"where_predicates": []
},
"fields_stripped": true,
"fields": [
"0:7",
"0:8"
]
"fields_stripped": true
}
},
"0:4": {
"crate_id": 0,
"name": "PlainEmpty",
"source": {
"filename": "structs.rs",
"filename": "$TEST_BASE_DIR/structs.rs",
"begin": [
3,
0
Expand Down Expand Up @@ -403,7 +391,7 @@
"crate_id": 0,
"name": "0",
"source": {
"filename": "structs.rs",
"filename": "$TEST_BASE_DIR/structs.rs",
"begin": [
5,
17
Expand Down
Loading

0 comments on commit 97011eb

Please sign in to comment.