Skip to content

Commit c665d2e

Browse files
Fix invalid handling for static method calls in jump to definition feature
1 parent c2dcfc7 commit c665d2e

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

src/librustdoc/html/render/span_map.rs

+34-20
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,30 @@ impl<'tcx> SpanMapVisitor<'tcx> {
154154
self.matches.insert(new_span, link_from_src);
155155
true
156156
}
157+
158+
fn handle_call(&mut self, hir_id: HirId, expr_hir_id: Option<HirId>, span: Span) {
159+
let hir = self.tcx.hir();
160+
let body_id = hir.enclosing_body_owner(hir_id);
161+
// FIXME: this is showing error messages for parts of the code that are not
162+
// compiled (because of cfg)!
163+
//
164+
// See discussion in https://github.com/rust-lang/rust/issues/69426#issuecomment-1019412352
165+
let typeck_results = self
166+
.tcx
167+
.typeck_body(hir.maybe_body_owned_by(body_id).expect("a body which isn't a body"));
168+
// Interestingly enough, for method calls, we need the whole expression whereas for static
169+
// method/function calls, we need the call expression specifically.
170+
if let Some(def_id) = typeck_results.type_dependent_def_id(hir_id).or_else(|| {
171+
expr_hir_id.and_then(|expr_hir_id| typeck_results.type_dependent_def_id(expr_hir_id))
172+
}) {
173+
let link = if def_id.as_local().is_some() {
174+
LinkFromSrc::Local(rustc_span(def_id, self.tcx))
175+
} else {
176+
LinkFromSrc::External(def_id)
177+
};
178+
self.matches.insert(span, link);
179+
}
180+
}
157181
}
158182

159183
impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
@@ -191,27 +215,17 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
191215
}
192216

193217
fn visit_expr(&mut self, expr: &'tcx rustc_hir::Expr<'tcx>) {
194-
if let ExprKind::MethodCall(segment, ..) = expr.kind {
195-
let hir = self.tcx.hir();
196-
let body_id = hir.enclosing_body_owner(segment.hir_id);
197-
// FIXME: this is showing error messages for parts of the code that are not
198-
// compiled (because of cfg)!
199-
//
200-
// See discussion in https://github.com/rust-lang/rust/issues/69426#issuecomment-1019412352
201-
let typeck_results = self
202-
.tcx
203-
.typeck_body(hir.maybe_body_owned_by(body_id).expect("a body which isn't a body"));
204-
if let Some(def_id) = typeck_results.type_dependent_def_id(expr.hir_id) {
205-
let link = if def_id.as_local().is_some() {
206-
LinkFromSrc::Local(rustc_span(def_id, self.tcx))
207-
} else {
208-
LinkFromSrc::External(def_id)
209-
};
210-
self.matches.insert(segment.ident.span, link);
218+
match expr.kind {
219+
ExprKind::MethodCall(segment, ..) => {
220+
self.handle_call(segment.hir_id, Some(expr.hir_id), segment.ident.span)
221+
}
222+
ExprKind::Call(call, ..) => self.handle_call(call.hir_id, None, call.span),
223+
_ => {
224+
if self.handle_macro(expr.span) {
225+
// We don't want to go deeper into the macro.
226+
return;
227+
}
211228
}
212-
} else if self.handle_macro(expr.span) {
213-
// We don't want to go deeper into the macro.
214-
return;
215229
}
216230
intravisit::walk_expr(self, expr);
217231
}

0 commit comments

Comments
 (0)