@@ -29,11 +29,11 @@ use rustc_trait_selection::traits::{
2929 FulfillmentError , Obligation , ObligationCause , ObligationCauseCode , OnUnimplementedNote ,
3030} ;
3131
32- use std:: cmp:: Ordering ;
33- use std:: iter;
34-
3532use super :: probe:: { AutorefOrPtrAdjustment , IsSuggestion , Mode , ProbeScope } ;
3633use super :: { CandidateSource , MethodError , NoMatchData } ;
34+ use rustc_hir:: intravisit:: Visitor ;
35+ use std:: cmp:: Ordering ;
36+ use std:: iter;
3737
3838impl < ' a , ' tcx > FnCtxt < ' a , ' tcx > {
3939 fn is_fn_ty ( & self , ty : Ty < ' tcx > , span : Span ) -> bool {
@@ -1407,6 +1407,63 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14071407 false
14081408 }
14091409
1410+ pub ( crate ) fn suggest_instance_call (
1411+ & self ,
1412+ seg1 : & hir:: PathSegment < ' _ > ,
1413+ seg2 : & hir:: PathSegment < ' _ > ,
1414+ local_span : Span ,
1415+ ) -> bool {
1416+ let map = self . infcx . tcx . hir ( ) ;
1417+ let body_id = map. body_owned_by ( seg1. hir_id . owner . def_id ) ;
1418+ let body = map. body ( body_id) ;
1419+
1420+ struct LetVisitor < ' a > {
1421+ local_span : Span ,
1422+ result : Option < & ' a Ty < ' a > > ,
1423+ }
1424+
1425+ impl < ' v > Visitor < ' v > for LetVisitor < ' _ > {
1426+ fn visit_stmt ( & mut self , ex : & ' v hir:: Stmt < ' v > ) {
1427+ if self . result . is_some ( ) {
1428+ return ;
1429+ }
1430+ if let hir:: StmtKind :: Local ( hir:: Local {
1431+ span, ty, init : None , ..
1432+ } ) = & ex. kind && span. contains ( self . local_span ) {
1433+ self . result = ty;
1434+ }
1435+ hir:: intravisit:: walk_stmt ( self , ex) ;
1436+ }
1437+ }
1438+
1439+ let mut visitor = LetVisitor { local_span, result : None } ;
1440+ visitor. visit_body ( & body) ;
1441+
1442+ let parent = self . tcx . hir ( ) . get_parent_node ( seg1. hir_id ) ;
1443+ let parent_expr = self . tcx . hir ( ) . find ( parent) ;
1444+ debug ! ( "yukang parent_expr: {:?}" , parent_expr) ;
1445+ let node = self . tcx . hir ( ) . get_parent_node ( seg1. hir_id ) ;
1446+ let ty = self . typeck_results . borrow ( ) . node_type_opt ( node) ;
1447+
1448+ debug ! ( "yukang suggest_instance_call ty: {:?}" , ty) ;
1449+ if let Some ( Node :: Expr ( call_expr) ) = self . tcx . hir ( ) . find ( parent) &&
1450+ let Some ( self_ty) = ty {
1451+ debug ! ( "yukang trying to prob method" ) ;
1452+ let probe = self . lookup_probe (
1453+ seg1. ident . span ,
1454+ seg2. ident ,
1455+ self_ty,
1456+ call_expr,
1457+ ProbeScope :: TraitsInScope ,
1458+ ) ;
1459+
1460+ if let Ok ( _pick) = probe {
1461+ return true ;
1462+ }
1463+ }
1464+ return false ;
1465+ }
1466+
14101467 fn check_for_field_method (
14111468 & self ,
14121469 err : & mut Diagnostic ,
0 commit comments