Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit b689ced

Browse files
Generate links for methods as well
1 parent 83dcd30 commit b689ced

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

src/librustdoc/html/highlight.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,9 +547,9 @@ fn string<T: Display>(
547547
LinkFromSrc::Local(span) => {
548548
eprintln!("==> {:?}:{:?}", span.lo(), span.hi());
549549
context
550-
.href_from_span(clean::Span::wrap(*span))
551-
.map(|s| format!("{}{}", root_path, s))
552-
},
550+
.href_from_span(clean::Span::wrap(*span))
551+
.map(|s| format!("{}{}", root_path, s))
552+
}
553553
LinkFromSrc::External(def_id) => {
554554
format::href(*def_id, context).map(|(url, _, _)| url)
555555
}

src/librustdoc/html/render/span_map.rs

Lines changed: 36 additions & 16 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};
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,17 +94,37 @@ impl Visitor<'tcx> for SpanMapVisitor<'tcx> {
9494
intravisit::walk_path(self, path);
9595
}
9696

97-
// fn visit_expr(&mut self, expr: &'tcx rustc_hir::Expr<'tcx>) {
98-
// match expr.kind {
99-
// ExprKind::MethodCall(segment, method_span, _, _) => {
100-
// if let Some(hir_id) = segment.hir_id {
101-
// // call https://doc.rust-lang.org/beta/nightly-rustc/rustc_middle/ty/context/struct.TypeckResults.html#method.type_dependent_def_id
102-
// }
103-
// }
104-
// _ => {}
105-
// }
106-
// intravisit::walk_expr(self, expr);
107-
// }
97+
fn visit_expr(&mut self, expr: &'tcx rustc_hir::Expr<'tcx>) {
98+
match expr.kind {
99+
ExprKind::MethodCall(segment, method_span, _, _) => {
100+
if let Some(hir_id) = segment.hir_id {
101+
let hir = self.tcx.hir();
102+
let body_id = hir.enclosing_body_owner(hir_id);
103+
// FIXME: this is showing error messages for parts of the code that are not
104+
// compiled (because of cfg)!
105+
let typeck_results = self.tcx.typeck_body(
106+
hir.maybe_body_owned_by(body_id).expect("a body which isn't a body"),
107+
);
108+
if let Some(def_id) = typeck_results.type_dependent_def_id(expr.hir_id) {
109+
match hir.span_if_local(def_id) {
110+
Some(span) => {
111+
self.matches
112+
.insert(span_to_tuple(method_span), LinkFromSrc::Local(span));
113+
}
114+
None => {
115+
self.matches.insert(
116+
span_to_tuple(method_span),
117+
LinkFromSrc::External(def_id),
118+
);
119+
}
120+
}
121+
}
122+
}
123+
}
124+
_ => {}
125+
}
126+
intravisit::walk_expr(self, expr);
127+
}
108128

109129
fn visit_use(&mut self, path: &'tcx rustc_hir::Path<'tcx>, id: HirId) {
110130
self.handle_path(path, None);

0 commit comments

Comments
 (0)