Skip to content

Commit 649bf22

Browse files
author
Timothée Delabrouille
committedMay 1, 2021
compute where_outer on demand, remove it from Module
1 parent 855c2d1 commit 649bf22

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed
 

‎src/librustdoc/clean/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,13 @@ impl Clean<Item> for doctree::Module<'_> {
100100
// determine if we should display the inner contents or
101101
// the outer `mod` item for the source code.
102102
let span = Span::from_rustc_span({
103+
let where_outer = self.where_outer(cx.tcx);
103104
let sm = cx.sess().source_map();
104-
let outer = sm.lookup_char_pos(self.where_outer.lo());
105+
let outer = sm.lookup_char_pos(where_outer.lo());
105106
let inner = sm.lookup_char_pos(self.where_inner.lo());
106107
if outer.file.start_pos == inner.file.start_pos {
107108
// mod foo { ... }
108-
self.where_outer
109+
where_outer
109110
} else {
110111
// mod foo; (and a separate SourceFile for the contents)
111112
self.where_inner

‎src/librustdoc/doctree.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//! This module is used to store stuff from Rust's AST in a more convenient
22
//! manner (and with prettier names) before cleaning.
3+
use rustc_middle::ty::TyCtxt;
34
use rustc_span::{self, Span, Symbol};
45

56
use rustc_hir as hir;
67

78
crate struct Module<'hir> {
89
crate name: Symbol,
9-
crate where_outer: Span,
1010
crate where_inner: Span,
1111
crate mods: Vec<Module<'hir>>,
1212
crate id: hir::HirId,
@@ -17,16 +17,19 @@ crate struct Module<'hir> {
1717
}
1818

1919
impl Module<'hir> {
20-
crate fn new(name: Symbol) -> Module<'hir> {
20+
crate fn new(name: Symbol, id: hir::HirId, where_inner: Span) -> Module<'hir> {
2121
Module {
2222
name,
23-
id: hir::CRATE_HIR_ID,
24-
where_outer: rustc_span::DUMMY_SP,
25-
where_inner: rustc_span::DUMMY_SP,
23+
id,
24+
where_inner,
2625
mods: Vec::new(),
2726
items: Vec::new(),
2827
foreigns: Vec::new(),
2928
macros: Vec::new(),
3029
}
3130
}
31+
32+
crate fn where_outer(&self, tcx: TyCtxt<'_>) -> Span {
33+
tcx.hir().span(self.id)
34+
}
3235
}

‎src/librustdoc/visit_ast.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use rustc_hir::def_id::DefId;
88
use rustc_hir::Node;
99
use rustc_middle::middle::privacy::AccessLevel;
1010
use rustc_middle::ty::TyCtxt;
11+
use rustc_span;
1112
use rustc_span::source_map::Spanned;
1213
use rustc_span::symbol::{kw, sym, Symbol};
13-
use rustc_span::{self, Span};
1414

1515
use std::mem;
1616

@@ -73,7 +73,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
7373
crate fn visit(mut self, krate: &'tcx hir::Crate<'_>) -> Module<'tcx> {
7474
let span = krate.item.inner;
7575
let mut top_level_module = self.visit_mod_contents(
76-
span,
7776
&Spanned { span, node: hir::VisibilityKind::Public },
7877
hir::CRATE_HIR_ID,
7978
&krate.item,
@@ -129,16 +128,12 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
129128

130129
fn visit_mod_contents(
131130
&mut self,
132-
span: Span,
133131
vis: &hir::Visibility<'_>,
134132
id: hir::HirId,
135133
m: &'tcx hir::Mod<'tcx>,
136134
name: Symbol,
137135
) -> Module<'tcx> {
138-
let mut om = Module::new(name);
139-
om.where_outer = span;
140-
om.where_inner = m.inner;
141-
om.id = id;
136+
let mut om = Module::new(name, id, m.inner);
142137
// Keep track of if there were any private modules in the path.
143138
let orig_inside_public_path = self.inside_public_path;
144139
self.inside_public_path &= vis.node.is_pub();
@@ -312,7 +307,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
312307
om.items.push((item, renamed))
313308
}
314309
hir::ItemKind::Mod(ref m) => {
315-
om.mods.push(self.visit_mod_contents(item.span, &item.vis, item.hir_id(), m, name));
310+
om.mods.push(self.visit_mod_contents(&item.vis, item.hir_id(), m, name));
316311
}
317312
hir::ItemKind::Fn(..)
318313
| hir::ItemKind::ExternCrate(..)

0 commit comments

Comments
 (0)
Please sign in to comment.