@@ -30,6 +30,7 @@ use rustc_middle::ty::{
3030use rustc_session:: lint;
3131use rustc_session:: lint:: builtin:: BARE_TRAIT_OBJECTS ;
3232use rustc_session:: parse:: feature_err;
33+ use rustc_span:: edition:: Edition ;
3334use rustc_span:: source_map:: { original_sp, DUMMY_SP } ;
3435use rustc_span:: symbol:: { kw, sym, Ident } ;
3536use rustc_span:: { self , BytePos , MultiSpan , Span } ;
@@ -969,20 +970,42 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
969970 if let TyKind :: TraitObject ( [ poly_trait_ref, ..] , _, TraitObjectSyntax :: None ) =
970971 self_ty. kind
971972 {
972- self . tcx . struct_span_lint_hir ( BARE_TRAIT_OBJECTS , hir_id, self_ty. span , |lint| {
973- let mut db = lint
974- . build ( & format ! ( "trait objects without an explicit `dyn` are deprecated" ) ) ;
975- let ( sugg, app) = match self . tcx . sess . source_map ( ) . span_to_snippet ( self_ty. span )
976- {
977- Ok ( s) if poly_trait_ref. trait_ref . path . is_global ( ) => {
978- ( format ! ( "<dyn ({})>" , s) , Applicability :: MachineApplicable )
979- }
980- Ok ( s) => ( format ! ( "<dyn {}>" , s) , Applicability :: MachineApplicable ) ,
981- Err ( _) => ( "<dyn <type>>" . to_string ( ) , Applicability :: HasPlaceholders ) ,
982- } ;
983- db. span_suggestion ( self_ty. span , "use `dyn`" , sugg, app) ;
984- db. emit ( )
985- } ) ;
973+ let msg = "trait objects without an explicit `dyn` are deprecated" ;
974+ let ( sugg, app) = match self . tcx . sess . source_map ( ) . span_to_snippet ( self_ty. span ) {
975+ Ok ( s) if poly_trait_ref. trait_ref . path . is_global ( ) => {
976+ ( format ! ( "<dyn ({})>" , s) , Applicability :: MachineApplicable )
977+ }
978+ Ok ( s) => ( format ! ( "<dyn {}>" , s) , Applicability :: MachineApplicable ) ,
979+ Err ( _) => ( "<dyn <type>>" . to_string ( ) , Applicability :: HasPlaceholders ) ,
980+ } ;
981+ let replace = String :: from ( "use `dyn`" ) ;
982+ if self . sess ( ) . edition ( ) >= Edition :: Edition2021 {
983+ let mut err = rustc_errors:: struct_span_err!(
984+ self . sess( ) ,
985+ self_ty. span,
986+ E0783 ,
987+ "{}" ,
988+ msg,
989+ ) ;
990+ err. span_suggestion (
991+ self_ty. span ,
992+ & sugg,
993+ replace,
994+ Applicability :: MachineApplicable ,
995+ )
996+ . emit ( ) ;
997+ } else {
998+ self . tcx . struct_span_lint_hir (
999+ BARE_TRAIT_OBJECTS ,
1000+ hir_id,
1001+ self_ty. span ,
1002+ |lint| {
1003+ let mut db = lint. build ( msg) ;
1004+ db. span_suggestion ( self_ty. span , & replace, sugg, app) ;
1005+ db. emit ( )
1006+ } ,
1007+ ) ;
1008+ }
9861009 }
9871010 }
9881011 }
0 commit comments