1
Fork 0

Rollup merge of #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
This commit is contained in:
León Orell Valerian Liehr 2024-04-16 01:12:39 +02:00 committed by GitHub
commit daa2ebc70c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 21 additions and 53 deletions

View file

@ -1752,32 +1752,31 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
let tcx = self.infcx.tcx; let tcx = self.infcx.tcx;
let hir = tcx.hir(); let hir = tcx.hir();
let Some(body_id) = tcx.hir_node(self.mir_hir_id()).body_id() else { return }; let Some(body_id) = tcx.hir_node(self.mir_hir_id()).body_id() else { return };
struct FindUselessClone<'hir> {
tcx: TyCtxt<'hir>, struct FindUselessClone<'tcx> {
def_id: DefId, tcx: TyCtxt<'tcx>,
pub clones: Vec<&'hir hir::Expr<'hir>>, typeck_results: &'tcx ty::TypeckResults<'tcx>,
pub clones: Vec<&'tcx hir::Expr<'tcx>>,
} }
impl<'hir> FindUselessClone<'hir> { impl<'tcx> FindUselessClone<'tcx> {
pub fn new(tcx: TyCtxt<'hir>, def_id: DefId) -> Self { pub fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
Self { tcx, def_id, clones: vec![] } Self { tcx, typeck_results: tcx.typeck(def_id), clones: vec![] }
} }
} }
impl<'tcx> Visitor<'tcx> for FindUselessClone<'tcx> {
impl<'v> Visitor<'v> for FindUselessClone<'v> { fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) { if let hir::ExprKind::MethodCall(..) = ex.kind
if let hir::ExprKind::MethodCall(segment, _rcvr, args, _span) = ex.kind && let Some(method_def_id) =
&& segment.ident.name == sym::clone self.typeck_results.type_dependent_def_id(ex.hir_id)
&& args.len() == 0 && self.tcx.lang_items().clone_trait() == Some(self.tcx.parent(method_def_id))
&& let Some(def_id) = self.def_id.as_local()
&& let Some(method) = self.tcx.lookup_method_for_diagnostic((def_id, ex.hir_id))
&& Some(self.tcx.parent(method)) == self.tcx.lang_items().clone_trait()
{ {
self.clones.push(ex); self.clones.push(ex);
} }
hir::intravisit::walk_expr(self, ex); hir::intravisit::walk_expr(self, ex);
} }
} }
let mut expr_finder = FindUselessClone::new(tcx, self.mir_def_id().into());
let mut expr_finder = FindUselessClone::new(tcx, self.mir_def_id());
let body = hir.body(body_id).value; let body = hir.body(body_id).value;
expr_finder.visit_expr(body); expr_finder.visit_expr(body);

View file

@ -1130,17 +1130,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
}; };
// The found `Self` type of the method call. // The found `Self` type of the method call.
let Some(possible_rcvr_ty) = tables.node_type_opt(rcvr.hir_id) else { return }; let Some(possible_rcvr_ty) = tables.node_type_opt(rcvr.hir_id) else { return };
let Some(method_def_id) = tables.type_dependent_def_id(expr.hir_id) else { return };
// The `MethodCall` expression is `Res::Err`, so we search for the method on the `rcvr_ty`.
let Some(method) = tcx.lookup_method_for_diagnostic((self.mir_def_id(), expr.hir_id))
else {
return;
};
// Get the type for the parameter corresponding to the argument the closure with the // Get the type for the parameter corresponding to the argument the closure with the
// lifetime error we had. // lifetime error we had.
let Some(input) = tcx let Some(input) = tcx
.fn_sig(method) .fn_sig(method_def_id)
.instantiate_identity() .instantiate_identity()
.inputs() .inputs()
.skip_binder() .skip_binder()
@ -1155,7 +1150,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
let ty::Param(closure_param) = input.kind() else { return }; let ty::Param(closure_param) = input.kind() else { return };
// Get the arguments for the found method, only specifying that `Self` is the receiver type. // Get the arguments for the found method, only specifying that `Self` is the receiver type.
let args = GenericArgs::for_item(tcx, method, |param, _| { let args = GenericArgs::for_item(tcx, method_def_id, |param, _| {
if param.index == 0 { if param.index == 0 {
possible_rcvr_ty.into() possible_rcvr_ty.into()
} else if param.index == closure_param.index { } else if param.index == closure_param.index {
@ -1165,7 +1160,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
} }
}); });
let preds = tcx.predicates_of(method).instantiate(tcx, args); let preds = tcx.predicates_of(method_def_id).instantiate(tcx, args);
let ocx = ObligationCtxt::new(&self.infcx); let ocx = ObligationCtxt::new(&self.infcx);
ocx.register_obligations(preds.iter().map(|(pred, span)| { ocx.register_obligations(preds.iter().map(|(pred, span)| {

View file

@ -56,7 +56,7 @@ use rustc_data_structures::unord::UnordSet;
use rustc_errors::{codes::*, struct_span_code_err, ErrorGuaranteed}; use rustc_errors::{codes::*, struct_span_code_err, ErrorGuaranteed};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res}; use rustc_hir::def::{DefKind, Res};
use rustc_hir::intravisit::{Map, Visitor}; use rustc_hir::intravisit::Visitor;
use rustc_hir::{HirIdMap, Node}; use rustc_hir::{HirIdMap, Node};
use rustc_hir_analysis::check::check_abi; use rustc_hir_analysis::check::check_abi;
use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer; use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
@ -427,28 +427,6 @@ fn fatally_break_rust(tcx: TyCtxt<'_>, span: Span) -> ! {
diag.emit() diag.emit()
} }
pub fn lookup_method_for_diagnostic<'tcx>(
tcx: TyCtxt<'tcx>,
(def_id, hir_id): (LocalDefId, hir::HirId),
) -> Option<DefId> {
let root_ctxt = TypeckRootCtxt::new(tcx, def_id);
let param_env = tcx.param_env(def_id);
let fn_ctxt = FnCtxt::new(&root_ctxt, param_env, def_id);
let hir::Node::Expr(expr) = tcx.hir().hir_node(hir_id) else {
return None;
};
let hir::ExprKind::MethodCall(segment, rcvr, _, _) = expr.kind else {
return None;
};
let tables = tcx.typeck(def_id);
// The found `Self` type of the method call.
let possible_rcvr_ty = tables.node_type_opt(rcvr.hir_id)?;
fn_ctxt
.lookup_method_for_diagnostic(possible_rcvr_ty, segment, expr.span, expr, rcvr)
.ok()
.map(|method| method.def_id)
}
pub fn provide(providers: &mut Providers) { pub fn provide(providers: &mut Providers) {
method::provide(providers); method::provide(providers);
*providers = Providers { *providers = Providers {
@ -456,7 +434,6 @@ pub fn provide(providers: &mut Providers) {
diagnostic_only_typeck, diagnostic_only_typeck,
has_typeck_results, has_typeck_results,
used_trait_imports, used_trait_imports,
lookup_method_for_diagnostic: lookup_method_for_diagnostic,
..*providers ..*providers
}; };
} }

View file

@ -983,9 +983,6 @@ rustc_queries! {
query diagnostic_only_typeck(key: LocalDefId) -> &'tcx ty::TypeckResults<'tcx> { query diagnostic_only_typeck(key: LocalDefId) -> &'tcx ty::TypeckResults<'tcx> {
desc { |tcx| "type-checking `{}`", tcx.def_path_str(key) } desc { |tcx| "type-checking `{}`", tcx.def_path_str(key) }
} }
query lookup_method_for_diagnostic((def_id, hir_id): (LocalDefId, hir::HirId)) -> Option<DefId> {
desc { |tcx| "lookup_method_for_diagnostics `{}`", tcx.def_path_str(def_id) }
}
query used_trait_imports(key: LocalDefId) -> &'tcx UnordSet<LocalDefId> { query used_trait_imports(key: LocalDefId) -> &'tcx UnordSet<LocalDefId> {
desc { |tcx| "finding used_trait_imports `{}`", tcx.def_path_str(key) } desc { |tcx| "finding used_trait_imports `{}`", tcx.def_path_str(key) }