@@ -16,11 +16,11 @@ use rustc_data_structures::unord::UnordSet;
16
16
use rustc_errors:: {
17
17
codes:: * , pluralize, struct_span_code_err, Applicability , Diag , MultiSpan , StashKey ,
18
18
} ;
19
- use rustc_hir as hir;
20
19
use rustc_hir:: def:: DefKind ;
21
20
use rustc_hir:: def_id:: DefId ;
22
21
use rustc_hir:: lang_items:: LangItem ;
23
22
use rustc_hir:: PathSegment ;
23
+ use rustc_hir:: { self as hir, HirId } ;
24
24
use rustc_hir:: { ExprKind , Node , QPath } ;
25
25
use rustc_infer:: infer:: { self , RegionVariableOrigin } ;
26
26
use rustc_middle:: bug;
@@ -187,37 +187,56 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
187
187
#[ instrument( level = "debug" , skip( self ) ) ]
188
188
pub fn report_method_error (
189
189
& self ,
190
- span : Span ,
191
- rcvr_opt : Option < & ' tcx hir:: Expr < ' tcx > > ,
190
+ call_id : HirId ,
192
191
rcvr_ty : Ty < ' tcx > ,
193
- item_name : Ident ,
194
- expr_id : hir:: HirId ,
195
- source : SelfSource < ' tcx > ,
196
192
error : MethodError < ' tcx > ,
197
- args : Option < & ' tcx [ hir:: Expr < ' tcx > ] > ,
198
193
expected : Expectation < ' tcx > ,
199
194
trait_missing_method : bool ,
200
195
) -> Option < Diag < ' _ > > {
196
+ let ( span, sugg_span, source, item_name, args) = match self . tcx . hir_node ( call_id) {
197
+ hir:: Node :: Expr ( & hir:: Expr {
198
+ kind : hir:: ExprKind :: MethodCall ( segment, rcvr, args, _) ,
199
+ span,
200
+ ..
201
+ } ) => {
202
+ ( segment. ident . span , span, SelfSource :: MethodCall ( rcvr) , segment. ident , Some ( args) )
203
+ }
204
+ hir:: Node :: Expr ( & hir:: Expr {
205
+ kind : hir:: ExprKind :: Path ( QPath :: TypeRelative ( rcvr, segment) ) ,
206
+ span,
207
+ ..
208
+ } )
209
+ | hir:: Node :: Pat ( & hir:: Pat {
210
+ kind :
211
+ hir:: PatKind :: Path ( QPath :: TypeRelative ( rcvr, segment) )
212
+ | hir:: PatKind :: Struct ( QPath :: TypeRelative ( rcvr, segment) , ..)
213
+ | hir:: PatKind :: TupleStruct ( QPath :: TypeRelative ( rcvr, segment) , ..) ,
214
+ span,
215
+ ..
216
+ } ) => {
217
+ let args = match self . tcx . parent_hir_node ( call_id) {
218
+ hir:: Node :: Expr ( & hir:: Expr {
219
+ kind : hir:: ExprKind :: Call ( callee, args) , ..
220
+ } ) if callee. hir_id == call_id => Some ( args) ,
221
+ _ => None ,
222
+ } ;
223
+ ( segment. ident . span , span, SelfSource :: QPath ( rcvr) , segment. ident , args)
224
+ }
225
+ node => unreachable ! ( "{node:?}" ) ,
226
+ } ;
227
+
201
228
// Avoid suggestions when we don't know what's going on.
202
229
if rcvr_ty. references_error ( ) {
203
230
return None ;
204
231
}
205
232
206
- let sugg_span = if let SelfSource :: MethodCall ( expr) = source {
207
- // Given `foo.bar(baz)`, `expr` is `bar`, but we want to point to the whole thing.
208
- self . tcx . hir ( ) . expect_expr ( self . tcx . parent_hir_id ( expr. hir_id ) ) . span
209
- } else {
210
- span
211
- } ;
212
-
213
233
match error {
214
234
MethodError :: NoMatch ( mut no_match_data) => {
215
235
return self . report_no_match_method_error (
216
236
span,
217
- rcvr_opt,
218
237
rcvr_ty,
219
238
item_name,
220
- expr_id ,
239
+ call_id ,
221
240
source,
222
241
args,
223
242
sugg_span,
@@ -362,7 +381,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
362
381
363
382
pub fn suggest_use_shadowed_binding_with_method (
364
383
& self ,
365
- rcvr_opt : Option < & ' tcx hir :: Expr < ' tcx > > ,
384
+ self_source : SelfSource < ' tcx > ,
366
385
method_name : Ident ,
367
386
ty_str_reported : & str ,
368
387
err : & mut Diag < ' _ > ,
@@ -502,7 +521,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
502
521
}
503
522
}
504
523
505
- if let Some ( rcvr) = rcvr_opt
524
+ if let SelfSource :: MethodCall ( rcvr) = self_source
506
525
&& let hir:: ExprKind :: Path ( QPath :: Resolved ( _, path) ) = rcvr. kind
507
526
&& let hir:: def:: Res :: Local ( recv_id) = path. res
508
527
&& let Some ( segment) = path. segments . first ( )
@@ -548,7 +567,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
548
567
pub fn report_no_match_method_error (
549
568
& self ,
550
569
mut span : Span ,
551
- rcvr_opt : Option < & ' tcx hir:: Expr < ' tcx > > ,
552
570
rcvr_ty : Ty < ' tcx > ,
553
571
item_name : Ident ,
554
572
expr_id : hir:: HirId ,
@@ -658,7 +676,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
658
676
659
677
if is_method {
660
678
self . suggest_use_shadowed_binding_with_method (
661
- rcvr_opt ,
679
+ source ,
662
680
item_name,
663
681
& ty_str_reported,
664
682
& mut err,
0 commit comments