1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// zigir/lower/expr/function.rs
// Arrow function and function expression lowering.
use oxc_ast::ast::*;
use crate::types::ZigType;
use crate::zigir::ident::IrIdent;
use crate::zigir::types::IrBlock;
use super::Lowerer;
impl Lowerer {
// ─── Shared pipeline ──────────────────────────────────
/// Enter the function context for closure lowering: save fn state, set captured vars.
/// Returns the saved state that must be passed to `exit_closure_context`.
fn enter_closure_context(
&mut self,
name: &str,
return_type: ZigType,
captured: &[(String, ZigType, bool)],
) -> (
Option<crate::zigir::lower::helpers::FnContext>,
Vec<(String, ZigType, bool)>,
) {
let saved_fn = self.enter_fn(name, false, Some(return_type));
let saved_captured = self.closure_mgr.take_captured();
self.closure_mgr.current_captured = captured
.iter()
.map(|(n, t, m)| (n.clone(), t.clone(), *m))
.collect();
(saved_fn, saved_captured)
}
/// Exit the function context for closure lowering: clear deinit, restore captured, exit fn.
fn exit_closure_context(
&mut self,
body: &mut IrBlock,
saved_fn: Option<crate::zigir::lower::helpers::FnContext>,
saved_captured: Vec<(String, ZigType, bool)>,
) {
Self::clear_deinit_for_returned_vars(body);
self.closure_mgr.restore_captured(saved_captured);
self.exit_fn(saved_fn);
}
/// Build an `IrClosure` (struct + instance) from closure lowering results.
///
/// Also registers the closure struct definition in `pending_arrow_structs`.
///
/// Two scenarios produce `@compileError`:
/// 1. Any captured variable is itself a field on the enclosing closure.
/// 2. A nested `IrExpr::Closure` in the body initializes one of its
/// captured fields with a name that overlaps with THIS closure's
/// own captured fields — the emitter would use a bare name instead
/// of `self.field`, so we reject it explicitly.
fn build_closure_expr(
&mut self,
captured: Vec<(String, ZigType, bool)>,
params: Vec<crate::zigir::types::IrParam>,
return_type: ZigType,
body: IrBlock,
struct_name: IrIdent,
instance_name: IrIdent,
) -> crate::zigir::types::IrExpr {
use crate::zigir::types::{IrClosure, IrExpr};
let ir_captures = self.make_ir_captures(captured.clone().into_iter().collect());
self.closure_mgr
.closure_instances
.insert(instance_name.zig_name.clone());
let mut body = body;
// Check 1: nested closure capture (variable captured from enclosing
// closure's fields).
let nested = self.detect_nested_captures(&captured);
// Check 2: body contains a Closure whose init values overlap with
// our own captured field names. The emitter writes bare names for
// init values, but inside our call() method those need `self.`
// prefix — unsupported, so emit @compileError.
let own_capture_names: Vec<String> = ir_captures
.iter()
.map(|c| c.name.zig_name.clone())
.collect();
let overlap = Self::find_nested_closure_capture_overlap(&body, &own_capture_names);
if !nested.is_empty() || !overlap.is_empty() {
let mut parts = Vec::new();
if !nested.is_empty() {
parts.push(format!(
"variable(s) {} captured from enclosing closure",
nested.join(", ")
));
}
if !overlap.is_empty() {
parts.push(format!(
"nested closure init uses own captured field(s) {}",
overlap.join(", ")
));
}
let msg = format!(
"nested closure capture is not supported: {}",
parts.join("; ")
);
body.stmts.insert(
0,
crate::zigir::types::IrStmt::CompileError {
span: crate::zigir::source_span::SourceSpan::default(),
msg,
},
);
}
self.pending_arrow_structs
.push(crate::zigir::types::IrClosureStruct {
name: struct_name.clone(),
captured: ir_captures.clone(),
fn_params: params.clone(),
return_type: return_type.clone(),
typeof_return_body: None,
body: body.clone(),
});
IrExpr::Closure(IrClosure {
struct_name,
captured: ir_captures,
fn_params: params,
return_type,
body,
instance_name,
})
}
/// Recursively scan an IR block for `IrExpr::Closure` nodes whose
/// captured field names overlap with `own_capture_names`. Returns
/// the names that appear in both sets.
fn find_nested_closure_capture_overlap(
block: &IrBlock,
own_capture_names: &[String],
) -> Vec<String> {
let mut overlap = std::collections::HashSet::new();
for stmt in &block.stmts {
Self::scan_stmt_for_closure_overlap(stmt, own_capture_names, &mut overlap);
}
let mut result: Vec<String> = overlap.into_iter().collect();
result.sort();
result
}
fn scan_stmt_for_closure_overlap(
stmt: &crate::zigir::types::IrStmt,
own: &[String],
overlap: &mut std::collections::HashSet<String>,
) {
use crate::zigir::types::IrStmt;
match stmt {
IrStmt::Expr(expr) | IrStmt::Return { value: Some(expr) } => {
Self::scan_expr_for_closure_overlap(expr, own, overlap);
}
IrStmt::VarDecl(vd) => {
if let Some(init) = &vd.init {
Self::scan_expr_for_closure_overlap(init, own, overlap);
}
}
IrStmt::Assign { value, .. } => {
Self::scan_expr_for_closure_overlap(value, own, overlap);
}
IrStmt::If { then, else_, .. } => {
for s in &then.stmts {
Self::scan_stmt_for_closure_overlap(s, own, overlap);
}
if let Some(eb) = else_ {
for s in &eb.stmts {
Self::scan_stmt_for_closure_overlap(s, own, overlap);
}
}
}
IrStmt::Block(inner) => {
for s in &inner.stmts {
Self::scan_stmt_for_closure_overlap(s, own, overlap);
}
}
IrStmt::While { body, .. } | IrStmt::DoWhile { body, .. } => {
for s in &body.stmts {
Self::scan_stmt_for_closure_overlap(s, own, overlap);
}
}
IrStmt::For { body, .. } | IrStmt::ForOf { body, .. } => {
for s in &body.stmts {
Self::scan_stmt_for_closure_overlap(s, own, overlap);
}
}
IrStmt::Switch { cases, .. } => {
for case in cases {
for s in &case.body {
Self::scan_stmt_for_closure_overlap(s, own, overlap);
}
}
}
IrStmt::Try {
try_block,
catch_block,
finally,
..
} => {
for s in &try_block.stmts {
Self::scan_stmt_for_closure_overlap(s, own, overlap);
}
for s in &catch_block.stmts {
Self::scan_stmt_for_closure_overlap(s, own, overlap);
}
if let Some(fb) = finally {
for s in &fb.stmts {
Self::scan_stmt_for_closure_overlap(s, own, overlap);
}
}
}
IrStmt::NestedFnDecl { .. } => {
// Nested fn decl contains its own closure — scan the instance
if let IrStmt::NestedFnDecl {
instance: Some(cl), ..
} = stmt
{
for cap in &cl.captured {
if own.contains(&cap.name.zig_name) {
overlap.insert(cap.name.zig_name.clone());
}
}
}
}
// Return None, Throw, Break, Continue, DestructureDecl, CompileError, Comment — no expr
_ => {}
}
}
fn scan_expr_for_closure_overlap(
expr: &crate::zigir::types::IrExpr,
own: &[String],
overlap: &mut std::collections::HashSet<String>,
) {
use crate::zigir::types::IrExpr;
match expr {
IrExpr::Closure(c) => {
for cap in &c.captured {
if own.contains(&cap.name.zig_name) {
overlap.insert(cap.name.zig_name.clone());
}
}
// Also scan the closure body for deeper nesting
for stmt in &c.body.stmts {
Self::scan_stmt_for_closure_overlap(stmt, own, overlap);
}
}
IrExpr::Binary { left, right, .. } => {
Self::scan_expr_for_closure_overlap(left, own, overlap);
Self::scan_expr_for_closure_overlap(right, own, overlap);
}
IrExpr::Call(call) => {
Self::scan_expr_for_closure_overlap(&call.callee, own, overlap);
for arg in &call.args {
Self::scan_expr_for_closure_overlap(arg, own, overlap);
}
}
IrExpr::FieldAccess { object, .. } | IrExpr::IndexAccess { object, .. } => {
Self::scan_expr_for_closure_overlap(object, own, overlap);
}
IrExpr::ArrowFn(af) => {
for stmt in &af.body.stmts {
Self::scan_stmt_for_closure_overlap(stmt, own, overlap);
}
}
IrExpr::FnExpr(fe) => {
for stmt in &fe.body.stmts {
Self::scan_stmt_for_closure_overlap(stmt, own, overlap);
}
}
IrExpr::ArrayLiteral(arr) => {
for el in &arr.elements {
Self::scan_expr_for_closure_overlap(el, own, overlap);
}
}
IrExpr::Conditional { cond, then, else_ } => {
Self::scan_expr_for_closure_overlap(cond, own, overlap);
Self::scan_expr_for_closure_overlap(then, own, overlap);
Self::scan_expr_for_closure_overlap(else_, own, overlap);
}
IrExpr::Paren(inner)
| IrExpr::Spread(inner)
| IrExpr::Typeof(inner)
| IrExpr::Void(inner) => {
Self::scan_expr_for_closure_overlap(inner, own, overlap);
}
IrExpr::Unary { operand, .. } => {
Self::scan_expr_for_closure_overlap(operand, own, overlap);
}
// Update target is IrAssignTarget — no nested Closure possible
IrExpr::Logical { left, right, .. } => {
Self::scan_expr_for_closure_overlap(left, own, overlap);
Self::scan_expr_for_closure_overlap(right, own, overlap);
}
IrExpr::ObjectLiteral(obj) => {
for item in &obj.items {
match item {
crate::zigir::types::IrObjectItem::Field(f) => {
Self::scan_expr_for_closure_overlap(&f.value, own, overlap);
}
crate::zigir::types::IrObjectItem::Spread(expr) => {
Self::scan_expr_for_closure_overlap(expr, own, overlap);
}
}
}
}
IrExpr::TemplateLiteral { exprs, .. } => {
for e in exprs {
Self::scan_expr_for_closure_overlap(e, own, overlap);
}
}
IrExpr::BlockExpr { body, result, .. } => {
for stmt in body {
Self::scan_stmt_for_closure_overlap(stmt, own, overlap);
}
Self::scan_expr_for_closure_overlap(result, own, overlap);
}
// Literals, Ident, This, Null, Undefined, etc. — safe to skip
_ => {}
}
}
/// Lower an arrow function expression.
///
/// If the arrow captures variables from the enclosing scope, we produce
/// an `IrClosure` (struct + instance). Otherwise we produce a plain
/// `IrArrowFn` (struct + static call — Zig 0.16 doesn't allow nested
/// fn declarations with return statements).
pub(super) fn lower_arrow_fn(
&mut self,
af: &ArrowFunctionExpression,
) -> crate::zigir::types::IrExpr {
use crate::zigir::types::{IrArrowFn, IrExpr};
let mut captured = self.collect_arrow_captures(af);
// C5: Detect `this` usage inside arrow body within a class method.
// Arrow functions capture `this` lexically from the enclosing scope.
// Add a synthetic `__self` capture so the class instance (`self`)
// is available as `self.__self` inside the closure's `call()` method.
//
// EXCEPTION: Inside a constructor (this_rewrite_fields is set), `this`
// is rewritten to local variables — no `self` exists in `init()`, so
// capturing `this` would be invalid. Skip the __self capture there.
let captures_this = self.current_class.is_some()
&& self.this_rewrite_fields.is_none()
&& Self::detect_this_in_body(&af.body.statements);
if captures_this && let Some(class_name) = self.current_class.clone() {
captured.insert(
0,
("__self".to_string(), ZigType::NamedStruct(class_name), true),
);
}
let is_concise = af.expression;
let return_type = self.infer_arrow_return_type(af, &captured);
let params = self.lower_arrow_params(af);
let arrow_fn_label = format!("_arrow_{}", self.name_mangler.next_name("arrow"));
// C5: Set in_closure_with_this flag during body lowering
let saved_closure_with_this = self.in_closure_with_this;
if captures_this {
self.in_closure_with_this = true;
}
// Enter fn context with captured vars set up
let (saved_fn, saved_captured) =
self.enter_closure_context(&arrow_fn_label, return_type.clone(), &captured);
// Lower body
let mut body = if is_concise {
if let Statement::ExpressionStatement(es) = &af.body.statements[0] {
let expr_ir = self.lower_expr(&es.expression);
IrBlock::new(vec![crate::zigir::types::IrStmt::Return {
value: Some(expr_ir),
}])
} else {
self.lower_block(&af.body.statements)
}
} else {
self.lower_block(&af.body.statements)
};
self.exit_closure_context(&mut body, saved_fn, saved_captured);
self.in_closure_with_this = saved_closure_with_this;
if !captured.is_empty() {
let idx = self.name_mangler.peek_count("closure");
let struct_name = IrIdent::new(&format!("Closure_{}", idx));
let instance_name = IrIdent::new(&format!("_cl_{}", idx));
self.name_mangler.next_name("closure");
self.build_closure_expr(
captured,
params,
return_type,
body,
struct_name,
instance_name,
)
} else {
IrExpr::ArrowFn(IrArrowFn {
params,
return_type,
body,
is_concise,
})
}
}
/// Lower a function expression.
///
/// Like arrow functions, if the function captures variables we produce
/// an `IrClosure`; otherwise a plain `IrFnExpr`.
pub(super) fn lower_fn_expr(&mut self, fe: &Function) -> crate::zigir::types::IrExpr {
use crate::zigir::types::{IrExpr, IrFnExpr};
let name = fe
.id
.as_ref()
.map(|id| id.name.to_string())
.unwrap_or_else(|| {
let idx = self.name_mangler.peek_count("_fn_expr");
self.name_mangler.next_name("_fn_expr");
format!("_fn_expr_{}", idx)
});
let mut captured = self.detect_fn_body_captures(fe);
// C5: Detect `this` usage inside function expression body within a class method.
// EXCEPTION: Inside a constructor (this_rewrite_fields is set), `this`
// is rewritten to local variables — no `self` exists in `init()`.
let captures_this = if let Some(body) = &fe.body {
self.current_class.is_some()
&& self.this_rewrite_fields.is_none()
&& Self::detect_this_in_body(&body.statements)
} else {
false
};
if captures_this && let Some(class_name) = self.current_class.clone() {
captured.insert(
0,
("__self".to_string(), ZigType::NamedStruct(class_name), true),
);
}
let return_type = self
.type_info
.fn_return_types
.get(&name)
.cloned()
.unwrap_or_else(|| self.infer_fn_expr_return_type(fe, &captured));
let params = self.lower_fn_params(fe, &name);
// C5: Set in_closure_with_this flag during body lowering
let saved_closure_with_this = self.in_closure_with_this;
if captures_this {
self.in_closure_with_this = true;
}
// Enter fn context with captured vars set up
let (saved_fn, saved_captured) =
self.enter_closure_context(&name, return_type.clone(), &captured);
// Lower body
let mut body = fe
.body
.as_ref()
.map(|b| self.lower_block(&b.statements))
.unwrap_or_else(|| IrBlock::new(vec![]));
self.exit_closure_context(&mut body, saved_fn, saved_captured);
self.in_closure_with_this = saved_closure_with_this;
if !captured.is_empty() {
let struct_name = self.make_ident(&name);
let instance_name = IrIdent::new(&format!("_{}_inst", name));
self.build_closure_expr(
captured,
params,
return_type,
body,
struct_name,
instance_name,
)
} else {
// No captures → IrFnExpr, but still register a closure struct so
// the Emitter emits the `const _fn_expr_N = struct { pub fn call() ... }`
// definition at module scope.
let struct_name = self.make_ident(&name);
self.pending_arrow_structs
.push(crate::zigir::types::IrClosureStruct {
name: struct_name.clone(),
captured: vec![],
fn_params: params.clone(),
return_type: return_type.clone(),
typeof_return_body: None,
body: body.clone(),
});
IrExpr::FnExpr(IrFnExpr {
name: Some(self.make_ident(&name)),
params,
return_type,
body,
})
}
}
}