@@ -310,7 +310,7 @@ impl<'cx, 'a: 'cx> Context<'cx, 'a> {
310
310
CondExpr :: literal ( lit. node . clone ( ) ) ,
311
311
escape_format_string ( & unescape_printable_unicode ( & pprust:: expr_to_string ( & expr) ) ) ,
312
312
) ,
313
- // Otherwise capture and stop recursing.
313
+ // Otherwise capture it and stop recursing.
314
314
_ => self . scan_capture_expr ( P ( expr) , BindingMode :: ByValue ( Mutability :: Immutable ) ) ,
315
315
}
316
316
}
@@ -329,8 +329,8 @@ impl<'cx, 'a: 'cx> Context<'cx, 'a> {
329
329
expr_str : pprust:: expr_to_string ( & expr) ,
330
330
var : capture,
331
331
decl : if mode. is_by_ref ( ) {
332
- // `#[allow(unused)] let __capture{} = Unevaluated;`
333
- // Can be unused if the variable cannot be seen from any branch .
332
+ // Placeholder variable: `#[allow(unused)] let __capture{} = Unevaluated;`
333
+ // Can be unused if the variable is shadowed in all branches .
334
334
let allow_unused = {
335
335
let word = self . ecx
336
336
. meta_list_item_word ( self . sp , Symbol :: intern ( "unused" ) ) ;
@@ -399,16 +399,23 @@ impl<'cx, 'a: 'cx> Context<'cx, 'a> {
399
399
//
400
400
// ```rust
401
401
// // By-reference capture: eagerly evaluated
402
- // match &expr {
403
- // __captureN => if ... { action(__captureN) }
402
+ // match expr0 { ref __capture0 =>
403
+ // match expr1 { ref __capture1 =>
404
+ // if __capture0 && __capture1 { .. }
405
+ // // Both are evaluated even if `!expr0`
406
+ // }
404
407
// }
405
408
//
406
409
// // By-value capture: lazily evaluated
407
- // if ... { action({ let tmp = expr; ... }) }
410
+ // if { let tmp = expr0; ...; tmp }
411
+ // && { let tmp = expr1; ...; tmp } { .. }
412
+ // // `expr1` is evaluated only if `expr0` is true
408
413
// ```
409
414
410
415
match expr. node {
411
416
CondExprKind :: BinOp ( op, left, right) => {
417
+ // Evaluates `left` first and then `right`.
418
+
412
419
let left_by_ref = left. is_by_ref_capture ( ) ;
413
420
let right_by_ref = right. is_by_ref_capture ( ) ;
414
421
@@ -475,6 +482,7 @@ impl<'cx, 'a: 'cx> Context<'cx, 'a> {
475
482
}
476
483
CondExprKind :: Capture ( expr, idx, mode) => {
477
484
if mode. is_by_ref ( ) {
485
+ // Shadows the placeholder variable.
478
486
let ident = Ident :: from_str ( & format ! ( "__capture{}" , idx) ) ;
479
487
self . single_match (
480
488
expr,
@@ -575,6 +583,7 @@ impl<'cx, 'a: 'cx> Context<'cx, 'a> {
575
583
) )
576
584
}
577
585
586
+ /// Generates `match expr { __tmpN => f(__tmpN) }`.
578
587
fn tmp_match < F : FnOnce ( P < Expr > ) -> P < Expr > > (
579
588
& self ,
580
589
expr : P < Expr > ,
0 commit comments