]> git.lizzy.rs Git - rust.git/commitdiff
lowering: move make_async_expr -> expr.rs
authorMazdak Farrokhzad <twingoow@gmail.com>
Sat, 10 Aug 2019 14:42:14 +0000 (16:42 +0200)
committerMazdak Farrokhzad <twingoow@gmail.com>
Sat, 10 Aug 2019 18:24:43 +0000 (20:24 +0200)
src/librustc/hir/lowering.rs
src/librustc/hir/lowering/expr.rs

index db70d18bd832585ab51ea9b663ae2cd18915cb9a..12d3ebb3e0f1f2eb2e81cc95d13a2a1dc4c8d673 100644 (file)
@@ -1151,47 +1151,6 @@ fn with_catch_scope<T, F>(&mut self, catch_id: NodeId, f: F) -> T
         result
     }
 
-    fn make_async_expr(
-        &mut self,
-        capture_clause: CaptureBy,
-        closure_node_id: NodeId,
-        ret_ty: Option<AstP<Ty>>,
-        span: Span,
-        body: impl FnOnce(&mut LoweringContext<'_>) -> hir::Expr,
-    ) -> hir::ExprKind {
-        let capture_clause = self.lower_capture_clause(capture_clause);
-        let output = match ret_ty {
-            Some(ty) => FunctionRetTy::Ty(ty),
-            None => FunctionRetTy::Default(span),
-        };
-        let ast_decl = FnDecl {
-            inputs: vec![],
-            output,
-            c_variadic: false
-        };
-        let decl = self.lower_fn_decl(&ast_decl, None, /* impl trait allowed */ false, None);
-        let body_id = self.lower_fn_body(&ast_decl, |this| {
-            this.generator_kind = Some(hir::GeneratorKind::Async);
-            body(this)
-        });
-        let generator = hir::Expr {
-            hir_id: self.lower_node_id(closure_node_id),
-            node: hir::ExprKind::Closure(capture_clause, decl, body_id, span,
-                Some(hir::GeneratorMovability::Static)),
-            span,
-            attrs: ThinVec::new(),
-        };
-
-        let unstable_span = self.mark_span_with_reason(
-            DesugaringKind::Async,
-            span,
-            self.allow_gen_future.clone(),
-        );
-        let gen_future = self.expr_std_path(
-            unstable_span, &[sym::future, sym::from_generator], None, ThinVec::new());
-        hir::ExprKind::Call(P(gen_future), hir_vec![generator])
-    }
-
     fn lower_body(
         &mut self,
         f: impl FnOnce(&mut LoweringContext<'_>) -> (HirVec<hir::Arg>, hir::Expr),
index 25a77f95a57931aa36a4d9e9f1e60b2961a4a43a..2ff67ac877c936053b079f16b81f805ccc733b5e 100644 (file)
@@ -416,6 +416,62 @@ fn wrap_in_try_constructor(
         P(self.expr_call(e.span, from_err, hir_vec![e]))
     }
 
+    pub(super) fn make_async_expr(
+        &mut self,
+        capture_clause: CaptureBy,
+        closure_node_id: NodeId,
+        ret_ty: Option<AstP<Ty>>,
+        span: Span,
+        body: impl FnOnce(&mut LoweringContext<'_>) -> hir::Expr,
+    ) -> hir::ExprKind {
+        let capture_clause = self.lower_capture_clause(capture_clause);
+        let output = match ret_ty {
+            Some(ty) => FunctionRetTy::Ty(ty),
+            None => FunctionRetTy::Default(span),
+        };
+        let ast_decl = FnDecl {
+            inputs: vec![],
+            output,
+            c_variadic: false
+        };
+        let decl = self.lower_fn_decl(&ast_decl, None, /* impl trait allowed */ false, None);
+        let body_id = self.lower_fn_body(&ast_decl, |this| {
+            this.generator_kind = Some(hir::GeneratorKind::Async);
+            body(this)
+        });
+
+        // `static || -> <ret_ty> { body }`:
+        let generator_node = hir::ExprKind::Closure(
+            capture_clause,
+            decl,
+            body_id,
+            span,
+            Some(hir::GeneratorMovability::Static)
+        );
+        let generator = hir::Expr {
+            hir_id: self.lower_node_id(closure_node_id),
+            node: generator_node,
+            span,
+            attrs: ThinVec::new(),
+        };
+
+        // `future::from_generator`:
+        let unstable_span = self.mark_span_with_reason(
+            DesugaringKind::Async,
+            span,
+            self.allow_gen_future.clone(),
+        );
+        let gen_future = self.expr_std_path(
+            unstable_span,
+            &[sym::future, sym::from_generator],
+            None,
+            ThinVec::new()
+        );
+
+        // `future::from_generator(generator)`:
+        hir::ExprKind::Call(P(gen_future), hir_vec![generator])
+    }
+
     /// Desugar `<expr>.await` into:
     /// ```rust
     /// {