Skip to content

Commit

Permalink
Get rid of doctree::Function
Browse files Browse the repository at this point in the history
  • Loading branch information
jyn514 committed Nov 23, 2020
1 parent 090fc51 commit 50f0edc
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 133 deletions.
107 changes: 65 additions & 42 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,11 @@ 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)).flatten());
items.extend(self.traits.iter().map(|x| x.clean(cx)));
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 @@ -871,6 +869,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 @@ -880,34 +938,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 @@ -1927,7 +1957,7 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Ident>) {

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,6 +2007,10 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Ident>) {
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)
}
_ => unreachable!("not yet converted"),
};

Expand Down Expand Up @@ -2239,17 +2273,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
21 changes: 0 additions & 21 deletions src/librustdoc/doctree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
crate use self::StructType::*;

use rustc_ast as ast;
use rustc_span::hygiene::MacroKind;
use rustc_span::{self, symbol::Ident, Span, Symbol};

use rustc_hir as hir;
Expand All @@ -17,15 +16,13 @@ crate struct Module<'hir> {
crate where_inner: Span,
crate extern_crates: Vec<ExternCrate<'hir>>,
crate imports: Vec<Import<'hir>>,
crate fns: Vec<Function<'hir>>,
crate mods: Vec<Module<'hir>>,
crate id: hir::HirId,
// (item, renamed)
crate items: Vec<(&'hir hir::Item<'hir>, Option<Ident>)>,
crate traits: Vec<Trait<'hir>>,
crate foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option<Ident>)>,
crate macros: Vec<Macro>,
crate proc_macros: Vec<ProcMacro>,
crate is_crate: bool,
}

Expand All @@ -39,13 +36,11 @@ impl Module<'hir> {
attrs,
extern_crates: Vec::new(),
imports: Vec::new(),
fns: Vec::new(),
mods: Vec::new(),
items: Vec::new(),
traits: Vec::new(),
foreigns: Vec::new(),
macros: Vec::new(),
proc_macros: Vec::new(),
is_crate: false,
}
}
Expand All @@ -67,15 +62,6 @@ crate struct Variant<'hir> {
crate def: &'hir hir::VariantData<'hir>,
}

crate struct Function<'hir> {
crate decl: &'hir hir::FnDecl<'hir>,
crate id: hir::HirId,
crate name: Symbol,
crate header: hir::FnHeader,
crate generics: &'hir hir::Generics<'hir>,
crate body: hir::BodyId,
}

crate struct Trait<'hir> {
crate is_auto: hir::IsAuto,
crate unsafety: hir::Unsafety,
Expand Down Expand Up @@ -117,13 +103,6 @@ crate struct Import<'hir> {
crate span: Span,
}

crate struct ProcMacro {
crate name: Symbol,
crate id: hir::HirId,
crate kind: MacroKind,
crate helpers: Vec<Symbol>,
}

crate fn struct_type_from_def(vdata: &hir::VariantData<'_>) -> StructType {
match *vdata {
hir::VariantData::Struct(..) => Plain,
Expand Down
64 changes: 2 additions & 62 deletions src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_hir::Node;
use rustc_middle::middle::privacy::AccessLevel;
use rustc_middle::ty::TyCtxt;
use rustc_span::hygiene::MacroKind;
use rustc_span::source_map::Spanned;
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{self, Span};
Expand Down Expand Up @@ -82,63 +81,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
module
}

fn visit_fn(
&mut self,
om: &mut Module<'tcx>,
item: &'tcx hir::Item<'_>,
name: Symbol,
decl: &'tcx hir::FnDecl<'_>,
header: hir::FnHeader,
generics: &'tcx hir::Generics<'_>,
body: hir::BodyId,
) {
debug!("visiting fn");
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) => {
let name = if kind == MacroKind::Derive {
item.attrs
.lists(sym::proc_macro_derive)
.find_map(|mi| mi.ident())
.expect("proc-macro derives require a name")
.name
} else {
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);
}
}
}
}

om.proc_macros.push(ProcMacro { name, id: item.hir_id, kind, helpers });
}
None => {
om.fns.push(Function { id: item.hir_id, decl, name, generics, header, body });
}
}
}

fn visit_mod_contents(
&mut self,
span: Span,
Expand Down Expand Up @@ -370,10 +312,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
Some(ident.name),
));
}
hir::ItemKind::Fn(ref sig, ref gen, body) => {
self.visit_fn(om, item, ident.name, &sig.decl, sig.header, gen, body)
}
hir::ItemKind::Enum(..)
hir::ItemKind::Fn(..)
| hir::ItemKind::Enum(..)
| hir::ItemKind::Struct(..)
| hir::ItemKind::Union(..)
| hir::ItemKind::TyAlias(..)
Expand Down
16 changes: 8 additions & 8 deletions src/test/rustdoc-ui/intra-links-warning.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ warning: unresolved link to `Qux::Z`
LL | //! , [Uniooon::X] and [Qux::Z].
| ^^^^^^ no item named `Qux` in scope

warning: unresolved link to `Qux:Y`
--> $DIR/intra-links-warning.rs:14:13
|
LL | /// [Qux:Y]
| ^^^^^ no item named `Qux:Y` in scope
|
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`

warning: unresolved link to `BarA`
--> $DIR/intra-links-warning.rs:21:10
|
Expand Down Expand Up @@ -90,14 +98,6 @@ LL | f!("Foo\nbar [BarF] bar\nbaz");
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

warning: unresolved link to `Qux:Y`
--> $DIR/intra-links-warning.rs:14:13
|
LL | /// [Qux:Y]
| ^^^^^ no item named `Qux:Y` in scope
|
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`

warning: unresolved link to `error`
--> $DIR/intra-links-warning.rs:58:30
|
Expand Down

0 comments on commit 50f0edc

Please sign in to comment.