Add from_def_id_and_kind
reducing duplication in rustdoc
- Add `Item::from_hir_id_and_kind` convenience wrapper - Make name parameter mandatory `tcx.opt_item_name` doesn't handle renames, so this is necessary for any item that could be renamed, which is almost all of them. - Override visibilities to be `Inherited` for enum variants `tcx.visibility` returns the effective visibility, not the visibility that was written in the source code. `pub enum E { A, B }` always has public variants `A` and `B`, so there's no sense printing `pub` again. - Don't duplicate handling of `Visibility::Crate` Instead, represent it as just another `Restricted` path.
This commit is contained in:
parent
c9a17b1d3f
commit
fc4ca55291
@ -124,16 +124,8 @@
|
||||
let attrs = merge_attrs(cx, Some(parent_module), target_attrs, attrs_clone);
|
||||
|
||||
cx.renderinfo.borrow_mut().inlined.insert(did);
|
||||
ret.push(clean::Item {
|
||||
source: cx.tcx.def_span(did).clean(cx),
|
||||
name: Some(name.clean(cx)),
|
||||
attrs,
|
||||
kind,
|
||||
visibility: clean::Public,
|
||||
stability: cx.tcx.lookup_stability(did).cloned(),
|
||||
deprecation: cx.tcx.lookup_deprecation(did).clean(cx),
|
||||
def_id: did,
|
||||
});
|
||||
let what_rustc_thinks = clean::Item::from_def_id_and_parts(did, Some(name), kind, cx);
|
||||
ret.push(clean::Item { attrs, ..what_rustc_thinks });
|
||||
Some(ret)
|
||||
}
|
||||
|
||||
@ -443,8 +435,10 @@ fn merge_attrs(
|
||||
|
||||
debug!("build_impl: impl {:?} for {:?}", trait_.def_id(), for_.def_id());
|
||||
|
||||
ret.push(clean::Item {
|
||||
kind: clean::ImplItem(clean::Impl {
|
||||
ret.push(clean::Item::from_def_id_and_parts(
|
||||
did,
|
||||
None,
|
||||
clean::ImplItem(clean::Impl {
|
||||
unsafety: hir::Unsafety::Normal,
|
||||
generics,
|
||||
provided_trait_methods: provided,
|
||||
@ -455,14 +449,8 @@ fn merge_attrs(
|
||||
synthetic: false,
|
||||
blanket_impl: None,
|
||||
}),
|
||||
source: tcx.def_span(did).clean(cx),
|
||||
name: None,
|
||||
attrs,
|
||||
visibility: clean::Inherited,
|
||||
stability: tcx.lookup_stability(did).cloned(),
|
||||
deprecation: tcx.lookup_deprecation(did).clean(cx),
|
||||
def_id: did,
|
||||
});
|
||||
cx,
|
||||
));
|
||||
}
|
||||
|
||||
fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet<DefId>) -> clean::Module {
|
||||
|
@ -223,12 +223,6 @@ fn clean(&self, cx: &DocContext<'_>) -> ExternalCrate {
|
||||
|
||||
impl Clean<Item> for doctree::Module<'_> {
|
||||
fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
let name = if self.name.is_some() {
|
||||
self.name.expect("No name provided").clean(cx)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
// maintain a stack of mod ids, for doc comment path resolution
|
||||
// but we also need to resolve the module's own docs based on whether its docs were written
|
||||
// inside or outside the module, so check for that
|
||||
@ -268,15 +262,17 @@ fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
}
|
||||
};
|
||||
|
||||
let what_rustc_thinks = Item::from_hir_id_and_parts(
|
||||
self.id,
|
||||
self.name,
|
||||
ModuleItem(Module { is_crate: self.is_crate, items }),
|
||||
cx,
|
||||
);
|
||||
Item {
|
||||
name: Some(name),
|
||||
name: Some(what_rustc_thinks.name.unwrap_or_default()),
|
||||
attrs,
|
||||
source: span.clean(cx),
|
||||
visibility: self.vis.clean(cx),
|
||||
stability: cx.stability(self.id),
|
||||
deprecation: cx.deprecation(self.id).clean(cx),
|
||||
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
|
||||
kind: ModuleItem(Module { is_crate: self.is_crate, items }),
|
||||
..what_rustc_thinks
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -897,31 +893,26 @@ fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
let (generics, decl) =
|
||||
enter_impl_trait(cx, || (self.generics.clean(cx), (self.decl, self.body).clean(cx)));
|
||||
|
||||
let did = cx.tcx.hir().local_def_id(self.id);
|
||||
let constness = if is_const_fn(cx.tcx, did.to_def_id())
|
||||
&& !is_unstable_const_fn(cx.tcx, did.to_def_id()).is_some()
|
||||
let did = cx.tcx.hir().local_def_id(self.id).to_def_id();
|
||||
let constness = if is_const_fn(cx.tcx, did) && !is_unstable_const_fn(cx.tcx, did).is_some()
|
||||
{
|
||||
hir::Constness::Const
|
||||
} else {
|
||||
hir::Constness::NotConst
|
||||
};
|
||||
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
|
||||
Item {
|
||||
name: Some(self.name.clean(cx)),
|
||||
attrs: self.attrs.clean(cx),
|
||||
source: self.span.clean(cx),
|
||||
visibility: self.vis.clean(cx),
|
||||
stability: cx.stability(self.id),
|
||||
deprecation: cx.deprecation(self.id).clean(cx),
|
||||
def_id: did.to_def_id(),
|
||||
kind: FunctionItem(Function {
|
||||
Item::from_def_id_and_parts(
|
||||
did,
|
||||
Some(self.name),
|
||||
FunctionItem(Function {
|
||||
decl,
|
||||
generics,
|
||||
header: hir::FnHeader { constness, ..self.header },
|
||||
all_types,
|
||||
ret_types,
|
||||
}),
|
||||
}
|
||||
cx,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1013,15 +1004,10 @@ impl Clean<Item> for doctree::Trait<'_> {
|
||||
fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
let attrs = self.attrs.clean(cx);
|
||||
let is_spotlight = attrs.has_doc_flag(sym::spotlight);
|
||||
Item {
|
||||
name: Some(self.name.clean(cx)),
|
||||
attrs,
|
||||
source: self.span.clean(cx),
|
||||
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
|
||||
visibility: self.vis.clean(cx),
|
||||
stability: cx.stability(self.id),
|
||||
deprecation: cx.deprecation(self.id).clean(cx),
|
||||
kind: TraitItem(Trait {
|
||||
Item::from_hir_id_and_parts(
|
||||
self.id,
|
||||
Some(self.name),
|
||||
TraitItem(Trait {
|
||||
unsafety: self.unsafety,
|
||||
items: self.items.iter().map(|ti| ti.clean(cx)).collect(),
|
||||
generics: self.generics.clean(cx),
|
||||
@ -1029,26 +1015,22 @@ fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
is_spotlight,
|
||||
is_auto: self.is_auto.clean(cx),
|
||||
}),
|
||||
}
|
||||
cx,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Clean<Item> for doctree::TraitAlias<'_> {
|
||||
fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
let attrs = self.attrs.clean(cx);
|
||||
Item {
|
||||
name: Some(self.name.clean(cx)),
|
||||
attrs,
|
||||
source: self.span.clean(cx),
|
||||
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
|
||||
visibility: self.vis.clean(cx),
|
||||
stability: cx.stability(self.id),
|
||||
deprecation: cx.deprecation(self.id).clean(cx),
|
||||
kind: TraitAliasItem(TraitAlias {
|
||||
Item::from_hir_id_and_parts(
|
||||
self.id,
|
||||
Some(self.name),
|
||||
TraitAliasItem(TraitAlias {
|
||||
generics: self.generics.clean(cx),
|
||||
bounds: self.bounds.clean(cx),
|
||||
}),
|
||||
}
|
||||
cx,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1098,15 +1080,15 @@ fn clean(&self, _: &DocContext<'_>) -> TypeKind {
|
||||
|
||||
impl Clean<Item> for hir::TraitItem<'_> {
|
||||
fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
let local_did = cx.tcx.hir().local_def_id(self.hir_id);
|
||||
let kind = match self.kind {
|
||||
let local_did = cx.tcx.hir().local_def_id(self.hir_id).to_def_id();
|
||||
let inner = match self.kind {
|
||||
hir::TraitItemKind::Const(ref ty, default) => {
|
||||
AssocConstItem(ty.clean(cx), default.map(|e| print_const_expr(cx, e)))
|
||||
}
|
||||
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
|
||||
let mut m = (sig, &self.generics, body).clean(cx);
|
||||
if m.header.constness == hir::Constness::Const
|
||||
&& is_unstable_const_fn(cx.tcx, local_did.to_def_id()).is_some()
|
||||
&& is_unstable_const_fn(cx.tcx, local_did).is_some()
|
||||
{
|
||||
m.header.constness = hir::Constness::NotConst;
|
||||
}
|
||||
@ -1119,7 +1101,7 @@ fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
|
||||
let mut t = Function { header: sig.header, decl, generics, all_types, ret_types };
|
||||
if t.header.constness == hir::Constness::Const
|
||||
&& is_unstable_const_fn(cx.tcx, local_did.to_def_id()).is_some()
|
||||
&& is_unstable_const_fn(cx.tcx, local_did).is_some()
|
||||
{
|
||||
t.header.constness = hir::Constness::NotConst;
|
||||
}
|
||||
@ -1129,30 +1111,21 @@ fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
AssocTypeItem(bounds.clean(cx), default.clean(cx))
|
||||
}
|
||||
};
|
||||
Item {
|
||||
name: Some(self.ident.name.clean(cx)),
|
||||
attrs: self.attrs.clean(cx),
|
||||
source: self.span.clean(cx),
|
||||
def_id: local_did.to_def_id(),
|
||||
visibility: Visibility::Inherited,
|
||||
stability: get_stability(cx, local_did.to_def_id()),
|
||||
deprecation: get_deprecation(cx, local_did.to_def_id()),
|
||||
kind,
|
||||
}
|
||||
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
|
||||
}
|
||||
}
|
||||
|
||||
impl Clean<Item> for hir::ImplItem<'_> {
|
||||
fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
let local_did = cx.tcx.hir().local_def_id(self.hir_id);
|
||||
let kind = match self.kind {
|
||||
let local_did = cx.tcx.hir().local_def_id(self.hir_id).to_def_id();
|
||||
let inner = match self.kind {
|
||||
hir::ImplItemKind::Const(ref ty, expr) => {
|
||||
AssocConstItem(ty.clean(cx), Some(print_const_expr(cx, expr)))
|
||||
}
|
||||
hir::ImplItemKind::Fn(ref sig, body) => {
|
||||
let mut m = (sig, &self.generics, body).clean(cx);
|
||||
if m.header.constness == hir::Constness::Const
|
||||
&& is_unstable_const_fn(cx.tcx, local_did.to_def_id()).is_some()
|
||||
&& is_unstable_const_fn(cx.tcx, local_did).is_some()
|
||||
{
|
||||
m.header.constness = hir::Constness::NotConst;
|
||||
}
|
||||
@ -1164,16 +1137,7 @@ fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
TypedefItem(Typedef { type_, generics: Generics::default(), item_type }, true)
|
||||
}
|
||||
};
|
||||
Item {
|
||||
name: Some(self.ident.name.clean(cx)),
|
||||
source: self.span.clean(cx),
|
||||
attrs: self.attrs.clean(cx),
|
||||
def_id: local_did.to_def_id(),
|
||||
visibility: self.vis.clean(cx),
|
||||
stability: get_stability(cx, local_did.to_def_id()),
|
||||
deprecation: get_deprecation(cx, local_did.to_def_id()),
|
||||
kind,
|
||||
}
|
||||
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1329,21 +1293,7 @@ fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
}
|
||||
};
|
||||
|
||||
let visibility = match self.container {
|
||||
ty::ImplContainer(_) => self.vis.clean(cx),
|
||||
ty::TraitContainer(_) => Inherited,
|
||||
};
|
||||
|
||||
Item {
|
||||
name: Some(self.ident.name.clean(cx)),
|
||||
visibility,
|
||||
stability: get_stability(cx, self.def_id),
|
||||
deprecation: get_deprecation(cx, self.def_id),
|
||||
def_id: self.def_id,
|
||||
attrs: inline::load_attrs(cx, self.def_id).clean(cx),
|
||||
source: cx.tcx.def_span(self.def_id).clean(cx),
|
||||
kind,
|
||||
}
|
||||
Item::from_def_id_and_parts(self.def_id, Some(self.ident.name), kind, cx)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1773,33 +1723,27 @@ fn clean(&self, cx: &DocContext<'_>) -> Constant {
|
||||
|
||||
impl Clean<Item> for hir::StructField<'_> {
|
||||
fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
let local_did = cx.tcx.hir().local_def_id(self.hir_id);
|
||||
|
||||
Item {
|
||||
name: Some(self.ident.name).clean(cx),
|
||||
attrs: self.attrs.clean(cx),
|
||||
source: self.span.clean(cx),
|
||||
visibility: self.vis.clean(cx),
|
||||
stability: get_stability(cx, local_did.to_def_id()),
|
||||
deprecation: get_deprecation(cx, local_did.to_def_id()),
|
||||
def_id: local_did.to_def_id(),
|
||||
kind: StructFieldItem(self.ty.clean(cx)),
|
||||
}
|
||||
let what_rustc_thinks = Item::from_hir_id_and_parts(
|
||||
self.hir_id,
|
||||
Some(self.ident.name),
|
||||
StructFieldItem(self.ty.clean(cx)),
|
||||
cx,
|
||||
);
|
||||
// Don't show `pub` for fields on enum variants; they are always public
|
||||
Item { visibility: self.vis.clean(cx), ..what_rustc_thinks }
|
||||
}
|
||||
}
|
||||
|
||||
impl Clean<Item> for ty::FieldDef {
|
||||
fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
Item {
|
||||
name: Some(self.ident.name).clean(cx),
|
||||
attrs: cx.tcx.get_attrs(self.did).clean(cx),
|
||||
source: cx.tcx.def_span(self.did).clean(cx),
|
||||
visibility: self.vis.clean(cx),
|
||||
stability: get_stability(cx, self.did),
|
||||
deprecation: get_deprecation(cx, self.did),
|
||||
def_id: self.did,
|
||||
kind: StructFieldItem(cx.tcx.type_of(self.did).clean(cx)),
|
||||
}
|
||||
let what_rustc_thinks = Item::from_def_id_and_parts(
|
||||
self.did,
|
||||
Some(self.ident.name),
|
||||
StructFieldItem(cx.tcx.type_of(self.did).clean(cx)),
|
||||
cx,
|
||||
);
|
||||
// Don't show `pub` for fields on enum variants; they are always public
|
||||
Item { visibility: self.vis.clean(cx), ..what_rustc_thinks }
|
||||
}
|
||||
}
|
||||
|
||||
@ -1808,7 +1752,10 @@ fn clean(&self, cx: &DocContext<'_>) -> Visibility {
|
||||
match self.node {
|
||||
hir::VisibilityKind::Public => Visibility::Public,
|
||||
hir::VisibilityKind::Inherited => Visibility::Inherited,
|
||||
hir::VisibilityKind::Crate(_) => Visibility::Crate,
|
||||
hir::VisibilityKind::Crate(_) => {
|
||||
let krate = DefId::local(CRATE_DEF_INDEX);
|
||||
Visibility::Restricted(krate, cx.tcx.def_path(krate))
|
||||
}
|
||||
hir::VisibilityKind::Restricted { ref path, .. } => {
|
||||
let path = path.clean(cx);
|
||||
let did = register_res(cx, path.res);
|
||||
@ -1819,48 +1766,46 @@ fn clean(&self, cx: &DocContext<'_>) -> Visibility {
|
||||
}
|
||||
|
||||
impl Clean<Visibility> for ty::Visibility {
|
||||
fn clean(&self, _: &DocContext<'_>) -> Visibility {
|
||||
if *self == ty::Visibility::Public { Public } else { Inherited }
|
||||
fn clean(&self, cx: &DocContext<'_>) -> Visibility {
|
||||
match *self {
|
||||
ty::Visibility::Public => Visibility::Public,
|
||||
ty::Visibility::Invisible => Visibility::Inherited,
|
||||
ty::Visibility::Restricted(module) => {
|
||||
Visibility::Restricted(module, cx.tcx.def_path(module))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Clean<Item> for doctree::Struct<'_> {
|
||||
fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
Item {
|
||||
name: Some(self.name.clean(cx)),
|
||||
attrs: self.attrs.clean(cx),
|
||||
source: self.span.clean(cx),
|
||||
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
|
||||
visibility: self.vis.clean(cx),
|
||||
stability: cx.stability(self.id),
|
||||
deprecation: cx.deprecation(self.id).clean(cx),
|
||||
kind: StructItem(Struct {
|
||||
Item::from_hir_id_and_parts(
|
||||
self.id,
|
||||
Some(self.name),
|
||||
StructItem(Struct {
|
||||
struct_type: self.struct_type,
|
||||
generics: self.generics.clean(cx),
|
||||
fields: self.fields.clean(cx),
|
||||
fields_stripped: false,
|
||||
}),
|
||||
}
|
||||
cx,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Clean<Item> for doctree::Union<'_> {
|
||||
fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
Item {
|
||||
name: Some(self.name.clean(cx)),
|
||||
attrs: self.attrs.clean(cx),
|
||||
source: self.span.clean(cx),
|
||||
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
|
||||
visibility: self.vis.clean(cx),
|
||||
stability: cx.stability(self.id),
|
||||
deprecation: cx.deprecation(self.id).clean(cx),
|
||||
kind: UnionItem(Union {
|
||||
Item::from_hir_id_and_parts(
|
||||
self.id,
|
||||
Some(self.name),
|
||||
UnionItem(Union {
|
||||
struct_type: self.struct_type,
|
||||
generics: self.generics.clean(cx),
|
||||
fields: self.fields.clean(cx),
|
||||
fields_stripped: false,
|
||||
}),
|
||||
}
|
||||
cx,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1876,35 +1821,29 @@ fn clean(&self, cx: &DocContext<'_>) -> VariantStruct {
|
||||
|
||||
impl Clean<Item> for doctree::Enum<'_> {
|
||||
fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
Item {
|
||||
name: Some(self.name.clean(cx)),
|
||||
attrs: self.attrs.clean(cx),
|
||||
source: self.span.clean(cx),
|
||||
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
|
||||
visibility: self.vis.clean(cx),
|
||||
stability: cx.stability(self.id),
|
||||
deprecation: cx.deprecation(self.id).clean(cx),
|
||||
kind: EnumItem(Enum {
|
||||
Item::from_hir_id_and_parts(
|
||||
self.id,
|
||||
Some(self.name),
|
||||
EnumItem(Enum {
|
||||
variants: self.variants.iter().map(|v| v.clean(cx)).collect(),
|
||||
generics: self.generics.clean(cx),
|
||||
variants_stripped: false,
|
||||
}),
|
||||
}
|
||||
cx,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Clean<Item> for doctree::Variant<'_> {
|
||||
fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
Item {
|
||||
name: Some(self.name.clean(cx)),
|
||||
attrs: self.attrs.clean(cx),
|
||||
source: self.span.clean(cx),
|
||||
visibility: Inherited,
|
||||
stability: cx.stability(self.id),
|
||||
deprecation: cx.deprecation(self.id).clean(cx),
|
||||
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
|
||||
kind: VariantItem(Variant { kind: self.def.clean(cx) }),
|
||||
}
|
||||
let what_rustc_thinks = Item::from_hir_id_and_parts(
|
||||
self.id,
|
||||
Some(self.name),
|
||||
VariantItem(Variant { kind: self.def.clean(cx) }),
|
||||
cx,
|
||||
);
|
||||
// don't show `pub` for variants, which are always public
|
||||
Item { visibility: Inherited, ..what_rustc_thinks }
|
||||
}
|
||||
}
|
||||
|
||||
@ -1925,7 +1864,7 @@ fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
source: cx.tcx.def_span(field.did).clean(cx),
|
||||
name: Some(field.ident.name.clean(cx)),
|
||||
attrs: cx.tcx.get_attrs(field.did).clean(cx),
|
||||
visibility: field.vis.clean(cx),
|
||||
visibility: Visibility::Inherited,
|
||||
def_id: field.did,
|
||||
stability: get_stability(cx, field.did),
|
||||
deprecation: get_deprecation(cx, field.did),
|
||||
@ -1934,16 +1873,14 @@ fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
.collect(),
|
||||
}),
|
||||
};
|
||||
Item {
|
||||
name: Some(self.ident.clean(cx)),
|
||||
attrs: inline::load_attrs(cx, self.def_id).clean(cx),
|
||||
source: cx.tcx.def_span(self.def_id).clean(cx),
|
||||
visibility: Inherited,
|
||||
def_id: self.def_id,
|
||||
kind: VariantItem(Variant { kind }),
|
||||
stability: get_stability(cx, self.def_id),
|
||||
deprecation: get_deprecation(cx, self.def_id),
|
||||
}
|
||||
let what_rustc_thinks = Item::from_def_id_and_parts(
|
||||
self.def_id,
|
||||
Some(self.ident.name),
|
||||
VariantItem(Variant { kind }),
|
||||
cx,
|
||||
);
|
||||
// don't show `pub` for fields, which are always public
|
||||
Item { visibility: Inherited, ..what_rustc_thinks }
|
||||
}
|
||||
}
|
||||
|
||||
@ -2048,34 +1985,26 @@ impl Clean<Item> for doctree::Typedef<'_> {
|
||||
fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
let type_ = self.ty.clean(cx);
|
||||
let item_type = type_.def_id().and_then(|did| inline::build_ty(cx, did));
|
||||
Item {
|
||||
name: Some(self.name.clean(cx)),
|
||||
attrs: self.attrs.clean(cx),
|
||||
source: self.span.clean(cx),
|
||||
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
|
||||
visibility: self.vis.clean(cx),
|
||||
stability: cx.stability(self.id),
|
||||
deprecation: cx.deprecation(self.id).clean(cx),
|
||||
kind: TypedefItem(Typedef { type_, generics: self.gen.clean(cx), item_type }, false),
|
||||
}
|
||||
Item::from_hir_id_and_parts(
|
||||
self.id,
|
||||
Some(self.name),
|
||||
TypedefItem(Typedef { type_, generics: self.gen.clean(cx), item_type }, false),
|
||||
cx,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Clean<Item> for doctree::OpaqueTy<'_> {
|
||||
fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
Item {
|
||||
name: Some(self.name.clean(cx)),
|
||||
attrs: self.attrs.clean(cx),
|
||||
source: self.span.clean(cx),
|
||||
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
|
||||
visibility: self.vis.clean(cx),
|
||||
stability: cx.stability(self.id),
|
||||
deprecation: cx.deprecation(self.id).clean(cx),
|
||||
kind: OpaqueTyItem(OpaqueTy {
|
||||
Item::from_hir_id_and_parts(
|
||||
self.id,
|
||||
Some(self.name),
|
||||
OpaqueTyItem(OpaqueTy {
|
||||
bounds: self.opaque_ty.bounds.clean(cx),
|
||||
generics: self.opaque_ty.generics.clean(cx),
|
||||
}),
|
||||
}
|
||||
cx,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -2091,42 +2020,34 @@ fn clean(&self, cx: &DocContext<'_>) -> BareFunctionDecl {
|
||||
impl Clean<Item> for doctree::Static<'_> {
|
||||
fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
debug!("cleaning static {}: {:?}", self.name.clean(cx), self);
|
||||
Item {
|
||||
name: Some(self.name.clean(cx)),
|
||||
attrs: self.attrs.clean(cx),
|
||||
source: self.span.clean(cx),
|
||||
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
|
||||
visibility: self.vis.clean(cx),
|
||||
stability: cx.stability(self.id),
|
||||
deprecation: cx.deprecation(self.id).clean(cx),
|
||||
kind: StaticItem(Static {
|
||||
Item::from_hir_id_and_parts(
|
||||
self.id,
|
||||
Some(self.name),
|
||||
StaticItem(Static {
|
||||
type_: self.type_.clean(cx),
|
||||
mutability: self.mutability,
|
||||
expr: print_const_expr(cx, self.expr),
|
||||
}),
|
||||
}
|
||||
cx,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Clean<Item> for doctree::Constant<'_> {
|
||||
fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
let def_id = cx.tcx.hir().local_def_id(self.id);
|
||||
let def_id = cx.tcx.hir().local_def_id(self.id).to_def_id();
|
||||
|
||||
Item {
|
||||
name: Some(self.name.clean(cx)),
|
||||
attrs: self.attrs.clean(cx),
|
||||
source: self.span.clean(cx),
|
||||
def_id: def_id.to_def_id(),
|
||||
visibility: self.vis.clean(cx),
|
||||
stability: cx.stability(self.id),
|
||||
deprecation: cx.deprecation(self.id).clean(cx),
|
||||
kind: ConstantItem(Constant {
|
||||
Item::from_def_id_and_parts(
|
||||
def_id,
|
||||
Some(self.name),
|
||||
ConstantItem(Constant {
|
||||
type_: self.type_.clean(cx),
|
||||
expr: print_const_expr(cx, self.expr),
|
||||
value: print_evaluated_const(cx, def_id.to_def_id()),
|
||||
value: print_evaluated_const(cx, def_id),
|
||||
is_literal: is_literal_expr(cx, self.expr.hir_id),
|
||||
}),
|
||||
}
|
||||
cx,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -2355,34 +2276,19 @@ fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
hir::ForeignItemKind::Type => ForeignTypeItem,
|
||||
};
|
||||
|
||||
Item {
|
||||
name: Some(self.name.clean(cx)),
|
||||
attrs: self.attrs.clean(cx),
|
||||
source: self.span.clean(cx),
|
||||
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
|
||||
visibility: self.vis.clean(cx),
|
||||
stability: cx.stability(self.id),
|
||||
deprecation: cx.deprecation(self.id).clean(cx),
|
||||
kind,
|
||||
}
|
||||
Item::from_hir_id_and_parts(self.id, Some(self.name), kind, cx)
|
||||
}
|
||||
}
|
||||
|
||||
impl Clean<Item> for doctree::Macro<'_> {
|
||||
fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
let name = self.name.clean(cx);
|
||||
Item {
|
||||
name: Some(name.clone()),
|
||||
attrs: self.attrs.clean(cx),
|
||||
source: self.span.clean(cx),
|
||||
visibility: Public,
|
||||
stability: cx.stability(self.hid),
|
||||
deprecation: cx.deprecation(self.hid).clean(cx),
|
||||
def_id: self.def_id,
|
||||
kind: MacroItem(Macro {
|
||||
Item::from_def_id_and_parts(
|
||||
self.def_id,
|
||||
Some(self.name),
|
||||
MacroItem(Macro {
|
||||
source: format!(
|
||||
"macro_rules! {} {{\n{}}}",
|
||||
name,
|
||||
self.name,
|
||||
self.matchers
|
||||
.iter()
|
||||
.map(|span| { format!(" {} => {{ ... }};\n", span.to_src(cx)) })
|
||||
@ -2390,22 +2296,19 @@ fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
),
|
||||
imported_from: self.imported_from.clean(cx),
|
||||
}),
|
||||
}
|
||||
cx,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Clean<Item> for doctree::ProcMacro<'_> {
|
||||
fn clean(&self, cx: &DocContext<'_>) -> Item {
|
||||
Item {
|
||||
name: Some(self.name.clean(cx)),
|
||||
attrs: self.attrs.clean(cx),
|
||||
source: self.span.clean(cx),
|
||||
visibility: Public,
|
||||
stability: cx.stability(self.id),
|
||||
deprecation: cx.deprecation(self.id).clean(cx),
|
||||
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
|
||||
kind: ProcMacroItem(ProcMacro { kind: self.kind, helpers: self.helpers.clean(cx) }),
|
||||
}
|
||||
Item::from_hir_id_and_parts(
|
||||
self.id,
|
||||
Some(self.name),
|
||||
ProcMacroItem(ProcMacro { kind: self.kind, helpers: self.helpers.clean(cx) }),
|
||||
cx,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -112,6 +112,48 @@ impl Item {
|
||||
self.attrs.doc_value()
|
||||
}
|
||||
|
||||
/// Convenience wrapper around [`Self::from_def_id_and_parts`] which converts
|
||||
/// `hir_id` to a [`DefId`]
|
||||
pub fn from_hir_id_and_parts(
|
||||
hir_id: hir::HirId,
|
||||
name: Option<Symbol>,
|
||||
kind: ItemKind,
|
||||
cx: &DocContext<'_>,
|
||||
) -> Item {
|
||||
Item::from_def_id_and_parts(cx.tcx.hir().local_def_id(hir_id).to_def_id(), name, kind, cx)
|
||||
}
|
||||
|
||||
pub fn from_def_id_and_parts(
|
||||
def_id: DefId,
|
||||
name: Option<Symbol>,
|
||||
kind: ItemKind,
|
||||
cx: &DocContext<'_>,
|
||||
) -> Item {
|
||||
use super::Clean;
|
||||
|
||||
debug!("name={:?}, def_id={:?}", name, def_id);
|
||||
|
||||
// `span_if_local()` lies about functions and only gives the span of the function signature
|
||||
let source = def_id.as_local().map_or_else(
|
||||
|| cx.tcx.def_span(def_id),
|
||||
|local| {
|
||||
let hir = cx.tcx.hir();
|
||||
hir.span_with_body(hir.local_def_id_to_hir_id(local))
|
||||
},
|
||||
);
|
||||
|
||||
Item {
|
||||
def_id,
|
||||
kind,
|
||||
name: name.clean(cx),
|
||||
source: source.clean(cx),
|
||||
attrs: cx.tcx.get_attrs(def_id).clean(cx),
|
||||
visibility: cx.tcx.visibility(def_id).clean(cx),
|
||||
stability: cx.tcx.lookup_stability(def_id).cloned(),
|
||||
deprecation: cx.tcx.lookup_deprecation(def_id).clean(cx),
|
||||
}
|
||||
}
|
||||
|
||||
/// Finds all `doc` attributes as NameValues and returns their corresponding values, joined
|
||||
/// with newlines.
|
||||
crate fn collapsed_doc_value(&self) -> Option<String> {
|
||||
@ -1464,7 +1506,6 @@ fn from(prim_ty: hir::PrimTy) -> PrimitiveType {
|
||||
crate enum Visibility {
|
||||
Public,
|
||||
Inherited,
|
||||
Crate,
|
||||
Restricted(DefId, rustc_hir::definitions::DefPath),
|
||||
}
|
||||
|
||||
|
@ -1094,7 +1094,6 @@ impl clean::Visibility {
|
||||
display_fn(move |f| match *self {
|
||||
clean::Public => f.write_str("pub "),
|
||||
clean::Inherited => Ok(()),
|
||||
clean::Visibility::Crate => write!(f, "pub(crate) "),
|
||||
// If this is `pub(crate)`, `path` will be empty.
|
||||
clean::Visibility::Restricted(did, _) if did.index == CRATE_DEF_INDEX => {
|
||||
write!(f, "pub(crate) ")
|
||||
|
@ -4,10 +4,12 @@
|
||||
|
||||
// @has variant_struct/enum.Foo.html
|
||||
// @!has - 'pub qux'
|
||||
// @!has - 'pub(crate) qux'
|
||||
// @!has - 'pub Bar'
|
||||
extern crate variant_struct;
|
||||
|
||||
// @has issue_32395/enum.Foo.html
|
||||
// @!has - 'pub qux'
|
||||
// @!has - 'pub(crate) qux'
|
||||
// @!has - 'pub Bar'
|
||||
pub use variant_struct::Foo;
|
||||
|
Loading…
Reference in New Issue
Block a user