@@ -2617,40 +2617,53 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2617
2617
if let hir:: TyKind :: TraitObject ( [ poly_trait_ref, ..] , _, TraitObjectSyntax :: None ) =
2618
2618
self_ty. kind
2619
2619
{
2620
- let ( mut sugg, app) = match tcx. sess . source_map ( ) . span_to_snippet ( self_ty. span ) {
2621
- Ok ( s) if poly_trait_ref. trait_ref . path . is_global ( ) => {
2622
- ( format ! ( "dyn ({})" , s) , Applicability :: MachineApplicable )
2623
- }
2624
- Ok ( s) => ( format ! ( "dyn {}" , s) , Applicability :: MachineApplicable ) ,
2625
- Err ( _) => ( "dyn <type>" . to_string ( ) , Applicability :: HasPlaceholders ) ,
2626
- } ;
2627
- if in_path {
2628
- let has_bracket = tcx
2620
+ let needs_bracket = in_path
2621
+ && !tcx
2629
2622
. sess
2630
2623
. source_map ( )
2631
2624
. span_to_prev_source ( self_ty. span )
2632
2625
. ok ( )
2633
2626
. map_or ( false , |s| s. trim_end ( ) . ends_with ( '<' ) ) ;
2634
- if !has_bracket {
2635
- sugg = format ! ( "<{}>" , sugg) ;
2636
- }
2637
- }
2627
+
2628
+ let is_global = poly_trait_ref. trait_ref . path . is_global ( ) ;
2629
+ let sugg = Vec :: from_iter ( [
2630
+ (
2631
+ self_ty. span . shrink_to_lo ( ) ,
2632
+ format ! (
2633
+ "{}dyn {}" ,
2634
+ if needs_bracket { "<" } else { "" } ,
2635
+ if is_global { "(" } else { "" } ,
2636
+ ) ,
2637
+ ) ,
2638
+ (
2639
+ self_ty. span . shrink_to_hi ( ) ,
2640
+ format ! (
2641
+ "{}{}" ,
2642
+ if is_global { ")" } else { "" } ,
2643
+ if needs_bracket { ">" } else { "" } ,
2644
+ ) ,
2645
+ ) ,
2646
+ ] ) ;
2638
2647
if self_ty. span . edition ( ) >= Edition :: Edition2021 {
2639
2648
let msg = "trait objects must include the `dyn` keyword" ;
2640
2649
let label = "add `dyn` keyword before this trait" ;
2641
- let mut err =
2642
- rustc_errors :: struct_span_err! ( tcx . sess , self_ty . span , E0782 , "{}" , msg ) ;
2643
- err . span_suggestion_verbose ( self_ty . span , label , sugg , app ) . emit ( ) ;
2650
+ rustc_errors :: struct_span_err! ( tcx . sess , self_ty . span , E0782 , "{}" , msg )
2651
+ . multipart_suggestion_verbose ( label , sugg , Applicability :: MachineApplicable )
2652
+ . emit ( ) ;
2644
2653
} else {
2645
2654
let msg = "trait objects without an explicit `dyn` are deprecated" ;
2646
2655
tcx. struct_span_lint_hir (
2647
2656
BARE_TRAIT_OBJECTS ,
2648
2657
self_ty. hir_id ,
2649
2658
self_ty. span ,
2650
2659
|lint| {
2651
- let mut db = lint. build ( msg) ;
2652
- db. span_suggestion ( self_ty. span , "use `dyn`" , sugg, app) ;
2653
- db. emit ( )
2660
+ lint. build ( msg)
2661
+ . multipart_suggestion_verbose (
2662
+ "use `dyn`" ,
2663
+ sugg,
2664
+ Applicability :: MachineApplicable ,
2665
+ )
2666
+ . emit ( )
2654
2667
} ,
2655
2668
) ;
2656
2669
}
0 commit comments