From 37100024de26207005a86b634d599f120309dd55 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Wed, 24 Jul 2019 12:59:53 -0400 Subject: [PATCH 1/9] Make fields of RustdocVisitor private --- src/librustdoc/clean/mod.rs | 6 +++--- src/librustdoc/core.rs | 4 ++-- src/librustdoc/visit_ast.rs | 14 ++++---------- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index d3accff5c2ce..006f73d5f665 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -138,7 +138,7 @@ pub struct Crate { pub masked_crates: FxHashSet, } -impl<'a, 'tcx> Clean for visit_ast::RustdocVisitor<'a, 'tcx> { +impl<'a, 'tcx> Clean for (visit_ast::RustdocVisitor<'a, 'tcx>, doctree::Module<'tcx>) { fn clean(&self, cx: &DocContext<'_>) -> Crate { use crate::visit_lib::LibEmbargoVisitor; @@ -159,7 +159,7 @@ impl<'a, 'tcx> Clean for visit_ast::RustdocVisitor<'a, 'tcx> { // Clean the crate, translating the entire libsyntax AST to one that is // understood by rustdoc. - let mut module = self.module.as_ref().unwrap().clean(cx); + let mut module = self.1.clean(cx); let mut masked_crates = FxHashSet::default(); match module.inner { @@ -169,7 +169,7 @@ impl<'a, 'tcx> Clean for visit_ast::RustdocVisitor<'a, 'tcx> { // `#[doc(masked)]` to the injected `extern crate` because it's unstable. if it.is_extern_crate() && (it.attrs.has_doc_flag(sym::masked) - || self.cx.tcx.is_compiler_builtins(it.def_id.krate)) + || cx.tcx.is_compiler_builtins(it.def_id.krate)) { masked_crates.insert(it.def_id.krate); } diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index cc79f4ab09a5..d09328547a3a 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -395,8 +395,8 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt let mut krate = { let mut v = RustdocVisitor::new(&ctxt); - v.visit(tcx.hir().krate()); - v.clean(&ctxt) + let module = v.visit(tcx.hir().krate()); + (v, module).clean(&ctxt) }; fn report_deprecated_attr(name: &str, diag: &errors::Handler) { diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 1ba2c0333d6b..02daea9925e1 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -20,17 +20,11 @@ use crate::clean::{self, AttributesExt, NestedAttributesExt, def_id_to_path}; use crate::doctree::*; -// Looks to me like the first two of these are actually -// output parameters, maybe only mutated once; perhaps -// better simply to have the visit method return a tuple -// containing them? - // Also, is there some reason that this doesn't use the 'visit' // framework from syntax?. pub struct RustdocVisitor<'a, 'tcx> { - pub module: Option>, - pub cx: &'a core::DocContext<'tcx>, + cx: &'a core::DocContext<'tcx>, view_item_stack: FxHashSet, inlining: bool, /// Are the current module and all of its parents public? @@ -46,7 +40,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { let mut stack = FxHashSet::default(); stack.insert(hir::CRATE_HIR_ID); RustdocVisitor { - module: None, cx, view_item_stack: stack, inlining: false, @@ -75,7 +68,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { .and_then(|def_id| self.cx.tcx.lookup_deprecation(def_id)) } - pub fn visit(&mut self, krate: &'tcx hir::Crate) { + pub fn visit(&mut self, krate: &'tcx hir::Crate) -> Module<'tcx> { let mut module = self.visit_mod_contents(krate.span, &krate.attrs, &Spanned { span: syntax_pos::DUMMY_SP, @@ -88,9 +81,10 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { krate.exported_macros.iter().map(|def| self.visit_local_macro(def, None)), ); module.is_crate = true; - self.module = Some(module); self.cx.renderinfo.borrow_mut().exact_paths = self.exact_paths.take().unwrap(); + + module } pub fn visit_variant_data(&mut self, item: &'tcx hir::Item, From 2d18504c275272ab102f039cf114e9409bac2464 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Wed, 24 Jul 2019 14:43:40 -0400 Subject: [PATCH 2/9] Remove Option from resolver --- src/librustc_interface/passes.rs | 18 +++++++++--------- src/librustc_interface/queries.rs | 14 +++++++------- src/librustdoc/core.rs | 8 +++----- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/librustc_interface/passes.rs b/src/librustc_interface/passes.rs index c9d68943b088..8b0b5a5b7a2b 100644 --- a/src/librustc_interface/passes.rs +++ b/src/librustc_interface/passes.rs @@ -58,7 +58,6 @@ use std::path::PathBuf; use std::sync::mpsc; use std::cell::RefCell; use std::rc::Rc; -use std::mem; pub fn parse<'a>(sess: &'a Session, input: &Input) -> PResult<'a, ast::Crate> { sess.diagnostic() @@ -204,15 +203,16 @@ impl ExpansionResult { impl BoxedResolver { pub fn to_expansion_result( - mut resolver: Rc>>, + resolver: Rc>, ) -> ExpansionResult { - if let Some(resolver) = Rc::get_mut(&mut resolver) { - mem::replace(resolver, None).unwrap().into_inner().complete() - } else { - let resolver = &*resolver; - resolver.as_ref().unwrap().borrow_mut().access(|resolver| { - ExpansionResult::from_resolver_ref(resolver) - }) + match Rc::try_unwrap(resolver) { + Ok(resolver) => resolver.into_inner().complete(), + Err(resolver) => { + let resolver = &*resolver; + resolver.borrow_mut().access(|resolver| { + ExpansionResult::from_resolver_ref(resolver) + }) + } } } } diff --git a/src/librustc_interface/queries.rs b/src/librustc_interface/queries.rs index 2e7952cd0044..ed50dadb6009 100644 --- a/src/librustc_interface/queries.rs +++ b/src/librustc_interface/queries.rs @@ -76,7 +76,7 @@ pub(crate) struct Queries { parse: Query, crate_name: Query, register_plugins: Query<(ast::Crate, PluginInfo)>, - expansion: Query<(ast::Crate, Rc>>)>, + expansion: Query<(ast::Crate, Steal>>)>, dep_graph: Query, lower_to_hir: Query<(Steal, ExpansionResult)>, prepare_outputs: Query, @@ -142,7 +142,7 @@ impl Compiler { pub fn expansion( &self - ) -> Result<&Query<(ast::Crate, Rc>>)>> { + ) -> Result<&Query<(ast::Crate, Steal>>)>> { self.queries.expansion.compute(|| { let crate_name = self.crate_name()?.peek().clone(); let (krate, plugin_info) = self.register_plugins()?.take(); @@ -152,7 +152,7 @@ impl Compiler { krate, &crate_name, plugin_info, - ).map(|(krate, resolver)| (krate, Rc::new(Some(RefCell::new(resolver))))) + ).map(|(krate, resolver)| (krate, Steal::new(Rc::new(RefCell::new(resolver))))) }) } @@ -176,9 +176,10 @@ impl Compiler { pub fn lower_to_hir(&self) -> Result<&Query<(Steal, ExpansionResult)>> { self.queries.lower_to_hir.compute(|| { let expansion_result = self.expansion()?; - let (krate, resolver) = expansion_result.take(); - let resolver_ref = &*resolver; - let hir = Steal::new(resolver_ref.as_ref().unwrap().borrow_mut().access(|resolver| { + let peeked = expansion_result.peek(); + let krate = &peeked.0; + let resolver = peeked.1.steal(); + let hir = Steal::new(resolver.borrow_mut().access(|resolver| { passes::lower_to_hir( self.session(), self.cstore(), @@ -187,7 +188,6 @@ impl Compiler { &krate ) })?); - expansion_result.give((krate, Rc::new(None))); Ok((hir, BoxedResolver::to_expansion_result(resolver))) }) } diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index d09328547a3a..c2fb9fb7fb5e 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -45,7 +45,7 @@ pub type ExternalPaths = FxHashMap, clean::TypeKind)>; pub struct DocContext<'tcx> { pub tcx: TyCtxt<'tcx>, - pub resolver: Rc>>, + pub resolver: Rc>, /// The stack of module NodeIds up till this point pub crate_name: Option, pub cstore: Lrc, @@ -83,9 +83,7 @@ impl<'tcx> DocContext<'tcx> { pub fn enter_resolver(&self, f: F) -> R where F: FnOnce(&mut resolve::Resolver<'_>) -> R { - let resolver = &*self.resolver; - let resolver = resolver.as_ref().unwrap(); - resolver.borrow_mut().access(f) + self.resolver.borrow_mut().access(f) } /// Call the closure with the given parameters set as @@ -344,7 +342,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt // We need to hold on to the complete resolver, so we cause everything to be // cloned for the analysis passes to use. Suboptimal, but necessary in the // current architecture. - let resolver = abort_on_err(compiler.expansion(), sess).peek().1.clone(); + let resolver = abort_on_err(compiler.expansion(), sess).peek().1.borrow().clone(); if sess.has_errors() { sess.fatal("Compilation failed, aborting rustdoc"); From 2fadc4524d0c5b51f945422f8417e30d88ed0164 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Wed, 24 Jul 2019 15:24:25 -0400 Subject: [PATCH 3/9] Represent ownership transfer in RustdocVisitor::visit Previously visit could be called multiple times, but this is inaccurate, as it deconstructs Visitor state. --- src/librustdoc/clean/mod.rs | 6 ++++-- src/librustdoc/core.rs | 4 ++-- src/librustdoc/visit_ast.rs | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 006f73d5f665..693375daf43e 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -49,7 +49,6 @@ use parking_lot::ReentrantMutex; use crate::core::{self, DocContext}; use crate::doctree; -use crate::visit_ast; use crate::html::render::{cache, ExternalLocation}; use crate::html::item_type::ItemType; @@ -138,7 +137,10 @@ pub struct Crate { pub masked_crates: FxHashSet, } -impl<'a, 'tcx> Clean for (visit_ast::RustdocVisitor<'a, 'tcx>, doctree::Module<'tcx>) { +// The `()` here is rather ugly and would be great to remove. Unfortunately, we +// already have a different Clean impl for `doctree::Module` which makes this +// the only way to easily disambiguate. +impl<'tcx> Clean for ((), doctree::Module<'tcx>) { fn clean(&self, cx: &DocContext<'_>) -> Crate { use crate::visit_lib::LibEmbargoVisitor; diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index c2fb9fb7fb5e..2451557ca8c5 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -392,9 +392,9 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt debug!("crate: {:?}", tcx.hir().krate()); let mut krate = { - let mut v = RustdocVisitor::new(&ctxt); + let v = RustdocVisitor::new(&ctxt); let module = v.visit(tcx.hir().krate()); - (v, module).clean(&ctxt) + ((), module).clean(&ctxt) }; fn report_deprecated_attr(name: &str, diag: &errors::Handler) { diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 02daea9925e1..5c10fb9417f5 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -68,7 +68,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { .and_then(|def_id| self.cx.tcx.lookup_deprecation(def_id)) } - pub fn visit(&mut self, krate: &'tcx hir::Crate) -> Module<'tcx> { + pub fn visit(mut self, krate: &'tcx hir::Crate) -> Module<'tcx> { let mut module = self.visit_mod_contents(krate.span, &krate.attrs, &Spanned { span: syntax_pos::DUMMY_SP, From 6c5d212f5fc7fd287e43a771cf64d302caf5a289 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Wed, 24 Jul 2019 15:25:23 -0400 Subject: [PATCH 4/9] Make exact_paths a non-optional field on RustdocVisitor Also privatizes needlessly public methods to enforce which methods callers are intended to call, i.e., only `new` and `visit`. --- src/librustdoc/visit_ast.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 5c10fb9417f5..1676da34201d 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -29,7 +29,7 @@ pub struct RustdocVisitor<'a, 'tcx> { inlining: bool, /// Are the current module and all of its parents public? inside_public_path: bool, - exact_paths: Option>>, + exact_paths: FxHashMap>, } impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { @@ -44,17 +44,16 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { view_item_stack: stack, inlining: false, inside_public_path: true, - exact_paths: Some(FxHashMap::default()), + exact_paths: FxHashMap::default(), } } fn store_path(&mut self, did: DefId) { // We can't use the entry API, as that keeps the mutable borrow of `self` active // when we try to use `cx`. - let exact_paths = self.exact_paths.as_mut().unwrap(); - if exact_paths.get(&did).is_none() { + if self.exact_paths.get(&did).is_none() { let path = def_id_to_path(self.cx, did, self.cx.crate_name.clone()); - exact_paths.insert(did, path); + self.exact_paths.insert(did, path); } } @@ -82,12 +81,12 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { ); module.is_crate = true; - self.cx.renderinfo.borrow_mut().exact_paths = self.exact_paths.take().unwrap(); + self.cx.renderinfo.borrow_mut().exact_paths = self.exact_paths; module } - pub fn visit_variant_data(&mut self, item: &'tcx hir::Item, + fn visit_variant_data(&mut self, item: &'tcx hir::Item, name: ast::Name, sd: &'tcx hir::VariantData, generics: &'tcx hir::Generics) -> Struct<'tcx> { debug!("visiting struct"); @@ -106,7 +105,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { } } - pub fn visit_union_data(&mut self, item: &'tcx hir::Item, + fn visit_union_data(&mut self, item: &'tcx hir::Item, name: ast::Name, sd: &'tcx hir::VariantData, generics: &'tcx hir::Generics) -> Union<'tcx> { debug!("visiting union"); @@ -125,7 +124,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { } } - pub fn visit_enum_def(&mut self, it: &'tcx hir::Item, + fn visit_enum_def(&mut self, it: &'tcx hir::Item, name: ast::Name, def: &'tcx hir::EnumDef, generics: &'tcx hir::Generics) -> Enum<'tcx> { debug!("visiting enum"); @@ -150,7 +149,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { } } - pub fn visit_fn(&mut self, om: &mut Module<'tcx>, item: &'tcx hir::Item, + fn visit_fn(&mut self, om: &mut Module<'tcx>, item: &'tcx hir::Item, name: ast::Name, decl: &'tcx hir::FnDecl, header: hir::FnHeader, generics: &'tcx hir::Generics, @@ -223,7 +222,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { } } - pub fn visit_mod_contents(&mut self, span: Span, attrs: &'tcx hir::HirVec, + fn visit_mod_contents(&mut self, span: Span, attrs: &'tcx hir::HirVec, vis: &'tcx hir::Visibility, id: hir::HirId, m: &'tcx hir::Mod, name: Option) -> Module<'tcx> { @@ -363,7 +362,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { ret } - pub fn visit_item(&mut self, item: &'tcx hir::Item, + fn visit_item(&mut self, item: &'tcx hir::Item, renamed: Option, om: &mut Module<'tcx>) { debug!("visiting item {:?}", item); let ident = renamed.unwrap_or(item.ident); From 11735b6235d0f1cdccf41296c076ffb133e9f1b2 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Wed, 24 Jul 2019 16:18:32 -0400 Subject: [PATCH 5/9] Gather stability information during cleaning --- src/librustdoc/clean/mod.rs | 32 ++++++++++++++++---------------- src/librustdoc/core.rs | 6 ++++++ src/librustdoc/doctree.rs | 20 +++----------------- src/librustdoc/visit_ast.rs | 24 ++---------------------- 4 files changed, 27 insertions(+), 55 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 693375daf43e..c031e8d2bc15 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -654,7 +654,7 @@ impl Clean for doctree::Module<'_> { attrs, source: whence.clean(cx), visibility: self.vis.clean(cx), - stability: self.stab.clean(cx), + stability: cx.stability(self.hid).clean(cx), deprecation: self.depr.clean(cx), def_id: cx.tcx.hir().local_def_id_from_node_id(self.id), inner: ModuleItem(Module { @@ -1940,7 +1940,7 @@ impl Clean for doctree::Function<'_> { attrs: self.attrs.clean(cx), source: self.whence.clean(cx), visibility: self.vis.clean(cx), - stability: self.stab.clean(cx), + stability: cx.stability(self.id).clean(cx), deprecation: self.depr.clean(cx), def_id: did, inner: FunctionItem(Function { @@ -2140,7 +2140,7 @@ impl Clean for doctree::Trait<'_> { source: self.whence.clean(cx), def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), - stability: self.stab.clean(cx), + stability: cx.stability(self.id).clean(cx), deprecation: self.depr.clean(cx), inner: TraitItem(Trait { auto: self.is_auto.clean(cx), @@ -2170,7 +2170,7 @@ impl Clean for doctree::TraitAlias<'_> { source: self.whence.clean(cx), def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), - stability: self.stab.clean(cx), + stability: cx.stability(self.id).clean(cx), deprecation: self.depr.clean(cx), inner: TraitAliasItem(TraitAlias { generics: self.generics.clean(cx), @@ -3244,7 +3244,7 @@ impl Clean for doctree::Struct<'_> { source: self.whence.clean(cx), def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), - stability: self.stab.clean(cx), + stability: cx.stability(self.id).clean(cx), deprecation: self.depr.clean(cx), inner: StructItem(Struct { struct_type: self.struct_type, @@ -3264,7 +3264,7 @@ impl Clean for doctree::Union<'_> { source: self.whence.clean(cx), def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), - stability: self.stab.clean(cx), + stability: cx.stability(self.id).clean(cx), deprecation: self.depr.clean(cx), inner: UnionItem(Union { struct_type: self.struct_type, @@ -3311,7 +3311,7 @@ impl Clean for doctree::Enum<'_> { source: self.whence.clean(cx), def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), - stability: self.stab.clean(cx), + stability: cx.stability(self.id).clean(cx), deprecation: self.depr.clean(cx), inner: EnumItem(Enum { variants: self.variants.iter().map(|v| v.clean(cx)).collect(), @@ -3334,7 +3334,7 @@ impl Clean for doctree::Variant<'_> { attrs: self.attrs.clean(cx), source: self.whence.clean(cx), visibility: None, - stability: self.stab.clean(cx), + stability: cx.stability(self.id).clean(cx), deprecation: self.depr.clean(cx), def_id: cx.tcx.hir().local_def_id(self.id), inner: VariantItem(Variant { @@ -3639,7 +3639,7 @@ impl Clean for doctree::Typedef<'_> { source: self.whence.clean(cx), def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), - stability: self.stab.clean(cx), + stability: cx.stability(self.id).clean(cx), deprecation: self.depr.clean(cx), inner: TypedefItem(Typedef { type_: self.ty.clean(cx), @@ -3663,7 +3663,7 @@ impl Clean for doctree::OpaqueTy<'_> { source: self.whence.clean(cx), def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), - stability: self.stab.clean(cx), + stability: cx.stability(self.id).clean(cx), deprecation: self.depr.clean(cx), inner: OpaqueTyItem(OpaqueTy { bounds: self.opaque_ty.bounds.clean(cx), @@ -3714,7 +3714,7 @@ impl Clean for doctree::Static<'_> { source: self.whence.clean(cx), def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), - stability: self.stab.clean(cx), + stability: cx.stability(self.id).clean(cx), deprecation: self.depr.clean(cx), inner: StaticItem(Static { type_: self.type_.clean(cx), @@ -3739,7 +3739,7 @@ impl Clean for doctree::Constant<'_> { source: self.whence.clean(cx), def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), - stability: self.stab.clean(cx), + stability: cx.stability(self.id).clean(cx), deprecation: self.depr.clean(cx), inner: ConstantItem(Constant { type_: self.type_.clean(cx), @@ -3826,7 +3826,7 @@ impl Clean> for doctree::Impl<'_> { source: self.whence.clean(cx), def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), - stability: self.stab.clean(cx), + stability: cx.stability(self.id).clean(cx), deprecation: self.depr.clean(cx), inner: ImplItem(Impl { unsafety: self.unsafety, @@ -4065,7 +4065,7 @@ impl Clean for doctree::ForeignItem<'_> { source: self.whence.clean(cx), def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), - stability: self.stab.clean(cx), + stability: cx.stability(self.id).clean(cx), deprecation: self.depr.clean(cx), inner, } @@ -4248,7 +4248,7 @@ impl Clean for doctree::Macro<'_> { attrs: self.attrs.clean(cx), source: self.whence.clean(cx), visibility: Some(Public), - stability: self.stab.clean(cx), + stability: cx.stability(self.hid).clean(cx), deprecation: self.depr.clean(cx), def_id: self.def_id, inner: MacroItem(Macro { @@ -4276,7 +4276,7 @@ impl Clean for doctree::ProcMacro<'_> { attrs: self.attrs.clean(cx), source: self.whence.clean(cx), visibility: Some(Public), - stability: self.stab.clean(cx), + stability: cx.stability(self.id).clean(cx), deprecation: self.depr.clean(cx), def_id: cx.tcx.hir().local_def_id(self.id), inner: ProcMacroItem(ProcMacro { diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 2451557ca8c5..976d1b0b6b19 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -16,6 +16,7 @@ use rustc_metadata::cstore::CStore; use rustc_target::spec::TargetTriple; use syntax::source_map; +use syntax::attr; use syntax::feature_gate::UnstableFeatures; use syntax::json::JsonEmitter; use syntax::symbol::sym; @@ -165,6 +166,11 @@ impl<'tcx> DocContext<'tcx> { self.tcx.hir().as_local_hir_id(def_id) } } + + pub fn stability(&self, id: HirId) -> Option { + self.tcx.hir().opt_local_def_id(id) + .and_then(|def_id| self.tcx.lookup_stability(def_id)).cloned() + } } pub trait DocAccessLevels { diff --git a/src/librustdoc/doctree.rs b/src/librustdoc/doctree.rs index ec60241a92d4..c380c5e08a33 100644 --- a/src/librustdoc/doctree.rs +++ b/src/librustdoc/doctree.rs @@ -25,13 +25,13 @@ pub struct Module<'hir> { pub fns: Vec>, pub mods: Vec>, pub id: NodeId, + pub hid: hir::HirId, pub typedefs: Vec>, pub opaque_tys: Vec>, pub statics: Vec>, pub constants: Vec>, pub traits: Vec>, pub vis: &'hir hir::Visibility, - pub stab: Option, pub depr: Option, pub impls: Vec>, pub foreigns: Vec>, @@ -50,8 +50,8 @@ impl Module<'hir> { Module { name : name, id: ast::CRATE_NODE_ID, + hid: hir::CRATE_HIR_ID, vis, - stab: None, depr: None, where_outer: syntax_pos::DUMMY_SP, where_inner: syntax_pos::DUMMY_SP, @@ -90,7 +90,6 @@ pub enum StructType { pub struct Struct<'hir> { pub vis: &'hir hir::Visibility, - pub stab: Option, pub depr: Option, pub id: hir::HirId, pub struct_type: StructType, @@ -103,7 +102,6 @@ pub struct Struct<'hir> { pub struct Union<'hir> { pub vis: &'hir hir::Visibility, - pub stab: Option, pub depr: Option, pub id: hir::HirId, pub struct_type: StructType, @@ -116,7 +114,6 @@ pub struct Union<'hir> { pub struct Enum<'hir> { pub vis: &'hir hir::Visibility, - pub stab: Option, pub depr: Option, pub variants: Vec>, pub generics: &'hir hir::Generics, @@ -131,7 +128,6 @@ pub struct Variant<'hir> { pub id: hir::HirId, pub attrs: &'hir hir::HirVec, pub def: &'hir hir::VariantData, - pub stab: Option, pub depr: Option, pub whence: Span, } @@ -142,7 +138,6 @@ pub struct Function<'hir> { pub id: hir::HirId, pub name: Name, pub vis: &'hir hir::Visibility, - pub stab: Option, pub depr: Option, pub header: hir::FnHeader, pub whence: Span, @@ -158,7 +153,6 @@ pub struct Typedef<'hir> { pub attrs: &'hir hir::HirVec, pub whence: Span, pub vis: &'hir hir::Visibility, - pub stab: Option, pub depr: Option, } @@ -169,7 +163,6 @@ pub struct OpaqueTy<'hir> { pub attrs: &'hir hir::HirVec, pub whence: Span, pub vis: &'hir hir::Visibility, - pub stab: Option, pub depr: Option, } @@ -181,7 +174,6 @@ pub struct Static<'hir> { pub name: Name, pub attrs: &'hir hir::HirVec, pub vis: &'hir hir::Visibility, - pub stab: Option, pub depr: Option, pub id: hir::HirId, pub whence: Span, @@ -193,7 +185,6 @@ pub struct Constant<'hir> { pub name: Name, pub attrs: &'hir hir::HirVec, pub vis: &'hir hir::Visibility, - pub stab: Option, pub depr: Option, pub id: hir::HirId, pub whence: Span, @@ -210,7 +201,6 @@ pub struct Trait<'hir> { pub id: hir::HirId, pub whence: Span, pub vis: &'hir hir::Visibility, - pub stab: Option, pub depr: Option, } @@ -222,7 +212,6 @@ pub struct TraitAlias<'hir> { pub id: hir::HirId, pub whence: Span, pub vis: &'hir hir::Visibility, - pub stab: Option, pub depr: Option, } @@ -238,14 +227,12 @@ pub struct Impl<'hir> { pub attrs: &'hir hir::HirVec, pub whence: Span, pub vis: &'hir hir::Visibility, - pub stab: Option, pub depr: Option, pub id: hir::HirId, } pub struct ForeignItem<'hir> { pub vis: &'hir hir::Visibility, - pub stab: Option, pub depr: Option, pub id: hir::HirId, pub name: Name, @@ -258,11 +245,11 @@ pub struct ForeignItem<'hir> { // these imported macro_rules (which only have a DUMMY_NODE_ID). pub struct Macro<'hir> { pub name: Name, + pub hid: hir::HirId, pub def_id: hir::def_id::DefId, pub attrs: &'hir hir::HirVec, pub whence: Span, pub matchers: hir::HirVec, - pub stab: Option, pub depr: Option, pub imported_from: Option, } @@ -293,7 +280,6 @@ pub struct ProcMacro<'hir> { pub helpers: Vec, pub attrs: &'hir hir::HirVec, pub whence: Span, - pub stab: Option, pub depr: Option, } diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 1676da34201d..56c91bf405fc 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -57,11 +57,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { } } - fn stability(&self, id: hir::HirId) -> Option { - self.cx.tcx.hir().opt_local_def_id(id) - .and_then(|def_id| self.cx.tcx.lookup_stability(def_id)).cloned() - } - fn deprecation(&self, id: hir::HirId) -> Option { self.cx.tcx.hir().opt_local_def_id(id) .and_then(|def_id| self.cx.tcx.lookup_deprecation(def_id)) @@ -96,7 +91,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { struct_type, name, vis: &item.vis, - stab: self.stability(item.hir_id), depr: self.deprecation(item.hir_id), attrs: &item.attrs, generics, @@ -115,7 +109,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { struct_type, name, vis: &item.vis, - stab: self.stability(item.hir_id), depr: self.deprecation(item.hir_id), attrs: &item.attrs, generics, @@ -134,13 +127,11 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { name: v.node.ident.name, id: v.node.id, attrs: &v.node.attrs, - stab: self.stability(v.node.id), depr: self.deprecation(v.node.id), def: &v.node.data, whence: v.span, }).collect(), vis: &it.vis, - stab: self.stability(it.hir_id), depr: self.deprecation(it.hir_id), generics, attrs: &it.attrs, @@ -200,7 +191,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { helpers, attrs: &item.attrs, whence: item.span, - stab: self.stability(item.hir_id), depr: self.deprecation(item.hir_id), }); } @@ -208,7 +198,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { om.fns.push(Function { id: item.hir_id, vis: &item.vis, - stab: self.stability(item.hir_id), depr: self.deprecation(item.hir_id), attrs: &item.attrs, decl, @@ -229,8 +218,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { let mut om = Module::new(name, attrs, vis); om.where_outer = span; om.where_inner = m.inner; - om.stab = self.stability(id); om.depr = self.deprecation(id); + om.hid = id; om.id = self.cx.tcx.hir().hir_to_node_id(id); // Keep track of if there were any private modules in the path. let orig_inside_public_path = self.inside_public_path; @@ -460,7 +449,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { attrs: &item.attrs, whence: item.span, vis: &item.vis, - stab: self.stability(item.hir_id), depr: self.deprecation(item.hir_id), }; om.typedefs.push(t); @@ -473,7 +461,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { attrs: &item.attrs, whence: item.span, vis: &item.vis, - stab: self.stability(item.hir_id), depr: self.deprecation(item.hir_id), }; om.opaque_tys.push(t); @@ -488,7 +475,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { attrs: &item.attrs, whence: item.span, vis: &item.vis, - stab: self.stability(item.hir_id), depr: self.deprecation(item.hir_id), }; om.statics.push(s); @@ -502,7 +488,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { attrs: &item.attrs, whence: item.span, vis: &item.vis, - stab: self.stability(item.hir_id), depr: self.deprecation(item.hir_id), }; om.constants.push(s); @@ -522,7 +507,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { attrs: &item.attrs, whence: item.span, vis: &item.vis, - stab: self.stability(item.hir_id), depr: self.deprecation(item.hir_id), }; om.traits.push(t); @@ -536,7 +520,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { attrs: &item.attrs, whence: item.span, vis: &item.vis, - stab: self.stability(item.hir_id), depr: self.deprecation(item.hir_id), }; om.trait_aliases.push(t); @@ -567,7 +550,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { id: item.hir_id, whence: item.span, vis: &item.vis, - stab: self.stability(item.hir_id), depr: self.deprecation(item.hir_id), }; om.impls.push(i); @@ -588,7 +570,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { name: renamed.unwrap_or(item.ident).name, kind: &item.node, vis: &item.vis, - stab: self.stability(item.hir_id), depr: self.deprecation(item.hir_id), attrs: &item.attrs, whence: item.span @@ -607,13 +588,12 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { let matchers = tts.chunks(4).map(|arm| arm[0].span()).collect(); Macro { - + hid: def.hir_id, def_id: self.cx.tcx.hir().local_def_id(def.hir_id), attrs: &def.attrs, name: renamed.unwrap_or(def.name), whence: def.span, matchers, - stab: self.stability(def.hir_id), depr: self.deprecation(def.hir_id), imported_from: None, } From 4beb751575b40c0425775817bfd8f9c756c27487 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Wed, 24 Jul 2019 16:22:48 -0400 Subject: [PATCH 6/9] Gather deprecation information during cleaning --- src/librustdoc/clean/mod.rs | 32 ++++++++++++++++---------------- src/librustdoc/core.rs | 5 +++++ src/librustdoc/doctree.rs | 18 ------------------ src/librustdoc/visit_ast.rs | 22 ---------------------- 4 files changed, 21 insertions(+), 56 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index c031e8d2bc15..bf09e8785ad4 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -655,7 +655,7 @@ impl Clean for doctree::Module<'_> { source: whence.clean(cx), visibility: self.vis.clean(cx), stability: cx.stability(self.hid).clean(cx), - deprecation: self.depr.clean(cx), + deprecation: cx.deprecation(self.hid).clean(cx), def_id: cx.tcx.hir().local_def_id_from_node_id(self.id), inner: ModuleItem(Module { is_crate: self.is_crate, @@ -1941,7 +1941,7 @@ impl Clean for doctree::Function<'_> { source: self.whence.clean(cx), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), - deprecation: self.depr.clean(cx), + deprecation: cx.deprecation(self.id).clean(cx), def_id: did, inner: FunctionItem(Function { decl, @@ -2141,7 +2141,7 @@ impl Clean for doctree::Trait<'_> { def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), - deprecation: self.depr.clean(cx), + deprecation: cx.deprecation(self.id).clean(cx), inner: TraitItem(Trait { auto: self.is_auto.clean(cx), unsafety: self.unsafety, @@ -2171,7 +2171,7 @@ impl Clean for doctree::TraitAlias<'_> { def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), - deprecation: self.depr.clean(cx), + deprecation: cx.deprecation(self.id).clean(cx), inner: TraitAliasItem(TraitAlias { generics: self.generics.clean(cx), bounds: self.bounds.clean(cx), @@ -3245,7 +3245,7 @@ impl Clean for doctree::Struct<'_> { def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), - deprecation: self.depr.clean(cx), + deprecation: cx.deprecation(self.id).clean(cx), inner: StructItem(Struct { struct_type: self.struct_type, generics: self.generics.clean(cx), @@ -3265,7 +3265,7 @@ impl Clean for doctree::Union<'_> { def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), - deprecation: self.depr.clean(cx), + deprecation: cx.deprecation(self.id).clean(cx), inner: UnionItem(Union { struct_type: self.struct_type, generics: self.generics.clean(cx), @@ -3312,7 +3312,7 @@ impl Clean for doctree::Enum<'_> { def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), - deprecation: self.depr.clean(cx), + deprecation: cx.deprecation(self.id).clean(cx), inner: EnumItem(Enum { variants: self.variants.iter().map(|v| v.clean(cx)).collect(), generics: self.generics.clean(cx), @@ -3335,7 +3335,7 @@ impl Clean for doctree::Variant<'_> { source: self.whence.clean(cx), visibility: None, stability: cx.stability(self.id).clean(cx), - deprecation: self.depr.clean(cx), + deprecation: cx.deprecation(self.id).clean(cx), def_id: cx.tcx.hir().local_def_id(self.id), inner: VariantItem(Variant { kind: self.def.clean(cx), @@ -3640,7 +3640,7 @@ impl Clean for doctree::Typedef<'_> { def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), - deprecation: self.depr.clean(cx), + deprecation: cx.deprecation(self.id).clean(cx), inner: TypedefItem(Typedef { type_: self.ty.clean(cx), generics: self.gen.clean(cx), @@ -3664,7 +3664,7 @@ impl Clean for doctree::OpaqueTy<'_> { def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), - deprecation: self.depr.clean(cx), + deprecation: cx.deprecation(self.id).clean(cx), inner: OpaqueTyItem(OpaqueTy { bounds: self.opaque_ty.bounds.clean(cx), generics: self.opaque_ty.generics.clean(cx), @@ -3715,7 +3715,7 @@ impl Clean for doctree::Static<'_> { def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), - deprecation: self.depr.clean(cx), + deprecation: cx.deprecation(self.id).clean(cx), inner: StaticItem(Static { type_: self.type_.clean(cx), mutability: self.mutability.clean(cx), @@ -3740,7 +3740,7 @@ impl Clean for doctree::Constant<'_> { def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), - deprecation: self.depr.clean(cx), + deprecation: cx.deprecation(self.id).clean(cx), inner: ConstantItem(Constant { type_: self.type_.clean(cx), expr: print_const_expr(cx, self.expr), @@ -3827,7 +3827,7 @@ impl Clean> for doctree::Impl<'_> { def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), - deprecation: self.depr.clean(cx), + deprecation: cx.deprecation(self.id).clean(cx), inner: ImplItem(Impl { unsafety: self.unsafety, generics: self.generics.clean(cx), @@ -4066,7 +4066,7 @@ impl Clean for doctree::ForeignItem<'_> { def_id: cx.tcx.hir().local_def_id(self.id), visibility: self.vis.clean(cx), stability: cx.stability(self.id).clean(cx), - deprecation: self.depr.clean(cx), + deprecation: cx.deprecation(self.id).clean(cx), inner, } } @@ -4249,7 +4249,7 @@ impl Clean for doctree::Macro<'_> { source: self.whence.clean(cx), visibility: Some(Public), stability: cx.stability(self.hid).clean(cx), - deprecation: self.depr.clean(cx), + deprecation: cx.deprecation(self.hid).clean(cx), def_id: self.def_id, inner: MacroItem(Macro { source: format!("macro_rules! {} {{\n{}}}", @@ -4277,7 +4277,7 @@ impl Clean for doctree::ProcMacro<'_> { source: self.whence.clean(cx), visibility: Some(Public), stability: cx.stability(self.id).clean(cx), - deprecation: self.depr.clean(cx), + deprecation: cx.deprecation(self.id).clean(cx), def_id: cx.tcx.hir().local_def_id(self.id), inner: ProcMacroItem(ProcMacro { kind: self.kind, diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 976d1b0b6b19..e23a24a8dc5b 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -171,6 +171,11 @@ impl<'tcx> DocContext<'tcx> { self.tcx.hir().opt_local_def_id(id) .and_then(|def_id| self.tcx.lookup_stability(def_id)).cloned() } + + pub fn deprecation(&self, id: HirId) -> Option { + self.tcx.hir().opt_local_def_id(id) + .and_then(|def_id| self.tcx.lookup_deprecation(def_id)) + } } pub trait DocAccessLevels { diff --git a/src/librustdoc/doctree.rs b/src/librustdoc/doctree.rs index c380c5e08a33..397147085723 100644 --- a/src/librustdoc/doctree.rs +++ b/src/librustdoc/doctree.rs @@ -4,7 +4,6 @@ pub use self::StructType::*; use syntax::ast; use syntax::ast::{Name, NodeId}; -use syntax::attr; use syntax::ext::base::MacroKind; use syntax_pos::{self, Span}; @@ -32,7 +31,6 @@ pub struct Module<'hir> { pub constants: Vec>, pub traits: Vec>, pub vis: &'hir hir::Visibility, - pub depr: Option, pub impls: Vec>, pub foreigns: Vec>, pub macros: Vec>, @@ -52,7 +50,6 @@ impl Module<'hir> { id: ast::CRATE_NODE_ID, hid: hir::CRATE_HIR_ID, vis, - depr: None, where_outer: syntax_pos::DUMMY_SP, where_inner: syntax_pos::DUMMY_SP, attrs, @@ -90,7 +87,6 @@ pub enum StructType { pub struct Struct<'hir> { pub vis: &'hir hir::Visibility, - pub depr: Option, pub id: hir::HirId, pub struct_type: StructType, pub name: Name, @@ -102,7 +98,6 @@ pub struct Struct<'hir> { pub struct Union<'hir> { pub vis: &'hir hir::Visibility, - pub depr: Option, pub id: hir::HirId, pub struct_type: StructType, pub name: Name, @@ -114,7 +109,6 @@ pub struct Union<'hir> { pub struct Enum<'hir> { pub vis: &'hir hir::Visibility, - pub depr: Option, pub variants: Vec>, pub generics: &'hir hir::Generics, pub attrs: &'hir hir::HirVec, @@ -128,7 +122,6 @@ pub struct Variant<'hir> { pub id: hir::HirId, pub attrs: &'hir hir::HirVec, pub def: &'hir hir::VariantData, - pub depr: Option, pub whence: Span, } @@ -138,7 +131,6 @@ pub struct Function<'hir> { pub id: hir::HirId, pub name: Name, pub vis: &'hir hir::Visibility, - pub depr: Option, pub header: hir::FnHeader, pub whence: Span, pub generics: &'hir hir::Generics, @@ -153,7 +145,6 @@ pub struct Typedef<'hir> { pub attrs: &'hir hir::HirVec, pub whence: Span, pub vis: &'hir hir::Visibility, - pub depr: Option, } pub struct OpaqueTy<'hir> { @@ -163,7 +154,6 @@ pub struct OpaqueTy<'hir> { pub attrs: &'hir hir::HirVec, pub whence: Span, pub vis: &'hir hir::Visibility, - pub depr: Option, } #[derive(Debug)] @@ -174,7 +164,6 @@ pub struct Static<'hir> { pub name: Name, pub attrs: &'hir hir::HirVec, pub vis: &'hir hir::Visibility, - pub depr: Option, pub id: hir::HirId, pub whence: Span, } @@ -185,7 +174,6 @@ pub struct Constant<'hir> { pub name: Name, pub attrs: &'hir hir::HirVec, pub vis: &'hir hir::Visibility, - pub depr: Option, pub id: hir::HirId, pub whence: Span, } @@ -201,7 +189,6 @@ pub struct Trait<'hir> { pub id: hir::HirId, pub whence: Span, pub vis: &'hir hir::Visibility, - pub depr: Option, } pub struct TraitAlias<'hir> { @@ -212,7 +199,6 @@ pub struct TraitAlias<'hir> { pub id: hir::HirId, pub whence: Span, pub vis: &'hir hir::Visibility, - pub depr: Option, } #[derive(Debug)] @@ -227,13 +213,11 @@ pub struct Impl<'hir> { pub attrs: &'hir hir::HirVec, pub whence: Span, pub vis: &'hir hir::Visibility, - pub depr: Option, pub id: hir::HirId, } pub struct ForeignItem<'hir> { pub vis: &'hir hir::Visibility, - pub depr: Option, pub id: hir::HirId, pub name: Name, pub kind: &'hir hir::ForeignItemKind, @@ -250,7 +234,6 @@ pub struct Macro<'hir> { pub attrs: &'hir hir::HirVec, pub whence: Span, pub matchers: hir::HirVec, - pub depr: Option, pub imported_from: Option, } @@ -280,7 +263,6 @@ pub struct ProcMacro<'hir> { pub helpers: Vec, pub attrs: &'hir hir::HirVec, pub whence: Span, - pub depr: Option, } pub fn struct_type_from_def(vdata: &hir::VariantData) -> StructType { diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 56c91bf405fc..002721afbc4f 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -7,7 +7,6 @@ use rustc::hir::def_id::{DefId, LOCAL_CRATE}; use rustc::middle::privacy::AccessLevel; use rustc::util::nodemap::{FxHashSet, FxHashMap}; use syntax::ast; -use syntax::attr; use syntax::ext::base::MacroKind; use syntax::source_map::Spanned; use syntax::symbol::sym; @@ -57,11 +56,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { } } - fn deprecation(&self, id: hir::HirId) -> Option { - self.cx.tcx.hir().opt_local_def_id(id) - .and_then(|def_id| self.cx.tcx.lookup_deprecation(def_id)) - } - pub fn visit(mut self, krate: &'tcx hir::Crate) -> Module<'tcx> { let mut module = self.visit_mod_contents(krate.span, &krate.attrs, @@ -91,7 +85,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { struct_type, name, vis: &item.vis, - depr: self.deprecation(item.hir_id), attrs: &item.attrs, generics, fields: sd.fields(), @@ -109,7 +102,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { struct_type, name, vis: &item.vis, - depr: self.deprecation(item.hir_id), attrs: &item.attrs, generics, fields: sd.fields(), @@ -127,12 +119,10 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { name: v.node.ident.name, id: v.node.id, attrs: &v.node.attrs, - depr: self.deprecation(v.node.id), def: &v.node.data, whence: v.span, }).collect(), vis: &it.vis, - depr: self.deprecation(it.hir_id), generics, attrs: &it.attrs, id: it.hir_id, @@ -191,14 +181,12 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { helpers, attrs: &item.attrs, whence: item.span, - depr: self.deprecation(item.hir_id), }); } None => { om.fns.push(Function { id: item.hir_id, vis: &item.vis, - depr: self.deprecation(item.hir_id), attrs: &item.attrs, decl, name, @@ -218,7 +206,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { let mut om = Module::new(name, attrs, vis); om.where_outer = span; om.where_inner = m.inner; - om.depr = self.deprecation(id); om.hid = id; om.id = self.cx.tcx.hir().hir_to_node_id(id); // Keep track of if there were any private modules in the path. @@ -449,7 +436,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { attrs: &item.attrs, whence: item.span, vis: &item.vis, - depr: self.deprecation(item.hir_id), }; om.typedefs.push(t); }, @@ -461,7 +447,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { attrs: &item.attrs, whence: item.span, vis: &item.vis, - depr: self.deprecation(item.hir_id), }; om.opaque_tys.push(t); }, @@ -475,7 +460,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { attrs: &item.attrs, whence: item.span, vis: &item.vis, - depr: self.deprecation(item.hir_id), }; om.statics.push(s); }, @@ -488,7 +472,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { attrs: &item.attrs, whence: item.span, vis: &item.vis, - depr: self.deprecation(item.hir_id), }; om.constants.push(s); }, @@ -507,7 +490,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { attrs: &item.attrs, whence: item.span, vis: &item.vis, - depr: self.deprecation(item.hir_id), }; om.traits.push(t); }, @@ -520,7 +502,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { attrs: &item.attrs, whence: item.span, vis: &item.vis, - depr: self.deprecation(item.hir_id), }; om.trait_aliases.push(t); }, @@ -550,7 +531,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { id: item.hir_id, whence: item.span, vis: &item.vis, - depr: self.deprecation(item.hir_id), }; om.impls.push(i); } @@ -570,7 +550,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { name: renamed.unwrap_or(item.ident).name, kind: &item.node, vis: &item.vis, - depr: self.deprecation(item.hir_id), attrs: &item.attrs, whence: item.span }); @@ -594,7 +573,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { name: renamed.unwrap_or(def.name), whence: def.span, matchers, - depr: self.deprecation(def.hir_id), imported_from: None, } } From c36e0c04243c2aa5e6fb9289345e9215ea0ebde5 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Wed, 24 Jul 2019 16:28:51 -0400 Subject: [PATCH 7/9] Remove NodeId from doctree::Module --- src/librustdoc/clean/mod.rs | 6 +++--- src/librustdoc/doctree.rs | 8 +++----- src/librustdoc/visit_ast.rs | 3 +-- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index bf09e8785ad4..aee9b74c31c6 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -654,9 +654,9 @@ impl Clean for doctree::Module<'_> { attrs, source: whence.clean(cx), visibility: self.vis.clean(cx), - stability: cx.stability(self.hid).clean(cx), - deprecation: cx.deprecation(self.hid).clean(cx), - def_id: cx.tcx.hir().local_def_id_from_node_id(self.id), + stability: cx.stability(self.id).clean(cx), + deprecation: cx.deprecation(self.id).clean(cx), + def_id: cx.tcx.hir().local_def_id(self.id), inner: ModuleItem(Module { is_crate: self.is_crate, items, diff --git a/src/librustdoc/doctree.rs b/src/librustdoc/doctree.rs index 397147085723..90dcf1be76c0 100644 --- a/src/librustdoc/doctree.rs +++ b/src/librustdoc/doctree.rs @@ -3,7 +3,7 @@ pub use self::StructType::*; use syntax::ast; -use syntax::ast::{Name, NodeId}; +use syntax::ast::Name; use syntax::ext::base::MacroKind; use syntax_pos::{self, Span}; @@ -23,8 +23,7 @@ pub struct Module<'hir> { pub enums: Vec>, pub fns: Vec>, pub mods: Vec>, - pub id: NodeId, - pub hid: hir::HirId, + pub id: hir::HirId, pub typedefs: Vec>, pub opaque_tys: Vec>, pub statics: Vec>, @@ -47,8 +46,7 @@ impl Module<'hir> { ) -> Module<'hir> { Module { name : name, - id: ast::CRATE_NODE_ID, - hid: hir::CRATE_HIR_ID, + id: hir::CRATE_HIR_ID, vis, where_outer: syntax_pos::DUMMY_SP, where_inner: syntax_pos::DUMMY_SP, diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 002721afbc4f..f01b9eeb30f2 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -206,8 +206,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { let mut om = Module::new(name, attrs, vis); om.where_outer = span; om.where_inner = m.inner; - om.hid = id; - om.id = self.cx.tcx.hir().hir_to_node_id(id); + om.id = id; // Keep track of if there were any private modules in the path. let orig_inside_public_path = self.inside_public_path; self.inside_public_path &= vis.node.is_pub(); From 78d9088e7784849dbfe14f1e550a08111d547622 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Wed, 24 Jul 2019 17:26:21 -0400 Subject: [PATCH 8/9] Replace is_doc_reachable with is_public --- src/librustdoc/clean/blanket_impl.rs | 4 +--- src/librustdoc/clean/inline.rs | 6 +++--- src/librustdoc/core.rs | 10 ---------- src/librustdoc/html/format.rs | 3 +-- src/librustdoc/passes/mod.rs | 4 ++-- 5 files changed, 7 insertions(+), 20 deletions(-) diff --git a/src/librustdoc/clean/blanket_impl.rs b/src/librustdoc/clean/blanket_impl.rs index 5c42d705bd57..64cffaec2eaf 100644 --- a/src/librustdoc/clean/blanket_impl.rs +++ b/src/librustdoc/clean/blanket_impl.rs @@ -5,8 +5,6 @@ use rustc::ty::subst::Subst; use rustc::infer::InferOk; use syntax_pos::DUMMY_SP; -use crate::core::DocAccessLevels; - use super::*; pub struct BlanketImplFinder<'a, 'tcx> { @@ -30,7 +28,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> { debug!("get_blanket_impls({:?})", ty); let mut impls = Vec::new(); for &trait_def_id in self.cx.all_traits.iter() { - if !self.cx.renderinfo.borrow().access_levels.is_doc_reachable(trait_def_id) || + if !self.cx.renderinfo.borrow().access_levels.is_public(trait_def_id) || self.cx.generated_synthetics .borrow_mut() .get(&(ty, trait_def_id)) diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 8463fbfbd200..d2a6dcf19bcc 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -14,7 +14,7 @@ use rustc_metadata::cstore::LoadedMacro; use rustc::ty; use rustc::util::nodemap::FxHashSet; -use crate::core::{DocContext, DocAccessLevels}; +use crate::core::DocContext; use crate::doctree; use crate::clean::{ self, @@ -326,7 +326,7 @@ pub fn build_impl(cx: &DocContext<'_>, did: DefId, attrs: Option>, // reachable in rustdoc generated documentation if !did.is_local() { if let Some(traitref) = associated_trait { - if !cx.renderinfo.borrow().access_levels.is_doc_reachable(traitref.def_id) { + if !cx.renderinfo.borrow().access_levels.is_public(traitref.def_id) { return } } @@ -347,7 +347,7 @@ pub fn build_impl(cx: &DocContext<'_>, did: DefId, attrs: Option>, // reachable in rustdoc generated documentation if !did.is_local() { if let Some(did) = for_.def_id() { - if !cx.renderinfo.borrow().access_levels.is_doc_reachable(did) { + if !cx.renderinfo.borrow().access_levels.is_public(did) { return } } diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index e23a24a8dc5b..2d19cfacd964 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -178,16 +178,6 @@ impl<'tcx> DocContext<'tcx> { } } -pub trait DocAccessLevels { - fn is_doc_reachable(&self, did: DefId) -> bool; -} - -impl DocAccessLevels for AccessLevels { - fn is_doc_reachable(&self, did: DefId) -> bool { - self.is_public(did) - } -} - /// Creates a new diagnostic `Handler` that can be used to emit warnings and errors. /// /// If the given `error_format` is `ErrorOutputType::Json` and no `SourceMap` is given, a new one diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 9e5cc03b8312..9c22837bdae8 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -14,7 +14,6 @@ use rustc_target::spec::abi::Abi; use rustc::hir; use crate::clean::{self, PrimitiveType}; -use crate::core::DocAccessLevels; use crate::html::item_type::ItemType; use crate::html::render::{self, cache, CURRENT_LOCATION_KEY}; @@ -404,7 +403,7 @@ impl fmt::Display for clean::Path { pub fn href(did: DefId) -> Option<(String, ItemType, Vec)> { let cache = cache(); - if !did.is_local() && !cache.access_levels.is_doc_reachable(did) { + if !did.is_local() && !cache.access_levels.is_public(did) { return None } diff --git a/src/librustdoc/passes/mod.rs b/src/librustdoc/passes/mod.rs index ca40d6d02f86..4e10b46bc8a6 100644 --- a/src/librustdoc/passes/mod.rs +++ b/src/librustdoc/passes/mod.rs @@ -10,7 +10,7 @@ use syntax_pos::{DUMMY_SP, InnerSpan, Span}; use std::ops::Range; use crate::clean::{self, GetDefId, Item}; -use crate::core::{DocContext, DocAccessLevels}; +use crate::core::DocContext; use crate::fold::{DocFolder, StripItem}; use crate::html::markdown::{find_testable_code, ErrorCodes, LangString}; @@ -347,7 +347,7 @@ pub fn look_for_tests<'tcx>( diag.emit(); } else if check_missing_code == false && tests.found_tests > 0 && - !cx.renderinfo.borrow().access_levels.is_doc_reachable(item.def_id) { + !cx.renderinfo.borrow().access_levels.is_public(item.def_id) { let mut diag = cx.tcx.struct_span_lint_hir( lint::builtin::PRIVATE_DOC_TESTS, hir_id, From 32f144a5277d80baafcc192a4fd10336b999b6a8 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sat, 10 Aug 2019 07:30:39 -0400 Subject: [PATCH 9/9] Implement Clean on hir::Crate directly --- src/librustdoc/clean/mod.rs | 12 +++++++----- src/librustdoc/core.rs | 7 +------ 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index aee9b74c31c6..6f33bdd7f4d2 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -137,13 +137,15 @@ pub struct Crate { pub masked_crates: FxHashSet, } -// The `()` here is rather ugly and would be great to remove. Unfortunately, we -// already have a different Clean impl for `doctree::Module` which makes this -// the only way to easily disambiguate. -impl<'tcx> Clean for ((), doctree::Module<'tcx>) { +impl Clean for hir::Crate { + // note that self here is ignored in favor of `cx.tcx.hir().krate()` since + // that gets around tying self's lifetime to the '_ in cx. fn clean(&self, cx: &DocContext<'_>) -> Crate { use crate::visit_lib::LibEmbargoVisitor; + let v = crate::visit_ast::RustdocVisitor::new(&cx); + let module = v.visit(cx.tcx.hir().krate()); + { let mut r = cx.renderinfo.borrow_mut(); r.deref_trait_did = cx.tcx.lang_items().deref_trait(); @@ -161,7 +163,7 @@ impl<'tcx> Clean for ((), doctree::Module<'tcx>) { // Clean the crate, translating the entire libsyntax AST to one that is // understood by rustdoc. - let mut module = self.1.clean(cx); + let mut module = module.clean(cx); let mut masked_crates = FxHashSet::default(); match module.inner { diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 2d19cfacd964..5138e4a23a47 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -30,7 +30,6 @@ use rustc_data_structures::sync::{self, Lrc}; use std::sync::Arc; use std::rc::Rc; -use crate::visit_ast::RustdocVisitor; use crate::config::{Options as RustdocOptions, RenderOptions}; use crate::clean; use crate::clean::{Clean, MAX_DEF_ID, AttributesExt}; @@ -392,11 +391,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt }; debug!("crate: {:?}", tcx.hir().krate()); - let mut krate = { - let v = RustdocVisitor::new(&ctxt); - let module = v.visit(tcx.hir().krate()); - ((), module).clean(&ctxt) - }; + let mut krate = tcx.hir().krate().clean(&ctxt); fn report_deprecated_attr(name: &str, diag: &errors::Handler) { let mut msg = diag.struct_warn(&format!("the `#![doc({})]` attribute is \