2
2
//! found or is otherwise invalid.
3
3
4
4
use crate :: errors;
5
+ use crate :: Expectation ;
5
6
use crate :: FnCtxt ;
6
7
use rustc_ast:: ast:: Mutability ;
7
8
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
@@ -108,6 +109,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
108
109
source : SelfSource < ' tcx > ,
109
110
error : MethodError < ' tcx > ,
110
111
args : Option < ( & ' tcx hir:: Expr < ' tcx > , & ' tcx [ hir:: Expr < ' tcx > ] ) > ,
112
+ expected : Expectation < ' tcx > ,
111
113
) -> Option < DiagnosticBuilder < ' _ , ErrorGuaranteed > > {
112
114
// Avoid suggestions when we don't know what's going on.
113
115
if rcvr_ty. references_error ( ) {
@@ -131,6 +133,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
131
133
args,
132
134
sugg_span,
133
135
& mut no_match_data,
136
+ expected,
134
137
) ;
135
138
}
136
139
@@ -250,6 +253,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
250
253
args : Option < ( & ' tcx hir:: Expr < ' tcx > , & ' tcx [ hir:: Expr < ' tcx > ] ) > ,
251
254
sugg_span : Span ,
252
255
no_match_data : & mut NoMatchData < ' tcx > ,
256
+ expected : Expectation < ' tcx > ,
253
257
) -> Option < DiagnosticBuilder < ' _ , ErrorGuaranteed > > {
254
258
let mode = no_match_data. mode ;
255
259
let tcx = self . tcx ;
@@ -320,7 +324,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
320
324
321
325
if let Mode :: MethodCall = mode && let SelfSource :: MethodCall ( cal) = source {
322
326
self . suggest_await_before_method (
323
- & mut err, item_name, rcvr_ty, cal, span,
327
+ & mut err, item_name, rcvr_ty, cal, span, expected . only_has_type ( self ) ,
324
328
) ;
325
329
}
326
330
if let Some ( span) =
@@ -898,7 +902,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
898
902
// Don't suggest (for example) `expr.field.clone()` if `expr.clone()`
899
903
// can't be called due to `typeof(expr): Clone` not holding.
900
904
if unsatisfied_predicates. is_empty ( ) {
901
- self . suggest_calling_method_on_field ( & mut err, source, span, rcvr_ty, item_name) ;
905
+ self . suggest_calling_method_on_field (
906
+ & mut err,
907
+ source,
908
+ span,
909
+ rcvr_ty,
910
+ item_name,
911
+ expected. only_has_type ( self ) ,
912
+ ) ;
902
913
}
903
914
904
915
self . check_for_inner_self ( & mut err, source, rcvr_ty, item_name) ;
@@ -922,6 +933,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
922
933
& unsatisfied_predicates,
923
934
& static_candidates,
924
935
unsatisfied_bounds,
936
+ expected. only_has_type ( self ) ,
925
937
) ;
926
938
}
927
939
@@ -987,7 +999,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
987
999
}
988
1000
}
989
1001
990
- self . check_for_deref_method ( & mut err, source, rcvr_ty, item_name) ;
1002
+ self . check_for_deref_method ( & mut err, source, rcvr_ty, item_name, expected ) ;
991
1003
return Some ( err) ;
992
1004
}
993
1005
@@ -1377,6 +1389,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1377
1389
let pick = self . probe_for_name (
1378
1390
Mode :: MethodCall ,
1379
1391
item_name,
1392
+ None ,
1380
1393
IsSuggestion ( true ) ,
1381
1394
range_ty,
1382
1395
expr. hir_id ,
@@ -1587,6 +1600,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1587
1600
span : Span ,
1588
1601
actual : Ty < ' tcx > ,
1589
1602
item_name : Ident ,
1603
+ return_type : Option < Ty < ' tcx > > ,
1590
1604
) {
1591
1605
if let SelfSource :: MethodCall ( expr) = source
1592
1606
&& let mod_id = self . tcx . parent_module ( expr. hir_id ) . to_def_id ( )
@@ -1610,10 +1624,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1610
1624
self . check_for_nested_field_satisfying (
1611
1625
span,
1612
1626
& |_, field_ty| {
1613
- self . lookup_probe (
1627
+ self . probe_for_name (
1628
+ Mode :: MethodCall ,
1614
1629
item_name,
1630
+ return_type,
1631
+ IsSuggestion ( true ) ,
1615
1632
field_ty,
1616
- call_expr,
1633
+ call_expr. hir_id ,
1617
1634
ProbeScope :: TraitsInScope ,
1618
1635
)
1619
1636
. map_or ( false , |pick| {
@@ -2010,12 +2027,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2010
2027
self_source : SelfSource < ' tcx > ,
2011
2028
rcvr_ty : Ty < ' tcx > ,
2012
2029
item_name : Ident ,
2030
+ expected : Expectation < ' tcx > ,
2013
2031
) {
2014
2032
let SelfSource :: QPath ( ty) = self_source else { return ; } ;
2015
2033
for ( deref_ty, _) in self . autoderef ( rustc_span:: DUMMY_SP , rcvr_ty) . skip ( 1 ) {
2016
2034
if let Ok ( pick) = self . probe_for_name (
2017
2035
Mode :: Path ,
2018
2036
item_name,
2037
+ expected. only_has_type ( self ) ,
2019
2038
IsSuggestion ( true ) ,
2020
2039
deref_ty,
2021
2040
ty. hir_id ,
@@ -2080,12 +2099,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2080
2099
ty : Ty < ' tcx > ,
2081
2100
call : & hir:: Expr < ' _ > ,
2082
2101
span : Span ,
2102
+ return_type : Option < Ty < ' tcx > > ,
2083
2103
) {
2084
2104
let output_ty = match self . get_impl_future_output_ty ( ty) {
2085
2105
Some ( output_ty) => self . resolve_vars_if_possible ( output_ty) ,
2086
2106
_ => return ,
2087
2107
} ;
2088
- let method_exists = self . method_exists ( item_name, output_ty, call. hir_id , true ) ;
2108
+ let method_exists =
2109
+ self . method_exists ( item_name, output_ty, call. hir_id , true , return_type) ;
2089
2110
debug ! ( "suggest_await_before_method: is_method_exist={}" , method_exists) ;
2090
2111
if method_exists {
2091
2112
err. span_suggestion_verbose (
@@ -2199,6 +2220,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2199
2220
) ] ,
2200
2221
static_candidates : & [ CandidateSource ] ,
2201
2222
unsatisfied_bounds : bool ,
2223
+ return_type : Option < Ty < ' tcx > > ,
2202
2224
) {
2203
2225
let mut alt_rcvr_sugg = false ;
2204
2226
if let ( SelfSource :: MethodCall ( rcvr) , false ) = ( source, unsatisfied_bounds) {
@@ -2221,7 +2243,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2221
2243
( self . tcx . mk_mut_ref ( self . tcx . lifetimes . re_erased , rcvr_ty) , "&mut " ) ,
2222
2244
( self . tcx . mk_imm_ref ( self . tcx . lifetimes . re_erased , rcvr_ty) , "&" ) ,
2223
2245
] {
2224
- match self . lookup_probe ( item_name, * rcvr_ty, rcvr, ProbeScope :: AllTraits ) {
2246
+ match self . probe_for_name (
2247
+ Mode :: MethodCall ,
2248
+ item_name,
2249
+ return_type,
2250
+ IsSuggestion ( true ) ,
2251
+ * rcvr_ty,
2252
+ rcvr. hir_id ,
2253
+ ProbeScope :: AllTraits ,
2254
+ ) {
2225
2255
Ok ( pick) => {
2226
2256
// If the method is defined for the receiver we have, it likely wasn't `use`d.
2227
2257
// We point at the method, but we just skip the rest of the check for arbitrary
@@ -2254,10 +2284,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2254
2284
( self . tcx . mk_diagnostic_item ( * rcvr_ty, sym:: Rc ) , "Rc::new" ) ,
2255
2285
] {
2256
2286
if let Some ( new_rcvr_t) = * rcvr_ty
2257
- && let Ok ( pick) = self . lookup_probe (
2287
+ && let Ok ( pick) = self . probe_for_name (
2288
+ Mode :: MethodCall ,
2258
2289
item_name,
2290
+ return_type,
2291
+ IsSuggestion ( true ) ,
2259
2292
new_rcvr_t,
2260
- rcvr,
2293
+ rcvr. hir_id ,
2261
2294
ProbeScope :: AllTraits ,
2262
2295
)
2263
2296
{
0 commit comments