Skip to content

Commit 1abb7fa

Browse files
Generate links for modules as well
1 parent 71763a5 commit 1abb7fa

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/librustdoc/html/render/span_map.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_data_structures::fx::FxHashMap;
55
use rustc_hir::def::{DefKind, Res};
66
use rustc_hir::def_id::DefId;
77
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
8-
use rustc_hir::{ExprKind, GenericParam, GenericParamKind, HirId};
8+
use rustc_hir::{ExprKind, GenericParam, GenericParamKind, HirId, Mod, Node};
99
use rustc_middle::ty::TyCtxt;
1010
use rustc_span::Span;
1111

@@ -20,14 +20,14 @@ crate fn collect_spans_and_sources(
2020
krate: clean::Crate,
2121
src_root: &std::path::Path,
2222
include_sources: bool,
23-
_generate_link_to_definition: bool,
23+
generate_link_to_definition: bool,
2424
) -> (clean::Crate, FxHashMap<std::path::PathBuf, String>, FxHashMap<(u32, u32), LinkFromSrc>) {
2525
let mut visitor = SpanMapVisitor { tcx, matches: FxHashMap::default() };
2626

2727
if include_sources {
28-
// if generate_link_to_definition {
29-
intravisit::walk_crate(&mut visitor, tcx.hir().krate());
30-
// }
28+
if generate_link_to_definition {
29+
intravisit::walk_crate(&mut visitor, tcx.hir().krate());
30+
}
3131
let (krate, sources) = sources::collect_local_sources(tcx, src_root, krate);
3232
(krate, sources, visitor.matches)
3333
} else {
@@ -94,6 +94,25 @@ impl Visitor<'tcx> for SpanMapVisitor<'tcx> {
9494
intravisit::walk_path(self, path);
9595
}
9696

97+
fn visit_mod(&mut self, m: &'tcx Mod<'tcx>, span: Span, id: HirId) {
98+
// To make the difference between "mod foo {}" and "mod foo;". In case we "import" another
99+
// file, we want to link to it. Otherwise no need to create a link.
100+
if !span.overlaps(m.inner) {
101+
// Now that we confirmed it's a file import, we want to get the span for the module
102+
// name only and not all the "mod foo;".
103+
if let Some(node) = self.tcx.hir().find(id) {
104+
match node {
105+
Node::Item(item) => {
106+
self.matches
107+
.insert(span_to_tuple(item.ident.span), LinkFromSrc::Local(m.inner));
108+
}
109+
_ => {}
110+
}
111+
}
112+
}
113+
intravisit::walk_mod(self, m, id);
114+
}
115+
97116
fn visit_expr(&mut self, expr: &'tcx rustc_hir::Expr<'tcx>) {
98117
match expr.kind {
99118
ExprKind::MethodCall(segment, method_span, _, _) => {

src/test/rustdoc/check-source-code-urls-to-def.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
#![crate_name = "foo"]
44

5+
// @has 'src/foo/check-source-code-urls-to-def.rs.html'
6+
7+
// @has - '//a[@href="../../src/foo/auxiliary/source-code-bar.rs.html#1-17"]' 'bar'
58
#[path = "auxiliary/source-code-bar.rs"]
69
pub mod bar;
710

8-
// @has 'src/foo/check-source-code-urls-to-def.rs.html'
9-
1011
// @count - '//a[@href="../../src/foo/auxiliary/source-code-bar.rs.html#5-7"]' 4
1112
use bar::Bar;
1213
// @has - '//a[@href="../../src/foo/auxiliary/source-code-bar.rs.html#13-17"]' 'self'
@@ -22,13 +23,13 @@ impl Foo {
2223
fn babar() {}
2324

2425
// @has - '//a[@href="https://doc.rust-lang.org/nightly/alloc/string/struct.String.html"]' 'String'
25-
// @count - '//a[@href="../../src/foo/check-source-code-urls-to-def.rs.html#16"]' 5
26+
// @count - '//a[@href="../../src/foo/check-source-code-urls-to-def.rs.html#17"]' 5
2627
pub fn foo(a: u32, b: &str, c: String, d: Foo, e: bar::Bar) {
2728
let x = 12;
2829
let y: Foo = Foo;
2930
let z: Bar = bar::Bar { field: Foo };
3031
babar();
31-
// @has - '//a[@href="../../src/foo/check-source-code-urls-to-def.rs.html#19"]' 'hello'
32+
// @has - '//a[@href="../../src/foo/check-source-code-urls-to-def.rs.html#20"]' 'hello'
3233
y.hello();
3334
}
3435

0 commit comments

Comments
 (0)