Fix invalid handling for static method calls in jump to definition feature

This commit is contained in:
Guillaume Gomez 2024-01-04 20:19:39 +01:00
parent c2dcfc762d
commit 35ad2ae71c

View File

@ -154,6 +154,28 @@ impl<'tcx> SpanMapVisitor<'tcx> {
self.matches.insert(new_span, link_from_src); self.matches.insert(new_span, link_from_src);
true true
} }
fn handle_call(&mut self, hir_id: HirId, expr_hir_id: Option<HirId>, span: Span) {
let hir = self.tcx.hir();
let body_id = hir.enclosing_body_owner(hir_id);
// FIXME: this is showing error messages for parts of the code that are not
// compiled (because of cfg)!
//
// See discussion in https://github.com/rust-lang/rust/issues/69426#issuecomment-1019412352
let typeck_results = self
.tcx
.typeck_body(hir.maybe_body_owned_by(body_id).expect("a body which isn't a body"));
// Interestingly enough, for method calls, we need the whole expression whereas for static
// method/function calls, we need the call expression specifically.
if let Some(def_id) = typeck_results.type_dependent_def_id(expr_hir_id.unwrap_or(hir_id)) {
let link = if def_id.as_local().is_some() {
LinkFromSrc::Local(rustc_span(def_id, self.tcx))
} else {
LinkFromSrc::External(def_id)
};
self.matches.insert(span, link);
}
}
} }
impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> { impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
@ -191,27 +213,17 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
} }
fn visit_expr(&mut self, expr: &'tcx rustc_hir::Expr<'tcx>) { fn visit_expr(&mut self, expr: &'tcx rustc_hir::Expr<'tcx>) {
if let ExprKind::MethodCall(segment, ..) = expr.kind { match expr.kind {
let hir = self.tcx.hir(); ExprKind::MethodCall(segment, ..) => {
let body_id = hir.enclosing_body_owner(segment.hir_id); self.handle_call(segment.hir_id, Some(expr.hir_id), segment.ident.span)
// FIXME: this is showing error messages for parts of the code that are not }
// compiled (because of cfg)! ExprKind::Call(call, ..) => self.handle_call(call.hir_id, None, call.span),
// _ => {
// See discussion in https://github.com/rust-lang/rust/issues/69426#issuecomment-1019412352 if self.handle_macro(expr.span) {
let typeck_results = self // We don't want to go deeper into the macro.
.tcx return;
.typeck_body(hir.maybe_body_owned_by(body_id).expect("a body which isn't a body")); }
if let Some(def_id) = typeck_results.type_dependent_def_id(expr.hir_id) {
let link = if def_id.as_local().is_some() {
LinkFromSrc::Local(rustc_span(def_id, self.tcx))
} else {
LinkFromSrc::External(def_id)
};
self.matches.insert(segment.ident.span, link);
} }
} else if self.handle_macro(expr.span) {
// We don't want to go deeper into the macro.
return;
} }
intravisit::walk_expr(self, expr); intravisit::walk_expr(self, expr);
} }