Skip to content

Commit 25a6910

Browse files
committed
Auto merge of #79335 - jyn514:doctree-crate, r=GuillaumeGomez
Get rid of doctree::{ExternalCrate, ForeignItem, Trait, Function} Closes #79314, closes #79331, closes #79332. Follow-up to #79264 and #79312, continues breaking up #78082. r? `@GuillaumeGomez`
2 parents 53d19b3 + 66e30ec commit 25a6910

File tree

4 files changed

+150
-268
lines changed

4 files changed

+150
-268
lines changed

src/librustdoc/clean/mod.rs

+134-108
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_attr as attr;
1414
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1515
use rustc_hir as hir;
1616
use rustc_hir::def::{CtorKind, DefKind, Res};
17-
use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX};
17+
use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
1818
use rustc_index::vec::{Idx, IndexVec};
1919
use rustc_infer::infer::region_constraints::{Constraint, RegionConstraintData};
2020
use rustc_middle::bug;
@@ -229,15 +229,11 @@ impl Clean<Item> for doctree::Module<'_> {
229229
let attrs = self.attrs.clean(cx);
230230

231231
let mut items: Vec<Item> = vec![];
232-
items.extend(self.extern_crates.iter().flat_map(|x| x.clean(cx)));
233232
items.extend(self.imports.iter().flat_map(|x| x.clean(cx)));
234-
items.extend(self.fns.iter().map(|x| x.clean(cx)));
235233
items.extend(self.foreigns.iter().map(|x| x.clean(cx)));
236234
items.extend(self.mods.iter().map(|x| x.clean(cx)));
237235
items.extend(self.items.iter().map(|x| x.clean(cx)).flatten());
238-
items.extend(self.traits.iter().map(|x| x.clean(cx)));
239236
items.extend(self.macros.iter().map(|x| x.clean(cx)));
240-
items.extend(self.proc_macros.iter().map(|x| x.clean(cx)));
241237

242238
// determine if we should display the inner contents or
243239
// the outer `mod` item for the source code.
@@ -871,6 +867,66 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx
871867
}
872868
}
873869

870+
fn clean_fn_or_proc_macro(
871+
item: &hir::Item<'_>,
872+
sig: &'a hir::FnSig<'a>,
873+
generics: &'a hir::Generics<'a>,
874+
body_id: hir::BodyId,
875+
name: &mut Symbol,
876+
cx: &DocContext<'_>,
877+
) -> ItemKind {
878+
let macro_kind = item.attrs.iter().find_map(|a| {
879+
if a.has_name(sym::proc_macro) {
880+
Some(MacroKind::Bang)
881+
} else if a.has_name(sym::proc_macro_derive) {
882+
Some(MacroKind::Derive)
883+
} else if a.has_name(sym::proc_macro_attribute) {
884+
Some(MacroKind::Attr)
885+
} else {
886+
None
887+
}
888+
});
889+
match macro_kind {
890+
Some(kind) => {
891+
if kind == MacroKind::Derive {
892+
*name = item
893+
.attrs
894+
.lists(sym::proc_macro_derive)
895+
.find_map(|mi| mi.ident())
896+
.expect("proc-macro derives require a name")
897+
.name;
898+
}
899+
900+
let mut helpers = Vec::new();
901+
for mi in item.attrs.lists(sym::proc_macro_derive) {
902+
if !mi.has_name(sym::attributes) {
903+
continue;
904+
}
905+
906+
if let Some(list) = mi.meta_item_list() {
907+
for inner_mi in list {
908+
if let Some(ident) = inner_mi.ident() {
909+
helpers.push(ident.name);
910+
}
911+
}
912+
}
913+
}
914+
ProcMacroItem(ProcMacro { kind, helpers: helpers.clean(cx) })
915+
}
916+
None => {
917+
let mut func = (sig, generics, body_id).clean(cx);
918+
let def_id = cx.tcx.hir().local_def_id(item.hir_id).to_def_id();
919+
func.header.constness =
920+
if is_const_fn(cx.tcx, def_id) && is_unstable_const_fn(cx.tcx, def_id).is_none() {
921+
hir::Constness::Const
922+
} else {
923+
hir::Constness::NotConst
924+
};
925+
FunctionItem(func)
926+
}
927+
}
928+
}
929+
874930
impl<'a> Clean<Function> for (&'a hir::FnSig<'a>, &'a hir::Generics<'a>, hir::BodyId) {
875931
fn clean(&self, cx: &DocContext<'_>) -> Function {
876932
let (generics, decl) =
@@ -880,34 +936,6 @@ impl<'a> Clean<Function> for (&'a hir::FnSig<'a>, &'a hir::Generics<'a>, hir::Bo
880936
}
881937
}
882938

883-
impl Clean<Item> for doctree::Function<'_> {
884-
fn clean(&self, cx: &DocContext<'_>) -> Item {
885-
let (generics, decl) =
886-
enter_impl_trait(cx, || (self.generics.clean(cx), (self.decl, self.body).clean(cx)));
887-
888-
let did = cx.tcx.hir().local_def_id(self.id).to_def_id();
889-
let constness = if is_const_fn(cx.tcx, did) && !is_unstable_const_fn(cx.tcx, did).is_some()
890-
{
891-
hir::Constness::Const
892-
} else {
893-
hir::Constness::NotConst
894-
};
895-
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
896-
Item::from_def_id_and_parts(
897-
did,
898-
Some(self.name),
899-
FunctionItem(Function {
900-
decl,
901-
generics,
902-
header: hir::FnHeader { constness, ..self.header },
903-
all_types,
904-
ret_types,
905-
}),
906-
cx,
907-
)
908-
}
909-
}
910-
911939
impl<'a> Clean<Arguments> for (&'a [hir::Ty<'a>], &'a [Ident]) {
912940
fn clean(&self, cx: &DocContext<'_>) -> Arguments {
913941
Arguments {
@@ -992,26 +1020,6 @@ impl Clean<FnRetTy> for hir::FnRetTy<'_> {
9921020
}
9931021
}
9941022

995-
impl Clean<Item> for doctree::Trait<'_> {
996-
fn clean(&self, cx: &DocContext<'_>) -> Item {
997-
let attrs = self.attrs.clean(cx);
998-
let is_spotlight = attrs.has_doc_flag(sym::spotlight);
999-
Item::from_hir_id_and_parts(
1000-
self.id,
1001-
Some(self.name),
1002-
TraitItem(Trait {
1003-
unsafety: self.unsafety,
1004-
items: self.items.iter().map(|ti| ti.clean(cx)).collect(),
1005-
generics: self.generics.clean(cx),
1006-
bounds: self.bounds.clean(cx),
1007-
is_spotlight,
1008-
is_auto: self.is_auto.clean(cx),
1009-
}),
1010-
cx,
1011-
)
1012-
}
1013-
}
1014-
10151023
impl Clean<bool> for hir::IsAuto {
10161024
fn clean(&self, _: &DocContext<'_>) -> bool {
10171025
match *self {
@@ -1927,7 +1935,7 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Ident>) {
19271935

19281936
let (item, renamed) = self;
19291937
let def_id = cx.tcx.hir().local_def_id(item.hir_id).to_def_id();
1930-
let name = match renamed {
1938+
let mut name = match renamed {
19311939
Some(ident) => ident.name,
19321940
None => cx.tcx.hir().name(item.hir_id),
19331941
};
@@ -1977,6 +1985,27 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Ident>) {
19771985
fields_stripped: false,
19781986
}),
19791987
ItemKind::Impl { .. } => return clean_impl(item, cx),
1988+
// proc macros can have a name set by attributes
1989+
ItemKind::Fn(ref sig, ref generics, body_id) => {
1990+
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)
1991+
}
1992+
hir::ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref item_ids) => {
1993+
let items =
1994+
item_ids.iter().map(|ti| cx.tcx.hir().trait_item(ti.id).clean(cx)).collect();
1995+
let attrs = item.attrs.clean(cx);
1996+
let is_spotlight = attrs.has_doc_flag(sym::spotlight);
1997+
TraitItem(Trait {
1998+
unsafety,
1999+
items,
2000+
generics: generics.clean(cx),
2001+
bounds: bounds.clean(cx),
2002+
is_spotlight,
2003+
is_auto: is_auto.clean(cx),
2004+
})
2005+
}
2006+
ItemKind::ExternCrate(orig_name) => {
2007+
return clean_extern_crate(item, name, orig_name, cx);
2008+
}
19802009
_ => unreachable!("not yet converted"),
19812010
};
19822011

@@ -2054,45 +2083,54 @@ fn clean_impl(impl_: &hir::Item<'_>, cx: &DocContext<'_>) -> Vec<Item> {
20542083
ret
20552084
}
20562085

2057-
impl Clean<Vec<Item>> for doctree::ExternCrate<'_> {
2058-
fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
2059-
let please_inline = self.vis.node.is_pub()
2060-
&& self.attrs.iter().any(|a| {
2061-
a.has_name(sym::doc)
2062-
&& match a.meta_item_list() {
2063-
Some(l) => attr::list_contains_name(&l, sym::inline),
2064-
None => false,
2065-
}
2066-
});
2086+
fn clean_extern_crate(
2087+
krate: &hir::Item<'_>,
2088+
name: Symbol,
2089+
orig_name: Option<Symbol>,
2090+
cx: &DocContext<'_>,
2091+
) -> Vec<Item> {
2092+
// this is the ID of the `extern crate` statement
2093+
let def_id = cx.tcx.hir().local_def_id(krate.hir_id);
2094+
let cnum = cx.tcx.extern_mod_stmt_cnum(def_id).unwrap_or(LOCAL_CRATE);
2095+
// this is the ID of the crate itself
2096+
let crate_def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX };
2097+
let please_inline = krate.vis.node.is_pub()
2098+
&& krate.attrs.iter().any(|a| {
2099+
a.has_name(sym::doc)
2100+
&& match a.meta_item_list() {
2101+
Some(l) => attr::list_contains_name(&l, sym::inline),
2102+
None => false,
2103+
}
2104+
});
20672105

2068-
if please_inline {
2069-
let mut visited = FxHashSet::default();
2106+
if please_inline {
2107+
let mut visited = FxHashSet::default();
20702108

2071-
let res = Res::Def(DefKind::Mod, DefId { krate: self.cnum, index: CRATE_DEF_INDEX });
2109+
let res = Res::Def(DefKind::Mod, crate_def_id);
20722110

2073-
if let Some(items) = inline::try_inline(
2074-
cx,
2075-
cx.tcx.parent_module(self.hir_id).to_def_id(),
2076-
res,
2077-
self.name,
2078-
Some(self.attrs),
2079-
&mut visited,
2080-
) {
2081-
return items;
2082-
}
2111+
if let Some(items) = inline::try_inline(
2112+
cx,
2113+
cx.tcx.parent_module(krate.hir_id).to_def_id(),
2114+
res,
2115+
name,
2116+
Some(krate.attrs),
2117+
&mut visited,
2118+
) {
2119+
return items;
20832120
}
2084-
2085-
vec![Item {
2086-
name: None,
2087-
attrs: self.attrs.clean(cx),
2088-
source: self.span.clean(cx),
2089-
def_id: DefId { krate: self.cnum, index: CRATE_DEF_INDEX },
2090-
visibility: self.vis.clean(cx),
2091-
stability: None,
2092-
deprecation: None,
2093-
kind: ExternCrateItem(self.name.clean(cx), self.path.clone()),
2094-
}]
20952121
}
2122+
let path = orig_name.map(|x| x.to_string());
2123+
// FIXME: using `from_def_id_and_kind` breaks `rustdoc/masked` for some reason
2124+
vec![Item {
2125+
name: None,
2126+
attrs: krate.attrs.clean(cx),
2127+
source: krate.span.clean(cx),
2128+
def_id: crate_def_id,
2129+
visibility: krate.vis.clean(cx),
2130+
stability: None,
2131+
deprecation: None,
2132+
kind: ExternCrateItem(name.clean(cx), path),
2133+
}]
20962134
}
20972135

20982136
impl Clean<Vec<Item>> for doctree::Import<'_> {
@@ -2186,11 +2224,12 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
21862224
}
21872225
}
21882226

2189-
impl Clean<Item> for doctree::ForeignItem<'_> {
2227+
impl Clean<Item> for (&hir::ForeignItem<'_>, Option<Ident>) {
21902228
fn clean(&self, cx: &DocContext<'_>) -> Item {
2191-
let kind = match self.kind {
2229+
let (item, renamed) = self;
2230+
let kind = match item.kind {
21922231
hir::ForeignItemKind::Fn(ref decl, ref names, ref generics) => {
2193-
let abi = cx.tcx.hir().get_foreign_abi(self.id);
2232+
let abi = cx.tcx.hir().get_foreign_abi(item.hir_id);
21942233
let (generics, decl) =
21952234
enter_impl_trait(cx, || (generics.clean(cx), (&**decl, &names[..]).clean(cx)));
21962235
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
@@ -2207,15 +2246,13 @@ impl Clean<Item> for doctree::ForeignItem<'_> {
22072246
ret_types,
22082247
})
22092248
}
2210-
hir::ForeignItemKind::Static(ref ty, mutbl) => ForeignStaticItem(Static {
2211-
type_: ty.clean(cx),
2212-
mutability: *mutbl,
2213-
expr: String::new(),
2214-
}),
2249+
hir::ForeignItemKind::Static(ref ty, mutability) => {
2250+
ForeignStaticItem(Static { type_: ty.clean(cx), mutability, expr: String::new() })
2251+
}
22152252
hir::ForeignItemKind::Type => ForeignTypeItem,
22162253
};
22172254

2218-
Item::from_hir_id_and_parts(self.id, Some(self.name), kind, cx)
2255+
Item::from_hir_id_and_parts(item.hir_id, Some(renamed.unwrap_or(item.ident).name), kind, cx)
22192256
}
22202257
}
22212258

@@ -2240,17 +2277,6 @@ impl Clean<Item> for doctree::Macro {
22402277
}
22412278
}
22422279

2243-
impl Clean<Item> for doctree::ProcMacro {
2244-
fn clean(&self, cx: &DocContext<'_>) -> Item {
2245-
Item::from_hir_id_and_parts(
2246-
self.id,
2247-
Some(self.name),
2248-
ProcMacroItem(ProcMacro { kind: self.kind, helpers: self.helpers.clean(cx) }),
2249-
cx,
2250-
)
2251-
}
2252-
}
2253-
22542280
impl Clean<Deprecation> for attr::Deprecation {
22552281
fn clean(&self, _: &DocContext<'_>) -> Deprecation {
22562282
Deprecation {

0 commit comments

Comments
 (0)