From 3e2929d3623dd4c4d601a31e55e9d36dba7afe88 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 19 Nov 2014 11:18:17 -0500 Subject: [PATCH] Merge the ExprFnBlock and ExprUnboxedClosure into one ExprClosure with an optional unboxed closure kind. --- src/librustc/middle/borrowck/mod.rs | 9 ++-- src/librustc/middle/cfg/construct.rs | 3 +- src/librustc/middle/check_loop.rs | 5 +- src/librustc/middle/expr_use_visitor.rs | 3 +- src/librustc/middle/liveness.rs | 11 ++-- src/librustc/middle/mem_categorization.rs | 7 ++- src/librustc/middle/resolve.rs | 13 ++--- src/librustc/middle/ty.rs | 3 +- src/librustc/middle/typeck/astconv.rs | 2 +- src/librustc/middle/typeck/check/closure.rs | 50 ++++++++++++++----- src/librustc/middle/typeck/check/mod.rs | 37 ++++---------- src/librustc/middle/typeck/check/regionck.rs | 3 +- src/librustc/middle/typeck/check/writeback.rs | 5 +- src/librustc_back/svh.rs | 6 +-- src/librustc_trans/save/mod.rs | 2 +- src/librustc_trans/trans/base.rs | 5 +- src/librustc_trans/trans/debuginfo.rs | 8 ++- src/librustc_trans/trans/expr.rs | 26 ++++++---- src/libsyntax/ast.rs | 3 +- src/libsyntax/ast_map/blocks.rs | 6 +-- src/libsyntax/ext/build.rs | 4 +- src/libsyntax/ext/expand.rs | 7 +-- src/libsyntax/feature_gate.rs | 2 +- src/libsyntax/fold.rs | 11 ++-- src/libsyntax/parse/parser.rs | 26 +++------- src/libsyntax/print/pprust.rs | 47 ++--------------- src/libsyntax/visit.rs | 9 +--- 27 files changed, 126 insertions(+), 187 deletions(-) diff --git a/src/librustc/middle/borrowck/mod.rs b/src/librustc/middle/borrowck/mod.rs index fbb0a87cf3f8..d0118514d431 100644 --- a/src/librustc/middle/borrowck/mod.rs +++ b/src/librustc/middle/borrowck/mod.rs @@ -288,9 +288,12 @@ pub fn closure_to_block(closure_id: ast::NodeId, match tcx.map.get(closure_id) { ast_map::NodeExpr(expr) => match expr.node { ast::ExprProc(_, ref block) | - ast::ExprFnBlock(_, _, ref block) | - ast::ExprUnboxedFn(_, _, _, ref block) => { block.id } - _ => panic!("encountered non-closure id: {}", closure_id) + ast::ExprClosure(_, _, _, ref block) => { + block.id + } + _ => { + panic!("encountered non-closure id: {}", closure_id) + } }, _ => panic!("encountered non-expr id: {}", closure_id) } diff --git a/src/librustc/middle/cfg/construct.rs b/src/librustc/middle/cfg/construct.rs index e84e70e2cd99..04c8a4fe5941 100644 --- a/src/librustc/middle/cfg/construct.rs +++ b/src/librustc/middle/cfg/construct.rs @@ -496,9 +496,8 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> { } ast::ExprMac(..) | - ast::ExprFnBlock(..) | + ast::ExprClosure(..) | ast::ExprProc(..) | - ast::ExprUnboxedFn(..) | ast::ExprLit(..) | ast::ExprPath(..) => { self.straightline(expr, pred, None::.iter()) diff --git a/src/librustc/middle/check_loop.rs b/src/librustc/middle/check_loop.rs index 01ec9bbc13db..36742df98503 100644 --- a/src/librustc/middle/check_loop.rs +++ b/src/librustc/middle/check_loop.rs @@ -48,9 +48,8 @@ impl<'a, 'v> Visitor<'v> for CheckLoopVisitor<'a> { self.visit_expr(&**e); self.with_context(Loop, |v| v.visit_block(&**b)); } - ast::ExprFnBlock(_, _, ref b) | - ast::ExprProc(_, ref b) | - ast::ExprUnboxedFn(_, _, _, ref b) => { + ast::ExprClosure(_, _, _, ref b) | + ast::ExprProc(_, ref b) => { self.with_context(Closure, |v| v.visit_block(&**b)); } ast::ExprBreak(_) => self.require_loop("break", e.span), diff --git a/src/librustc/middle/expr_use_visitor.rs b/src/librustc/middle/expr_use_visitor.rs index f4c22c571639..08605f76e024 100644 --- a/src/librustc/middle/expr_use_visitor.rs +++ b/src/librustc/middle/expr_use_visitor.rs @@ -496,8 +496,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> { self.consume_expr(&**count); } - ast::ExprFnBlock(..) | - ast::ExprUnboxedFn(..) | + ast::ExprClosure(..) | ast::ExprProc(..) => { self.walk_captures(expr) } diff --git a/src/librustc/middle/liveness.rs b/src/librustc/middle/liveness.rs index 82edfdf146ea..7d13d2e5f943 100644 --- a/src/librustc/middle/liveness.rs +++ b/src/librustc/middle/liveness.rs @@ -458,7 +458,7 @@ fn visit_expr(ir: &mut IrMaps, expr: &Expr) { } visit::walk_expr(ir, expr); } - ast::ExprFnBlock(..) | ast::ExprProc(..) | ast::ExprUnboxedFn(..) => { + ast::ExprClosure(..) | ast::ExprProc(..) => { // Interesting control flow (for loops can contain labeled // breaks or continues) ir.add_live_node_for_node(expr.id, ExprNode(expr.span)); @@ -975,10 +975,9 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> { self.propagate_through_expr(&**e, succ) } - ast::ExprFnBlock(_, _, ref blk) | - ast::ExprProc(_, ref blk) | - ast::ExprUnboxedFn(_, _, _, ref blk) => { - debug!("{} is an ExprFnBlock, ExprProc, or ExprUnboxedFn", + ast::ExprClosure(_, _, _, ref blk) | + ast::ExprProc(_, ref blk) => { + debug!("{} is an ExprClosure or ExprProc", expr_to_string(expr)); /* @@ -1495,7 +1494,7 @@ fn check_expr(this: &mut Liveness, expr: &Expr) { ast::ExprBreak(..) | ast::ExprAgain(..) | ast::ExprLit(_) | ast::ExprBlock(..) | ast::ExprMac(..) | ast::ExprAddrOf(..) | ast::ExprStruct(..) | ast::ExprRepeat(..) | ast::ExprParen(..) | - ast::ExprFnBlock(..) | ast::ExprProc(..) | ast::ExprUnboxedFn(..) | + ast::ExprClosure(..) | ast::ExprProc(..) | ast::ExprPath(..) | ast::ExprBox(..) | ast::ExprSlice(..) => { visit::walk_expr(this, expr); } diff --git a/src/librustc/middle/mem_categorization.rs b/src/librustc/middle/mem_categorization.rs index 93c2e8f0d993..3166f6b1495e 100644 --- a/src/librustc/middle/mem_categorization.rs +++ b/src/librustc/middle/mem_categorization.rs @@ -520,8 +520,8 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> { ast::ExprAddrOf(..) | ast::ExprCall(..) | ast::ExprAssign(..) | ast::ExprAssignOp(..) | - ast::ExprFnBlock(..) | ast::ExprProc(..) | - ast::ExprUnboxedFn(..) | ast::ExprRet(..) | + ast::ExprClosure(..) | ast::ExprProc(..) | + ast::ExprRet(..) | ast::ExprUnary(..) | ast::ExprSlice(..) | ast::ExprMethodCall(..) | ast::ExprCast(..) | ast::ExprVec(..) | ast::ExprTup(..) | ast::ExprIf(..) | @@ -693,9 +693,8 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> { }; match fn_expr.node { - ast::ExprFnBlock(_, _, ref body) | ast::ExprProc(_, ref body) | - ast::ExprUnboxedFn(_, _, _, ref body) => body.id, + ast::ExprClosure(_, _, _, ref body) => body.id, _ => unreachable!() } }; diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index baf53cc34ba9..302dcf04e12b 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -50,8 +50,8 @@ use util::nodemap::{NodeMap, NodeSet, DefIdSet, FnvHashMap}; use syntax::ast::{Arm, BindByRef, BindByValue, BindingMode, Block, Crate, CrateNum}; use syntax::ast::{DeclItem, DefId, Expr, ExprAgain, ExprBreak, ExprField}; -use syntax::ast::{ExprFnBlock, ExprForLoop, ExprLoop, ExprWhile, ExprMethodCall}; -use syntax::ast::{ExprPath, ExprProc, ExprStruct, ExprUnboxedFn, FnDecl}; +use syntax::ast::{ExprClosure, ExprForLoop, ExprLoop, ExprWhile, ExprMethodCall}; +use syntax::ast::{ExprPath, ExprProc, ExprStruct, FnDecl}; use syntax::ast::{ForeignItem, ForeignItemFn, ForeignItemStatic, Generics}; use syntax::ast::{Ident, ImplItem, Item, ItemEnum, ItemFn, ItemForeignMod}; use syntax::ast::{ItemImpl, ItemMac, ItemMod, ItemStatic, ItemStruct}; @@ -5903,24 +5903,19 @@ impl<'a> Resolver<'a> { visit::walk_expr(self, expr); } - ExprFnBlock(capture_clause, ref fn_decl, ref block) => { + ExprClosure(capture_clause, _, ref fn_decl, ref block) => { self.capture_mode_map.insert(expr.id, capture_clause); self.resolve_function(ClosureRibKind(expr.id, ast::DUMMY_NODE_ID), Some(&**fn_decl), NoTypeParameters, &**block); } + ExprProc(ref fn_decl, ref block) => { self.capture_mode_map.insert(expr.id, ast::CaptureByValue); self.resolve_function(ClosureRibKind(expr.id, block.id), Some(&**fn_decl), NoTypeParameters, &**block); } - ExprUnboxedFn(capture_clause, _, ref fn_decl, ref block) => { - self.capture_mode_map.insert(expr.id, capture_clause); - self.resolve_function(ClosureRibKind(expr.id, block.id), - Some(&**fn_decl), NoTypeParameters, - &**block); - } ExprStruct(ref path, _, _) => { // Resolve the path to the structure it goes to. We don't diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index bf4260c46314..d3b7e69b8544 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -3922,9 +3922,8 @@ pub fn expr_kind(tcx: &ctxt, expr: &ast::Expr) -> ExprKind { ast::ExprTup(..) | ast::ExprIf(..) | ast::ExprMatch(..) | - ast::ExprFnBlock(..) | + ast::ExprClosure(..) | ast::ExprProc(..) | - ast::ExprUnboxedFn(..) | ast::ExprBlock(..) | ast::ExprRepeat(..) | ast::ExprVec(..) => { diff --git a/src/librustc/middle/typeck/astconv.rs b/src/librustc/middle/typeck/astconv.rs index ea652bc7e659..5fff355bafae 100644 --- a/src/librustc/middle/typeck/astconv.rs +++ b/src/librustc/middle/typeck/astconv.rs @@ -1047,7 +1047,7 @@ pub fn ast_ty_to_ty<'tcx, AC: AstConv<'tcx>, RS: RegionScope>( } ast::TyInfer => { // TyInfer also appears as the type of arguments or return - // values in a ExprFnBlock, ExprProc, or ExprUnboxedFn, or as + // values in a ExprClosure or ExprProc, or as // the type of local variables. Both of these cases are // handled specially and will not descend into this routine. this.ty_infer(ast_ty.span) diff --git a/src/librustc/middle/typeck/check/closure.rs b/src/librustc/middle/typeck/check/closure.rs index 6e6e2f1eee53..220c2f70d17a 100644 --- a/src/librustc/middle/typeck/check/closure.rs +++ b/src/librustc/middle/typeck/check/closure.rs @@ -26,12 +26,38 @@ use syntax::ast; use syntax::ast_util; use util::ppaux::Repr; -pub fn check_unboxed_closure<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>, - expr: &ast::Expr, - kind: ast::UnboxedClosureKind, - decl: &ast::FnDecl, - body: &ast::Block, - expected: Expectation<'tcx>) { +pub fn check_expr_closure<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>, + expr: &ast::Expr, + opt_kind: Option, + decl: &ast::FnDecl, + body: &ast::Block, + expected: Expectation<'tcx>) { + match opt_kind { + None => { // old-school boxed closure + let region = astconv::opt_ast_region_to_region(fcx, + fcx.infcx(), + expr.span, + &None); + check_boxed_closure(fcx, + expr, + ty::RegionTraitStore(region, ast::MutMutable), + decl, + body, + expected); + } + + Some(kind) => { + check_unboxed_closure(fcx, expr, kind, decl, body, expected) + } + } +} + +fn check_unboxed_closure<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>, + expr: &ast::Expr, + kind: ast::UnboxedClosureKind, + decl: &ast::FnDecl, + body: &ast::Block, + expected: Expectation<'tcx>) { let expr_def_id = ast_util::local_def(expr.id); let expected_sig_and_kind = match expected.resolve(fcx) { @@ -215,12 +241,12 @@ fn deduce_unboxed_closure_expectations_from_obligations<'a,'tcx>( } -pub fn check_expr_fn<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>, - expr: &ast::Expr, - store: ty::TraitStore, - decl: &ast::FnDecl, - body: &ast::Block, - expected: Expectation<'tcx>) { +pub fn check_boxed_closure<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>, + expr: &ast::Expr, + store: ty::TraitStore, + decl: &ast::FnDecl, + body: &ast::Block, + expected: Expectation<'tcx>) { let tcx = fcx.ccx.tcx; // Find the expected input/output types (if any). Substitute diff --git a/src/librustc/middle/typeck/check/mod.rs b/src/librustc/middle/typeck/check/mod.rs index 28648d6d4e22..65424567be4e 100644 --- a/src/librustc/middle/typeck/check/mod.rs +++ b/src/librustc/middle/typeck/check/mod.rs @@ -2823,9 +2823,7 @@ fn check_argument_types<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, }; for (i, arg) in args.iter().take(t).enumerate() { let is_block = match arg.node { - ast::ExprFnBlock(..) | - ast::ExprProc(..) | - ast::ExprUnboxedFn(..) => true, + ast::ExprClosure(..) | ast::ExprProc(..) => true, _ => false }; @@ -4148,33 +4146,16 @@ fn check_expr_with_unifier<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, ast::ExprMatch(ref discrim, ref arms, _) => { _match::check_match(fcx, expr, &**discrim, arms.as_slice()); } - ast::ExprFnBlock(_, ref decl, ref body) => { - let region = astconv::opt_ast_region_to_region(fcx, - fcx.infcx(), - expr.span, - &None); - closure::check_expr_fn(fcx, - expr, - ty::RegionTraitStore(region, ast::MutMutable), - &**decl, - &**body, - expected); - } - ast::ExprUnboxedFn(_, kind, ref decl, ref body) => { - closure::check_unboxed_closure(fcx, - expr, - kind, - &**decl, - &**body, - expected); + ast::ExprClosure(_, opt_kind, ref decl, ref body) => { + closure::check_expr_closure(fcx, expr, opt_kind, &**decl, &**body, expected); } ast::ExprProc(ref decl, ref body) => { - closure::check_expr_fn(fcx, - expr, - ty::UniqTraitStore, - &**decl, - &**body, - expected); + closure::check_boxed_closure(fcx, + expr, + ty::UniqTraitStore, + &**decl, + &**body, + expected); } ast::ExprBlock(ref b) => { check_block_with_expected(fcx, &**b, expected); diff --git a/src/librustc/middle/typeck/check/regionck.rs b/src/librustc/middle/typeck/check/regionck.rs index 44c11318038d..0a5af8baeaca 100644 --- a/src/librustc/middle/typeck/check/regionck.rs +++ b/src/librustc/middle/typeck/check/regionck.rs @@ -742,9 +742,8 @@ fn visit_expr(rcx: &mut Rcx, expr: &ast::Expr) { visit::walk_expr(rcx, expr); } - ast::ExprFnBlock(_, _, ref body) | ast::ExprProc(_, ref body) | - ast::ExprUnboxedFn(_, _, _, ref body) => { + ast::ExprClosure(_, _, _, ref body) => { check_expr_fn_block(rcx, expr, &**body); } diff --git a/src/librustc/middle/typeck/check/writeback.rs b/src/librustc/middle/typeck/check/writeback.rs index 76d9ed15e518..23af30b44d93 100644 --- a/src/librustc/middle/typeck/check/writeback.rs +++ b/src/librustc/middle/typeck/check/writeback.rs @@ -122,9 +122,8 @@ impl<'cx, 'tcx, 'v> Visitor<'v> for WritebackCx<'cx, 'tcx> { MethodCall::expr(e.id)); match e.node { - ast::ExprFnBlock(_, ref decl, _) | - ast::ExprProc(ref decl, _) | - ast::ExprUnboxedFn(_, _, ref decl, _) => { + ast::ExprClosure(_, _, ref decl, _) | + ast::ExprProc(ref decl, _) => { for input in decl.inputs.iter() { let _ = self.visit_node_id(ResolvingExpr(e.span), input.id); diff --git a/src/librustc_back/svh.rs b/src/librustc_back/svh.rs index c7a7888c1cd6..4f3daa91bbcf 100644 --- a/src/librustc_back/svh.rs +++ b/src/librustc_back/svh.rs @@ -241,8 +241,7 @@ mod svh_visitor { SawExprIf, SawExprWhile, SawExprMatch, - SawExprFnBlock, - SawExprUnboxedFn, + SawExprClosure, SawExprProc, SawExprBlock, SawExprAssign, @@ -274,8 +273,7 @@ mod svh_visitor { ExprWhile(..) => SawExprWhile, ExprLoop(_, id) => SawExprLoop(id.map(content)), ExprMatch(..) => SawExprMatch, - ExprFnBlock(..) => SawExprFnBlock, - ExprUnboxedFn(..) => SawExprUnboxedFn, + ExprClosure(..) => SawExprClosure, ExprProc(..) => SawExprProc, ExprBlock(..) => SawExprBlock, ExprAssign(..) => SawExprAssign, diff --git a/src/librustc_trans/save/mod.rs b/src/librustc_trans/save/mod.rs index 2591051e9b90..67ed95f83fd3 100644 --- a/src/librustc_trans/save/mod.rs +++ b/src/librustc_trans/save/mod.rs @@ -1345,7 +1345,7 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> { "Expected struct type, but not ty_struct"), } }, - ast::ExprFnBlock(_, ref decl, ref body) => { + ast::ExprClosure(_, _, ref decl, ref body) => { if generated_code(body.span) { return } diff --git a/src/librustc_trans/trans/base.rs b/src/librustc_trans/trans/base.rs index b3f64e3fd798..30cdda9366b6 100644 --- a/src/librustc_trans/trans/base.rs +++ b/src/librustc_trans/trans/base.rs @@ -1385,9 +1385,8 @@ fn has_nested_returns(tcx: &ty::ctxt, id: ast::NodeId) -> bool { } Some(ast_map::NodeExpr(e)) => { match e.node { - ast::ExprFnBlock(_, _, ref blk) | - ast::ExprProc(_, ref blk) | - ast::ExprUnboxedFn(_, _, _, ref blk) => { + ast::ExprClosure(_, _, _, ref blk) | + ast::ExprProc(_, ref blk) => { let mut explicit = CheckForNestedReturnsVisitor::explicit(); let mut implicit = CheckForNestedReturnsVisitor::implicit(); visit::walk_expr(&mut explicit, e); diff --git a/src/librustc_trans/trans/debuginfo.rs b/src/librustc_trans/trans/debuginfo.rs index 7b3f619f41f1..075b6b0dd6e0 100644 --- a/src/librustc_trans/trans/debuginfo.rs +++ b/src/librustc_trans/trans/debuginfo.rs @@ -1232,9 +1232,8 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, } ast_map::NodeExpr(ref expr) => { match expr.node { - ast::ExprFnBlock(_, ref fn_decl, ref top_level_block) | ast::ExprProc(ref fn_decl, ref top_level_block) | - ast::ExprUnboxedFn(_, _, ref fn_decl, ref top_level_block) => { + ast::ExprClosure(_, _, ref fn_decl, ref top_level_block) => { let name = format!("fn{}", token::gensym("fn")); let name = token::str_to_ident(name.as_slice()); (name, &**fn_decl, @@ -1310,7 +1309,7 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, file_metadata, &mut function_name); - // There is no ast_map::Path for ast::ExprFnBlock-type functions. For now, + // There is no ast_map::Path for ast::ExprClosure-type functions. For now, // just don't put them into a namespace. In the future this could be improved // somehow (storing a path in the ast_map, or construct a path using the // enclosing function). @@ -3578,9 +3577,8 @@ fn populate_scope_map(cx: &CrateContext, }) } - ast::ExprFnBlock(_, ref decl, ref block) | ast::ExprProc(ref decl, ref block) | - ast::ExprUnboxedFn(_, _, ref decl, ref block) => { + ast::ExprClosure(_, _, ref decl, ref block) => { with_new_scope(cx, block.span, scope_stack, diff --git a/src/librustc_trans/trans/expr.rs b/src/librustc_trans/trans/expr.rs index 8ccc59831993..c095b0e8c8ed 100644 --- a/src/librustc_trans/trans/expr.rs +++ b/src/librustc_trans/trans/expr.rs @@ -77,6 +77,7 @@ use trans::machine::{llsize_of, llsize_of_alloc}; use trans::type_::Type; use syntax::ast; +use syntax::ast_util; use syntax::codemap; use syntax::print::pprust::{expr_to_string}; use syntax::ptr::P; @@ -1059,16 +1060,23 @@ fn trans_rvalue_dps_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, ast::ExprVec(..) | ast::ExprRepeat(..) => { tvec::trans_fixed_vstore(bcx, expr, dest) } - ast::ExprFnBlock(_, ref decl, ref body) | + ast::ExprClosure(_, _, ref decl, ref body) | ast::ExprProc(ref decl, ref body) => { - let expr_ty = expr_ty(bcx, expr); - let store = ty::ty_closure_store(expr_ty); - debug!("translating block function {} with type {}", - expr_to_string(expr), expr_ty.repr(tcx)); - closure::trans_expr_fn(bcx, store, &**decl, &**body, expr.id, dest) - } - ast::ExprUnboxedFn(_, _, ref decl, ref body) => { - closure::trans_unboxed_closure(bcx, &**decl, &**body, expr.id, dest) + // Check the side-table to see whether this is an unboxed + // closure or an older, legacy style closure. Store this + // into a variable to ensure the the RefCell-lock is + // released before we recurse. + let is_unboxed_closure = + bcx.tcx().unboxed_closures.borrow().contains_key(&ast_util::local_def(expr.id)); + if is_unboxed_closure { + closure::trans_unboxed_closure(bcx, &**decl, &**body, expr.id, dest) + } else { + let expr_ty = expr_ty(bcx, expr); + let store = ty::ty_closure_store(expr_ty); + debug!("translating block function {} with type {}", + expr_to_string(expr), expr_ty.repr(tcx)); + closure::trans_expr_fn(bcx, store, &**decl, &**body, expr.id, dest) + } } ast::ExprCall(ref f, ref args) => { if bcx.tcx().is_method_call(expr.id) { diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 15e14902727f..f6f117c9a970 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -667,9 +667,8 @@ pub enum Expr_ { // FIXME #6993: change to Option ... or not, if these are hygienic. ExprLoop(P, Option), ExprMatch(P, Vec, MatchSource), - ExprFnBlock(CaptureClause, P, P), + ExprClosure(CaptureClause, Option, P, P), ExprProc(P, P), - ExprUnboxedFn(CaptureClause, UnboxedClosureKind, P, P), ExprBlock(P), ExprAssign(P, P), diff --git a/src/libsyntax/ast_map/blocks.rs b/src/libsyntax/ast_map/blocks.rs index a35ee3ab1d0d..8db12fbd8355 100644 --- a/src/libsyntax/ast_map/blocks.rs +++ b/src/libsyntax/ast_map/blocks.rs @@ -37,7 +37,7 @@ use visit; /// /// More specifically, it is one of either: /// - A function item, -/// - A closure expr (i.e. an ExprFnBlock or ExprProc), or +/// - A closure expr (i.e. an ExprClosure or ExprProc), or /// - The default implementation for a trait method. /// /// To construct one, use the `Code::from_node` function. @@ -71,7 +71,7 @@ impl MaybeFnLike for ast::TraitItem { impl MaybeFnLike for ast::Expr { fn is_fn_like(&self) -> bool { match self.node { - ast::ExprFnBlock(..) | ast::ExprProc(..) => true, + ast::ExprClosure(..) | ast::ExprProc(..) => true, _ => false, } } @@ -215,7 +215,7 @@ impl<'a> FnLikeNode<'a> { } } ast_map::NodeExpr(e) => match e.node { - ast::ExprFnBlock(_, ref decl, ref block) => + ast::ExprClosure(_, _, ref decl, ref block) => closure(ClosureParts::new(&**decl, &**block, e.id, e.span)), ast::ExprProc(ref decl, ref block) => closure(ClosureParts::new(&**decl, &**block, e.id, e.span)), diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index ffc42b670338..b18a0c8411cb 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -864,14 +864,14 @@ impl<'a> AstBuilder for ExtCtxt<'a> { fn lambda_fn_decl(&self, span: Span, fn_decl: P, blk: P) -> P { - self.expr(span, ast::ExprFnBlock(ast::CaptureByRef, fn_decl, blk)) + self.expr(span, ast::ExprClosure(ast::CaptureByRef, None, fn_decl, blk)) } fn lambda(&self, span: Span, ids: Vec, blk: P) -> P { let fn_decl = self.fn_decl( ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(), self.ty_infer(span)); - self.expr(span, ast::ExprFnBlock(ast::CaptureByRef, fn_decl, blk)) + self.expr(span, ast::ExprClosure(ast::CaptureByRef, None, fn_decl, blk)) } fn lambda0(&self, span: Span, blk: P) -> P { self.lambda(span, Vec::new(), blk) diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 081456bebe19..04132679a037 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -207,10 +207,11 @@ pub fn expand_expr(e: P, fld: &mut MacroExpander) -> P { fld.cx.expr(span, ast::ExprForLoop(pat, head, body, opt_ident)) } - ast::ExprFnBlock(capture_clause, fn_decl, block) => { + ast::ExprClosure(capture_clause, opt_kind, fn_decl, block) => { let (rewritten_fn_decl, rewritten_block) = expand_and_rename_fn_decl_and_block(fn_decl, block, fld); - let new_node = ast::ExprFnBlock(capture_clause, + let new_node = ast::ExprClosure(capture_clause, + opt_kind, rewritten_fn_decl, rewritten_block); P(ast::Expr{id:id, node: new_node, span: fld.new_span(span)}) @@ -1555,7 +1556,7 @@ mod test { 0) } - // closure arg hygiene (ExprFnBlock) + // closure arg hygiene (ExprClosure) // expands to fn f(){(|x_1 : int| {(x_2 + x_1)})(3);} #[test] fn closure_arg_hygiene(){ run_renaming_test( diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index ebdcf2789340..8a9b31032244 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -309,7 +309,7 @@ impl<'a, 'v> Visitor<'v> for Context<'a> { fn visit_expr(&mut self, e: &ast::Expr) { match e.node { - ast::ExprUnboxedFn(..) => { + ast::ExprClosure(_, Some(_), _, _) => { self.gate_feature("unboxed_closures", e.span, "unboxed closures are a work-in-progress \ diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index b3137ff5f7e5..7f8d33edc3c3 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -1316,18 +1316,13 @@ pub fn noop_fold_expr(Expr {id, node, span}: Expr, folder: &mut T) -> arms.move_map(|x| folder.fold_arm(x)), source) } - ExprFnBlock(capture_clause, decl, body) => { - ExprFnBlock(capture_clause, - folder.fold_fn_decl(decl), - folder.fold_block(body)) - } ExprProc(decl, body) => { ExprProc(folder.fold_fn_decl(decl), folder.fold_block(body)) } - ExprUnboxedFn(capture_clause, kind, decl, body) => { - ExprUnboxedFn(capture_clause, - kind, + ExprClosure(capture_clause, opt_kind, decl, body) => { + ExprClosure(capture_clause, + opt_kind, folder.fold_fn_decl(decl), folder.fold_block(body)) } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 50b1a2204b04..4557170d8b3c 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -25,10 +25,10 @@ use ast::{DeclLocal, DefaultBlock, UnDeref, BiDiv, EMPTY_CTXT, EnumDef, Explicit use ast::{Expr, Expr_, ExprAddrOf, ExprMatch, ExprAgain}; use ast::{ExprAssign, ExprAssignOp, ExprBinary, ExprBlock, ExprBox}; use ast::{ExprBreak, ExprCall, ExprCast}; -use ast::{ExprField, ExprTupField, ExprFnBlock, ExprIf, ExprIfLet, ExprIndex, ExprSlice}; +use ast::{ExprField, ExprTupField, ExprClosure, ExprIf, ExprIfLet, ExprIndex, ExprSlice}; use ast::{ExprLit, ExprLoop, ExprMac}; use ast::{ExprMethodCall, ExprParen, ExprPath, ExprProc}; -use ast::{ExprRepeat, ExprRet, ExprStruct, ExprTup, ExprUnary, ExprUnboxedFn}; +use ast::{ExprRepeat, ExprRet, ExprStruct, ExprTup, ExprUnary}; use ast::{ExprVec, ExprWhile, ExprWhileLet, ExprForLoop, Field, FnDecl}; use ast::{Once, Many}; use ast::{FnUnboxedClosureKind, FnMutUnboxedClosureKind}; @@ -2950,7 +2950,8 @@ impl<'a> Parser<'a> { // `|args| expr` pub fn parse_lambda_expr(&mut self, capture_clause: CaptureClause) - -> P { + -> P + { let lo = self.span.lo; let (decl, optional_unboxed_closure_kind) = self.parse_fn_block_decl(); @@ -2964,21 +2965,10 @@ impl<'a> Parser<'a> { rules: DefaultBlock, }); - match optional_unboxed_closure_kind { - Some(unboxed_closure_kind) => { - self.mk_expr(lo, - fakeblock.span.hi, - ExprUnboxedFn(capture_clause, - unboxed_closure_kind, - decl, - fakeblock)) - } - None => { - self.mk_expr(lo, - fakeblock.span.hi, - ExprFnBlock(capture_clause, decl, fakeblock)) - } - } + self.mk_expr( + lo, + fakeblock.span.hi, + ExprClosure(capture_clause, optional_unboxed_closure_kind, decl, fakeblock)) } pub fn parse_else_expr(&mut self) -> P { diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index e6e0c33a42db..919edee13caf 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -434,9 +434,8 @@ pub fn visibility_qualified(vis: ast::Visibility, s: &str) -> String { fn needs_parentheses(expr: &ast::Expr) -> bool { match expr.node { ast::ExprAssign(..) | ast::ExprBinary(..) | - ast::ExprFnBlock(..) | ast::ExprProc(..) | - ast::ExprUnboxedFn(..) | ast::ExprAssignOp(..) | - ast::ExprCast(..) => true, + ast::ExprClosure(..) | ast::ExprProc(..) | + ast::ExprAssignOp(..) | ast::ExprCast(..) => true, _ => false, } } @@ -1652,49 +1651,11 @@ impl<'a> State<'a> { } try!(self.bclose_(expr.span, indent_unit)); } - ast::ExprFnBlock(capture_clause, ref decl, ref body) => { + ast::ExprClosure(capture_clause, opt_kind, ref decl, ref body) => { try!(self.print_capture_clause(capture_clause)); - // in do/for blocks we don't want to show an empty - // argument list, but at this point we don't know which - // we are inside. - // - // if !decl.inputs.is_empty() { - try!(self.print_fn_block_args(&**decl, None)); + try!(self.print_fn_block_args(&**decl, opt_kind)); try!(space(&mut self.s)); - // } - - if !body.stmts.is_empty() || !body.expr.is_some() { - try!(self.print_block_unclosed(&**body)); - } else { - // we extract the block, so as not to create another set of boxes - match body.expr.as_ref().unwrap().node { - ast::ExprBlock(ref blk) => { - try!(self.print_block_unclosed(&**blk)); - } - _ => { - // this is a bare expression - try!(self.print_expr(&**body.expr.as_ref().unwrap())); - try!(self.end()); // need to close a box - } - } - } - // a box will be closed by print_expr, but we didn't want an overall - // wrapper so we closed the corresponding opening. so create an - // empty box to satisfy the close. - try!(self.ibox(0)); - } - ast::ExprUnboxedFn(capture_clause, kind, ref decl, ref body) => { - try!(self.print_capture_clause(capture_clause)); - - // in do/for blocks we don't want to show an empty - // argument list, but at this point we don't know which - // we are inside. - // - // if !decl.inputs.is_empty() { - try!(self.print_fn_block_args(&**decl, Some(kind))); - try!(space(&mut self.s)); - // } if !body.stmts.is_empty() || !body.expr.is_some() { try!(self.print_block_unclosed(&**body)); diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index efe1e18eda92..1ce1cbf4e59c 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -803,14 +803,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) { visitor.visit_arm(arm) } } - ExprFnBlock(_, ref function_declaration, ref body) => { - visitor.visit_fn(FkFnBlock, - &**function_declaration, - &**body, - expression.span, - expression.id) - } - ExprUnboxedFn(_, _, ref function_declaration, ref body) => { + ExprClosure(_, _, ref function_declaration, ref body) => { visitor.visit_fn(FkFnBlock, &**function_declaration, &**body,