@@ -57,7 +57,7 @@ use rustc_middle::ty::adjustment::{
5757} ;
5858use rustc_middle:: ty:: error:: TypeError ;
5959use rustc_middle:: ty:: visit:: TypeVisitableExt ;
60- use rustc_middle:: ty:: { self , GenericArgsRef , Ty , TyCtxt } ;
60+ use rustc_middle:: ty:: { self , GenericArgsRef , Pattern , Ty , TyCtxt } ;
6161use rustc_session:: parse:: feature_err;
6262use rustc_span:: { BytePos , DUMMY_SP , DesugaringKind , Span , sym} ;
6363use rustc_trait_selection:: infer:: InferCtxtExt as _;
@@ -259,6 +259,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
259259 // It cannot convert closures that require unsafe.
260260 self . coerce_closure_to_fn ( a, closure_def_id_a, args_a, b)
261261 }
262+ ty:: Pat ( base, pat) => self . coerce_from_pat_ty ( base, pat, b) ,
262263 _ => {
263264 // Otherwise, just use unification rules.
264265 self . unify_and ( a, b, identity)
@@ -1017,6 +1018,24 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
10171018 }
10181019 }
10191020
1021+ fn coerce_from_pat_ty (
1022+ & self ,
1023+ base : Ty < ' tcx > ,
1024+ pat : Pattern < ' tcx > ,
1025+ b : Ty < ' tcx > ,
1026+ ) -> CoerceResult < ' tcx > {
1027+ match * b. kind ( ) {
1028+ // FIXME(pattern_types): allow coercion to less restrictive patterns
1029+ ty:: Pat ( b_base, b_pat) if b_pat == pat => self . unify_and ( base, b_base, identity) ,
1030+ // Coercing to anything but a pattern type only works by dismissing the pattern
1031+ _ => {
1032+ let mut coerce = self . unify_and ( base, b, identity) ?;
1033+ coerce. value . 0 . insert ( 0 , Adjustment { kind : Adjust :: StripPattern , target : base } ) ;
1034+ Ok ( coerce)
1035+ }
1036+ }
1037+ }
1038+
10201039 fn coerce_raw_ptr (
10211040 & self ,
10221041 a : Ty < ' tcx > ,
0 commit comments