From 66e30ec712eedc535fdb6a5394e0406ef4ddc737 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 22 Nov 2020 21:51:57 -0500 Subject: [PATCH] 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 ac02818224bc..8356e50f6374 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 6bcd6f0c1e9c..20f747e20141 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 d9326b56e9c1..e57717dab76c 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(..)