compute where_outer on demand, remove it from Module

This commit is contained in:
Timothée Delabrouille 2021-05-01 15:33:49 +02:00
parent 855c2d130f
commit 649bf22df5
3 changed files with 14 additions and 15 deletions

View File

@ -100,12 +100,13 @@ fn clean(&self, cx: &mut DocContext<'_>) -> Item {
// determine if we should display the inner contents or
// the outer `mod` item for the source code.
let span = Span::from_rustc_span({
let where_outer = self.where_outer(cx.tcx);
let sm = cx.sess().source_map();
let outer = sm.lookup_char_pos(self.where_outer.lo());
let outer = sm.lookup_char_pos(where_outer.lo());
let inner = sm.lookup_char_pos(self.where_inner.lo());
if outer.file.start_pos == inner.file.start_pos {
// mod foo { ... }
self.where_outer
where_outer
} else {
// mod foo; (and a separate SourceFile for the contents)
self.where_inner

View File

@ -1,12 +1,12 @@
//! This module is used to store stuff from Rust's AST in a more convenient
//! manner (and with prettier names) before cleaning.
use rustc_middle::ty::TyCtxt;
use rustc_span::{self, Span, Symbol};
use rustc_hir as hir;
crate struct Module<'hir> {
crate name: Symbol,
crate where_outer: Span,
crate where_inner: Span,
crate mods: Vec<Module<'hir>>,
crate id: hir::HirId,
@ -17,16 +17,19 @@
}
impl Module<'hir> {
crate fn new(name: Symbol) -> Module<'hir> {
crate fn new(name: Symbol, id: hir::HirId, where_inner: Span) -> Module<'hir> {
Module {
name,
id: hir::CRATE_HIR_ID,
where_outer: rustc_span::DUMMY_SP,
where_inner: rustc_span::DUMMY_SP,
id,
where_inner,
mods: Vec::new(),
items: Vec::new(),
foreigns: Vec::new(),
macros: Vec::new(),
}
}
crate fn where_outer(&self, tcx: TyCtxt<'_>) -> Span {
tcx.hir().span(self.id)
}
}

View File

@ -8,9 +8,9 @@
use rustc_hir::Node;
use rustc_middle::middle::privacy::AccessLevel;
use rustc_middle::ty::TyCtxt;
use rustc_span;
use rustc_span::source_map::Spanned;
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::{self, Span};
use std::mem;
@ -73,7 +73,6 @@ fn store_path(&mut self, did: DefId) {
crate fn visit(mut self, krate: &'tcx hir::Crate<'_>) -> Module<'tcx> {
let span = krate.item.inner;
let mut top_level_module = self.visit_mod_contents(
span,
&Spanned { span, node: hir::VisibilityKind::Public },
hir::CRATE_HIR_ID,
&krate.item,
@ -129,16 +128,12 @@ fn store_path(&mut self, did: DefId) {
fn visit_mod_contents(
&mut self,
span: Span,
vis: &hir::Visibility<'_>,
id: hir::HirId,
m: &'tcx hir::Mod<'tcx>,
name: Symbol,
) -> Module<'tcx> {
let mut om = Module::new(name);
om.where_outer = span;
om.where_inner = m.inner;
om.id = id;
let mut om = Module::new(name, id, m.inner);
// 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();
@ -312,7 +307,7 @@ fn visit_item(
om.items.push((item, renamed))
}
hir::ItemKind::Mod(ref m) => {
om.mods.push(self.visit_mod_contents(item.span, &item.vis, item.hir_id(), m, name));
om.mods.push(self.visit_mod_contents(&item.vis, item.hir_id(), m, name));
}
hir::ItemKind::Fn(..)
| hir::ItemKind::ExternCrate(..)