]> git.lizzy.rs Git - rust.git/commitdiff
Fix an ICE with continue inside a closure inside a loop condition
authorvarkor <github@varkor.com>
Sat, 23 Jun 2018 12:12:15 +0000 (13:12 +0100)
committervarkor <github@varkor.com>
Sat, 23 Jun 2018 17:00:47 +0000 (18:00 +0100)
src/librustc/hir/lowering.rs
src/test/ui/closure-array-break-length.rs
src/test/ui/closure-array-break-length.stderr

index 02e9415fd8e3c401780c948b4ceaa7ae173a62b0..13e033e67fcbb4066b047e2aa04a4dc0116ffb2e 100644 (file)
@@ -3538,12 +3538,22 @@ fn lower_expr(&mut self, e: &Expr) -> hir::Expr {
                         this.expr_block(block, ThinVec::new())
                     })
                 })
-            },
+            }
             ExprKind::Closure(
-                capture_clause, asyncness, movability, ref decl, ref body, fn_decl_span) =>
-            {
-                self.with_new_scopes(|this| {
-                    if let IsAsync::Async(async_closure_node_id) = asyncness {
+                capture_clause, asyncness, movability, ref decl, ref body, fn_decl_span
+            ) => {
+                if let IsAsync::Async(async_closure_node_id) = asyncness {
+                    let outer_decl = FnDecl {
+                        inputs: decl.inputs.clone(),
+                        output: FunctionRetTy::Default(fn_decl_span),
+                        variadic: false,
+                    };
+                    // We need to lower the declaration outside the new scope, because we
+                    // have to conserve the state of being inside a loop condition for the
+                    // closure argument types.
+                    let fn_decl = self.lower_fn_decl(&outer_decl, None, false, false);
+
+                    self.with_new_scopes(|this| {
                         // FIXME(cramertj) allow `async` non-`move` closures with
                         if capture_clause == CaptureBy::Ref &&
                             !decl.inputs.is_empty()
@@ -3563,11 +3573,6 @@ fn lower_expr(&mut self, e: &Expr) -> hir::Expr {
 
                         // Transform `async |x: u8| -> X { ... }` into
                         // `|x: u8| future_from_generator(|| -> X { ... })`
-                        let outer_decl = FnDecl {
-                            inputs: decl.inputs.clone(),
-                            output: FunctionRetTy::Default(fn_decl_span),
-                            variadic: false,
-                        };
                         let body_id = this.lower_body(Some(&outer_decl), |this| {
                             let async_ret_ty = if let FunctionRetTy::Ty(ty) = &decl.output {
                                 Some(&**ty)
@@ -3581,12 +3586,17 @@ fn lower_expr(&mut self, e: &Expr) -> hir::Expr {
                         });
                         hir::ExprClosure(
                             this.lower_capture_clause(capture_clause),
-                            this.lower_fn_decl(&outer_decl, None, false, false),
+                            fn_decl,
                             body_id,
                             fn_decl_span,
                             None,
                         )
-                    } else {
+                    })
+                } else {
+                    // Lower outside new scope to preserve `is_in_loop_condition`.
+                    let fn_decl = self.lower_fn_decl(decl, None, false, false);
+
+                    self.with_new_scopes(|this| {
                         let mut is_generator = false;
                         let body_id = this.lower_body(Some(decl), |this| {
                             let e = this.lower_expr(body);
@@ -3620,13 +3630,13 @@ fn lower_expr(&mut self, e: &Expr) -> hir::Expr {
                         };
                         hir::ExprClosure(
                             this.lower_capture_clause(capture_clause),
-                            this.lower_fn_decl(decl, None, false, false),
+                            fn_decl,
                             body_id,
                             fn_decl_span,
                             generator_option,
                         )
-                    }
-                })
+                    })
+                }
             }
             ExprKind::Block(ref blk, opt_label) => {
                 hir::ExprBlock(self.lower_block(blk,
index 67feed38b6c3dab15727abe227f9923705ca4e37..4b0e941a3ce2aacef71cffd6b4fb5af2a41e322c 100644 (file)
@@ -10,4 +10,6 @@
 
 fn main() {
     |_: [_; continue]| {}; //~ ERROR: `continue` outside of loop
+
+    while |_: [_; continue]| {} {} //~ ERROR: `break` or `continue` with no label
 }
index a1e28e84ced663c655bf5614cda4a3cf64ea20ea..7cedcb254d697342a25d2d556cb63f87fe9219b0 100644 (file)
@@ -4,6 +4,13 @@ error[E0268]: `continue` outside of loop
 LL |     |_: [_; continue]| {}; //~ ERROR: `continue` outside of loop
    |             ^^^^^^^^ cannot break outside of a loop
 
-error: aborting due to previous error
+error[E0590]: `break` or `continue` with no label in the condition of a `while` loop
+  --> $DIR/closure-array-break-length.rs:14:19
+   |
+LL |     while |_: [_; continue]| {} {} //~ ERROR: `break` or `continue` with no label
+   |                   ^^^^^^^^ unlabeled `continue` in the condition of a `while` loop
+
+error: aborting due to 2 previous errors
 
-For more information about this error, try `rustc --explain E0268`.
+Some errors occurred: E0268, E0590.
+For more information about an error, try `rustc --explain E0268`.