@@ -306,6 +306,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
306
306
lang_item_for_op ( self . tcx , Op :: Binary ( op, is_assign) , op. span ) ;
307
307
let missing_trait = trait_def_id
308
308
. map ( |def_id| with_no_trimmed_paths ! ( self . tcx. def_path_str( def_id) ) ) ;
309
+ let mut path = None ;
310
+ let lhs_ty_str = self . tcx . short_string ( lhs_ty, & mut path) ;
311
+ let rhs_ty_str = self . tcx . short_string ( rhs_ty, & mut path) ;
309
312
let ( mut err, output_def_id) = match is_assign {
310
313
IsAssign :: Yes => {
311
314
let mut err = struct_span_code_err ! (
@@ -314,53 +317,54 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
314
317
E0368 ,
315
318
"binary assignment operation `{}=` cannot be applied to type `{}`" ,
316
319
op. node. as_str( ) ,
317
- lhs_ty ,
320
+ lhs_ty_str ,
318
321
) ;
319
322
err. span_label (
320
323
lhs_expr. span ,
321
- format ! ( "cannot use `{}=` on type `{}`" , op. node. as_str( ) , lhs_ty ) ,
324
+ format ! ( "cannot use `{}=` on type `{}`" , op. node. as_str( ) , lhs_ty_str ) ,
322
325
) ;
323
326
self . note_unmet_impls_on_type ( & mut err, errors, false ) ;
324
327
( err, None )
325
328
}
326
329
IsAssign :: No => {
327
330
let message = match op. node {
328
331
hir:: BinOpKind :: Add => {
329
- format ! ( "cannot add `{rhs_ty }` to `{lhs_ty }`" )
332
+ format ! ( "cannot add `{rhs_ty_str }` to `{lhs_ty_str }`" )
330
333
}
331
334
hir:: BinOpKind :: Sub => {
332
- format ! ( "cannot subtract `{rhs_ty }` from `{lhs_ty }`" )
335
+ format ! ( "cannot subtract `{rhs_ty_str }` from `{lhs_ty_str }`" )
333
336
}
334
337
hir:: BinOpKind :: Mul => {
335
- format ! ( "cannot multiply `{lhs_ty }` by `{rhs_ty }`" )
338
+ format ! ( "cannot multiply `{lhs_ty_str }` by `{rhs_ty_str }`" )
336
339
}
337
340
hir:: BinOpKind :: Div => {
338
- format ! ( "cannot divide `{lhs_ty }` by `{rhs_ty }`" )
341
+ format ! ( "cannot divide `{lhs_ty_str }` by `{rhs_ty_str }`" )
339
342
}
340
343
hir:: BinOpKind :: Rem => {
341
344
format ! (
342
- "cannot calculate the remainder of `{lhs_ty}` divided by `{rhs_ty}`"
345
+ "cannot calculate the remainder of `{lhs_ty_str}` divided by \
346
+ `{rhs_ty_str}`"
343
347
)
344
348
}
345
349
hir:: BinOpKind :: BitAnd => {
346
- format ! ( "no implementation for `{lhs_ty } & {rhs_ty }`" )
350
+ format ! ( "no implementation for `{lhs_ty_str } & {rhs_ty_str }`" )
347
351
}
348
352
hir:: BinOpKind :: BitXor => {
349
- format ! ( "no implementation for `{lhs_ty } ^ {rhs_ty }`" )
353
+ format ! ( "no implementation for `{lhs_ty_str } ^ {rhs_ty_str }`" )
350
354
}
351
355
hir:: BinOpKind :: BitOr => {
352
- format ! ( "no implementation for `{lhs_ty } | {rhs_ty }`" )
356
+ format ! ( "no implementation for `{lhs_ty_str } | {rhs_ty_str }`" )
353
357
}
354
358
hir:: BinOpKind :: Shl => {
355
- format ! ( "no implementation for `{lhs_ty } << {rhs_ty }`" )
359
+ format ! ( "no implementation for `{lhs_ty_str } << {rhs_ty_str }`" )
356
360
}
357
361
hir:: BinOpKind :: Shr => {
358
- format ! ( "no implementation for `{lhs_ty } >> {rhs_ty }`" )
362
+ format ! ( "no implementation for `{lhs_ty_str } >> {rhs_ty_str }`" )
359
363
}
360
364
_ => format ! (
361
365
"binary operation `{}` cannot be applied to type `{}`" ,
362
366
op. node. as_str( ) ,
363
- lhs_ty
367
+ lhs_ty_str ,
364
368
) ,
365
369
} ;
366
370
let output_def_id = trait_def_id. and_then ( |def_id| {
@@ -375,14 +379,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
375
379
let mut err =
376
380
struct_span_code_err ! ( self . dcx( ) , op. span, E0369 , "{message}" ) ;
377
381
if !lhs_expr. span . eq ( & rhs_expr. span ) {
378
- err. span_label ( lhs_expr. span , lhs_ty . to_string ( ) ) ;
379
- err. span_label ( rhs_expr. span , rhs_ty . to_string ( ) ) ;
382
+ err. span_label ( lhs_expr. span , lhs_ty_str . clone ( ) ) ;
383
+ err. span_label ( rhs_expr. span , rhs_ty_str ) ;
380
384
}
381
385
let suggest_derive = self . can_eq ( self . param_env , lhs_ty, rhs_ty) ;
382
386
self . note_unmet_impls_on_type ( & mut err, errors, suggest_derive) ;
383
387
( err, output_def_id)
384
388
}
385
389
} ;
390
+ * err. long_ty_path ( ) = path;
386
391
387
392
// Try to suggest a semicolon if it's `A \n *B` where `B` is a place expr
388
393
let maybe_missing_semi = self . check_for_missing_semi ( expr, & mut err) ;
@@ -417,7 +422,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
417
422
IsAssign :: Yes => "=" ,
418
423
IsAssign :: No => "" ,
419
424
} ,
420
- lhs_deref_ty,
425
+ self . tcx . short_string ( lhs_deref_ty, err . long_ty_path ( ) ) ,
421
426
) ;
422
427
err. span_suggestion_verbose (
423
428
lhs_expr. span . shrink_to_lo ( ) ,
@@ -443,8 +448,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
443
448
)
444
449
. is_ok ( )
445
450
{
446
- let op_str = op. node . as_str ( ) ;
447
- err. note ( format ! ( "an implementation for `{lhs_adjusted_ty} {op_str} {rhs_adjusted_ty}` exists" ) ) ;
451
+ let lhs = self . tcx . short_string ( lhs_adjusted_ty, err. long_ty_path ( ) ) ;
452
+ let rhs = self . tcx . short_string ( rhs_adjusted_ty, err. long_ty_path ( ) ) ;
453
+ let op = op. node . as_str ( ) ;
454
+ err. note ( format ! ( "an implementation for `{lhs} {op} {rhs}` exists" ) ) ;
448
455
449
456
if let Some ( lhs_new_mutbl) = lhs_new_mutbl
450
457
&& let Some ( rhs_new_mutbl) = rhs_new_mutbl
@@ -628,7 +635,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
628
635
// When we know that a missing bound is responsible, we don't show
629
636
// this note as it is redundant.
630
637
err. note ( format ! (
631
- "the trait `{missing_trait}` is not implemented for `{lhs_ty }`"
638
+ "the trait `{missing_trait}` is not implemented for `{lhs_ty_str }`"
632
639
) ) ;
633
640
}
634
641
}
@@ -654,24 +661,32 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
654
661
hir:: BinOpKind :: Sub => {
655
662
if lhs_ty. is_unsafe_ptr ( ) && rhs_ty. is_integral ( ) {
656
663
err. multipart_suggestion (
657
- "consider using `wrapping_sub` or `sub` for pointer - {integer}" ,
664
+ "consider using `wrapping_sub` or `sub` for \
665
+ pointer - {integer}",
658
666
vec ! [
659
- ( lhs_expr. span. between( rhs_expr. span) , ".wrapping_sub(" . to_owned( ) ) ,
667
+ (
668
+ lhs_expr. span. between( rhs_expr. span) ,
669
+ ".wrapping_sub(" . to_owned( ) ,
670
+ ) ,
660
671
( rhs_expr. span. shrink_to_hi( ) , ")" . to_owned( ) ) ,
661
672
] ,
662
- Applicability :: MaybeIncorrect
673
+ Applicability :: MaybeIncorrect ,
663
674
) ;
664
675
}
665
676
666
677
if lhs_ty. is_unsafe_ptr ( ) && rhs_ty. is_unsafe_ptr ( ) {
667
678
err. multipart_suggestion (
668
- "consider using `offset_from` for pointer - pointer if the pointers point to the same allocation" ,
679
+ "consider using `offset_from` for pointer - pointer if the \
680
+ pointers point to the same allocation",
669
681
vec ! [
670
682
( lhs_expr. span. shrink_to_lo( ) , "unsafe { " . to_owned( ) ) ,
671
- ( lhs_expr. span. between( rhs_expr. span) , ".offset_from(" . to_owned( ) ) ,
683
+ (
684
+ lhs_expr. span. between( rhs_expr. span) ,
685
+ ".offset_from(" . to_owned( ) ,
686
+ ) ,
672
687
( rhs_expr. span. shrink_to_hi( ) , ") }" . to_owned( ) ) ,
673
688
] ,
674
- Applicability :: MaybeIncorrect
689
+ Applicability :: MaybeIncorrect ,
675
690
) ;
676
691
}
677
692
}
@@ -793,14 +808,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
793
808
Err ( errors) => {
794
809
let actual = self . resolve_vars_if_possible ( operand_ty) ;
795
810
let guar = actual. error_reported ( ) . err ( ) . unwrap_or_else ( || {
811
+ let mut file = None ;
812
+ let ty_str = self . tcx . short_string ( actual, & mut file) ;
796
813
let mut err = struct_span_code_err ! (
797
814
self . dcx( ) ,
798
815
ex. span,
799
816
E0600 ,
800
- "cannot apply unary operator `{}` to type `{}`" ,
817
+ "cannot apply unary operator `{}` to type `{ty_str }`" ,
801
818
op. as_str( ) ,
802
- actual
803
819
) ;
820
+ * err. long_ty_path ( ) = file;
804
821
err. span_label (
805
822
ex. span ,
806
823
format ! ( "cannot apply unary operator `{}`" , op. as_str( ) ) ,
0 commit comments