feat(lang): block value requires no trailing ; (Rust-style)
A block's value is now its last statement ONLY when that statement is a trailing expression with no `;`. A trailing `;` discards the value, leaving the block void. This makes value-vs-statement explicit and lets the compiler reject "this block was supposed to produce a value". Compiler: - Parser records `Block.produces_value` (last stmt is a no-`;` trailing expression) + `Block.discarded_semi` (the `;` that discarded a value), via `expectSemicolonAfter`. A trailing expression before `}` may now omit its `;` (previously a parse error). Match-arm and else-arm bodies are built value-producing regardless of the arm `;` (arms are exempt — the `;` is an arm terminator). - Lowering: `lowerBlockValue` / the block-expr path / `inferExprType` respect `produces_value`. A value-position block that discards its value is a hard error (`lowerValueBody` for function bodies; the value-context `.block` path for if/else branches, `catch` bodies, value bindings, match arms). Pure-failable `-> !` bodies (value rides the error channel) and a value-if whose branches are void are handled without false errors. - `defer`/`onfail` cleanup bodies lower as statements (void), so a trailing `;` there is fine. Migration (behavior-preserving — output unchanged): - stdlib + ~210 examples: dropped the trailing `;` on value-position last expressions. `format` now ends with an explicit `#insert "return result;"` (it relied on `#insert`-as-block-value, which `;` discards). - Two `main :: () -> s32` examples that relied on the old silent default-return got an explicit trailing `0`. - Rejection snapshots 0412 / 1013 regenerated (their quoted source lines lost a `;`); the diagnostics themselves are unchanged. Docs/tests: specs.md "Block values" section; examples 0040 (rules) + 0041 (rejection); 3 parser unit tests. Filed issue 0066 (pre-existing match-arm negated-literal phi-width quirk, surfaced not caused here). Gates: zig build, zig build test, run_examples.sh -> 343 passed, cross_compile.sh -> 7 passed (also refreshed its stale example names).
This commit is contained in:
@@ -69,25 +69,25 @@ SeekFrom :: enum { set; current; end; }
|
||||
File :: struct {
|
||||
fd: s32 = -1;
|
||||
|
||||
is_valid :: (self: *File) -> bool { self.fd >= 0; }
|
||||
is_valid :: (self: *File) -> bool { self.fd >= 0 }
|
||||
|
||||
close :: (self: *File) -> bool {
|
||||
if self.fd < 0 { return false; }
|
||||
rc := close(self.fd);
|
||||
self.fd = -1;
|
||||
rc == 0;
|
||||
rc == 0
|
||||
}
|
||||
|
||||
read :: (self: *File, buf: string) -> s64 {
|
||||
if self.fd < 0 { return -1; }
|
||||
n := read(self.fd, buf.ptr, xx buf.len);
|
||||
cast(s64) n;
|
||||
cast(s64) n
|
||||
}
|
||||
|
||||
write :: (self: *File, data: string) -> s64 {
|
||||
if self.fd < 0 { return -1; }
|
||||
n := write(self.fd, data.ptr, xx data.len);
|
||||
cast(s64) n;
|
||||
cast(s64) n
|
||||
}
|
||||
|
||||
seek :: (self: *File, offset: s64, whence: SeekFrom) -> s64 {
|
||||
@@ -95,7 +95,7 @@ File :: struct {
|
||||
w := SEEK_SET;
|
||||
if whence == .current { w = SEEK_CUR; }
|
||||
if whence == .end { w = SEEK_END; }
|
||||
lseek(self.fd, offset, w);
|
||||
lseek(self.fd, offset, w)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,13 +110,13 @@ mode_to_flags :: (m: OpenMode) -> s32 {
|
||||
if m == .write { return O_WRONLY | O_CREAT | O_TRUNC; }
|
||||
if m == .append { return O_WRONLY | O_CREAT | O_APPEND; }
|
||||
if m == .read_write { return O_RDWR; }
|
||||
O_RDONLY;
|
||||
O_RDONLY
|
||||
}
|
||||
|
||||
open_file :: (path: [:0]u8, mode: OpenMode) -> ?File {
|
||||
fd := open(path, mode_to_flags(mode), 420); // 0o644 = 420
|
||||
if fd < 0 { return null; }
|
||||
File.{ fd = fd };
|
||||
File.{ fd = fd }
|
||||
}
|
||||
|
||||
// One-shot read: opens, slurps the whole file into a fresh buffer,
|
||||
@@ -134,7 +134,7 @@ read_file :: (path: [:0]u8) -> ?string {
|
||||
n := read(fd, buf.ptr, xx size);
|
||||
close(fd);
|
||||
if cast(s64) n != size { return null; }
|
||||
buf;
|
||||
buf
|
||||
}
|
||||
|
||||
// One-shot write: creates / truncates and writes the whole buffer.
|
||||
@@ -143,7 +143,7 @@ write_file :: (path: [:0]u8, data: string) -> bool {
|
||||
if fd < 0 { return false; }
|
||||
n := write(fd, data.ptr, xx data.len);
|
||||
close(fd);
|
||||
cast(s64) n == cast(s64) data.len;
|
||||
cast(s64) n == cast(s64) data.len
|
||||
}
|
||||
|
||||
append_file :: (path: [:0]u8, data: string) -> bool {
|
||||
@@ -151,33 +151,33 @@ append_file :: (path: [:0]u8, data: string) -> bool {
|
||||
if fd < 0 { return false; }
|
||||
n := write(fd, data.ptr, xx data.len);
|
||||
close(fd);
|
||||
cast(s64) n == cast(s64) data.len;
|
||||
cast(s64) n == cast(s64) data.len
|
||||
}
|
||||
|
||||
// ── Single-syscall ops ───────────────────────────────────────────────
|
||||
|
||||
exists :: (path: [:0]u8) -> bool {
|
||||
access(path, F_OK) == 0;
|
||||
access(path, F_OK) == 0
|
||||
}
|
||||
|
||||
delete_file :: (path: [:0]u8) -> bool {
|
||||
unlink(path) == 0;
|
||||
unlink(path) == 0
|
||||
}
|
||||
|
||||
delete_dir :: (path: [:0]u8) -> bool {
|
||||
rmdir(path) == 0;
|
||||
rmdir(path) == 0
|
||||
}
|
||||
|
||||
create_dir :: (path: [:0]u8) -> bool {
|
||||
mkdir(path, 493) == 0; // 0o755 = 493
|
||||
mkdir(path, 493) == 0 // 0o755 = 493
|
||||
}
|
||||
|
||||
set_mode :: (path: [:0]u8, mode: u32) -> bool {
|
||||
chmod(path, mode) == 0;
|
||||
chmod(path, mode) == 0
|
||||
}
|
||||
|
||||
move :: (oldp: [:0]u8, newp: [:0]u8) -> bool {
|
||||
rename(oldp, newp) == 0;
|
||||
rename(oldp, newp) == 0
|
||||
}
|
||||
|
||||
// Recursive mkdir -p. Walks the path and creates each missing
|
||||
@@ -195,7 +195,7 @@ create_dir_all :: (path: [:0]u8) -> bool {
|
||||
memcpy(parent.ptr, path.ptr, last);
|
||||
if !create_dir_all(parent) { return false; }
|
||||
}
|
||||
create_dir(path);
|
||||
create_dir(path)
|
||||
}
|
||||
|
||||
// Copy a file by streaming through a 64KB buffer. Uses libc directly
|
||||
@@ -224,7 +224,7 @@ copy_file :: (src: [:0]u8, dst: [:0]u8) -> bool {
|
||||
}
|
||||
close(src_fd);
|
||||
close(dst_fd);
|
||||
ok;
|
||||
ok
|
||||
}
|
||||
|
||||
// ── Path helpers ─────────────────────────────────────────────────────
|
||||
@@ -243,7 +243,7 @@ basename :: (p: string) -> string {
|
||||
if p[last - 1] == 47 { return substr(p, last, end - last); }
|
||||
last -= 1;
|
||||
}
|
||||
substr(p, 0, end);
|
||||
substr(p, 0, end)
|
||||
}
|
||||
|
||||
dirname :: (p: string) -> string {
|
||||
@@ -264,5 +264,5 @@ dirname :: (p: string) -> string {
|
||||
last -= 1;
|
||||
}
|
||||
if p[0] == 47 { return "/"; }
|
||||
".";
|
||||
"."
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user