@@ -5,6 +5,7 @@ use crate::errors;
55use crate :: FnCtxt ;
66use rustc_ast:: ast:: Mutability ;
77use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
8+ use rustc_errors:: StashKey ;
89use rustc_errors:: {
910 pluralize, struct_span_err, Applicability , Diagnostic , DiagnosticBuilder , ErrorGuaranteed ,
1011 MultiSpan ,
@@ -13,6 +14,8 @@ use rustc_hir as hir;
1314use rustc_hir:: def:: DefKind ;
1415use rustc_hir:: def_id:: DefId ;
1516use rustc_hir:: lang_items:: LangItem ;
17+ use rustc_hir:: PatKind :: Binding ;
18+ use rustc_hir:: PathSegment ;
1619use rustc_hir:: { ExprKind , Node , QPath } ;
1720use rustc_infer:: infer:: {
1821 type_variable:: { TypeVariableOrigin , TypeVariableOriginKind } ,
@@ -35,11 +38,11 @@ use rustc_trait_selection::traits::{
3538 FulfillmentError , Obligation , ObligationCause , ObligationCauseCode ,
3639} ;
3740
38- use std:: cmp:: Ordering ;
39- use std:: iter;
40-
4141use super :: probe:: { AutorefOrPtrAdjustment , IsSuggestion , Mode , ProbeScope } ;
4242use super :: { CandidateSource , MethodError , NoMatchData } ;
43+ use rustc_hir:: intravisit:: Visitor ;
44+ use std:: cmp:: Ordering ;
45+ use std:: iter;
4346
4447impl < ' a , ' tcx > FnCtxt < ' a , ' tcx > {
4548 fn is_fn_ty ( & self , ty : Ty < ' tcx > , span : Span ) -> bool {
@@ -1462,6 +1465,61 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14621465 false
14631466 }
14641467
1468+ /// For code `rect::area(...)`,
1469+ /// if `rect` is a local variable and `area` is a valid assoc method for it,
1470+ /// we try to suggest `rect.area()`
1471+ pub ( crate ) fn suggest_assoc_method_call ( & self , segs : & [ PathSegment < ' _ > ] ) {
1472+ debug ! ( "suggest_assoc_method_call segs: {:?}" , segs) ;
1473+ let [ seg1, seg2] = segs else { return ; } ;
1474+ let Some ( mut diag) =
1475+ self . tcx . sess . diagnostic ( ) . steal_diagnostic ( seg1. ident . span , StashKey :: CallAssocMethod )
1476+ else { return } ;
1477+
1478+ let map = self . infcx . tcx . hir ( ) ;
1479+ let body = map. body ( rustc_hir:: BodyId { hir_id : self . body_id } ) ;
1480+ struct LetVisitor < ' a > {
1481+ result : Option < & ' a hir:: Expr < ' a > > ,
1482+ ident_name : Symbol ,
1483+ }
1484+
1485+ impl < ' v > Visitor < ' v > for LetVisitor < ' v > {
1486+ fn visit_stmt ( & mut self , ex : & ' v hir:: Stmt < ' v > ) {
1487+ if let hir:: StmtKind :: Local ( hir:: Local { pat, init, .. } ) = & ex. kind {
1488+ if let Binding ( _, _, ident, ..) = pat. kind &&
1489+ ident. name == self . ident_name {
1490+ self . result = * init;
1491+ }
1492+ }
1493+ hir:: intravisit:: walk_stmt ( self , ex) ;
1494+ }
1495+ }
1496+
1497+ let mut visitor = LetVisitor { result : None , ident_name : seg1. ident . name } ;
1498+ visitor. visit_body ( & body) ;
1499+
1500+ let parent = self . tcx . hir ( ) . get_parent_node ( seg1. hir_id ) ;
1501+ if let Some ( Node :: Expr ( call_expr) ) = self . tcx . hir ( ) . find ( parent) &&
1502+ let Some ( expr) = visitor. result {
1503+ let self_ty = self . node_ty ( expr. hir_id ) ;
1504+ let probe = self . lookup_probe (
1505+ seg2. ident ,
1506+ self_ty,
1507+ call_expr,
1508+ ProbeScope :: TraitsInScope ,
1509+ ) ;
1510+ if probe. is_ok ( ) {
1511+ let sm = self . infcx . tcx . sess . source_map ( ) ;
1512+ diag. span_suggestion_verbose (
1513+ sm. span_extend_while ( seg1. ident . span . shrink_to_hi ( ) , |c| c == ':' ) . unwrap ( ) ,
1514+ "you may have meant to call an instance method" ,
1515+ "." . to_string ( ) ,
1516+ Applicability :: MaybeIncorrect
1517+ ) ;
1518+ }
1519+ }
1520+ diag. emit ( ) ;
1521+ }
1522+
14651523 /// Suggest calling a method on a field i.e. `a.field.bar()` instead of `a.bar()`
14661524 fn suggest_calling_method_on_field (
14671525 & self ,
0 commit comments