Skip to content

Commit f66e174

Browse files
authored
Rollup merge of #133180 - GuillaumeGomez:jump-to-def-links-generics, r=notriddle
[rustdoc] Fix items with generics not having their jump to def link generated Because the span originally included the generics, during the highlighting, it was not retrieved and therefore its jump to def link was not generated. r? ``@notriddle``
2 parents 2226541 + 8b0f8cb commit f66e174

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/librustdoc/html/render/span_map.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub(crate) enum LinkFromSrc {
3636
/// It returns the `krate`, the source code files and the `span` correspondence map.
3737
///
3838
/// Note about the `span` correspondence map: the keys are actually `(lo, hi)` of `span`s. We don't
39-
/// need the `span` context later on, only their position, so instead of keep a whole `Span`, we
39+
/// need the `span` context later on, only their position, so instead of keeping a whole `Span`, we
4040
/// only keep the `lo` and `hi`.
4141
pub(crate) fn collect_spans_and_sources(
4242
tcx: TyCtxt<'_>,
@@ -45,9 +45,9 @@ pub(crate) fn collect_spans_and_sources(
4545
include_sources: bool,
4646
generate_link_to_definition: bool,
4747
) -> (FxIndexMap<PathBuf, String>, FxHashMap<Span, LinkFromSrc>) {
48-
let mut visitor = SpanMapVisitor { tcx, matches: FxHashMap::default() };
49-
5048
if include_sources {
49+
let mut visitor = SpanMapVisitor { tcx, matches: FxHashMap::default() };
50+
5151
if generate_link_to_definition {
5252
tcx.hir().walk_toplevel_module(&mut visitor);
5353
}
@@ -76,7 +76,22 @@ impl<'tcx> SpanMapVisitor<'tcx> {
7676
} else {
7777
LinkFromSrc::External(def_id)
7878
};
79-
self.matches.insert(path.span, link);
79+
// In case the path ends with generics, we remove them from the span.
80+
let span = path
81+
.segments
82+
.last()
83+
.map(|last| {
84+
// In `use` statements, the included item is not in the path segments.
85+
// However, it doesn't matter because you can't have generics on `use`
86+
// statements.
87+
if path.span.contains(last.ident.span) {
88+
path.span.with_hi(last.ident.span.hi())
89+
} else {
90+
path.span
91+
}
92+
})
93+
.unwrap_or(path.span);
94+
self.matches.insert(span, link);
8095
}
8196
Res::Local(_) => {
8297
if let Some(span) = self.tcx.hir().res_span(path.res) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This test ensures that paths with generics still get their link to their definition
2+
// correctly generated.
3+
4+
//@ compile-flags: -Zunstable-options --generate-link-to-definition
5+
#![crate_name = "foo"]
6+
7+
//@ has 'src/foo/link-on-path-with-generics.rs.html'
8+
9+
pub struct Soyo<T>(T);
10+
pub struct Saya;
11+
12+
//@ has - '//pre[@class="rust"]//a[@href="#9"]' 'Soyo'
13+
//@ has - '//pre[@class="rust"]//a[@href="#10"]' 'Saya'
14+
pub fn bar<T>(s: Soyo<T>, x: Saya) {}

0 commit comments

Comments
 (0)