@@ -32,6 +32,7 @@ use rustc_ast::util::parser::ExprPrecedence;
3232use rustc_data_structures:: fx:: FxHashSet ;
3333use rustc_errors:: codes:: * ;
3434use rustc_errors:: { Applicability , Diag , ErrorGuaranteed } ;
35+ use rustc_hir:: def_id:: DefId ;
3536use rustc_hir:: { self as hir, ExprKind } ;
3637use rustc_infer:: infer:: DefineOpaqueTypes ;
3738use rustc_macros:: { TypeFoldable , TypeVisitable } ;
@@ -155,7 +156,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
155156 }
156157}
157158
158- #[ derive( Copy , Clone , Debug ) ]
159+ #[ derive( Debug ) ]
159160enum CastError < ' tcx > {
160161 ErrorGuaranteed ( ErrorGuaranteed ) ,
161162
@@ -182,6 +183,7 @@ enum CastError<'tcx> {
182183 /// when we're typechecking a type parameter with a ?Sized bound.
183184 IntToWideCast ( Option < & ' static str > ) ,
184185 ForeignNonExhaustiveAdt ,
186+ PtrPtrAddingAutoTrait ( Vec < DefId > ) ,
185187}
186188
187189impl From < ErrorGuaranteed > for CastError < ' _ > {
@@ -596,6 +598,21 @@ impl<'a, 'tcx> CastCheck<'tcx> {
596598 . with_note ( "cannot cast an enum with a non-exhaustive variant when it's defined in another crate" )
597599 . emit ( ) ;
598600 }
601+ CastError :: PtrPtrAddingAutoTrait ( added) => {
602+ fcx. dcx ( ) . emit_err ( errors:: PtrCastAddAutoToObject {
603+ span : self . span ,
604+ traits_len : added. len ( ) ,
605+ traits : {
606+ let mut traits: Vec < _ > = added
607+ . into_iter ( )
608+ . map ( |trait_did| fcx. tcx . def_path_str ( trait_did) )
609+ . collect ( ) ;
610+
611+ traits. sort ( ) ;
612+ traits. into ( )
613+ } ,
614+ } ) ;
615+ }
599616 }
600617 }
601618
@@ -940,19 +957,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
940957 . collect :: < Vec < _ > > ( ) ;
941958
942959 if !added. is_empty ( ) {
943- tcx. dcx ( ) . emit_err ( errors:: PtrCastAddAutoToObject {
944- span : self . span ,
945- traits_len : added. len ( ) ,
946- traits : {
947- let mut traits: Vec < _ > = added
948- . into_iter ( )
949- . map ( |trait_did| tcx. def_path_str ( trait_did) )
950- . collect ( ) ;
951-
952- traits. sort ( ) ;
953- traits. into ( )
954- } ,
955- } ) ;
960+ return Err ( CastError :: PtrPtrAddingAutoTrait ( added) ) ;
956961 }
957962
958963 Ok ( CastKind :: PtrPtrCast )
0 commit comments