Skip to content

Commit daa2ebc

Browse files
authored
Rollup merge of rust-lang#123989 - compiler-errors:type-dependent-def-id, r=oli-obk
Just use `type_dependent_def_id` to figure out what the method is for an expr The calls to `lookup_method_for_diagnostic` are overkill. r? oli-obk
2 parents 445eb2e + 05bb3d2 commit daa2ebc

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
@@ -1752,32 +1752,31 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
17521752
let tcx = self.infcx.tcx;
17531753
let hir = tcx.hir();
17541754
let Some(body_id) = tcx.hir_node(self.mir_hir_id()).body_id() else { return };
1755-
struct FindUselessClone<'hir> {
1756-
tcx: TyCtxt<'hir>,
1757-
def_id: DefId,
1758-
pub clones: Vec<&'hir hir::Expr<'hir>>,
1755+
1756+
struct FindUselessClone<'tcx> {
1757+
tcx: TyCtxt<'tcx>,
1758+
typeck_results: &'tcx ty::TypeckResults<'tcx>,
1759+
pub clones: Vec<&'tcx hir::Expr<'tcx>>,
17591760
}
1760-
impl<'hir> FindUselessClone<'hir> {
1761-
pub fn new(tcx: TyCtxt<'hir>, def_id: DefId) -> Self {
1762-
Self { tcx, def_id, clones: vec![] }
1761+
impl<'tcx> FindUselessClone<'tcx> {
1762+
pub fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
1763+
Self { tcx, typeck_results: tcx.typeck(def_id), clones: vec![] }
17631764
}
17641765
}
1765-
1766-
impl<'v> Visitor<'v> for FindUselessClone<'v> {
1767-
fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
1768-
if let hir::ExprKind::MethodCall(segment, _rcvr, args, _span) = ex.kind
1769-
&& segment.ident.name == sym::clone
1770-
&& args.len() == 0
1771-
&& let Some(def_id) = self.def_id.as_local()
1772-
&& let Some(method) = self.tcx.lookup_method_for_diagnostic((def_id, ex.hir_id))
1773-
&& Some(self.tcx.parent(method)) == self.tcx.lang_items().clone_trait()
1766+
impl<'tcx> Visitor<'tcx> for FindUselessClone<'tcx> {
1767+
fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
1768+
if let hir::ExprKind::MethodCall(..) = ex.kind
1769+
&& let Some(method_def_id) =
1770+
self.typeck_results.type_dependent_def_id(ex.hir_id)
1771+
&& self.tcx.lang_items().clone_trait() == Some(self.tcx.parent(method_def_id))
17741772
{
17751773
self.clones.push(ex);
17761774
}
17771775
hir::intravisit::walk_expr(self, ex);
17781776
}
17791777
}
1780-
let mut expr_finder = FindUselessClone::new(tcx, self.mir_def_id().into());
1778+
1779+
let mut expr_finder = FindUselessClone::new(tcx, self.mir_def_id());
17811780

17821781
let body = hir.body(body_id).value;
17831782
expr_finder.visit_expr(body);

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

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

11401135
// Get the type for the parameter corresponding to the argument the closure with the
11411136
// lifetime error we had.
11421137
let Some(input) = tcx
1143-
.fn_sig(method)
1138+
.fn_sig(method_def_id)
11441139
.instantiate_identity()
11451140
.inputs()
11461141
.skip_binder()
@@ -1155,7 +1150,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
11551150
let ty::Param(closure_param) = input.kind() else { return };
11561151

11571152
// Get the arguments for the found method, only specifying that `Self` is the receiver type.
1158-
let args = GenericArgs::for_item(tcx, method, |param, _| {
1153+
let args = GenericArgs::for_item(tcx, method_def_id, |param, _| {
11591154
if param.index == 0 {
11601155
possible_rcvr_ty.into()
11611156
} else if param.index == closure_param.index {
@@ -1165,7 +1160,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
11651160
}
11661161
});
11671162

1168-
let preds = tcx.predicates_of(method).instantiate(tcx, args);
1163+
let preds = tcx.predicates_of(method_def_id).instantiate(tcx, args);
11691164

11701165
let ocx = ObligationCtxt::new(&self.infcx);
11711166
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;
@@ -427,36 +427,13 @@ fn fatally_break_rust(tcx: TyCtxt<'_>, span: Span) -> ! {
427427
diag.emit()
428428
}
429429

430-
pub fn lookup_method_for_diagnostic<'tcx>(
431-
tcx: TyCtxt<'tcx>,
432-
(def_id, hir_id): (LocalDefId, hir::HirId),
433-
) -> Option<DefId> {
434-
let root_ctxt = TypeckRootCtxt::new(tcx, def_id);
435-
let param_env = tcx.param_env(def_id);
436-
let fn_ctxt = FnCtxt::new(&root_ctxt, param_env, def_id);
437-
let hir::Node::Expr(expr) = tcx.hir().hir_node(hir_id) else {
438-
return None;
439-
};
440-
let hir::ExprKind::MethodCall(segment, rcvr, _, _) = expr.kind else {
441-
return None;
442-
};
443-
let tables = tcx.typeck(def_id);
444-
// The found `Self` type of the method call.
445-
let possible_rcvr_ty = tables.node_type_opt(rcvr.hir_id)?;
446-
fn_ctxt
447-
.lookup_method_for_diagnostic(possible_rcvr_ty, segment, expr.span, expr, rcvr)
448-
.ok()
449-
.map(|method| method.def_id)
450-
}
451-
452430
pub fn provide(providers: &mut Providers) {
453431
method::provide(providers);
454432
*providers = Providers {
455433
typeck,
456434
diagnostic_only_typeck,
457435
has_typeck_results,
458436
used_trait_imports,
459-
lookup_method_for_diagnostic: lookup_method_for_diagnostic,
460437
..*providers
461438
};
462439
}

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)