Don't special-case ExprKind::Call

Don't unnecessarily try to obtain the type-dependent definition of callees
in `visit_expr`, just let `visit_qpath` handle callees.
This means that for callees that are

* `Resolved` paths (the majority of callees) we don't try to `typeck` the
  enclosing body which should improve perf if the body doesn't contain
  any type-dependent definitions.
* actually `TypeRelative` paths we don't resolve them twice (with
  slightly different spans)
This commit is contained in:
León Orell Valerian Liehr
2026-05-10 19:55:04 +02:00
parent bfcaf513b4
commit efaf441d7b
+7 -16
View File
@@ -316,23 +316,14 @@ fn visit_mod(&mut self, m: &'tcx Mod<'tcx>, span: rustc_span::Span, id: HirId) {
}
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
let mut handle_type_dependent_def = |hir_id: HirId, span: rustc_span::Span| {
// Exprs *have* to exist in a body, so typeck results should always be available.
let typeck_results = self.maybe_typeck_results().unwrap();
if let Some(def_id) = typeck_results.type_dependent_def_id(hir_id) {
self.matches.insert(span.into(), self.link_for_def(def_id));
}
};
match expr.kind {
ExprKind::MethodCall(seg, ..) => handle_type_dependent_def(expr.hir_id, seg.ident.span),
// FIXME(fmease): We needlessly request `TypeckResults` even if the callee isn't
// type-relative. In the majority of cases, it's just gonna be a
// `Resolved` path meaning we can end up unnecessarily
// `typeck`'ing the body which is super costly!
// Moreover, if it actually is a type-relative path, we end up
// "resolving" it twice (with slightly different spans).
ExprKind::Call(callee, ..) => handle_type_dependent_def(callee.hir_id, callee.span),
ExprKind::MethodCall(segment, ..) => {
// Exprs *have* to exist in a body, so typeck results should always be available.
let typeck_results = self.maybe_typeck_results().unwrap();
if let Some(def_id) = typeck_results.type_dependent_def_id(expr.hir_id) {
self.matches.insert(segment.ident.span.into(), self.link_for_def(def_id));
}
}
// We don't want to go deeper into the macro.
_ if self.handle_macro(expr.span) => return,
_ => {}