Skip to content

Commit 05bb3d2

Browse files
Just use type_dependent_def_id to figure out what the method is for an expr
1 parent 99d0186 commit 05bb3d2

File tree

4 files changed

+21
-53
lines changed

4 files changed

+21
-53
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+16-17
Original file line numberDiff line numberDiff line change
@@ -1813,32 +1813,31 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
18131813
let tcx = self.infcx.tcx;
18141814
let hir = tcx.hir();
18151815
let Some(body_id) = tcx.hir_node(self.mir_hir_id()).body_id() else { return };
1816-
struct FindUselessClone<'hir> {
1817-
tcx: TyCtxt<'hir>,
1818-
def_id: DefId,
1819-
pub clones: Vec<&'hir hir::Expr<'hir>>,
1816+
1817+
struct FindUselessClone<'tcx> {
1818+
tcx: TyCtxt<'tcx>,
1819+
typeck_results: &'tcx ty::TypeckResults<'tcx>,
1820+
pub clones: Vec<&'tcx hir::Expr<'tcx>>,
18201821
}
1821-
impl<'hir> FindUselessClone<'hir> {
1822-
pub fn new(tcx: TyCtxt<'hir>, def_id: DefId) -> Self {
1823-
Self { tcx, def_id, clones: vec![] }
1822+
impl<'tcx> FindUselessClone<'tcx> {
1823+
pub fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
1824+
Self { tcx, typeck_results: tcx.typeck(def_id), clones: vec![] }
18241825
}
18251826
}
1826-
1827-
impl<'v> Visitor<'v> for FindUselessClone<'v> {
1828-
fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
1829-
if let hir::ExprKind::MethodCall(segment, _rcvr, args, _span) = ex.kind
1830-
&& segment.ident.name == sym::clone
1831-
&& args.len() == 0
1832-
&& let Some(def_id) = self.def_id.as_local()
1833-
&& let Some(method) = self.tcx.lookup_method_for_diagnostic((def_id, ex.hir_id))
1834-
&& Some(self.tcx.parent(method)) == self.tcx.lang_items().clone_trait()
1827+
impl<'tcx> Visitor<'tcx> for FindUselessClone<'tcx> {
1828+
fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
1829+
if let hir::ExprKind::MethodCall(..) = ex.kind
1830+
&& let Some(method_def_id) =
1831+
self.typeck_results.type_dependent_def_id(ex.hir_id)
1832+
&& self.tcx.lang_items().clone_trait() == Some(self.tcx.parent(method_def_id))
18351833
{
18361834
self.clones.push(ex);
18371835
}
18381836
hir::intravisit::walk_expr(self, ex);
18391837
}
18401838
}
1841-
let mut expr_finder = FindUselessClone::new(tcx, self.mir_def_id().into());
1839+
1840+
let mut expr_finder = FindUselessClone::new(tcx, self.mir_def_id());
18421841

18431842
let body = hir.body(body_id).value;
18441843
expr_finder.visit_expr(body);

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -1131,17 +1131,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
11311131
};
11321132
// The found `Self` type of the method call.
11331133
let Some(possible_rcvr_ty) = tables.node_type_opt(rcvr.hir_id) else { return };
1134-
1135-
// The `MethodCall` expression is `Res::Err`, so we search for the method on the `rcvr_ty`.
1136-
let Some(method) = tcx.lookup_method_for_diagnostic((self.mir_def_id(), expr.hir_id))
1137-
else {
1138-
return;
1139-
};
1134+
let Some(method_def_id) = tables.type_dependent_def_id(expr.hir_id) else { return };
11401135

11411136
// Get the type for the parameter corresponding to the argument the closure with the
11421137
// lifetime error we had.
11431138
let Some(input) = tcx
1144-
.fn_sig(method)
1139+
.fn_sig(method_def_id)
11451140
.instantiate_identity()
11461141
.inputs()
11471142
.skip_binder()
@@ -1156,7 +1151,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
11561151
let ty::Param(closure_param) = input.kind() else { return };
11571152

11581153
// Get the arguments for the found method, only specifying that `Self` is the receiver type.
1159-
let args = GenericArgs::for_item(tcx, method, |param, _| {
1154+
let args = GenericArgs::for_item(tcx, method_def_id, |param, _| {
11601155
if param.index == 0 {
11611156
possible_rcvr_ty.into()
11621157
} else if param.index == closure_param.index {
@@ -1166,7 +1161,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
11661161
}
11671162
});
11681163

1169-
let preds = tcx.predicates_of(method).instantiate(tcx, args);
1164+
let preds = tcx.predicates_of(method_def_id).instantiate(tcx, args);
11701165

11711166
let ocx = ObligationCtxt::new(&self.infcx);
11721167
ocx.register_obligations(preds.iter().map(|(pred, span)| {

compiler/rustc_hir_typeck/src/lib.rs

+1-24
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ use rustc_data_structures::unord::UnordSet;
5656
use rustc_errors::{codes::*, struct_span_code_err, ErrorGuaranteed};
5757
use rustc_hir as hir;
5858
use rustc_hir::def::{DefKind, Res};
59-
use rustc_hir::intravisit::{Map, Visitor};
59+
use rustc_hir::intravisit::Visitor;
6060
use rustc_hir::{HirIdMap, Node};
6161
use rustc_hir_analysis::check::check_abi;
6262
use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
@@ -436,36 +436,13 @@ fn fatally_break_rust(tcx: TyCtxt<'_>, span: Span) -> ! {
436436
diag.emit()
437437
}
438438

439-
pub fn lookup_method_for_diagnostic<'tcx>(
440-
tcx: TyCtxt<'tcx>,
441-
(def_id, hir_id): (LocalDefId, hir::HirId),
442-
) -> Option<DefId> {
443-
let root_ctxt = TypeckRootCtxt::new(tcx, def_id);
444-
let param_env = tcx.param_env(def_id);
445-
let fn_ctxt = FnCtxt::new(&root_ctxt, param_env, def_id);
446-
let hir::Node::Expr(expr) = tcx.hir().hir_node(hir_id) else {
447-
return None;
448-
};
449-
let hir::ExprKind::MethodCall(segment, rcvr, _, _) = expr.kind else {
450-
return None;
451-
};
452-
let tables = tcx.typeck(def_id);
453-
// The found `Self` type of the method call.
454-
let possible_rcvr_ty = tables.node_type_opt(rcvr.hir_id)?;
455-
fn_ctxt
456-
.lookup_method_for_diagnostic(possible_rcvr_ty, segment, expr.span, expr, rcvr)
457-
.ok()
458-
.map(|method| method.def_id)
459-
}
460-
461439
pub fn provide(providers: &mut Providers) {
462440
method::provide(providers);
463441
*providers = Providers {
464442
typeck,
465443
diagnostic_only_typeck,
466444
has_typeck_results,
467445
used_trait_imports,
468-
lookup_method_for_diagnostic: lookup_method_for_diagnostic,
469446
..*providers
470447
};
471448
}

compiler/rustc_middle/src/query/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -983,9 +983,6 @@ rustc_queries! {
983983
query diagnostic_only_typeck(key: LocalDefId) -> &'tcx ty::TypeckResults<'tcx> {
984984
desc { |tcx| "type-checking `{}`", tcx.def_path_str(key) }
985985
}
986-
query lookup_method_for_diagnostic((def_id, hir_id): (LocalDefId, hir::HirId)) -> Option<DefId> {
987-
desc { |tcx| "lookup_method_for_diagnostics `{}`", tcx.def_path_str(def_id) }
988-
}
989986

990987
query used_trait_imports(key: LocalDefId) -> &'tcx UnordSet<LocalDefId> {
991988
desc { |tcx| "finding used_trait_imports `{}`", tcx.def_path_str(key) }

0 commit comments

Comments
 (0)