From 50f0edc4e2f5e4f05c2724faa23b523c3ba20d64 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 22 Nov 2020 21:20:08 -0500 Subject: [PATCH] Get rid of doctree::Function --- src/librustdoc/clean/mod.rs | 107 +++++++++++------- src/librustdoc/doctree.rs | 21 ---- src/librustdoc/visit_ast.rs | 64 +---------- .../rustdoc-ui/intra-links-warning.stderr | 16 +-- 4 files changed, 75 insertions(+), 133 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index c0786c1c127b8..752918250d802 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -231,13 +231,11 @@ impl Clean for doctree::Module<'_> { let mut items: Vec = 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. @@ -871,6 +869,66 @@ impl<'a, 'tcx> Clean 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 for (&'a hir::FnSig<'a>, &'a hir::Generics<'a>, hir::BodyId) { fn clean(&self, cx: &DocContext<'_>) -> Function { let (generics, decl) = @@ -880,34 +938,6 @@ impl<'a> Clean for (&'a hir::FnSig<'a>, &'a hir::Generics<'a>, hir::Bo } } -impl Clean 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 for (&'a [hir::Ty<'a>], &'a [Ident]) { fn clean(&self, cx: &DocContext<'_>) -> Arguments { Arguments { @@ -1927,7 +1957,7 @@ impl Clean> for (&hir::Item<'_>, Option) { 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), }; @@ -1977,6 +2007,10 @@ impl Clean> for (&hir::Item<'_>, Option) { 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"), }; @@ -2239,17 +2273,6 @@ impl Clean for doctree::Macro { } } -impl Clean 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 for attr::Deprecation { fn clean(&self, _: &DocContext<'_>) -> Deprecation { Deprecation { diff --git a/src/librustdoc/doctree.rs b/src/librustdoc/doctree.rs index 1ceaed45ea9cf..c6c11164e7df1 100644 --- a/src/librustdoc/doctree.rs +++ b/src/librustdoc/doctree.rs @@ -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; @@ -17,7 +16,6 @@ crate struct Module<'hir> { crate where_inner: Span, crate extern_crates: Vec>, crate imports: Vec>, - crate fns: Vec>, crate mods: Vec>, crate id: hir::HirId, // (item, renamed) @@ -25,7 +23,6 @@ crate struct Module<'hir> { crate traits: Vec>, crate foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option)>, crate macros: Vec, - crate proc_macros: Vec, crate is_crate: bool, } @@ -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, } } @@ -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, @@ -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, -} - crate fn struct_type_from_def(vdata: &hir::VariantData<'_>) -> StructType { match *vdata { hir::VariantData::Struct(..) => Plain, diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 03e31da371105..624d4d07cc031 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -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}; @@ -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, @@ -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(..) diff --git a/src/test/rustdoc-ui/intra-links-warning.stderr b/src/test/rustdoc-ui/intra-links-warning.stderr index bf437a7cf4674..0b074e9d53e3d 100644 --- a/src/test/rustdoc-ui/intra-links-warning.stderr +++ b/src/test/rustdoc-ui/intra-links-warning.stderr @@ -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 | @@ -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 |