Mutate DocContext from LibEmbargoVisitor and RustdocVisitor

We have &mut access, so remove the RefCell borrowing
This commit is contained in:
Mark Rousskov 2019-08-12 07:21:04 -04:00
parent e2b6f4c662
commit 57d57c6784
3 changed files with 23 additions and 27 deletions

View File

@ -136,24 +136,22 @@ pub struct Crate {
pub collapsed: bool,
}
pub fn krate(cx: &mut DocContext<'a>) -> Crate {
pub fn krate(mut cx: &mut DocContext<'_>) -> Crate {
use crate::visit_lib::LibEmbargoVisitor;
let v = crate::visit_ast::RustdocVisitor::new(&cx);
let module = v.visit(cx.tcx.hir().krate());
let krate = cx.tcx.hir().krate();
let module = crate::visit_ast::RustdocVisitor::new(&mut cx).visit(krate);
{
let mut r = cx.renderinfo.borrow_mut();
r.deref_trait_did = cx.tcx.lang_items().deref_trait();
r.deref_mut_trait_did = cx.tcx.lang_items().deref_mut_trait();
r.owned_box_did = cx.tcx.lang_items().owned_box();
}
let mut r = cx.renderinfo.get_mut();
r.deref_trait_did = cx.tcx.lang_items().deref_trait();
r.deref_mut_trait_did = cx.tcx.lang_items().deref_mut_trait();
r.owned_box_did = cx.tcx.lang_items().owned_box();
let mut externs = Vec::new();
for &cnum in cx.tcx.crates().iter() {
externs.push((cnum, cnum.clean(cx)));
// Analyze doc-reachability for extern items
LibEmbargoVisitor::new(cx).visit_lib(cnum);
LibEmbargoVisitor::new(&mut cx).visit_lib(cnum);
}
externs.sort_by(|&(a, _), &(b, _)| a.cmp(&b));

View File

@ -41,7 +41,7 @@ fn def_id_to_path(
// framework from syntax?.
pub struct RustdocVisitor<'a, 'tcx> {
cx: &'a core::DocContext<'tcx>,
cx: &'a mut core::DocContext<'tcx>,
view_item_stack: FxHashSet<hir::HirId>,
inlining: bool,
/// Are the current module and all of its parents public?
@ -51,7 +51,7 @@ pub struct RustdocVisitor<'a, 'tcx> {
impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
pub fn new(
cx: &'a core::DocContext<'tcx>
cx: &'a mut core::DocContext<'tcx>
) -> RustdocVisitor<'a, 'tcx> {
// If the root is re-exported, terminate all recursion.
let mut stack = FxHashSet::default();
@ -84,7 +84,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
);
module.is_crate = true;
self.cx.renderinfo.borrow_mut().exact_paths = self.exact_paths;
self.cx.renderinfo.get_mut().exact_paths = self.exact_paths;
module
}
@ -292,7 +292,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
Res::Def(DefKind::ForeignTy, did) |
Res::Def(DefKind::TyAlias, did) if !self_is_hidden => {
self.cx.renderinfo
.borrow_mut()
.get_mut()
.access_levels.map
.insert(did, AccessLevel::Public);
},

View File

@ -1,12 +1,10 @@
use rustc::middle::privacy::{AccessLevels, AccessLevel};
use rustc::hir::def::{Res, DefKind};
use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefId};
use rustc::ty::Visibility;
use rustc::ty::{TyCtxt, Visibility};
use rustc::util::nodemap::FxHashSet;
use syntax::symbol::sym;
use std::cell::RefMut;
use crate::clean::{AttributesExt, NestedAttributesExt};
// FIXME: this may not be exhaustive, but is sufficient for rustdocs current uses
@ -14,9 +12,9 @@ use crate::clean::{AttributesExt, NestedAttributesExt};
/// Similar to `librustc_privacy::EmbargoVisitor`, but also takes
/// specific rustdoc annotations into account (i.e., `doc(hidden)`)
pub struct LibEmbargoVisitor<'a, 'tcx> {
cx: &'a crate::core::DocContext<'tcx>,
tcx: TyCtxt<'tcx>,
// Accessibility levels for reachable nodes
access_levels: RefMut<'a, AccessLevels<DefId>>,
access_levels: &'a mut AccessLevels<DefId>,
// Previous accessibility level, None means unreachable
prev_level: Option<AccessLevel>,
// Keeps track of already visited modules, in case a module re-exports its parent
@ -25,13 +23,13 @@ pub struct LibEmbargoVisitor<'a, 'tcx> {
impl<'a, 'tcx> LibEmbargoVisitor<'a, 'tcx> {
pub fn new(
cx: &'a crate::core::DocContext<'tcx>
cx: &'a mut crate::core::DocContext<'tcx>
) -> LibEmbargoVisitor<'a, 'tcx> {
LibEmbargoVisitor {
cx,
access_levels: RefMut::map(cx.renderinfo.borrow_mut(), |ri| &mut ri.access_levels),
tcx: cx.tcx,
access_levels: &mut cx.renderinfo.get_mut().access_levels,
prev_level: Some(AccessLevel::Public),
visited_mods: FxHashSet::default()
visited_mods: FxHashSet::default(),
}
}
@ -43,7 +41,7 @@ impl<'a, 'tcx> LibEmbargoVisitor<'a, 'tcx> {
// Updates node level and returns the updated level
fn update(&mut self, did: DefId, level: Option<AccessLevel>) -> Option<AccessLevel> {
let is_hidden = self.cx.tcx.get_attrs(did).lists(sym::doc).has_word(sym::hidden);
let is_hidden = self.tcx.get_attrs(did).lists(sym::doc).has_word(sym::hidden);
let old_level = self.access_levels.map.get(&did).cloned();
// Accessibility levels can only grow
@ -60,9 +58,9 @@ impl<'a, 'tcx> LibEmbargoVisitor<'a, 'tcx> {
return;
}
for item in self.cx.tcx.item_children(def_id).iter() {
for item in self.tcx.item_children(def_id).iter() {
if let Some(def_id) = item.res.opt_def_id() {
if self.cx.tcx.def_key(def_id).parent.map_or(false, |d| d == def_id.index) ||
if self.tcx.def_key(def_id).parent.map_or(false, |d| d == def_id.index) ||
item.vis == Visibility::Public {
self.visit_item(item.res);
}
@ -72,7 +70,7 @@ impl<'a, 'tcx> LibEmbargoVisitor<'a, 'tcx> {
fn visit_item(&mut self, res: Res) {
let def_id = res.def_id();
let vis = self.cx.tcx.visibility(def_id);
let vis = self.tcx.visibility(def_id);
let inherited_item_level = if vis == Visibility::Public {
self.prev_level
} else {