@@ -203,60 +203,81 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
203
203
204
204
if span. edition ( ) < Edition :: Edition2021 {
205
205
if let sym:: try_into = segment. ident . name {
206
- if let probe:: PickKind :: TraitPick = pick. kind {
207
- if !matches ! ( self . tcx. crate_name( pick. item. def_id. krate) , sym:: std | sym:: core)
208
- {
209
- self . tcx . struct_span_lint_hir (
210
- FUTURE_PRELUDE_COLLISION ,
211
- call_expr. hir_id ,
212
- call_expr. span ,
213
- |lint| {
214
- let sp = call_expr. span ;
215
- let trait_name =
216
- self . tcx . def_path_str ( pick. item . container . assert_trait ( ) ) ;
217
-
218
- let mut lint = lint. build ( & format ! (
219
- "trait method `{}` will become ambiguous in Rust 2021" ,
220
- segment. ident. name
221
- ) ) ;
222
-
223
- if let Ok ( self_expr) =
224
- self . sess ( ) . source_map ( ) . span_to_snippet ( self_expr. span )
225
- {
226
- let derefs = "*" . repeat ( pick. autoderefs ) ;
227
- let self_adjusted = match pick. autoref_or_ptr_adjustment {
228
- Some ( probe:: AutorefOrPtrAdjustment :: Autoref {
229
- mutbl : Mutability :: Mut , ..
230
- } ) => format ! ( "&mut {}{}" , derefs, self_expr) ,
231
- Some ( probe:: AutorefOrPtrAdjustment :: Autoref {
232
- mutbl : Mutability :: Not , ..
233
- } ) => format ! ( "&{}{}" , derefs, self_expr) ,
234
- Some ( probe:: AutorefOrPtrAdjustment :: ToConstPtr ) | None
235
- => format ! ( "{}{}" , derefs, self_expr) ,
206
+ if !matches ! ( self . tcx. crate_name( pick. item. def_id. krate) , sym:: std | sym:: core) {
207
+ self . tcx . struct_span_lint_hir (
208
+ FUTURE_PRELUDE_COLLISION ,
209
+ call_expr. hir_id ,
210
+ call_expr. span ,
211
+ |lint| {
212
+ let sp = call_expr. span ;
213
+ let type_name = self . tcx . def_path_str ( pick. item . container . id ( ) ) ;
214
+ let type_generics = self . tcx . generics_of ( pick. item . container . id ( ) ) ;
215
+ let parameter_count =
216
+ type_generics. count ( ) - ( type_generics. has_self as usize ) ;
217
+ let trait_name = if parameter_count == 0 {
218
+ type_name
219
+ } else {
220
+ format ! (
221
+ "{}<{}>" ,
222
+ type_name,
223
+ std:: iter:: repeat( "_" )
224
+ . take( parameter_count)
225
+ . collect:: <Vec <_>>( )
226
+ . join( ", " )
227
+ )
228
+ } ;
229
+
230
+ let mut lint = lint. build ( & format ! (
231
+ "trait method `{}` will become ambiguous in Rust 2021" ,
232
+ segment. ident. name
233
+ ) ) ;
234
+
235
+ if let Ok ( self_expr) =
236
+ self . sess ( ) . source_map ( ) . span_to_snippet ( self_expr. span )
237
+ {
238
+ let derefs = "*" . repeat ( pick. autoderefs ) ;
239
+
240
+ let autoref = match pick. autoref_or_ptr_adjustment {
241
+ Some ( probe:: AutorefOrPtrAdjustment :: Autoref {
242
+ mutbl : Mutability :: Mut ,
243
+ ..
244
+ } ) => "&mut " ,
245
+ Some ( probe:: AutorefOrPtrAdjustment :: Autoref {
246
+ mutbl : Mutability :: Not ,
247
+ ..
248
+ } ) => "&" ,
249
+ Some ( probe:: AutorefOrPtrAdjustment :: ToConstPtr ) | None => "" ,
250
+ } ;
251
+ let self_adjusted =
252
+ if let Some ( probe:: AutorefOrPtrAdjustment :: ToConstPtr ) =
253
+ pick. autoref_or_ptr_adjustment
254
+ {
255
+ format ! ( "{}{} as *const _" , derefs, self_expr)
256
+ } else {
257
+ format ! ( "{}{}{}" , autoref, derefs, self_expr)
236
258
} ;
237
- lint. span_suggestion (
238
- sp,
239
- "disambiguate the associated function" ,
240
- format ! (
241
- "{}::{}({})" ,
242
- trait_name, segment. ident. name, self_adjusted,
243
- ) ,
244
- Applicability :: MachineApplicable ,
245
- ) ;
246
- } else {
247
- lint. span_help (
248
- sp,
249
- & format ! (
250
- "disambiguate the associated function with `{}::{}(...)`" ,
251
- trait_name, segment. ident,
252
- ) ,
253
- ) ;
254
- }
255
-
256
- lint. emit ( ) ;
257
- } ,
258
- ) ;
259
- }
259
+ lint. span_suggestion (
260
+ sp,
261
+ "disambiguate the associated function" ,
262
+ format ! (
263
+ "{}::{}({})" ,
264
+ trait_name, segment. ident. name, self_adjusted,
265
+ ) ,
266
+ Applicability :: MachineApplicable ,
267
+ ) ;
268
+ } else {
269
+ lint. span_help (
270
+ sp,
271
+ & format ! (
272
+ "disambiguate the associated function with `{}::{}(...)`" ,
273
+ trait_name, segment. ident,
274
+ ) ,
275
+ ) ;
276
+ }
277
+
278
+ lint. emit ( ) ;
279
+ } ,
280
+ ) ;
260
281
}
261
282
}
262
283
}
@@ -541,38 +562,42 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
541
562
542
563
if span. edition ( ) < Edition :: Edition2021 {
543
564
if let sym:: try_into | sym:: try_from | sym:: from_iter = method_name. name {
544
- if let probe:: PickKind :: TraitPick = pick. kind {
545
- if !matches ! ( tcx. crate_name( pick. item. def_id. krate) , sym:: std | sym:: core) {
546
- tcx. struct_span_lint_hir ( FUTURE_PRELUDE_COLLISION , expr_id, span, |lint| {
547
- let trait_def_id = pick. item . container . assert_trait ( ) ;
548
- let trait_generics = tcx. generics_of ( trait_def_id) ;
549
- let parameter_count = trait_generics. count ( ) - ( trait_generics. has_self as usize ) ;
550
-
551
- let trait_name = if parameter_count == 0 {
552
- tcx. def_path_str ( trait_def_id)
553
- } else {
554
- format ! (
555
- "{}<{}>" ,
556
- tcx. def_path_str( trait_def_id) ,
557
- std:: iter:: repeat( "_" ) . take( parameter_count) . collect:: <Vec <_>>( ) . join( ", " )
558
- )
559
- } ;
560
-
561
- let mut lint = lint. build ( & format ! (
562
- "trait-associated function `{}` will become ambiguous in Rust 2021" ,
563
- method_name. name
564
- ) ) ;
565
-
566
- lint. span_suggestion (
567
- span,
568
- "disambiguate the associated function" ,
569
- format ! ( "<{} as {}>::{}" , self_ty, trait_name, method_name. name, ) ,
570
- Applicability :: MachineApplicable ,
571
- ) ;
565
+ if !matches ! ( tcx. crate_name( pick. item. def_id. krate) , sym:: std | sym:: core) {
566
+ tcx. struct_span_lint_hir ( FUTURE_PRELUDE_COLLISION , expr_id, span, |lint| {
567
+ // "type" refers to either a type or, more likely, a trait from which
568
+ // the associated function or method is from.
569
+ let type_name = tcx. def_path_str ( pick. item . container . id ( ) ) ;
570
+ let type_generics = tcx. generics_of ( pick. item . container . id ( ) ) ;
571
+
572
+ let parameter_count =
573
+ type_generics. count ( ) - ( type_generics. has_self as usize ) ;
574
+ let trait_name = if parameter_count == 0 {
575
+ type_name
576
+ } else {
577
+ format ! (
578
+ "{}<{}>" ,
579
+ type_name,
580
+ std:: iter:: repeat( "_" )
581
+ . take( parameter_count)
582
+ . collect:: <Vec <_>>( )
583
+ . join( ", " )
584
+ )
585
+ } ;
586
+
587
+ let mut lint = lint. build ( & format ! (
588
+ "trait-associated function `{}` will become ambiguous in Rust 2021" ,
589
+ method_name. name
590
+ ) ) ;
591
+
592
+ lint. span_suggestion (
593
+ span,
594
+ "disambiguate the associated function" ,
595
+ format ! ( "<{} as {}>::{}" , self_ty, trait_name, method_name. name, ) ,
596
+ Applicability :: MachineApplicable ,
597
+ ) ;
572
598
573
- lint. emit ( ) ;
574
- } ) ;
575
- }
599
+ lint. emit ( ) ;
600
+ } ) ;
576
601
}
577
602
}
578
603
}
0 commit comments