Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of doctree::Trait #79332

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
261 changes: 136 additions & 125 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,10 @@ impl Clean<Item> for doctree::Module<'_> {
let mut items: Vec<Item> = vec![];
items.extend(self.extern_crates.iter().flat_map(|x| x.clean(cx)));
items.extend(self.imports.iter().flat_map(|x| x.clean(cx)));
items.extend(self.fns.iter().map(|x| x.clean(cx)));
items.extend(self.foreigns.iter().map(|x| x.clean(cx)));
items.extend(self.mods.iter().map(|x| x.clean(cx)));
items.extend(self.items.iter().map(|x| x.clean(cx)));
items.extend(self.traits.iter().map(|x| x.clean(cx)));
items.extend(self.impls.iter().flat_map(|x| x.clean(cx)));
items.extend(self.items.iter().map(|x| x.clean(cx)).flatten());
items.extend(self.macros.iter().map(|x| x.clean(cx)));
items.extend(self.proc_macros.iter().map(|x| x.clean(cx)));

// determine if we should display the inner contents or
// the outer `mod` item for the source code.
Expand Down Expand Up @@ -872,6 +868,66 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx
}
}

fn clean_fn_or_proc_macro(
item: &hir::Item<'_>,
sig: &'a hir::FnSig<'a>,
generics: &'a hir::Generics<'a>,
body_id: hir::BodyId,
name: &mut Symbol,
cx: &DocContext<'_>,
) -> ItemKind {
let macro_kind = item.attrs.iter().find_map(|a| {
if a.has_name(sym::proc_macro) {
Some(MacroKind::Bang)
} else if a.has_name(sym::proc_macro_derive) {
Some(MacroKind::Derive)
} else if a.has_name(sym::proc_macro_attribute) {
Some(MacroKind::Attr)
} else {
None
}
});
match macro_kind {
Some(kind) => {
if kind == MacroKind::Derive {
*name = item
.attrs
.lists(sym::proc_macro_derive)
.find_map(|mi| mi.ident())
.expect("proc-macro derives require a name")
.name;
}

let mut helpers = Vec::new();
for mi in item.attrs.lists(sym::proc_macro_derive) {
if !mi.has_name(sym::attributes) {
continue;
}

if let Some(list) = mi.meta_item_list() {
for inner_mi in list {
if let Some(ident) = inner_mi.ident() {
helpers.push(ident.name);
}
}
}
}
ProcMacroItem(ProcMacro { kind, helpers: helpers.clean(cx) })
}
None => {
let mut func = (sig, generics, body_id).clean(cx);
let def_id = cx.tcx.hir().local_def_id(item.hir_id).to_def_id();
func.header.constness =
if is_const_fn(cx.tcx, def_id) && is_unstable_const_fn(cx.tcx, def_id).is_none() {
hir::Constness::Const
} else {
hir::Constness::NotConst
};
FunctionItem(func)
}
}
}

impl<'a> Clean<Function> for (&'a hir::FnSig<'a>, &'a hir::Generics<'a>, hir::BodyId) {
fn clean(&self, cx: &DocContext<'_>) -> Function {
let (generics, decl) =
Expand All @@ -881,34 +937,6 @@ impl<'a> Clean<Function> for (&'a hir::FnSig<'a>, &'a hir::Generics<'a>, hir::Bo
}
}

impl Clean<Item> for doctree::Function<'_> {
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).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::from_def_id_and_parts(
did,
Some(self.name),
FunctionItem(Function {
decl,
generics,
header: hir::FnHeader { constness, ..self.header },
all_types,
ret_types,
}),
cx,
)
}
}

impl<'a> Clean<Arguments> for (&'a [hir::Ty<'a>], &'a [Ident]) {
fn clean(&self, cx: &DocContext<'_>) -> Arguments {
Arguments {
Expand Down Expand Up @@ -993,26 +1021,6 @@ impl Clean<FnRetTy> for hir::FnRetTy<'_> {
}
}

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::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),
bounds: self.bounds.clean(cx),
is_spotlight,
is_auto: self.is_auto.clean(cx),
}),
cx,
)
}
}

impl Clean<bool> for hir::IsAuto {
fn clean(&self, _: &DocContext<'_>) -> bool {
match *self {
Expand Down Expand Up @@ -1922,13 +1930,13 @@ impl Clean<BareFunctionDecl> for hir::BareFnTy<'_> {
}
}

impl Clean<Item> for (&hir::Item<'_>, Option<Ident>) {
fn clean(&self, cx: &DocContext<'_>) -> Item {
impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Ident>) {
fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
use hir::ItemKind;

let (item, renamed) = self;
let def_id = cx.tcx.hir().local_def_id(item.hir_id).to_def_id();
let name = match renamed {
let mut name = match renamed {
Some(ident) => ident.name,
None => cx.tcx.hir().name(item.hir_id),
};
Expand Down Expand Up @@ -1977,10 +1985,29 @@ impl Clean<Item> for (&hir::Item<'_>, Option<Ident>) {
fields: variant_data.fields().clean(cx),
fields_stripped: false,
}),
ItemKind::Impl { .. } => return clean_impl(item, cx),
// proc macros can have a name set by attributes
ItemKind::Fn(ref sig, ref generics, body_id) => {
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)
}
hir::ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref item_ids) => {
let items =
item_ids.iter().map(|ti| cx.tcx.hir().trait_item(ti.id).clean(cx)).collect();
let attrs = item.attrs.clean(cx);
let is_spotlight = attrs.has_doc_flag(sym::spotlight);
TraitItem(Trait {
unsafety,
items,
generics: generics.clean(cx),
bounds: bounds.clean(cx),
is_spotlight,
is_auto: is_auto.clean(cx),
})
}
_ => unreachable!("not yet converted"),
};

Item::from_def_id_and_parts(def_id, Some(name), kind, cx)
vec![Item::from_def_id_and_parts(def_id, Some(name), kind, cx)]
}
}

Expand All @@ -2005,57 +2032,53 @@ impl Clean<ImplPolarity> for ty::ImplPolarity {
}
}

impl Clean<Vec<Item>> for doctree::Impl<'_> {
fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
let mut ret = Vec::new();
let trait_ = self.trait_.clean(cx);
let items = self.items.iter().map(|ii| ii.clean(cx)).collect::<Vec<_>>();
let def_id = cx.tcx.hir().local_def_id(self.id);

// If this impl block is an implementation of the Deref trait, then we
// need to try inlining the target's inherent impl blocks as well.
if trait_.def_id() == cx.tcx.lang_items().deref_trait() {
build_deref_target_impls(cx, &items, &mut ret);
fn clean_impl(impl_: &hir::Item<'_>, cx: &DocContext<'_>) -> Vec<Item> {
let mut ret = Vec::new();
let (trait_, items, for_, unsafety, generics) = match &impl_.kind {
hir::ItemKind::Impl { of_trait, items, self_ty, unsafety, generics, .. } => {
(of_trait, items, self_ty, *unsafety, generics)
}

let provided: FxHashSet<String> = trait_
.def_id()
.map(|did| {
cx.tcx.provided_trait_methods(did).map(|meth| meth.ident.to_string()).collect()
})
.unwrap_or_default();

let for_ = self.for_.clean(cx);
let type_alias = for_.def_id().and_then(|did| match cx.tcx.def_kind(did) {
DefKind::TyAlias => Some(cx.tcx.type_of(did).clean(cx)),
_ => None,
_ => unreachable!(),
};
let trait_ = trait_.clean(cx);
let items = items.iter().map(|ii| cx.tcx.hir().impl_item(ii.id).clean(cx)).collect::<Vec<_>>();
let def_id = cx.tcx.hir().local_def_id(impl_.hir_id);

// If this impl block is an implementation of the Deref trait, then we
// need to try inlining the target's inherent impl blocks as well.
if trait_.def_id() == cx.tcx.lang_items().deref_trait() {
build_deref_target_impls(cx, &items, &mut ret);
}

let provided: FxHashSet<String> = trait_
.def_id()
.map(|did| cx.tcx.provided_trait_methods(did).map(|meth| meth.ident.to_string()).collect())
.unwrap_or_default();

let for_ = for_.clean(cx);
let type_alias = for_.def_id().and_then(|did| match cx.tcx.def_kind(did) {
DefKind::TyAlias => Some(cx.tcx.type_of(did).clean(cx)),
_ => None,
});
let make_item = |trait_: Option<Type>, for_: Type, items: Vec<Item>| {
let kind = ImplItem(Impl {
unsafety,
generics: generics.clean(cx),
provided_trait_methods: provided.clone(),
trait_,
for_,
items,
polarity: Some(cx.tcx.impl_polarity(def_id).clean(cx)),
synthetic: false,
blanket_impl: None,
});
let make_item = |trait_: Option<Type>, for_: Type, items: Vec<Item>| Item {
name: None,
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: ImplItem(Impl {
unsafety: self.unsafety,
generics: self.generics.clean(cx),
provided_trait_methods: provided.clone(),
trait_,
for_,
items,
polarity: Some(cx.tcx.impl_polarity(def_id).clean(cx)),
synthetic: false,
blanket_impl: None,
}),
};
if let Some(type_alias) = type_alias {
ret.push(make_item(trait_.clone(), type_alias, items.clone()));
}
ret.push(make_item(trait_, for_, items));
ret
Item::from_hir_id_and_parts(impl_.hir_id, None, kind, cx)
};
if let Some(type_alias) = type_alias {
ret.push(make_item(trait_.clone(), type_alias, items.clone()));
}
ret.push(make_item(trait_, for_, items));
ret
}

impl Clean<Vec<Item>> for doctree::ExternCrate<'_> {
Expand Down Expand Up @@ -2190,11 +2213,12 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
}
}

impl Clean<Item> for doctree::ForeignItem<'_> {
impl Clean<Item> for (&hir::ForeignItem<'_>, Option<Ident>) {
fn clean(&self, cx: &DocContext<'_>) -> Item {
let kind = match self.kind {
let (item, renamed) = self;
let kind = match item.kind {
hir::ForeignItemKind::Fn(ref decl, ref names, ref generics) => {
let abi = cx.tcx.hir().get_foreign_abi(self.id);
let abi = cx.tcx.hir().get_foreign_abi(item.hir_id);
let (generics, decl) =
enter_impl_trait(cx, || (generics.clean(cx), (&**decl, &names[..]).clean(cx)));
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
Expand All @@ -2211,15 +2235,13 @@ impl Clean<Item> for doctree::ForeignItem<'_> {
ret_types,
})
}
hir::ForeignItemKind::Static(ref ty, mutbl) => ForeignStaticItem(Static {
type_: ty.clean(cx),
mutability: *mutbl,
expr: String::new(),
}),
hir::ForeignItemKind::Static(ref ty, mutability) => {
ForeignStaticItem(Static { type_: ty.clean(cx), mutability, expr: String::new() })
}
hir::ForeignItemKind::Type => ForeignTypeItem,
};

Item::from_hir_id_and_parts(self.id, Some(self.name), kind, cx)
Item::from_hir_id_and_parts(item.hir_id, Some(renamed.unwrap_or(item.ident).name), kind, cx)
}
}

Expand All @@ -2244,17 +2266,6 @@ impl Clean<Item> for doctree::Macro {
}
}

impl Clean<Item> for doctree::ProcMacro {
fn clean(&self, cx: &DocContext<'_>) -> Item {
Item::from_hir_id_and_parts(
self.id,
Some(self.name),
ProcMacroItem(ProcMacro { kind: self.kind, helpers: self.helpers.clean(cx) }),
cx,
)
}
}

impl Clean<Deprecation> for attr::Deprecation {
fn clean(&self, _: &DocContext<'_>) -> Deprecation {
Deprecation {
Expand Down
16 changes: 0 additions & 16 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use rustc_attr as attr;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::sync::{self, Lrc};
use rustc_driver::abort_on_err;
Expand Down Expand Up @@ -156,21 +155,6 @@ impl<'tcx> DocContext<'tcx> {
def_id.as_local().map(|def_id| self.tcx.hir().local_def_id_to_hir_id(def_id))
}
}

crate fn stability(&self, id: HirId) -> Option<attr::Stability> {
self.tcx
.hir()
.opt_local_def_id(id)
.and_then(|def_id| self.tcx.lookup_stability(def_id.to_def_id()))
.cloned()
}

crate fn deprecation(&self, id: HirId) -> Option<attr::Deprecation> {
self.tcx
.hir()
.opt_local_def_id(id)
.and_then(|def_id| self.tcx.lookup_deprecation(def_id.to_def_id()))
}
}

/// Creates a new diagnostic `Handler` that can be used to emit warnings and errors.
Expand Down
Loading