@@ -65,6 +65,8 @@ enum IsRepeatExpr {
65
65
Yes ,
66
66
}
67
67
68
+ struct IsNeverPattern ;
69
+
68
70
/// Describes whether an `AnonConst` is a type level const arg or
69
71
/// some other form of anon const (i.e. inline consts or enum discriminants)
70
72
#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
@@ -3191,11 +3193,15 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
3191
3193
}
3192
3194
3193
3195
/// Build a map from pattern identifiers to binding-info's, and check the bindings are
3194
- /// consistent when encountering or-patterns.
3196
+ /// consistent when encountering or-patterns and never patterns .
3195
3197
/// This is done hygienically: this could arise for a macro that expands into an or-pattern
3196
3198
/// where one 'x' was from the user and one 'x' came from the macro.
3197
- fn compute_and_check_binding_map ( & mut self , pat : & Pat ) -> FxIndexMap < Ident , BindingInfo > {
3199
+ fn compute_and_check_binding_map (
3200
+ & mut self ,
3201
+ pat : & Pat ,
3202
+ ) -> Result < FxIndexMap < Ident , BindingInfo > , IsNeverPattern > {
3198
3203
let mut binding_map = FxIndexMap :: default ( ) ;
3204
+ let mut is_never_pat = false ;
3199
3205
3200
3206
pat. walk ( & mut |pat| {
3201
3207
match pat. kind {
@@ -3207,17 +3213,26 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
3207
3213
PatKind :: Or ( ref ps) => {
3208
3214
// Check the consistency of this or-pattern and
3209
3215
// then add all bindings to the larger map.
3210
- let bm = self . compute_and_check_or_pat_binding_map ( ps) ;
3216
+ let ( bm , np ) = self . compute_and_check_or_pat_binding_map ( ps) ;
3211
3217
binding_map. extend ( bm) ;
3218
+ is_never_pat |= np;
3212
3219
return false ;
3213
3220
}
3221
+ PatKind :: Never => is_never_pat = true ,
3214
3222
_ => { }
3215
3223
}
3216
3224
3217
3225
true
3218
3226
} ) ;
3219
3227
3220
- binding_map
3228
+ if is_never_pat {
3229
+ for ( _, binding) in binding_map {
3230
+ self . report_error ( binding. span , ResolutionError :: BindingInNeverPattern ) ;
3231
+ }
3232
+ Err ( IsNeverPattern )
3233
+ } else {
3234
+ Ok ( binding_map)
3235
+ }
3221
3236
}
3222
3237
3223
3238
fn is_base_res_local ( & self , nid : NodeId ) -> bool {
@@ -3229,24 +3244,29 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
3229
3244
3230
3245
/// Compute the binding map for an or-pattern. Checks that all of the arms in the or-pattern
3231
3246
/// have exactly the same set of bindings, with the same binding modes for each.
3232
- /// Returns the computed binding map.
3247
+ /// Returns the computed binding map and a boolean indicating whether the pattern is a never
3248
+ /// pattern.
3233
3249
fn compute_and_check_or_pat_binding_map (
3234
3250
& mut self ,
3235
3251
pats : & [ P < Pat > ] ,
3236
- ) -> FxIndexMap < Ident , BindingInfo > {
3252
+ ) -> ( FxIndexMap < Ident , BindingInfo > , bool ) {
3237
3253
let mut missing_vars = FxIndexMap :: default ( ) ;
3238
3254
let mut inconsistent_vars = FxIndexMap :: default ( ) ;
3239
3255
3240
- // 1) Compute the binding maps of all arms.
3241
- let maps =
3242
- pats. iter ( ) . map ( |pat| self . compute_and_check_binding_map ( pat) ) . collect :: < Vec < _ > > ( ) ;
3256
+ // 1) Compute the binding maps of all arms; never patterns don't participate in this.
3257
+ let not_never_pats = pats
3258
+ . iter ( )
3259
+ . filter_map ( |pat| {
3260
+ let binding_map = self . compute_and_check_binding_map ( pat) . ok ( ) ?;
3261
+ Some ( ( binding_map, pat) )
3262
+ } )
3263
+ . collect :: < Vec < _ > > ( ) ;
3243
3264
3244
3265
// 2) Record any missing bindings or binding mode inconsistencies.
3245
- for ( map_outer, pat_outer) in maps . iter ( ) . zip ( pats . iter ( ) ) {
3266
+ for ( map_outer, pat_outer) in not_never_pats . iter ( ) {
3246
3267
// Check against all arms except for the same pattern which is always self-consistent.
3247
- let inners = maps
3268
+ let inners = not_never_pats
3248
3269
. iter ( )
3249
- . zip ( pats. iter ( ) )
3250
3270
. filter ( |( _, pat) | pat. id != pat_outer. id )
3251
3271
. flat_map ( |( map, _) | map) ;
3252
3272
@@ -3294,22 +3314,17 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
3294
3314
}
3295
3315
3296
3316
// 5) Bubble up the final binding map.
3317
+ let is_never_pat = not_never_pats. is_empty ( ) ;
3297
3318
let mut binding_map = FxIndexMap :: default ( ) ;
3298
- for bm in maps {
3319
+ for ( bm , _ ) in not_never_pats {
3299
3320
binding_map. extend ( bm) ;
3300
3321
}
3301
- binding_map
3322
+ ( binding_map, is_never_pat )
3302
3323
}
3303
3324
3304
- /// Check the consistency of bindings wrt or-patterns.
3325
+ /// Check the consistency of bindings wrt or-patterns and never patterns .
3305
3326
fn check_consistent_bindings ( & mut self , pat : & ' ast Pat ) {
3306
- pat. walk ( & mut |pat| match pat. kind {
3307
- PatKind :: Or ( ref ps) => {
3308
- let _ = self . compute_and_check_or_pat_binding_map ( ps) ;
3309
- false
3310
- }
3311
- _ => true ,
3312
- } )
3327
+ let _ = self . compute_and_check_binding_map ( pat) ;
3313
3328
}
3314
3329
3315
3330
fn resolve_arm ( & mut self , arm : & ' ast Arm ) {
0 commit comments