Skip to content

Commit c8a29c4

Browse files
committed
rustdoc: External module item links to the module contents. Fixes #12926.
the basic strategy is to distinguish `mod foo;` from `mod foo {...}` by checking if the span for the module item and module contents is in different files. if it's the case, we prefer module contents. it is technically possible to fix #12926 without changing the AST, probably by checking the individual items' span. this is not without a problem though, since it is possible that some items inside `mod foo {...}` may have originated from other file (e.g. `include!`). therefore it is better to record both spans explicitly.
1 parent dee21a6 commit c8a29c4

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/librustdoc/clean.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,27 @@ impl Clean<Item> for doctree::Module {
223223
self.view_items.clean().move_iter().collect(),
224224
self.macros.clean().move_iter().collect()
225225
);
226+
227+
// determine if we should display the inner contents or
228+
// the outer `mod` item for the source code.
229+
let where = {
230+
let ctxt = local_data::get(super::ctxtkey, |x| *x.unwrap());
231+
let cm = ctxt.sess().codemap();
232+
let outer = cm.lookup_char_pos(self.where_outer.lo);
233+
let inner = cm.lookup_char_pos(self.where_inner.lo);
234+
if outer.file.start_pos == inner.file.start_pos {
235+
// mod foo { ... }
236+
self.where_outer
237+
} else {
238+
// mod foo; (and a separate FileMap for the contents)
239+
self.where_inner
240+
}
241+
};
242+
226243
Item {
227244
name: Some(name),
228245
attrs: self.attrs.clean(),
229-
source: self.where.clean(),
246+
source: where.clean(),
230247
visibility: self.vis.clean(),
231248
id: self.id,
232249
inner: ModuleItem(Module {

src/librustdoc/doctree.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use syntax::ast::{Ident, NodeId};
1919
pub struct Module {
2020
pub name: Option<Ident>,
2121
pub attrs: Vec<ast::Attribute>,
22-
pub where: Span,
22+
pub where_outer: Span,
23+
pub where_inner: Span,
2324
pub structs: Vec<Struct>,
2425
pub enums: Vec<Enum>,
2526
pub fns: Vec<Function>,
@@ -42,7 +43,8 @@ impl Module {
4243
name : name,
4344
id: 0,
4445
vis: ast::Inherited,
45-
where: syntax::codemap::DUMMY_SP,
46+
where_outer: syntax::codemap::DUMMY_SP,
47+
where_inner: syntax::codemap::DUMMY_SP,
4648
attrs : Vec::new(),
4749
structs : Vec::new(),
4850
enums : Vec::new(),

src/librustdoc/visit_ast.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ impl<'a> RustdocVisitor<'a> {
118118
for item in m.view_items.iter() {
119119
self.visit_view_item(item, &mut om);
120120
}
121-
om.where = span;
121+
om.where_outer = span;
122+
om.where_inner = m.inner;
122123
om.attrs = attrs;
123124
om.vis = vis;
124125
om.id = id;

0 commit comments

Comments
 (0)