@@ -16,6 +16,7 @@ use rustc_span::lev_distance::find_best_match_for_name;
16
16
use rustc_span:: source_map:: { Span , Spanned } ;
17
17
use rustc_span:: symbol:: Ident ;
18
18
use rustc_span:: { BytePos , DUMMY_SP } ;
19
+ use rustc_trait_selection:: autoderef:: Autoderef ;
19
20
use rustc_trait_selection:: traits:: { ObligationCause , Pattern } ;
20
21
use ty:: VariantDef ;
21
22
@@ -1769,7 +1770,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1769
1770
// The expected type must be an array or slice, but was neither, so error.
1770
1771
_ => {
1771
1772
if !expected. references_error ( ) {
1772
- self . error_expected_array_or_slice ( span, expected) ;
1773
+ self . error_expected_array_or_slice ( span, expected, ti ) ;
1773
1774
}
1774
1775
let err = self . tcx . ty_error ( ) ;
1775
1776
( err, Some ( err) , err)
@@ -1882,7 +1883,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1882
1883
. emit ( ) ;
1883
1884
}
1884
1885
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 > ) {
1886
1887
let mut err = struct_span_err ! (
1887
1888
self . tcx. sess,
1888
1889
span,
@@ -1894,6 +1895,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1894
1895
if let ty:: Array ( ..) | ty:: Slice ( ..) = ty. kind ( ) {
1895
1896
err. help ( "the semantics of slice patterns changed recently; see issue #62254" ) ;
1896
1897
}
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
+ }
1897
1911
}
1898
1912
err. span_label ( span, format ! ( "pattern cannot match with input type `{}`" , expected_ty) ) ;
1899
1913
err. emit ( ) ;
0 commit comments