Skip to content

Commit ced1062

Browse files
committed
Address @eddyb nit by simplifying ClosureEnv.
1 parent a962bdb commit ced1062

File tree

2 files changed

+29
-38
lines changed

2 files changed

+29
-38
lines changed

src/librustc_trans/trans/base.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -1775,7 +1775,7 @@ pub fn trans_closure<'a, 'b, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
17751775
_attributes: &[ast::Attribute],
17761776
output_type: ty::FnOutput<'tcx>,
17771777
abi: Abi,
1778-
closure_env: closure::ClosureEnv<'b, 'tcx>) {
1778+
closure_env: closure::ClosureEnv<'b>) {
17791779
ccx.stats().n_closures.set(ccx.stats().n_closures.get() + 1);
17801780

17811781
let _icx = push_ctxt("trans_closure");
@@ -1784,12 +1784,17 @@ pub fn trans_closure<'a, 'b, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
17841784
debug!("trans_closure(..., param_substs={})",
17851785
param_substs.repr(ccx.tcx()));
17861786

1787+
let has_env = match closure_env {
1788+
closure::ClosureEnv::Closure(_) => true,
1789+
closure::ClosureEnv::NotClosure => false,
1790+
};
1791+
17871792
let (arena, fcx): (TypedArena<_>, FunctionContext);
17881793
arena = TypedArena::new();
17891794
fcx = new_fn_ctxt(ccx,
17901795
llfndecl,
17911796
fn_ast_id,
1792-
closure_env.kind != closure::ClosureKind::NotClosure,
1797+
has_env,
17931798
output_type,
17941799
param_substs,
17951800
Some(body.span),
@@ -1808,13 +1813,13 @@ pub fn trans_closure<'a, 'b, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
18081813
decl.inputs.iter()
18091814
.map(|arg| node_id_type(bcx, arg.id))
18101815
.collect::<Vec<_>>();
1811-
let monomorphized_arg_types = match closure_env.kind {
1812-
closure::ClosureKind::NotClosure => {
1816+
let monomorphized_arg_types = match closure_env {
1817+
closure::ClosureEnv::NotClosure => {
18131818
monomorphized_arg_types
18141819
}
18151820

18161821
// Tuple up closure argument types for the "rust-call" ABI.
1817-
closure::ClosureKind::Closure => {
1822+
closure::ClosureEnv::Closure(_) => {
18181823
vec![ty::mk_tup(ccx.tcx(), monomorphized_arg_types)]
18191824
}
18201825
};
@@ -1835,14 +1840,14 @@ pub fn trans_closure<'a, 'b, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
18351840
&monomorphized_arg_types[])
18361841
};
18371842

1838-
bcx = match closure_env.kind {
1839-
closure::ClosureKind::NotClosure => {
1843+
bcx = match closure_env {
1844+
closure::ClosureEnv::NotClosure => {
18401845
copy_args_to_allocas(bcx,
18411846
arg_scope,
18421847
&decl.inputs[],
18431848
arg_datums)
18441849
}
1845-
closure::ClosureKind::Closure => {
1850+
closure::ClosureEnv::Closure(_) => {
18461851
copy_closure_args_to_allocas(
18471852
bcx,
18481853
arg_scope,
@@ -1932,7 +1937,7 @@ pub fn trans_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
19321937
attrs,
19331938
output_type,
19341939
abi,
1935-
closure::ClosureEnv::new(&[], closure::ClosureKind::NotClosure));
1940+
closure::ClosureEnv::NotClosure);
19361941
}
19371942

19381943
pub fn trans_enum_variant<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,

src/librustc_trans/trans/closure.rs

+15-29
Original file line numberDiff line numberDiff line change
@@ -97,37 +97,23 @@ fn load_closure_environment<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
9797
bcx
9898
}
9999

100-
#[derive(PartialEq)]
101-
pub enum ClosureKind<'tcx> {
100+
pub enum ClosureEnv<'a> {
102101
NotClosure,
103-
Closure,
102+
Closure(&'a [ty::Freevar]),
104103
}
105104

106-
pub struct ClosureEnv<'a, 'tcx> {
107-
freevars: &'a [ty::Freevar],
108-
pub kind: ClosureKind<'tcx>
109-
}
110-
111-
impl<'a, 'tcx> ClosureEnv<'a, 'tcx> {
112-
pub fn new(freevars: &'a [ty::Freevar], kind: ClosureKind<'tcx>)
113-
-> ClosureEnv<'a, 'tcx> {
114-
ClosureEnv {
115-
freevars: freevars,
116-
kind: kind
117-
}
118-
}
119-
120-
pub fn load<'blk>(self, bcx: Block<'blk, 'tcx>, arg_scope: ScopeId)
121-
-> Block<'blk, 'tcx> {
122-
// Don't bother to create the block if there's nothing to load
123-
if self.freevars.is_empty() {
124-
return bcx;
125-
}
126-
127-
match self.kind {
128-
ClosureKind::NotClosure => bcx,
129-
ClosureKind::Closure => {
130-
load_closure_environment(bcx, arg_scope, self.freevars)
105+
impl<'a> ClosureEnv<'a> {
106+
pub fn load<'blk,'tcx>(self, bcx: Block<'blk, 'tcx>, arg_scope: ScopeId)
107+
-> Block<'blk, 'tcx>
108+
{
109+
match self {
110+
ClosureEnv::NotClosure => bcx,
111+
ClosureEnv::Closure(freevars) => {
112+
if freevars.is_empty() {
113+
bcx
114+
} else {
115+
load_closure_environment(bcx, arg_scope, freevars)
116+
}
131117
}
132118
}
133119
}
@@ -224,7 +210,7 @@ pub fn trans_closure_expr<'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
224210
&[],
225211
sig.output,
226212
function_type.abi,
227-
ClosureEnv::new(&freevars[], ClosureKind::Closure));
213+
ClosureEnv::Closure(&freevars[]));
228214

229215
// Don't hoist this to the top of the function. It's perfectly legitimate
230216
// to have a zero-size closure (in which case dest will be `Ignore`) and

0 commit comments

Comments
 (0)