doc: some HirIdification
This commit is contained in:
parent
b71107912d
commit
37954df1a7
@ -166,15 +166,6 @@ impl<'tcx> DocContext<'tcx> {
|
|||||||
|
|
||||||
/// Like the function of the same name on the HIR map, but skips calling it on fake DefIds.
|
/// Like the function of the same name on the HIR map, but skips calling it on fake DefIds.
|
||||||
/// (This avoids a slice-index-out-of-bounds panic.)
|
/// (This avoids a slice-index-out-of-bounds panic.)
|
||||||
pub fn as_local_node_id(&self, def_id: DefId) -> Option<ast::NodeId> {
|
|
||||||
if self.all_fake_def_ids.borrow().contains(&def_id) {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
self.tcx.hir().as_local_node_id(def_id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME(@ljedrz): remove the NodeId variant
|
|
||||||
pub fn as_local_hir_id(&self, def_id: DefId) -> Option<HirId> {
|
pub fn as_local_hir_id(&self, def_id: DefId) -> Option<HirId> {
|
||||||
if self.all_fake_def_ids.borrow().contains(&def_id) {
|
if self.all_fake_def_ids.borrow().contains(&def_id) {
|
||||||
None
|
None
|
||||||
|
@ -38,7 +38,7 @@ pub fn collect_intra_doc_links(krate: Crate, cx: &DocContext<'_>) -> Crate {
|
|||||||
|
|
||||||
struct LinkCollector<'a, 'tcx> {
|
struct LinkCollector<'a, 'tcx> {
|
||||||
cx: &'a DocContext<'tcx>,
|
cx: &'a DocContext<'tcx>,
|
||||||
mod_ids: Vec<ast::NodeId>,
|
mod_ids: Vec<hir::HirId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
|
impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
|
||||||
@ -55,7 +55,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
|
|||||||
path_str: &str,
|
path_str: &str,
|
||||||
ns: Namespace,
|
ns: Namespace,
|
||||||
current_item: &Option<String>,
|
current_item: &Option<String>,
|
||||||
parent_id: Option<ast::NodeId>)
|
parent_id: Option<hir::HirId>)
|
||||||
-> Result<(Def, Option<String>), ()>
|
-> Result<(Def, Option<String>), ()>
|
||||||
{
|
{
|
||||||
let cx = self.cx;
|
let cx = self.cx;
|
||||||
@ -64,8 +64,9 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
|
|||||||
// path.
|
// path.
|
||||||
if let Some(id) = parent_id.or(self.mod_ids.last().cloned()) {
|
if let Some(id) = parent_id.or(self.mod_ids.last().cloned()) {
|
||||||
// FIXME: `with_scope` requires the `NodeId` of a module.
|
// FIXME: `with_scope` requires the `NodeId` of a module.
|
||||||
|
let node_id = cx.tcx.hir().hir_to_node_id(id);
|
||||||
let result = cx.enter_resolver(|resolver| {
|
let result = cx.enter_resolver(|resolver| {
|
||||||
resolver.with_scope(id, |resolver| {
|
resolver.with_scope(node_id, |resolver| {
|
||||||
resolver.resolve_str_path_error(DUMMY_SP, &path_str, ns == ValueNS)
|
resolver.resolve_str_path_error(DUMMY_SP, &path_str, ns == ValueNS)
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
@ -127,7 +128,8 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: `with_scope` requires the `NodeId` of a module.
|
// FIXME: `with_scope` requires the `NodeId` of a module.
|
||||||
let ty = cx.enter_resolver(|resolver| resolver.with_scope(id, |resolver| {
|
let node_id = cx.tcx.hir().hir_to_node_id(id);
|
||||||
|
let ty = cx.enter_resolver(|resolver| resolver.with_scope(node_id, |resolver| {
|
||||||
resolver.resolve_str_path_error(DUMMY_SP, &path, false)
|
resolver.resolve_str_path_error(DUMMY_SP, &path, false)
|
||||||
}))?;
|
}))?;
|
||||||
match ty.def {
|
match ty.def {
|
||||||
@ -215,11 +217,11 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// FIXME: get the resolver to work with non-local resolve scopes.
|
// FIXME: get the resolver to work with non-local resolve scopes.
|
||||||
let parent_node = self.cx.as_local_node_id(item.def_id).and_then(|node_id| {
|
let parent_node = self.cx.as_local_hir_id(item.def_id).and_then(|hir_id| {
|
||||||
// FIXME: this fails hard for impls in non-module scope, but is necessary for the
|
// FIXME: this fails hard for impls in non-module scope, but is necessary for the
|
||||||
// current `resolve()` implementation.
|
// current `resolve()` implementation.
|
||||||
match self.cx.tcx.hir().get_module_parent_node(node_id) {
|
match self.cx.tcx.hir().get_module_parent_node(hir_id) {
|
||||||
id if id != node_id => Some(id),
|
id if id != hir_id => Some(id),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -238,9 +240,9 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
match parent_node.or(self.mod_ids.last().cloned()) {
|
match parent_node.or(self.mod_ids.last().cloned()) {
|
||||||
Some(parent) if parent != ast::CRATE_NODE_ID => {
|
Some(parent) if parent != hir::CRATE_HIR_ID => {
|
||||||
// FIXME: can we pull the parent module's name from elsewhere?
|
// FIXME: can we pull the parent module's name from elsewhere?
|
||||||
Some(self.cx.tcx.hir().name(parent).to_string())
|
Some(self.cx.tcx.hir().name_by_hir_id(parent).to_string())
|
||||||
}
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
@ -257,7 +259,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if item.is_mod() && item.attrs.inner_docs {
|
if item.is_mod() && item.attrs.inner_docs {
|
||||||
self.mod_ids.push(self.cx.tcx.hir().hir_to_node_id(item_hir_id.unwrap()));
|
self.mod_ids.push(item_hir_id.unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
let cx = self.cx;
|
let cx = self.cx;
|
||||||
@ -391,7 +393,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if item.is_mod() && !item.attrs.inner_docs {
|
if item.is_mod() && !item.attrs.inner_docs {
|
||||||
self.mod_ids.push(self.cx.tcx.hir().hir_to_node_id(item_hir_id.unwrap()));
|
self.mod_ids.push(item_hir_id.unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
if item.is_mod() {
|
if item.is_mod() {
|
||||||
|
@ -31,7 +31,7 @@ pub struct RustdocVisitor<'a, 'tcx> {
|
|||||||
pub module: Module,
|
pub module: Module,
|
||||||
pub attrs: hir::HirVec<ast::Attribute>,
|
pub attrs: hir::HirVec<ast::Attribute>,
|
||||||
pub cx: &'a core::DocContext<'tcx>,
|
pub cx: &'a core::DocContext<'tcx>,
|
||||||
view_item_stack: FxHashSet<ast::NodeId>,
|
view_item_stack: FxHashSet<hir::HirId>,
|
||||||
inlining: bool,
|
inlining: bool,
|
||||||
/// Are the current module and all of its parents public?
|
/// Are the current module and all of its parents public?
|
||||||
inside_public_path: bool,
|
inside_public_path: bool,
|
||||||
@ -44,7 +44,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
|
|||||||
) -> RustdocVisitor<'a, 'tcx> {
|
) -> RustdocVisitor<'a, 'tcx> {
|
||||||
// If the root is re-exported, terminate all recursion.
|
// If the root is re-exported, terminate all recursion.
|
||||||
let mut stack = FxHashSet::default();
|
let mut stack = FxHashSet::default();
|
||||||
stack.insert(ast::CRATE_NODE_ID);
|
stack.insert(hir::CRATE_HIR_ID);
|
||||||
RustdocVisitor {
|
RustdocVisitor {
|
||||||
module: Module::new(None),
|
module: Module::new(None),
|
||||||
attrs: hir::HirVec::new(),
|
attrs: hir::HirVec::new(),
|
||||||
@ -271,13 +271,13 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
|
|||||||
om: &mut Module,
|
om: &mut Module,
|
||||||
please_inline: bool) -> bool {
|
please_inline: bool) -> bool {
|
||||||
|
|
||||||
fn inherits_doc_hidden(cx: &core::DocContext<'_>, mut node: ast::NodeId) -> bool {
|
fn inherits_doc_hidden(cx: &core::DocContext<'_>, mut node: hir::HirId) -> bool {
|
||||||
while let Some(id) = cx.tcx.hir().get_enclosing_scope(node) {
|
while let Some(id) = cx.tcx.hir().get_enclosing_scope(node) {
|
||||||
node = id;
|
node = id;
|
||||||
if cx.tcx.hir().attrs(node).lists("doc").has_word("hidden") {
|
if cx.tcx.hir().attrs_by_hir_id(node).lists("doc").has_word("hidden") {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if node == ast::CRATE_NODE_ID {
|
if node == hir::CRATE_HIR_ID {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -326,21 +326,21 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
let def_node_id = match tcx.hir().as_local_node_id(def_did) {
|
let def_hir_id = match tcx.hir().as_local_hir_id(def_did) {
|
||||||
Some(n) => n, None => return false
|
Some(n) => n, None => return false
|
||||||
};
|
};
|
||||||
|
|
||||||
let is_private = !self.cx.renderinfo.borrow().access_levels.is_public(def_did);
|
let is_private = !self.cx.renderinfo.borrow().access_levels.is_public(def_did);
|
||||||
let is_hidden = inherits_doc_hidden(self.cx, def_node_id);
|
let is_hidden = inherits_doc_hidden(self.cx, def_hir_id);
|
||||||
|
|
||||||
// Only inline if requested or if the item would otherwise be stripped.
|
// Only inline if requested or if the item would otherwise be stripped.
|
||||||
if (!please_inline && !is_private && !is_hidden) || is_no_inline {
|
if (!please_inline && !is_private && !is_hidden) || is_no_inline {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if !self.view_item_stack.insert(def_node_id) { return false }
|
if !self.view_item_stack.insert(def_hir_id) { return false }
|
||||||
|
|
||||||
let ret = match tcx.hir().get(def_node_id) {
|
let ret = match tcx.hir().get_by_hir_id(def_hir_id) {
|
||||||
Node::Item(&hir::Item { node: hir::ItemKind::Mod(ref m), .. }) if glob => {
|
Node::Item(&hir::Item { node: hir::ItemKind::Mod(ref m), .. }) if glob => {
|
||||||
let prev = mem::replace(&mut self.inlining, true);
|
let prev = mem::replace(&mut self.inlining, true);
|
||||||
for i in &m.item_ids {
|
for i in &m.item_ids {
|
||||||
@ -373,7 +373,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
|
|||||||
}
|
}
|
||||||
_ => false,
|
_ => false,
|
||||||
};
|
};
|
||||||
self.view_item_stack.remove(&def_node_id);
|
self.view_item_stack.remove(&def_hir_id);
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user