@@ -34,7 +34,7 @@ use rustc_middle::ty::{self, GenericArgKind, Ty, TyCtxt, TypeVisitableExt};
34
34
use rustc_span:: def_id:: DefIdSet ;
35
35
use rustc_span:: symbol:: { kw, sym, Ident } ;
36
36
use rustc_span:: Symbol ;
37
- use rustc_span:: { edit_distance, source_map , ExpnKind , FileName , MacroKind , Span } ;
37
+ use rustc_span:: { edit_distance, ExpnKind , FileName , MacroKind , Span } ;
38
38
use rustc_trait_selection:: infer:: InferCtxtExt ;
39
39
use rustc_trait_selection:: traits:: error_reporting:: on_unimplemented:: OnUnimplementedNote ;
40
40
use rustc_trait_selection:: traits:: error_reporting:: on_unimplemented:: TypeErrCtxtExt as _;
@@ -1320,39 +1320,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1320
1320
}
1321
1321
if let Some ( sugg_span) = sugg_span
1322
1322
&& let Some ( trait_ref) = self . tcx . impl_trait_ref ( impl_did)
1323
- {
1324
- let path = self . tcx . def_path_str ( trait_ref. skip_binder ( ) . def_id ) ;
1325
-
1326
- let ty = match item. kind {
1327
- ty:: AssocKind :: Const | ty:: AssocKind :: Type => impl_ty,
1328
- ty:: AssocKind :: Fn => self
1329
- . tcx
1330
- . fn_sig ( item. def_id )
1331
- . instantiate_identity ( )
1332
- . inputs ( )
1333
- . skip_binder ( )
1334
- . get ( 0 )
1335
- . filter ( |ty| ty. is_ref ( ) && !rcvr_ty. is_ref ( ) )
1336
- . copied ( )
1337
- . unwrap_or ( rcvr_ty) ,
1338
- } ;
1339
- if let Some ( sugg) = print_disambiguation_help (
1340
- item_name,
1341
- args,
1342
- self_source,
1323
+ && let Some ( sugg) = print_disambiguation_help (
1324
+ self . tcx ,
1343
1325
err,
1344
- path ,
1345
- ty ,
1346
- Some ( impl_ty ) ,
1347
- item . kind ,
1348
- self . tcx . def_kind_descr ( item . kind . as_def_kind ( ) , item . def_id ) ,
1349
- sugg_span ,
1326
+ self_source ,
1327
+ args ,
1328
+ trait_ref . instantiate (
1329
+ self . tcx ,
1330
+ self . fresh_args_for_item ( sugg_span , impl_did )
1331
+ ) . with_self_ty ( self . tcx , rcvr_ty ) ,
1350
1332
idx,
1351
- self . tcx . sess . source_map ( ) ,
1352
- item. fn_has_self_parameter ,
1353
- ) {
1354
- suggs . push ( sugg ) ;
1355
- }
1333
+ sugg_span ,
1334
+ item,
1335
+ )
1336
+ {
1337
+ suggs . push ( sugg ) ;
1356
1338
}
1357
1339
}
1358
1340
CandidateSource :: Trait ( trait_did) => {
@@ -1374,25 +1356,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1374
1356
err. span_note ( item_span, msg) ;
1375
1357
None
1376
1358
} ;
1377
- if let Some ( sugg_span) = sugg_span {
1378
- let path = self . tcx . def_path_str ( trait_did) ;
1379
- if let Some ( sugg) = print_disambiguation_help (
1380
- item_name,
1381
- args,
1382
- self_source,
1359
+ if let Some ( sugg_span) = sugg_span
1360
+ && let Some ( sugg) = print_disambiguation_help (
1361
+ self . tcx ,
1383
1362
err,
1384
- path,
1385
- rcvr_ty,
1386
- None ,
1387
- item. kind ,
1388
- self . tcx . def_kind_descr ( item. kind . as_def_kind ( ) , item. def_id ) ,
1389
- sugg_span,
1363
+ self_source,
1364
+ args,
1365
+ ty:: TraitRef :: new (
1366
+ self . tcx ,
1367
+ trait_did,
1368
+ self . fresh_args_for_item ( sugg_span, trait_did)
1369
+ ) . with_self_ty ( self . tcx , rcvr_ty) ,
1390
1370
idx,
1391
- self . tcx . sess . source_map ( ) ,
1392
- item. fn_has_self_parameter ,
1393
- ) {
1394
- suggs . push ( sugg ) ;
1395
- }
1371
+ sugg_span ,
1372
+ item,
1373
+ )
1374
+ {
1375
+ suggs . push ( sugg ) ;
1396
1376
}
1397
1377
}
1398
1378
}
@@ -3263,59 +3243,59 @@ pub fn all_traits(tcx: TyCtxt<'_>) -> Vec<TraitInfo> {
3263
3243
}
3264
3244
3265
3245
fn print_disambiguation_help < ' tcx > (
3266
- item_name : Ident ,
3267
- args : Option < & ' tcx [ hir:: Expr < ' tcx > ] > ,
3268
- source : SelfSource < ' tcx > ,
3246
+ tcx : TyCtxt < ' tcx > ,
3269
3247
err : & mut Diagnostic ,
3270
- trait_name : String ,
3271
- rcvr_ty : Ty < ' _ > ,
3272
- impl_self_ty : Option < Ty < ' _ > > ,
3273
- kind : ty:: AssocKind ,
3274
- def_kind_descr : & ' static str ,
3248
+ source : SelfSource < ' tcx > ,
3249
+ args : Option < & ' tcx [ hir:: Expr < ' tcx > ] > ,
3250
+ trait_ref : ty:: TraitRef < ' tcx > ,
3251
+ candidate_idx : Option < usize > ,
3275
3252
span : Span ,
3276
- candidate : Option < usize > ,
3277
- source_map : & source_map:: SourceMap ,
3278
- fn_has_self_parameter : bool ,
3253
+ item : ty:: AssocItem ,
3279
3254
) -> Option < String > {
3255
+ let trait_ref = if item. fn_has_self_parameter {
3256
+ trait_ref. print_only_trait_name ( ) . to_string ( )
3257
+ } else {
3258
+ format ! ( "<{} as {}>" , trait_ref. args[ 0 ] , trait_ref. print_only_trait_name( ) )
3259
+ } ;
3280
3260
Some (
3281
- if matches ! ( kind, ty:: AssocKind :: Fn )
3261
+ if matches ! ( item . kind, ty:: AssocKind :: Fn )
3282
3262
&& let SelfSource :: MethodCall ( receiver) = source
3283
3263
&& let Some ( args) = args
3284
3264
{
3265
+ let def_kind_descr = tcx. def_kind_descr ( item. kind . as_def_kind ( ) , item. def_id ) ;
3266
+ let item_name = item. ident ( tcx) ;
3267
+ let rcvr_ref = tcx. fn_sig ( item. def_id ) . skip_binder ( ) . skip_binder ( ) . inputs ( ) [ 0 ]
3268
+ . ref_mutability ( )
3269
+ . map_or ( "" , |mutbl| mutbl. ref_prefix_str ( ) ) ;
3285
3270
let args = format ! (
3286
3271
"({}{})" ,
3287
- rcvr_ty . ref_mutability ( ) . map_or ( "" , |mutbl| mutbl . ref_prefix_str ( ) ) ,
3272
+ rcvr_ref ,
3288
3273
std:: iter:: once( receiver)
3289
3274
. chain( args. iter( ) )
3290
- . map( |arg| source_map
3275
+ . map( |arg| tcx
3276
+ . sess
3277
+ . source_map( )
3291
3278
. span_to_snippet( arg. span)
3292
3279
. unwrap_or_else( |_| { "_" . to_owned( ) } ) )
3293
3280
. collect:: <Vec <_>>( )
3294
3281
. join( ", " ) ,
3295
3282
) ;
3296
- let trait_name = if !fn_has_self_parameter && let Some ( impl_self_ty) = impl_self_ty {
3297
- format ! ( "<{impl_self_ty} as {trait_name}>" )
3298
- } else {
3299
- trait_name
3300
- } ;
3301
3283
err. span_suggestion_verbose (
3302
3284
span,
3303
3285
format ! (
3304
3286
"disambiguate the {def_kind_descr} for {}" ,
3305
- if let Some ( candidate) = candidate {
3287
+ if let Some ( candidate) = candidate_idx {
3306
3288
format!( "candidate #{candidate}" )
3307
3289
} else {
3308
3290
"the candidate" . to_string( )
3309
3291
} ,
3310
3292
) ,
3311
- format ! ( "{trait_name }::{item_name}{args}" ) ,
3293
+ format ! ( "{trait_ref }::{item_name}{args}" ) ,
3312
3294
Applicability :: HasPlaceholders ,
3313
3295
) ;
3314
3296
return None ;
3315
- } else if let Some ( impl_self_ty) = impl_self_ty {
3316
- format ! ( "<{impl_self_ty} as {trait_name}>::" )
3317
3297
} else {
3318
- format ! ( "{trait_name }::" )
3298
+ format ! ( "{trait_ref }::" )
3319
3299
} ,
3320
3300
)
3321
3301
}
0 commit comments