From 35e7beed0e5a01816f1f447dc7d2200e97137af1 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 22 Nov 2020 14:03:02 -0500 Subject: [PATCH 1/4] Get rid of doctree::ForeignItem --- src/librustdoc/clean/mod.rs | 17 ++++++++--------- src/librustdoc/doctree.rs | 8 +------- src/librustdoc/visit_ast.rs | 10 ++-------- 3 files changed, 11 insertions(+), 24 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index e76ca1022a943..c0786c1c127b8 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2186,11 +2186,12 @@ impl Clean> for doctree::Import<'_> { } } -impl Clean for doctree::ForeignItem<'_> { +impl Clean for (&hir::ForeignItem<'_>, Option) { 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); @@ -2207,15 +2208,13 @@ impl Clean 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) } } diff --git a/src/librustdoc/doctree.rs b/src/librustdoc/doctree.rs index 4d2fe04123bc2..1ceaed45ea9cf 100644 --- a/src/librustdoc/doctree.rs +++ b/src/librustdoc/doctree.rs @@ -23,7 +23,7 @@ crate struct Module<'hir> { // (item, renamed) crate items: Vec<(&'hir hir::Item<'hir>, Option)>, crate traits: Vec>, - crate foreigns: Vec>, + crate foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option)>, crate macros: Vec, crate proc_macros: Vec, crate is_crate: bool, @@ -87,12 +87,6 @@ crate struct Trait<'hir> { crate id: hir::HirId, } -crate struct ForeignItem<'hir> { - crate id: hir::HirId, - crate name: Symbol, - crate kind: &'hir hir::ForeignItemKind<'hir>, -} - // For Macro we store the DefId instead of the NodeId, since we also create // these imported macro_rules (which only have a DUMMY_NODE_ID). crate struct Macro { diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 37050a57ca017..03e31da371105 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -418,15 +418,9 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { om: &mut Module<'tcx>, ) { // If inlining we only want to include public functions. - if self.inlining && !item.vis.node.is_pub() { - return; + if !self.inlining || item.vis.node.is_pub() { + om.foreigns.push((item, renamed)); } - - om.foreigns.push(ForeignItem { - id: item.hir_id, - name: renamed.unwrap_or(item.ident).name, - kind: &item.kind, - }); } // Convert each `exported_macro` into a doc item. From a3a5d2cd133d659e25099eaa490f82fbfcdf57fd Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 22 Nov 2020 21:20:08 -0500 Subject: [PATCH 2/4] 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 | From 2a58fa06827f2b824176ed01643bd76852a2028a Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 22 Nov 2020 21:32:18 -0500 Subject: [PATCH 3/4] Get rid of doctree::Trait --- src/librustdoc/clean/mod.rs | 35 ++++++++++++++--------------------- src/librustdoc/doctree.rs | 13 ------------- src/librustdoc/visit_ast.rs | 15 +-------------- 3 files changed, 15 insertions(+), 48 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 752918250d802..ac02818224bc4 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -234,7 +234,6 @@ impl Clean for doctree::Module<'_> { 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))); // determine if we should display the inner contents or @@ -1022,26 +1021,6 @@ impl Clean for hir::FnRetTy<'_> { } } -impl Clean 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 for hir::IsAuto { fn clean(&self, _: &DocContext<'_>) -> bool { match *self { @@ -2011,6 +1990,20 @@ impl Clean> for (&hir::Item<'_>, Option) { 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"), }; diff --git a/src/librustdoc/doctree.rs b/src/librustdoc/doctree.rs index c6c11164e7df1..6bcd6f0c1e9c3 100644 --- a/src/librustdoc/doctree.rs +++ b/src/librustdoc/doctree.rs @@ -20,7 +20,6 @@ crate struct Module<'hir> { crate id: hir::HirId, // (item, renamed) crate items: Vec<(&'hir hir::Item<'hir>, Option)>, - crate traits: Vec>, crate foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option)>, crate macros: Vec, crate is_crate: bool, @@ -38,7 +37,6 @@ impl Module<'hir> { imports: Vec::new(), mods: Vec::new(), items: Vec::new(), - traits: Vec::new(), foreigns: Vec::new(), macros: Vec::new(), is_crate: false, @@ -62,17 +60,6 @@ crate struct Variant<'hir> { crate def: &'hir hir::VariantData<'hir>, } -crate struct Trait<'hir> { - crate is_auto: hir::IsAuto, - crate unsafety: hir::Unsafety, - crate name: Symbol, - crate items: Vec<&'hir hir::TraitItem<'hir>>, - crate generics: &'hir hir::Generics<'hir>, - crate bounds: &'hir [hir::GenericBound<'hir>], - crate attrs: &'hir [ast::Attribute], - crate id: hir::HirId, -} - // For Macro we store the DefId instead of the NodeId, since we also create // these imported macro_rules (which only have a DUMMY_NODE_ID). crate struct Macro { diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 624d4d07cc031..d9326b56e9c10 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -319,6 +319,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { | hir::ItemKind::TyAlias(..) | hir::ItemKind::OpaqueTy(..) | hir::ItemKind::Static(..) + | hir::ItemKind::Trait(..) | hir::ItemKind::TraitAlias(..) => om.items.push((item, renamed)), hir::ItemKind::Const(..) => { // Underscore constants do not correspond to a nameable item and @@ -327,20 +328,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { om.items.push((item, renamed)); } } - hir::ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref item_ids) => { - let items = item_ids.iter().map(|ti| self.cx.tcx.hir().trait_item(ti.id)).collect(); - let t = Trait { - is_auto, - unsafety, - name: ident.name, - items, - generics, - bounds, - id: item.hir_id, - attrs: &item.attrs, - }; - om.traits.push(t); - } hir::ItemKind::Impl { ref of_trait, .. } => { // Don't duplicate impls when inlining or if it's implementing a trait, we'll pick // them up regardless of where they're located. From 66e30ec712eedc535fdb6a5394e0406ef4ddc737 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 22 Nov 2020 21:51:57 -0500 Subject: [PATCH 4/4] Get rid of doctree::ExternCrate --- src/librustdoc/clean/mod.rs | 83 +++++++++++++++++++++---------------- src/librustdoc/doctree.rs | 14 ------- src/librustdoc/visit_ast.rs | 15 +------ 3 files changed, 49 insertions(+), 63 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index ac02818224bc4..8356e50f6374a 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -14,7 +14,7 @@ use rustc_attr as attr; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_hir as hir; use rustc_hir::def::{CtorKind, DefKind, Res}; -use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX}; +use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE}; use rustc_index::vec::{Idx, IndexVec}; use rustc_infer::infer::region_constraints::{Constraint, RegionConstraintData}; use rustc_middle::bug; @@ -229,7 +229,6 @@ impl Clean for doctree::Module<'_> { let attrs = self.attrs.clean(cx); 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.foreigns.iter().map(|x| x.clean(cx))); items.extend(self.mods.iter().map(|x| x.clean(cx))); @@ -2004,6 +2003,9 @@ impl Clean> for (&hir::Item<'_>, Option) { is_auto: is_auto.clean(cx), }) } + ItemKind::ExternCrate(orig_name) => { + return clean_extern_crate(item, name, orig_name, cx); + } _ => unreachable!("not yet converted"), }; @@ -2081,45 +2083,54 @@ fn clean_impl(impl_: &hir::Item<'_>, cx: &DocContext<'_>) -> Vec { ret } -impl Clean> for doctree::ExternCrate<'_> { - fn clean(&self, cx: &DocContext<'_>) -> Vec { - let please_inline = self.vis.node.is_pub() - && self.attrs.iter().any(|a| { - a.has_name(sym::doc) - && match a.meta_item_list() { - Some(l) => attr::list_contains_name(&l, sym::inline), - None => false, - } - }); +fn clean_extern_crate( + krate: &hir::Item<'_>, + name: Symbol, + orig_name: Option, + cx: &DocContext<'_>, +) -> Vec { + // this is the ID of the `extern crate` statement + let def_id = cx.tcx.hir().local_def_id(krate.hir_id); + let cnum = cx.tcx.extern_mod_stmt_cnum(def_id).unwrap_or(LOCAL_CRATE); + // this is the ID of the crate itself + let crate_def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX }; + let please_inline = krate.vis.node.is_pub() + && krate.attrs.iter().any(|a| { + a.has_name(sym::doc) + && match a.meta_item_list() { + Some(l) => attr::list_contains_name(&l, sym::inline), + None => false, + } + }); - if please_inline { - let mut visited = FxHashSet::default(); + if please_inline { + let mut visited = FxHashSet::default(); - let res = Res::Def(DefKind::Mod, DefId { krate: self.cnum, index: CRATE_DEF_INDEX }); + let res = Res::Def(DefKind::Mod, crate_def_id); - if let Some(items) = inline::try_inline( - cx, - cx.tcx.parent_module(self.hir_id).to_def_id(), - res, - self.name, - Some(self.attrs), - &mut visited, - ) { - return items; - } + if let Some(items) = inline::try_inline( + cx, + cx.tcx.parent_module(krate.hir_id).to_def_id(), + res, + name, + Some(krate.attrs), + &mut visited, + ) { + return items; } - - vec![Item { - name: None, - attrs: self.attrs.clean(cx), - source: self.span.clean(cx), - def_id: DefId { krate: self.cnum, index: CRATE_DEF_INDEX }, - visibility: self.vis.clean(cx), - stability: None, - deprecation: None, - kind: ExternCrateItem(self.name.clean(cx), self.path.clone()), - }] } + let path = orig_name.map(|x| x.to_string()); + // FIXME: using `from_def_id_and_kind` breaks `rustdoc/masked` for some reason + vec![Item { + name: None, + attrs: krate.attrs.clean(cx), + source: krate.span.clean(cx), + def_id: crate_def_id, + visibility: krate.vis.clean(cx), + stability: None, + deprecation: None, + kind: ExternCrateItem(name.clean(cx), path), + }] } impl Clean> for doctree::Import<'_> { diff --git a/src/librustdoc/doctree.rs b/src/librustdoc/doctree.rs index 6bcd6f0c1e9c3..20f747e201415 100644 --- a/src/librustdoc/doctree.rs +++ b/src/librustdoc/doctree.rs @@ -6,15 +6,12 @@ use rustc_ast as ast; use rustc_span::{self, symbol::Ident, Span, Symbol}; use rustc_hir as hir; -use rustc_hir::def_id::CrateNum; -use rustc_hir::HirId; crate struct Module<'hir> { crate name: Option, crate attrs: &'hir [ast::Attribute], crate where_outer: Span, crate where_inner: Span, - crate extern_crates: Vec>, crate imports: Vec>, crate mods: Vec>, crate id: hir::HirId, @@ -33,7 +30,6 @@ impl Module<'hir> { where_outer: rustc_span::DUMMY_SP, where_inner: rustc_span::DUMMY_SP, attrs, - extern_crates: Vec::new(), imports: Vec::new(), mods: Vec::new(), items: Vec::new(), @@ -69,16 +65,6 @@ crate struct Macro { crate imported_from: Option, } -crate struct ExternCrate<'hir> { - crate name: Symbol, - crate hir_id: HirId, - crate cnum: CrateNum, - crate path: Option, - crate vis: &'hir hir::Visibility<'hir>, - crate attrs: &'hir [ast::Attribute], - crate span: Span, -} - #[derive(Debug)] crate struct Import<'hir> { crate name: Symbol, diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index d9326b56e9c10..e57717dab76cf 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -5,7 +5,7 @@ use rustc_ast as ast; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::def_id::{DefId, LOCAL_CRATE}; +use rustc_hir::def_id::DefId; use rustc_hir::Node; use rustc_middle::middle::privacy::AccessLevel; use rustc_middle::ty::TyCtxt; @@ -248,18 +248,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { // If we're inlining, skip private items. _ if self.inlining && !item.vis.node.is_pub() => {} hir::ItemKind::GlobalAsm(..) => {} - hir::ItemKind::ExternCrate(orig_name) => { - let def_id = self.cx.tcx.hir().local_def_id(item.hir_id); - om.extern_crates.push(ExternCrate { - cnum: self.cx.tcx.extern_mod_stmt_cnum(def_id).unwrap_or(LOCAL_CRATE), - name: ident.name, - hir_id: item.hir_id, - path: orig_name.map(|x| x.to_string()), - vis: &item.vis, - attrs: &item.attrs, - span: item.span, - }) - } hir::ItemKind::Use(_, hir::UseKind::ListStem) => {} hir::ItemKind::Use(ref path, kind) => { let is_glob = kind == hir::UseKind::Glob; @@ -313,6 +301,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { )); } hir::ItemKind::Fn(..) + | hir::ItemKind::ExternCrate(..) | hir::ItemKind::Enum(..) | hir::ItemKind::Struct(..) | hir::ItemKind::Union(..)