The for header is now a comma-separated list of iterables with a
positional capture group and no ':' separator:
for xs (x) { } // collection
for 0..n (i) { } // range (end exclusive)
for 1..=5 (a) { } // ..= inclusive end
for xs, 0.. (x, i) { } // index idiom (replaces (x, i))
for xs, ys (x, y) { } // parallel (zip) iteration
for xs (x) => sum += x; // arrow body (full statement)
First-iterable-wins: the first iterable's length drives the loop and
must be bounded; the other positions follow by their own cursors (a
non-first range's end is not consulted or evaluated; a shorter
non-first collection is read past its length on mismatch). The old
single-iterable index capture is replaced by the trailing open range.
Capture/call disambiguation is positional: the paren group immediately
before '{' or '=>' is the capture, every earlier top-level group is a
call. 'for zip(a, b) (x, y)' calls zip; 'for f(n) { }' reads (n) as
the capture and errors with a parenthesize/add-capture hint. The old
':' form errors with a migration hint.
Lowering is unified across forms: one cursor slot per position (ranges
start at their start, collections at 0), all advanced together, the
first position's bound terminating. inline for keeps the single
bounded comptime range.
Migrated the full corpus (examples, library modules, issue repros,
in-source test strings). New coverage: examples/0050 (the full feature
surface) and examples/1149-1155 (seven diagnostic faces). specs.md For
Loop section + grammar rewritten; readme teaser updated.
27 lines
1.0 KiB
Plaintext
27 lines
1.0 KiB
Plaintext
// Cleanup-body control-flow restrictions (ERR step E1.7 follow-up). A `defer`
|
|
// or `onfail` body runs while the block/function is already exiting, so it has
|
|
// no target to transfer control to: `raise` / `try` / `return` / `break` /
|
|
// `continue` are all rejected inside one. The ban is transitive through nested
|
|
// `catch` bodies and loops, but NOT through a nested closure (its own function
|
|
// boundary). `raise` was already banned (E1.3); this adds the other four.
|
|
// This file is expected to FAIL compilation (exit 1).
|
|
//
|
|
// Run: ./zig-out/bin/sx run examples/237-cleanup-body-restrictions.sx
|
|
|
|
#import "modules/std.sx";
|
|
|
|
E :: error { Bad }
|
|
|
|
g :: () -> !E { return; }
|
|
|
|
f :: () -> !E {
|
|
defer { return; } // ERROR: return in defer body
|
|
onfail { try g(); } // ERROR: try in onfail body
|
|
defer { for 0..1 (i) { break; } } // ERROR: break in defer body (transitive through loop)
|
|
onfail e { if e == error.Bad { continue; } } // ERROR: continue in onfail body
|
|
try g();
|
|
return;
|
|
}
|
|
|
|
main :: () -> s32 { return 0; }
|