11use rustc_abi:: { FieldIdx , VariantIdx } ;
22use rustc_apfloat:: Float ;
33use rustc_data_structures:: fx:: FxHashSet ;
4- use rustc_errors:: { Diag , PResult } ;
4+ use rustc_errors:: Diag ;
55use rustc_hir as hir;
66use rustc_index:: Idx ;
77use rustc_infer:: infer:: TyCtxtInferExt ;
@@ -42,10 +42,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
4242
4343 match c. kind ( ) {
4444 ty:: ConstKind :: Unevaluated ( uv) => convert. unevaluated_to_pat ( uv, ty) ,
45- ty:: ConstKind :: Value ( _, val) => match convert. valtree_to_pat ( val, ty) {
46- Ok ( pat) => pat,
47- Err ( err) => convert. mk_err ( err, ty) ,
48- } ,
45+ ty:: ConstKind :: Value ( _, val) => convert. valtree_to_pat ( val, ty) ,
4946 _ => span_bug ! ( span, "Invalid `ConstKind` for `const_to_pat`: {:?}" , c) ,
5047 }
5148 }
@@ -151,7 +148,7 @@ impl<'tcx> ConstToPat<'tcx> {
151148 let generics = self . tcx . generics_of ( def_id) ;
152149 let param = generics. type_param ( * param_ty, self . tcx ) ;
153150 let span = self . tcx . def_span ( param. def_id ) ;
154- e. span_label ( span, "constant depends on this generic param " ) ;
151+ e. span_label ( span, "constant depends on this generic parameter " ) ;
155152 if let Some ( ident) = self . tcx . def_ident_span ( def_id)
156153 && self . tcx . sess . source_map ( ) . is_multiline ( ident. between ( span) )
157154 {
@@ -184,10 +181,7 @@ impl<'tcx> ConstToPat<'tcx> {
184181 } ;
185182
186183 // Convert the valtree to a const.
187- let inlined_const_as_pat = match self . valtree_to_pat ( valtree, ty) {
188- Ok ( pat) => pat,
189- Err ( err) => self . mk_err ( err, ty) ,
190- } ;
184+ let inlined_const_as_pat = self . valtree_to_pat ( valtree, ty) ;
191185
192186 if !inlined_const_as_pat. references_error ( ) {
193187 // Always check for `PartialEq` if we had no other errors yet.
@@ -210,33 +204,27 @@ impl<'tcx> ConstToPat<'tcx> {
210204 let field = FieldIdx :: new ( idx) ;
211205 // Patterns can only use monomorphic types.
212206 let ty = self . tcx . normalize_erasing_regions ( self . typing_env , ty) ;
213- FieldPat {
214- field,
215- pattern : match self . valtree_to_pat ( val, ty) {
216- Ok ( pat) => pat,
217- Err ( err) => self . mk_err ( err, ty) ,
218- } ,
219- }
207+ FieldPat { field, pattern : self . valtree_to_pat ( val, ty) }
220208 } )
221209 . collect ( )
222210 }
223211
224212 // Recursive helper for `to_pat`; invoke that (instead of calling this directly).
225213 #[ instrument( skip( self ) , level = "debug" ) ]
226- fn valtree_to_pat ( & self , cv : ValTree < ' tcx > , ty : Ty < ' tcx > ) -> PResult < ' _ , Box < Pat < ' tcx > > > {
214+ fn valtree_to_pat ( & self , cv : ValTree < ' tcx > , ty : Ty < ' tcx > ) -> Box < Pat < ' tcx > > {
227215 let span = self . span ;
228216 let tcx = self . tcx ;
229217 let kind = match ty. kind ( ) {
230218 ty:: Adt ( adt_def, _) if !self . type_marked_structural ( ty) => {
231219 // Extremely important check for all ADTs! Make sure they opted-in to be used in
232220 // patterns.
233221 debug ! ( "adt_def {:?} has !type_marked_structural for cv.ty: {:?}" , adt_def, ty) ;
234- let ( impls_partial_eq , derived, structural, impl_def_id) =
222+ let ( _impls_partial_eq , derived, structural, impl_def_id) =
235223 type_has_partial_eq_impl ( self . tcx , self . typing_env , ty) ;
236224 let ( manual_partialeq_impl_span, manual_partialeq_impl_note) =
237- match ( impls_partial_eq , derived , structural, impl_def_id) {
238- ( _ , _ , true , _) => ( None , false ) ,
239- ( _, false , _ , Some ( def_id) ) if def_id. is_local ( ) => {
225+ match ( structural, impl_def_id) {
226+ ( true , _) => ( None , false ) ,
227+ ( _, Some ( def_id) ) if def_id. is_local ( ) && !derived => {
240228 ( Some ( tcx. def_span ( def_id) ) , false )
241229 }
242230 _ => ( None , true ) ,
@@ -249,7 +237,7 @@ impl<'tcx> ConstToPat<'tcx> {
249237 manual_partialeq_impl_span,
250238 manual_partialeq_impl_note,
251239 } ;
252- return Err ( tcx. dcx ( ) . create_err ( err) ) ;
240+ return self . mk_err ( tcx. dcx ( ) . create_err ( err) , ty ) ;
253241 }
254242 ty:: Adt ( adt_def, args) if adt_def. is_enum ( ) => {
255243 let ( & variant_index, fields) = cv. unwrap_branch ( ) . split_first ( ) . unwrap ( ) ;
@@ -283,10 +271,7 @@ impl<'tcx> ConstToPat<'tcx> {
283271 prefix : cv
284272 . unwrap_branch ( )
285273 . iter ( )
286- . map ( |val| match self . valtree_to_pat ( * val, * elem_ty) {
287- Ok ( pat) => pat,
288- Err ( err) => self . mk_err ( err, ty) ,
289- } )
274+ . map ( |val| self . valtree_to_pat ( * val, * elem_ty) )
290275 . collect ( ) ,
291276 slice : None ,
292277 suffix : Box :: new ( [ ] ) ,
@@ -295,10 +280,7 @@ impl<'tcx> ConstToPat<'tcx> {
295280 prefix : cv
296281 . unwrap_branch ( )
297282 . iter ( )
298- . map ( |val| match self . valtree_to_pat ( * val, * elem_ty) {
299- Ok ( pat) => pat,
300- Err ( err) => self . mk_err ( err, ty) ,
301- } )
283+ . map ( |val| self . valtree_to_pat ( * val, * elem_ty) )
302284 . collect ( ) ,
303285 slice : None ,
304286 suffix : Box :: new ( [ ] ) ,
@@ -314,9 +296,10 @@ impl<'tcx> ConstToPat<'tcx> {
314296 // deref pattern.
315297 _ => {
316298 if !pointee_ty. is_sized ( tcx, self . typing_env ) && !pointee_ty. is_slice ( ) {
317- return Err ( tcx
318- . dcx ( )
319- . create_err ( UnsizedPattern { span, non_sm_ty : * pointee_ty } ) ) ;
299+ return self . mk_err (
300+ tcx. dcx ( ) . create_err ( UnsizedPattern { span, non_sm_ty : * pointee_ty } ) ,
301+ ty,
302+ ) ;
320303 } else {
321304 // `b"foo"` produces a `&[u8; 3]`, but you can't use constants of array type when
322305 // matching against references, you can only use byte string literals.
@@ -331,10 +314,7 @@ impl<'tcx> ConstToPat<'tcx> {
331314 _ => * pointee_ty,
332315 } ;
333316 // References have the same valtree representation as their pointee.
334- let subpattern = match self . valtree_to_pat ( cv, pointee_ty) {
335- Ok ( pat) => pat,
336- Err ( err) => self . mk_err ( err, ty) ,
337- } ;
317+ let subpattern = self . valtree_to_pat ( cv, pointee_ty) ;
338318 PatKind :: Deref { subpattern }
339319 }
340320 }
@@ -350,7 +330,7 @@ impl<'tcx> ConstToPat<'tcx> {
350330 if is_nan {
351331 // NaNs are not ever equal to anything so they make no sense as patterns.
352332 // Also see <https://github.com/rust-lang/rfcs/pull/3535>.
353- return Err ( tcx. dcx ( ) . create_err ( NaNPattern { span } ) ) ;
333+ return self . mk_err ( tcx. dcx ( ) . create_err ( NaNPattern { span } ) , ty ) ;
354334 } else {
355335 PatKind :: Constant {
356336 value : mir:: Const :: Ty ( ty, ty:: Const :: new_value ( tcx, cv, ty) ) ,
@@ -373,16 +353,16 @@ impl<'tcx> ConstToPat<'tcx> {
373353 non_sm_ty : ty,
374354 prefix : ty. prefix_string ( tcx) . to_string ( ) ,
375355 } ;
376- return Err ( tcx. dcx ( ) . create_err ( err) ) ;
356+ return self . mk_err ( tcx. dcx ( ) . create_err ( err) , ty ) ;
377357 }
378358 } ;
379359
380- Ok ( Box :: new ( Pat { span, ty, kind } ) )
360+ Box :: new ( Pat { span, ty, kind } )
381361 }
382362}
383363
384364/// Given a type with type parameters, visit every ADT looking for types that need to
385- /// `#[derive(PartialEq)]` to be a structural type.
365+ /// `#[derive(PartialEq)]` for it to be a structural type.
386366fn extend_type_not_partial_eq < ' tcx > (
387367 tcx : TyCtxt < ' tcx > ,
388368 typing_env : ty:: TypingEnv < ' tcx > ,
0 commit comments