@@ -16,6 +16,7 @@ use rustc_span::lev_distance::find_best_match_for_name;
1616use rustc_span:: source_map:: { Span , Spanned } ;
1717use rustc_span:: symbol:: Ident ;
1818use rustc_span:: { BytePos , DUMMY_SP } ;
19+ use rustc_trait_selection:: autoderef:: Autoderef ;
1920use rustc_trait_selection:: traits:: { ObligationCause , Pattern } ;
2021use ty:: VariantDef ;
2122
@@ -1769,7 +1770,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17691770 // The expected type must be an array or slice, but was neither, so error.
17701771 _ => {
17711772 if !expected. references_error ( ) {
1772- self . error_expected_array_or_slice ( span, expected) ;
1773+ self . error_expected_array_or_slice ( span, expected, ti ) ;
17731774 }
17741775 let err = self . tcx . ty_error ( ) ;
17751776 ( err, Some ( err) , err)
@@ -1882,7 +1883,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18821883 . emit ( ) ;
18831884 }
18841885
1885- fn error_expected_array_or_slice ( & self , span : Span , expected_ty : Ty < ' tcx > ) {
1886+ fn error_expected_array_or_slice ( & self , span : Span , expected_ty : Ty < ' tcx > , ti : TopInfo < ' tcx > ) {
18861887 let mut err = struct_span_err ! (
18871888 self . tcx. sess,
18881889 span,
@@ -1894,6 +1895,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18941895 if let ty:: Array ( ..) | ty:: Slice ( ..) = ty. kind ( ) {
18951896 err. help ( "the semantics of slice patterns changed recently; see issue #62254" ) ;
18961897 }
1898+ } else if Autoderef :: new ( & self . infcx , self . param_env , self . body_id , span, expected_ty, span)
1899+ . any ( |( ty, _) | matches ! ( ty. kind( ) , ty:: Slice ( ..) ) )
1900+ {
1901+ if let ( Some ( span) , true ) = ( ti. span , ti. origin_expr ) {
1902+ if let Ok ( snippet) = self . tcx . sess . source_map ( ) . span_to_snippet ( span) {
1903+ err. span_suggestion (
1904+ span,
1905+ "consider slicing here" ,
1906+ format ! ( "{}[..]" , snippet) ,
1907+ Applicability :: MachineApplicable ,
1908+ ) ;
1909+ }
1910+ }
18971911 }
18981912 err. span_label ( span, format ! ( "pattern cannot match with input type `{}`" , expected_ty) ) ;
18991913 err. emit ( ) ;
0 commit comments