1
1
use rustc_abi:: { FieldIdx , VariantIdx } ;
2
2
use rustc_apfloat:: Float ;
3
3
use rustc_data_structures:: fx:: FxHashSet ;
4
- use rustc_errors:: { Diag , PResult } ;
4
+ use rustc_errors:: Diag ;
5
5
use rustc_hir as hir;
6
6
use rustc_index:: Idx ;
7
7
use rustc_infer:: infer:: TyCtxtInferExt ;
@@ -42,10 +42,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
42
42
43
43
match c. kind ( ) {
44
44
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) ,
49
46
_ => span_bug ! ( span, "Invalid `ConstKind` for `const_to_pat`: {:?}" , c) ,
50
47
}
51
48
}
@@ -151,7 +148,7 @@ impl<'tcx> ConstToPat<'tcx> {
151
148
let generics = self . tcx . generics_of ( def_id) ;
152
149
let param = generics. type_param ( * param_ty, self . tcx ) ;
153
150
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 " ) ;
155
152
if let Some ( ident) = self . tcx . def_ident_span ( def_id)
156
153
&& self . tcx . sess . source_map ( ) . is_multiline ( ident. between ( span) )
157
154
{
@@ -184,10 +181,7 @@ impl<'tcx> ConstToPat<'tcx> {
184
181
} ;
185
182
186
183
// 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) ;
191
185
192
186
if !inlined_const_as_pat. references_error ( ) {
193
187
// Always check for `PartialEq` if we had no other errors yet.
@@ -210,33 +204,27 @@ impl<'tcx> ConstToPat<'tcx> {
210
204
let field = FieldIdx :: new ( idx) ;
211
205
// Patterns can only use monomorphic types.
212
206
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) }
220
208
} )
221
209
. collect ( )
222
210
}
223
211
224
212
// Recursive helper for `to_pat`; invoke that (instead of calling this directly).
225
213
#[ 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 > > {
227
215
let span = self . span ;
228
216
let tcx = self . tcx ;
229
217
let kind = match ty. kind ( ) {
230
218
ty:: Adt ( adt_def, _) if !self . type_marked_structural ( ty) => {
231
219
// Extremely important check for all ADTs! Make sure they opted-in to be used in
232
220
// patterns.
233
221
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) =
235
223
type_has_partial_eq_impl ( self . tcx , self . typing_env , ty) ;
236
224
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 => {
240
228
( Some ( tcx. def_span ( def_id) ) , false )
241
229
}
242
230
_ => ( None , true ) ,
@@ -249,7 +237,7 @@ impl<'tcx> ConstToPat<'tcx> {
249
237
manual_partialeq_impl_span,
250
238
manual_partialeq_impl_note,
251
239
} ;
252
- return Err ( tcx. dcx ( ) . create_err ( err) ) ;
240
+ return self . mk_err ( tcx. dcx ( ) . create_err ( err) , ty ) ;
253
241
}
254
242
ty:: Adt ( adt_def, args) if adt_def. is_enum ( ) => {
255
243
let ( & variant_index, fields) = cv. unwrap_branch ( ) . split_first ( ) . unwrap ( ) ;
@@ -283,10 +271,7 @@ impl<'tcx> ConstToPat<'tcx> {
283
271
prefix : cv
284
272
. unwrap_branch ( )
285
273
. 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) )
290
275
. collect ( ) ,
291
276
slice : None ,
292
277
suffix : Box :: new ( [ ] ) ,
@@ -295,10 +280,7 @@ impl<'tcx> ConstToPat<'tcx> {
295
280
prefix : cv
296
281
. unwrap_branch ( )
297
282
. 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) )
302
284
. collect ( ) ,
303
285
slice : None ,
304
286
suffix : Box :: new ( [ ] ) ,
@@ -314,9 +296,10 @@ impl<'tcx> ConstToPat<'tcx> {
314
296
// deref pattern.
315
297
_ => {
316
298
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
+ ) ;
320
303
} else {
321
304
// `b"foo"` produces a `&[u8; 3]`, but you can't use constants of array type when
322
305
// matching against references, you can only use byte string literals.
@@ -331,10 +314,7 @@ impl<'tcx> ConstToPat<'tcx> {
331
314
_ => * pointee_ty,
332
315
} ;
333
316
// 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) ;
338
318
PatKind :: Deref { subpattern }
339
319
}
340
320
}
@@ -350,7 +330,7 @@ impl<'tcx> ConstToPat<'tcx> {
350
330
if is_nan {
351
331
// NaNs are not ever equal to anything so they make no sense as patterns.
352
332
// 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 ) ;
354
334
} else {
355
335
PatKind :: Constant {
356
336
value : mir:: Const :: Ty ( ty, ty:: Const :: new_value ( tcx, cv, ty) ) ,
@@ -373,16 +353,16 @@ impl<'tcx> ConstToPat<'tcx> {
373
353
non_sm_ty : ty,
374
354
prefix : ty. prefix_string ( tcx) . to_string ( ) ,
375
355
} ;
376
- return Err ( tcx. dcx ( ) . create_err ( err) ) ;
356
+ return self . mk_err ( tcx. dcx ( ) . create_err ( err) , ty ) ;
377
357
}
378
358
} ;
379
359
380
- Ok ( Box :: new ( Pat { span, ty, kind } ) )
360
+ Box :: new ( Pat { span, ty, kind } )
381
361
}
382
362
}
383
363
384
364
/// 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.
386
366
fn extend_type_not_partial_eq < ' tcx > (
387
367
tcx : TyCtxt < ' tcx > ,
388
368
typing_env : ty:: TypingEnv < ' tcx > ,
0 commit comments