Skip to content

Commit a3a5d2c

Browse files
committed
Get rid of doctree::Function
1 parent 35e7bee commit a3a5d2c

File tree

4 files changed

+75
-133
lines changed

4 files changed

+75
-133
lines changed

src/librustdoc/clean/mod.rs

+65-42
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,11 @@ impl Clean<Item> for doctree::Module<'_> {
231231
let mut items: Vec<Item> = vec![];
232232
items.extend(self.extern_crates.iter().flat_map(|x| x.clean(cx)));
233233
items.extend(self.imports.iter().flat_map(|x| x.clean(cx)));
234-
items.extend(self.fns.iter().map(|x| x.clean(cx)));
235234
items.extend(self.foreigns.iter().map(|x| x.clean(cx)));
236235
items.extend(self.mods.iter().map(|x| x.clean(cx)));
237236
items.extend(self.items.iter().map(|x| x.clean(cx)).flatten());
238237
items.extend(self.traits.iter().map(|x| x.clean(cx)));
239238
items.extend(self.macros.iter().map(|x| x.clean(cx)));
240-
items.extend(self.proc_macros.iter().map(|x| x.clean(cx)));
241239

242240
// determine if we should display the inner contents or
243241
// the outer `mod` item for the source code.
@@ -871,6 +869,66 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx
871869
}
872870
}
873871

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

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-
911941
impl<'a> Clean<Arguments> for (&'a [hir::Ty<'a>], &'a [Ident]) {
912942
fn clean(&self, cx: &DocContext<'_>) -> Arguments {
913943
Arguments {
@@ -1927,7 +1957,7 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Ident>) {
19271957

19281958
let (item, renamed) = self;
19291959
let def_id = cx.tcx.hir().local_def_id(item.hir_id).to_def_id();
1930-
let name = match renamed {
1960+
let mut name = match renamed {
19311961
Some(ident) => ident.name,
19321962
None => cx.tcx.hir().name(item.hir_id),
19331963
};
@@ -1977,6 +2007,10 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Ident>) {
19772007
fields_stripped: false,
19782008
}),
19792009
ItemKind::Impl { .. } => return clean_impl(item, cx),
2010+
// proc macros can have a name set by attributes
2011+
ItemKind::Fn(ref sig, ref generics, body_id) => {
2012+
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)
2013+
}
19802014
_ => unreachable!("not yet converted"),
19812015
};
19822016

@@ -2239,17 +2273,6 @@ impl Clean<Item> for doctree::Macro {
22392273
}
22402274
}
22412275

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

src/librustdoc/doctree.rs

-21
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
crate use self::StructType::*;
44

55
use rustc_ast as ast;
6-
use rustc_span::hygiene::MacroKind;
76
use rustc_span::{self, symbol::Ident, Span, Symbol};
87

98
use rustc_hir as hir;
@@ -17,15 +16,13 @@ crate struct Module<'hir> {
1716
crate where_inner: Span,
1817
crate extern_crates: Vec<ExternCrate<'hir>>,
1918
crate imports: Vec<Import<'hir>>,
20-
crate fns: Vec<Function<'hir>>,
2119
crate mods: Vec<Module<'hir>>,
2220
crate id: hir::HirId,
2321
// (item, renamed)
2422
crate items: Vec<(&'hir hir::Item<'hir>, Option<Ident>)>,
2523
crate traits: Vec<Trait<'hir>>,
2624
crate foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option<Ident>)>,
2725
crate macros: Vec<Macro>,
28-
crate proc_macros: Vec<ProcMacro>,
2926
crate is_crate: bool,
3027
}
3128

@@ -39,13 +36,11 @@ impl Module<'hir> {
3936
attrs,
4037
extern_crates: Vec::new(),
4138
imports: Vec::new(),
42-
fns: Vec::new(),
4339
mods: Vec::new(),
4440
items: Vec::new(),
4541
traits: Vec::new(),
4642
foreigns: Vec::new(),
4743
macros: Vec::new(),
48-
proc_macros: Vec::new(),
4944
is_crate: false,
5045
}
5146
}
@@ -67,15 +62,6 @@ crate struct Variant<'hir> {
6762
crate def: &'hir hir::VariantData<'hir>,
6863
}
6964

70-
crate struct Function<'hir> {
71-
crate decl: &'hir hir::FnDecl<'hir>,
72-
crate id: hir::HirId,
73-
crate name: Symbol,
74-
crate header: hir::FnHeader,
75-
crate generics: &'hir hir::Generics<'hir>,
76-
crate body: hir::BodyId,
77-
}
78-
7965
crate struct Trait<'hir> {
8066
crate is_auto: hir::IsAuto,
8167
crate unsafety: hir::Unsafety,
@@ -117,13 +103,6 @@ crate struct Import<'hir> {
117103
crate span: Span,
118104
}
119105

120-
crate struct ProcMacro {
121-
crate name: Symbol,
122-
crate id: hir::HirId,
123-
crate kind: MacroKind,
124-
crate helpers: Vec<Symbol>,
125-
}
126-
127106
crate fn struct_type_from_def(vdata: &hir::VariantData<'_>) -> StructType {
128107
match *vdata {
129108
hir::VariantData::Struct(..) => Plain,

src/librustdoc/visit_ast.rs

+2-62
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_hir::def_id::{DefId, LOCAL_CRATE};
99
use rustc_hir::Node;
1010
use rustc_middle::middle::privacy::AccessLevel;
1111
use rustc_middle::ty::TyCtxt;
12-
use rustc_span::hygiene::MacroKind;
1312
use rustc_span::source_map::Spanned;
1413
use rustc_span::symbol::{kw, sym, Ident, Symbol};
1514
use rustc_span::{self, Span};
@@ -82,63 +81,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
8281
module
8382
}
8483

85-
fn visit_fn(
86-
&mut self,
87-
om: &mut Module<'tcx>,
88-
item: &'tcx hir::Item<'_>,
89-
name: Symbol,
90-
decl: &'tcx hir::FnDecl<'_>,
91-
header: hir::FnHeader,
92-
generics: &'tcx hir::Generics<'_>,
93-
body: hir::BodyId,
94-
) {
95-
debug!("visiting fn");
96-
let macro_kind = item.attrs.iter().find_map(|a| {
97-
if a.has_name(sym::proc_macro) {
98-
Some(MacroKind::Bang)
99-
} else if a.has_name(sym::proc_macro_derive) {
100-
Some(MacroKind::Derive)
101-
} else if a.has_name(sym::proc_macro_attribute) {
102-
Some(MacroKind::Attr)
103-
} else {
104-
None
105-
}
106-
});
107-
match macro_kind {
108-
Some(kind) => {
109-
let name = if kind == MacroKind::Derive {
110-
item.attrs
111-
.lists(sym::proc_macro_derive)
112-
.find_map(|mi| mi.ident())
113-
.expect("proc-macro derives require a name")
114-
.name
115-
} else {
116-
name
117-
};
118-
119-
let mut helpers = Vec::new();
120-
for mi in item.attrs.lists(sym::proc_macro_derive) {
121-
if !mi.has_name(sym::attributes) {
122-
continue;
123-
}
124-
125-
if let Some(list) = mi.meta_item_list() {
126-
for inner_mi in list {
127-
if let Some(ident) = inner_mi.ident() {
128-
helpers.push(ident.name);
129-
}
130-
}
131-
}
132-
}
133-
134-
om.proc_macros.push(ProcMacro { name, id: item.hir_id, kind, helpers });
135-
}
136-
None => {
137-
om.fns.push(Function { id: item.hir_id, decl, name, generics, header, body });
138-
}
139-
}
140-
}
141-
14284
fn visit_mod_contents(
14385
&mut self,
14486
span: Span,
@@ -370,10 +312,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
370312
Some(ident.name),
371313
));
372314
}
373-
hir::ItemKind::Fn(ref sig, ref gen, body) => {
374-
self.visit_fn(om, item, ident.name, &sig.decl, sig.header, gen, body)
375-
}
376-
hir::ItemKind::Enum(..)
315+
hir::ItemKind::Fn(..)
316+
| hir::ItemKind::Enum(..)
377317
| hir::ItemKind::Struct(..)
378318
| hir::ItemKind::Union(..)
379319
| hir::ItemKind::TyAlias(..)

src/test/rustdoc-ui/intra-links-warning.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ warning: unresolved link to `Qux::Z`
3636
LL | //! , [Uniooon::X] and [Qux::Z].
3737
| ^^^^^^ no item named `Qux` in scope
3838

39+
warning: unresolved link to `Qux:Y`
40+
--> $DIR/intra-links-warning.rs:14:13
41+
|
42+
LL | /// [Qux:Y]
43+
| ^^^^^ no item named `Qux:Y` in scope
44+
|
45+
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
46+
3947
warning: unresolved link to `BarA`
4048
--> $DIR/intra-links-warning.rs:21:10
4149
|
@@ -90,14 +98,6 @@ LL | f!("Foo\nbar [BarF] bar\nbaz");
9098
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
9199
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
92100

93-
warning: unresolved link to `Qux:Y`
94-
--> $DIR/intra-links-warning.rs:14:13
95-
|
96-
LL | /// [Qux:Y]
97-
| ^^^^^ no item named `Qux:Y` in scope
98-
|
99-
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
100-
101101
warning: unresolved link to `error`
102102
--> $DIR/intra-links-warning.rs:58:30
103103
|

0 commit comments

Comments
 (0)