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:
@@ -52,7 +52,7 @@ cstring :: (size: s64) -> string {
|
||||
s : string = ---;
|
||||
s.ptr = xx raw;
|
||||
s.len = size;
|
||||
s;
|
||||
s
|
||||
}
|
||||
|
||||
alloc_slice :: ($T: Type, count: s64) -> []T {
|
||||
@@ -61,7 +61,7 @@ alloc_slice :: ($T: Type, count: s64) -> []T {
|
||||
s : []T = ---;
|
||||
s.ptr = xx raw;
|
||||
s.len = count;
|
||||
s;
|
||||
s
|
||||
}
|
||||
|
||||
int_to_string :: (n: s64) -> string {
|
||||
@@ -77,11 +77,11 @@ int_to_string :: (n: s64) -> string {
|
||||
i -= 1;
|
||||
}
|
||||
if neg { tmp[i] = 45; i -= 1; }
|
||||
substr(tmp, i + 1, 20 - i - 1);
|
||||
substr(tmp, i + 1, 20 - i - 1)
|
||||
}
|
||||
|
||||
bool_to_string :: (b: bool) -> string {
|
||||
if b then "true" else "false";
|
||||
if b then "true" else "false"
|
||||
}
|
||||
|
||||
float_to_string :: (f: f64) -> string {
|
||||
@@ -107,7 +107,7 @@ float_to_string :: (f: f64) -> string {
|
||||
memset(@buf[pos], 48, pad);
|
||||
pos = pos + pad;
|
||||
memcpy(@buf[pos], fstr.ptr, fl);
|
||||
buf;
|
||||
buf
|
||||
}
|
||||
|
||||
hex_group :: (buf: string, offset: s64, val: s64) {
|
||||
@@ -149,7 +149,7 @@ int_to_hex_string :: (n: s64) -> string {
|
||||
if buf[start] != 48 { break; }
|
||||
start += 1;
|
||||
}
|
||||
substr(buf, start, 16 - start);
|
||||
substr(buf, start, 16 - start)
|
||||
}
|
||||
|
||||
concat :: (a: string, b: string) -> string {
|
||||
@@ -158,13 +158,13 @@ concat :: (a: string, b: string) -> string {
|
||||
buf := cstring(al + bl);
|
||||
memcpy(buf.ptr, a.ptr, al);
|
||||
memcpy(@buf[al], b.ptr, bl);
|
||||
buf;
|
||||
buf
|
||||
}
|
||||
|
||||
substr :: (s: string, start: s64, len: s64) -> string {
|
||||
buf := cstring(len);
|
||||
memcpy(buf.ptr, @s[start], len);
|
||||
buf;
|
||||
buf
|
||||
}
|
||||
|
||||
// Replace XML special characters with their entity references. Used
|
||||
@@ -195,7 +195,7 @@ xml_escape :: (s: string) -> string {
|
||||
if seg_start < s.len {
|
||||
result = concat(result, substr(s, seg_start, s.len - seg_start));
|
||||
}
|
||||
result;
|
||||
result
|
||||
}
|
||||
|
||||
// Join path components with the POSIX separator ('/'). Skips empty
|
||||
@@ -225,7 +225,7 @@ path_join :: (..parts: []string) -> string {
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
result;
|
||||
result
|
||||
}
|
||||
|
||||
struct_to_string :: (s: $T) -> string {
|
||||
@@ -238,7 +238,7 @@ struct_to_string :: (s: $T) -> string {
|
||||
result = concat(result, any_to_string(field_value(s, i)));
|
||||
i += 1;
|
||||
}
|
||||
concat(result, "}");
|
||||
concat(result, "}")
|
||||
}
|
||||
|
||||
vector_to_string :: (v: $T) -> string {
|
||||
@@ -249,7 +249,7 @@ vector_to_string :: (v: $T) -> string {
|
||||
result = concat(result, any_to_string(field_value(v, i)));
|
||||
i += 1;
|
||||
}
|
||||
concat(result, "]");
|
||||
concat(result, "]")
|
||||
}
|
||||
|
||||
array_to_string :: (a: $T) -> string {
|
||||
@@ -260,7 +260,7 @@ array_to_string :: (a: $T) -> string {
|
||||
result = concat(result, any_to_string(field_value(a, i)));
|
||||
i += 1;
|
||||
}
|
||||
concat(result, "]");
|
||||
concat(result, "]")
|
||||
}
|
||||
|
||||
slice_to_string :: (items: []$T) -> string {
|
||||
@@ -271,13 +271,13 @@ slice_to_string :: (items: []$T) -> string {
|
||||
result = concat(result, any_to_string(field_value(items, i)));
|
||||
i += 1;
|
||||
}
|
||||
concat(result, "]");
|
||||
concat(result, "]")
|
||||
}
|
||||
|
||||
pointer_to_string :: (p: $T) -> string {
|
||||
addr : s64 = xx p;
|
||||
if addr == 0 { "null"; } else {
|
||||
concat(type_name(T), concat("@0x", int_to_hex_string(addr)));
|
||||
if addr == 0 { "null" } else {
|
||||
concat(type_name(T), concat("@0x", int_to_hex_string(addr)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -294,7 +294,7 @@ flags_to_string :: (val: $T) -> string {
|
||||
i += 1;
|
||||
}
|
||||
if result.len == 0 { result = "0"; }
|
||||
result;
|
||||
result
|
||||
}
|
||||
|
||||
enum_to_string :: (u: $T) -> string {
|
||||
@@ -306,7 +306,7 @@ enum_to_string :: (u: $T) -> string {
|
||||
if pstr.len > 0 {
|
||||
result = concat(result, concat("(", concat(pstr, ")")));
|
||||
}
|
||||
result;
|
||||
result
|
||||
}
|
||||
|
||||
optional_to_string :: (o: $T) -> string {
|
||||
@@ -333,7 +333,7 @@ any_to_string :: (val: Any) -> string {
|
||||
case optional: result = optional_to_string(cast(type) val);
|
||||
case type: result = type_name(val);
|
||||
}
|
||||
result;
|
||||
result
|
||||
}
|
||||
|
||||
build_format :: (fmt: string) -> string {
|
||||
@@ -399,12 +399,12 @@ build_format :: (fmt: string) -> string {
|
||||
code = concat(code, int_to_string(fmt.len - seg_start));
|
||||
code = concat(code, ")); ");
|
||||
}
|
||||
code;
|
||||
code
|
||||
}
|
||||
|
||||
format :: ($fmt: string, ..$args) -> string {
|
||||
#insert build_format(fmt);
|
||||
#insert "result;";
|
||||
#insert "return result;";
|
||||
}
|
||||
|
||||
print :: ($fmt: string, ..$args) {
|
||||
|
||||
Reference in New Issue
Block a user