@@ -68,7 +68,7 @@ impl LoweringContext<'_> {
68
68
let ohs = P ( self . lower_expr ( ohs) ) ;
69
69
hir:: ExprKind :: AddrOf ( m, ohs)
70
70
}
71
- ExprKind :: Let ( ref pats , ref scrutinee) => self . lower_expr_let ( e. span , pats , scrutinee) ,
71
+ ExprKind :: Let ( ref pat , ref scrutinee) => self . lower_expr_let ( e. span , pat , scrutinee) ,
72
72
ExprKind :: If ( ref cond, ref then, ref else_opt) => {
73
73
self . lower_expr_if ( e. span , cond, then, else_opt. as_deref ( ) )
74
74
}
@@ -227,16 +227,11 @@ impl LoweringContext<'_> {
227
227
}
228
228
}
229
229
230
- /// Emit an error and lower `ast::ExprKind::Let(pats , scrutinee)` into:
230
+ /// Emit an error and lower `ast::ExprKind::Let(pat , scrutinee)` into:
231
231
/// ```rust
232
232
/// match scrutinee { pats => true, _ => false }
233
233
/// ```
234
- fn lower_expr_let (
235
- & mut self ,
236
- span : Span ,
237
- pats : & [ AstP < Pat > ] ,
238
- scrutinee : & Expr
239
- ) -> hir:: ExprKind {
234
+ fn lower_expr_let ( & mut self , span : Span , pat : & Pat , scrutinee : & Expr ) -> hir:: ExprKind {
240
235
// If we got here, the `let` expression is not allowed.
241
236
self . sess
242
237
. struct_span_err ( span, "`let` expressions are not supported here" )
@@ -246,23 +241,23 @@ impl LoweringContext<'_> {
246
241
247
242
// For better recovery, we emit:
248
243
// ```
249
- // match scrutinee { pats => true, _ => false }
244
+ // match scrutinee { pat => true, _ => false }
250
245
// ```
251
246
// While this doesn't fully match the user's intent, it has key advantages:
252
247
// 1. We can avoid using `abort_if_errors`.
253
- // 2. We can typeck both `pats ` and `scrutinee`.
254
- // 3. `pats ` is allowed to be refutable.
248
+ // 2. We can typeck both `pat ` and `scrutinee`.
249
+ // 3. `pat ` is allowed to be refutable.
255
250
// 4. The return type of the block is `bool` which seems like what the user wanted.
256
251
let scrutinee = self . lower_expr ( scrutinee) ;
257
252
let then_arm = {
258
- let pats = pats . iter ( ) . map ( |pat| self . lower_pat ( pat) ) . collect ( ) ;
253
+ let pat = self . lower_pat_top_hack ( pat) ;
259
254
let expr = self . expr_bool ( span, true ) ;
260
- self . arm ( pats , P ( expr) )
255
+ self . arm ( pat , P ( expr) )
261
256
} ;
262
257
let else_arm = {
263
- let pats = hir_vec ! [ self . pat_wild( span) ] ;
258
+ let pat = self . pat_wild ( span) ;
264
259
let expr = self . expr_bool ( span, false ) ;
265
- self . arm ( pats , P ( expr) )
260
+ self . arm ( hir_vec ! [ pat ] , P ( expr) )
266
261
} ;
267
262
hir:: ExprKind :: Match (
268
263
P ( scrutinee) ,
@@ -291,13 +286,12 @@ impl LoweringContext<'_> {
291
286
// Handle then + scrutinee:
292
287
let then_blk = self . lower_block ( then, false ) ;
293
288
let then_expr = self . expr_block ( then_blk, ThinVec :: new ( ) ) ;
294
- let ( then_pats , scrutinee, desugar) = match cond. node {
289
+ let ( then_pat , scrutinee, desugar) = match cond. node {
295
290
// `<pat> => <then>`:
296
- ExprKind :: Let ( ref pats , ref scrutinee) => {
291
+ ExprKind :: Let ( ref pat , ref scrutinee) => {
297
292
let scrutinee = self . lower_expr ( scrutinee) ;
298
- let pats = pats. iter ( ) . map ( |pat| self . lower_pat ( pat) ) . collect ( ) ;
299
- let desugar = hir:: MatchSource :: IfLetDesugar { contains_else_clause } ;
300
- ( pats, scrutinee, desugar)
293
+ let pat = self . lower_pat_top_hack ( pat) ;
294
+ ( pat, scrutinee, hir:: MatchSource :: IfLetDesugar { contains_else_clause } )
301
295
}
302
296
// `true => <then>`:
303
297
_ => {
@@ -312,13 +306,11 @@ impl LoweringContext<'_> {
312
306
// to preserve drop semantics since `if cond { ... }` does not
313
307
// let temporaries live outside of `cond`.
314
308
let cond = self . expr_drop_temps ( span_block, P ( cond) , ThinVec :: new ( ) ) ;
315
-
316
- let desugar = hir:: MatchSource :: IfDesugar { contains_else_clause } ;
317
- let pats = hir_vec ! [ self . pat_bool( span, true ) ] ;
318
- ( pats, cond, desugar)
309
+ let pat = self . pat_bool ( span, true ) ;
310
+ ( hir_vec ! [ pat] , cond, hir:: MatchSource :: IfDesugar { contains_else_clause } )
319
311
}
320
312
} ;
321
- let then_arm = self . arm ( then_pats , P ( then_expr) ) ;
313
+ let then_arm = self . arm ( then_pat , P ( then_expr) ) ;
322
314
323
315
hir:: ExprKind :: Match ( P ( scrutinee) , vec ! [ then_arm, else_arm] . into ( ) , desugar)
324
316
}
@@ -345,8 +337,8 @@ impl LoweringContext<'_> {
345
337
// Handle then + scrutinee:
346
338
let then_blk = self . lower_block ( body, false ) ;
347
339
let then_expr = self . expr_block ( then_blk, ThinVec :: new ( ) ) ;
348
- let ( then_pats , scrutinee, desugar, source) = match cond. node {
349
- ExprKind :: Let ( ref pats , ref scrutinee) => {
340
+ let ( then_pat , scrutinee, desugar, source) = match cond. node {
341
+ ExprKind :: Let ( ref pat , ref scrutinee) => {
350
342
// to:
351
343
//
352
344
// [opt_ident]: loop {
@@ -356,9 +348,8 @@ impl LoweringContext<'_> {
356
348
// }
357
349
// }
358
350
let scrutinee = self . with_loop_condition_scope ( |t| t. lower_expr ( scrutinee) ) ;
359
- let pats = pats. iter ( ) . map ( |pat| self . lower_pat ( pat) ) . collect ( ) ;
360
- let desugar = hir:: MatchSource :: WhileLetDesugar ;
361
- ( pats, scrutinee, desugar, hir:: LoopSource :: WhileLet )
351
+ let pat = self . lower_pat_top_hack ( pat) ;
352
+ ( pat, scrutinee, hir:: MatchSource :: WhileLetDesugar , hir:: LoopSource :: WhileLet )
362
353
}
363
354
_ => {
364
355
// We desugar: `'label: while $cond $body` into:
@@ -383,14 +374,12 @@ impl LoweringContext<'_> {
383
374
// to preserve drop semantics since `while cond { ... }` does not
384
375
// let temporaries live outside of `cond`.
385
376
let cond = self . expr_drop_temps ( span_block, P ( cond) , ThinVec :: new ( ) ) ;
386
-
387
- let desugar = hir:: MatchSource :: WhileDesugar ;
388
377
// `true => <then>`:
389
- let pats = hir_vec ! [ self . pat_bool( span, true ) ] ;
390
- ( pats , cond, desugar , hir:: LoopSource :: While )
378
+ let pat = self . pat_bool ( span, true ) ;
379
+ ( hir_vec ! [ pat ] , cond, hir :: MatchSource :: WhileDesugar , hir:: LoopSource :: While )
391
380
}
392
381
} ;
393
- let then_arm = self . arm ( then_pats , P ( then_expr) ) ;
382
+ let then_arm = self . arm ( then_pat , P ( then_expr) ) ;
394
383
395
384
// `match <scrutinee> { ... }`
396
385
let match_expr = self . expr_match (
@@ -440,7 +429,7 @@ impl LoweringContext<'_> {
440
429
hir:: Arm {
441
430
hir_id : self . next_id ( ) ,
442
431
attrs : self . lower_attrs ( & arm. attrs ) ,
443
- pats : arm . pats . iter ( ) . map ( |x| self . lower_pat ( x ) ) . collect ( ) ,
432
+ pats : self . lower_pat_top_hack ( & arm . pat ) ,
444
433
guard : match arm. guard {
445
434
Some ( ref x) => Some ( hir:: Guard :: If ( P ( self . lower_expr ( x) ) ) ) ,
446
435
_ => None ,
@@ -450,6 +439,16 @@ impl LoweringContext<'_> {
450
439
}
451
440
}
452
441
442
+ /// HACK(or_patterns; Centril | dlrobertson): For now we don't push down top level or-patterns
443
+ /// `p | q` into `hir::PatKind::Or(...)` as post-lowering bits of the compiler are not ready
444
+ /// to deal with it. This should by fixed by pushing it down to HIR and then HAIR.
445
+ fn lower_pat_top_hack ( & mut self , pat : & Pat ) -> HirVec < P < hir:: Pat > > {
446
+ match pat. node {
447
+ PatKind :: Or ( ref ps) => ps. iter ( ) . map ( |x| self . lower_pat ( x) ) . collect ( ) ,
448
+ _ => hir_vec ! [ self . lower_pat( pat) ] ,
449
+ }
450
+ }
451
+
453
452
pub ( super ) fn make_async_expr (
454
453
& mut self ,
455
454
capture_clause : CaptureBy ,
@@ -1255,7 +1254,6 @@ impl LoweringContext<'_> {
1255
1254
ThinVec :: from ( attrs. clone ( ) ) ,
1256
1255
) ) ;
1257
1256
let ok_pat = self . pat_ok ( span, val_pat) ;
1258
-
1259
1257
self . arm ( hir_vec ! [ ok_pat] , val_expr)
1260
1258
} ;
1261
1259
@@ -1486,7 +1484,10 @@ impl LoweringContext<'_> {
1486
1484
}
1487
1485
}
1488
1486
1489
- fn arm ( & mut self , pats : hir:: HirVec < P < hir:: Pat > > , expr : P < hir:: Expr > ) -> hir:: Arm {
1487
+ /// HACK(or_patterns; Centril | dlrobertson): For now we don't push down top level or-patterns
1488
+ /// `p | q` into `hir::PatKind::Or(...)` as post-lowering bits of the compiler are not ready
1489
+ /// to deal with it. This should by fixed by pushing it down to HIR and then HAIR.
1490
+ fn arm ( & mut self , pats : HirVec < P < hir:: Pat > > , expr : P < hir:: Expr > ) -> hir:: Arm {
1490
1491
hir:: Arm {
1491
1492
hir_id : self . next_id ( ) ,
1492
1493
attrs : hir_vec ! [ ] ,
0 commit comments