mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-16 00:43:50 +00:00
Address @eddyb nit by simplifying ClosureEnv
.
This commit is contained in:
parent
a962bdb3da
commit
ced10626de
@ -1775,7 +1775,7 @@ pub fn trans_closure<'a, 'b, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|||||||
_attributes: &[ast::Attribute],
|
_attributes: &[ast::Attribute],
|
||||||
output_type: ty::FnOutput<'tcx>,
|
output_type: ty::FnOutput<'tcx>,
|
||||||
abi: Abi,
|
abi: Abi,
|
||||||
closure_env: closure::ClosureEnv<'b, 'tcx>) {
|
closure_env: closure::ClosureEnv<'b>) {
|
||||||
ccx.stats().n_closures.set(ccx.stats().n_closures.get() + 1);
|
ccx.stats().n_closures.set(ccx.stats().n_closures.get() + 1);
|
||||||
|
|
||||||
let _icx = push_ctxt("trans_closure");
|
let _icx = push_ctxt("trans_closure");
|
||||||
@ -1784,12 +1784,17 @@ pub fn trans_closure<'a, 'b, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|||||||
debug!("trans_closure(..., param_substs={})",
|
debug!("trans_closure(..., param_substs={})",
|
||||||
param_substs.repr(ccx.tcx()));
|
param_substs.repr(ccx.tcx()));
|
||||||
|
|
||||||
|
let has_env = match closure_env {
|
||||||
|
closure::ClosureEnv::Closure(_) => true,
|
||||||
|
closure::ClosureEnv::NotClosure => false,
|
||||||
|
};
|
||||||
|
|
||||||
let (arena, fcx): (TypedArena<_>, FunctionContext);
|
let (arena, fcx): (TypedArena<_>, FunctionContext);
|
||||||
arena = TypedArena::new();
|
arena = TypedArena::new();
|
||||||
fcx = new_fn_ctxt(ccx,
|
fcx = new_fn_ctxt(ccx,
|
||||||
llfndecl,
|
llfndecl,
|
||||||
fn_ast_id,
|
fn_ast_id,
|
||||||
closure_env.kind != closure::ClosureKind::NotClosure,
|
has_env,
|
||||||
output_type,
|
output_type,
|
||||||
param_substs,
|
param_substs,
|
||||||
Some(body.span),
|
Some(body.span),
|
||||||
@ -1808,13 +1813,13 @@ pub fn trans_closure<'a, 'b, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|||||||
decl.inputs.iter()
|
decl.inputs.iter()
|
||||||
.map(|arg| node_id_type(bcx, arg.id))
|
.map(|arg| node_id_type(bcx, arg.id))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
let monomorphized_arg_types = match closure_env.kind {
|
let monomorphized_arg_types = match closure_env {
|
||||||
closure::ClosureKind::NotClosure => {
|
closure::ClosureEnv::NotClosure => {
|
||||||
monomorphized_arg_types
|
monomorphized_arg_types
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tuple up closure argument types for the "rust-call" ABI.
|
// Tuple up closure argument types for the "rust-call" ABI.
|
||||||
closure::ClosureKind::Closure => {
|
closure::ClosureEnv::Closure(_) => {
|
||||||
vec![ty::mk_tup(ccx.tcx(), monomorphized_arg_types)]
|
vec![ty::mk_tup(ccx.tcx(), monomorphized_arg_types)]
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -1835,14 +1840,14 @@ pub fn trans_closure<'a, 'b, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|||||||
&monomorphized_arg_types[])
|
&monomorphized_arg_types[])
|
||||||
};
|
};
|
||||||
|
|
||||||
bcx = match closure_env.kind {
|
bcx = match closure_env {
|
||||||
closure::ClosureKind::NotClosure => {
|
closure::ClosureEnv::NotClosure => {
|
||||||
copy_args_to_allocas(bcx,
|
copy_args_to_allocas(bcx,
|
||||||
arg_scope,
|
arg_scope,
|
||||||
&decl.inputs[],
|
&decl.inputs[],
|
||||||
arg_datums)
|
arg_datums)
|
||||||
}
|
}
|
||||||
closure::ClosureKind::Closure => {
|
closure::ClosureEnv::Closure(_) => {
|
||||||
copy_closure_args_to_allocas(
|
copy_closure_args_to_allocas(
|
||||||
bcx,
|
bcx,
|
||||||
arg_scope,
|
arg_scope,
|
||||||
@ -1932,7 +1937,7 @@ pub fn trans_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|||||||
attrs,
|
attrs,
|
||||||
output_type,
|
output_type,
|
||||||
abi,
|
abi,
|
||||||
closure::ClosureEnv::new(&[], closure::ClosureKind::NotClosure));
|
closure::ClosureEnv::NotClosure);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn trans_enum_variant<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
pub fn trans_enum_variant<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
||||||
|
@ -97,37 +97,23 @@ fn load_closure_environment<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
|
|||||||
bcx
|
bcx
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
pub enum ClosureEnv<'a> {
|
||||||
pub enum ClosureKind<'tcx> {
|
|
||||||
NotClosure,
|
NotClosure,
|
||||||
Closure,
|
Closure(&'a [ty::Freevar]),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ClosureEnv<'a, 'tcx> {
|
impl<'a> ClosureEnv<'a> {
|
||||||
freevars: &'a [ty::Freevar],
|
pub fn load<'blk,'tcx>(self, bcx: Block<'blk, 'tcx>, arg_scope: ScopeId)
|
||||||
pub kind: ClosureKind<'tcx>
|
-> Block<'blk, 'tcx>
|
||||||
}
|
{
|
||||||
|
match self {
|
||||||
impl<'a, 'tcx> ClosureEnv<'a, 'tcx> {
|
ClosureEnv::NotClosure => bcx,
|
||||||
pub fn new(freevars: &'a [ty::Freevar], kind: ClosureKind<'tcx>)
|
ClosureEnv::Closure(freevars) => {
|
||||||
-> ClosureEnv<'a, 'tcx> {
|
if freevars.is_empty() {
|
||||||
ClosureEnv {
|
bcx
|
||||||
freevars: freevars,
|
} else {
|
||||||
kind: kind
|
load_closure_environment(bcx, arg_scope, freevars)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub fn load<'blk>(self, bcx: Block<'blk, 'tcx>, arg_scope: ScopeId)
|
|
||||||
-> Block<'blk, 'tcx> {
|
|
||||||
// Don't bother to create the block if there's nothing to load
|
|
||||||
if self.freevars.is_empty() {
|
|
||||||
return bcx;
|
|
||||||
}
|
|
||||||
|
|
||||||
match self.kind {
|
|
||||||
ClosureKind::NotClosure => bcx,
|
|
||||||
ClosureKind::Closure => {
|
|
||||||
load_closure_environment(bcx, arg_scope, self.freevars)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -224,7 +210,7 @@ pub fn trans_closure_expr<'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
|
|||||||
&[],
|
&[],
|
||||||
sig.output,
|
sig.output,
|
||||||
function_type.abi,
|
function_type.abi,
|
||||||
ClosureEnv::new(&freevars[], ClosureKind::Closure));
|
ClosureEnv::Closure(&freevars[]));
|
||||||
|
|
||||||
// Don't hoist this to the top of the function. It's perfectly legitimate
|
// Don't hoist this to the top of the function. It's perfectly legitimate
|
||||||
// to have a zero-size closure (in which case dest will be `Ignore`) and
|
// to have a zero-size closure (in which case dest will be `Ignore`) and
|
||||||
|
Loading…
Reference in New Issue
Block a user