Skip to content

Commit

Permalink
Rollup merge of rust-lang#84763 - tdelabro:shrink-doctree-module, r=j…
Browse files Browse the repository at this point in the history
…yn514

shrink doctree::Module

helps rust-lang#76382
  • Loading branch information
Dylan-DPC authored May 1, 2021
2 parents 6c5ff79 + 649bf22 commit 71fe9df
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
5 changes: 3 additions & 2 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,13 @@ impl Clean<Item> for doctree::Module<'_> {
// 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
Expand Down
13 changes: 8 additions & 5 deletions src/librustdoc/doctree.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -17,16 +17,19 @@ crate struct Module<'hir> {
}

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)
}
}
11 changes: 3 additions & 8 deletions src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use rustc_hir::def_id::DefId;
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;

Expand Down Expand Up @@ -73,7 +73,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
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,
Expand Down Expand Up @@ -129,16 +128,12 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {

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();
Expand Down Expand Up @@ -312,7 +307,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
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(..)
Expand Down

0 comments on commit 71fe9df

Please sign in to comment.