@@ -1150,7 +1150,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1150
1150
}
1151
1151
}
1152
1152
1153
- let label_span_not_found = |err : & mut Diag < ' _ > | {
1153
+ let mut find_candidate_for_method = false ;
1154
+
1155
+ let mut label_span_not_found = |err : & mut Diag < ' _ > | {
1154
1156
if unsatisfied_predicates. is_empty ( ) {
1155
1157
err. span_label ( span, format ! ( "{item_kind} not found in `{ty_str}`" ) ) ;
1156
1158
let is_string_or_ref_str = match rcvr_ty. kind ( ) {
@@ -1226,6 +1228,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1226
1228
err. note ( format ! (
1227
1229
"the {item_kind} was found for\n {type_candidates}{additional_types}"
1228
1230
) ) ;
1231
+ find_candidate_for_method = mode == Mode :: MethodCall ;
1229
1232
}
1230
1233
}
1231
1234
} else {
@@ -1378,15 +1381,36 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1378
1381
) ;
1379
1382
}
1380
1383
}
1381
- // If an appropriate error source is not found, check method chain for possible candiates
1382
- if unsatisfied_predicates. is_empty ( )
1383
- && let Mode :: MethodCall = mode
1384
+
1385
+ if !find_candidate_for_method {
1386
+ self . lookup_segments_chain_for_no_match_method (
1387
+ & mut err,
1388
+ item_name,
1389
+ item_kind,
1390
+ source,
1391
+ no_match_data,
1392
+ ) ;
1393
+ }
1394
+
1395
+ self . note_derefed_ty_has_method ( & mut err, source, rcvr_ty, item_name, expected) ;
1396
+ Some ( err)
1397
+ }
1398
+
1399
+ /// If an appropriate error source is not found, check method chain for possible candidates
1400
+ fn lookup_segments_chain_for_no_match_method (
1401
+ & self ,
1402
+ err : & mut Diag < ' _ > ,
1403
+ item_name : Ident ,
1404
+ item_kind : & str ,
1405
+ source : SelfSource < ' tcx > ,
1406
+ no_match_data : & NoMatchData < ' tcx > ,
1407
+ ) {
1408
+ if no_match_data. unsatisfied_predicates . is_empty ( )
1409
+ && let Mode :: MethodCall = no_match_data. mode
1384
1410
&& let SelfSource :: MethodCall ( mut source_expr) = source
1385
1411
{
1386
1412
let mut stack_methods = vec ! [ ] ;
1387
- while let hir:: ExprKind :: MethodCall ( _path_segment, rcvr_expr, _args, method_span) =
1388
- source_expr. kind
1389
- {
1413
+ while let hir:: ExprKind :: MethodCall ( _, rcvr_expr, _, method_span) = source_expr. kind {
1390
1414
// Pop the matching receiver, to align on it's notional span
1391
1415
if let Some ( prev_match) = stack_methods. pop ( ) {
1392
1416
err. span_label (
@@ -1401,7 +1425,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1401
1425
. unwrap_or ( Ty :: new_misc_error ( self . tcx ) ) ,
1402
1426
) ;
1403
1427
1404
- for _matched_method in self . probe_for_name_many (
1428
+ for _ in self . probe_for_name_many (
1405
1429
Mode :: MethodCall ,
1406
1430
item_name,
1407
1431
None ,
@@ -1411,6 +1435,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1411
1435
ProbeScope :: TraitsInScope ,
1412
1436
) {
1413
1437
// found a match, push to stack
1438
+ // FIXME: should check all impl candidates, see issue#124946
1414
1439
stack_methods. push ( rcvr_ty) ;
1415
1440
}
1416
1441
source_expr = rcvr_expr;
@@ -1423,8 +1448,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1423
1448
) ;
1424
1449
}
1425
1450
}
1426
- self . note_derefed_ty_has_method ( & mut err, source, rcvr_ty, item_name, expected) ;
1427
- Some ( err)
1428
1451
}
1429
1452
1430
1453
fn find_likely_intended_associated_item (
0 commit comments