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:
agra
2026-06-02 09:23:50 +03:00
parent 634cf9bc7f
commit bdd0e96d78
265 changed files with 1070 additions and 761 deletions

View File

@@ -270,7 +270,7 @@ sx_android_render_thread_entry :: (arg: *void) -> *void callconv(.c) {
fn := plat.user_main_fn;
fn();
}
null;
null
}
// Bring up EGL on `plat.app_window`. Sets the egl_* fields and makes
@@ -313,7 +313,7 @@ sx_android_egl_init :: (plat: *AndroidPlatform) -> bool {
plat.pixel_w = ANativeWindow_getWidth(plat.app_window);
plat.pixel_h = ANativeWindow_getHeight(plat.app_window);
sx_android_recompute_scale(plat);
true;
true
}
// Recompute `dpi_scale` from `pixel_w / logical_w`. Called on viewport
@@ -378,7 +378,7 @@ impl Platform for AndroidPlatform {
self.width = w;
self.height = h;
sx_android_recompute_scale(self);
true;
true
}
// NOTE: method order must match the `Platform` protocol declaration
@@ -405,7 +405,7 @@ impl Platform for AndroidPlatform {
result : []Event = ---;
result.ptr = self.events.items;
result.len = self.events.len;
result;
result
}
begin_frame :: (self: *AndroidPlatform) -> FrameContext {
@@ -421,7 +421,7 @@ impl Platform for AndroidPlatform {
dpi_scale = self.dpi_scale,
delta_time = 0.016,
target_present_time = 0.0,
};
}
}
end_frame :: (self: *AndroidPlatform) {
@@ -431,11 +431,11 @@ impl Platform for AndroidPlatform {
}
safe_insets :: (self: *AndroidPlatform) -> EdgeInsets {
EdgeInsets.{};
EdgeInsets.{}
}
keyboard :: (self: *AndroidPlatform) -> KeyboardState {
KeyboardState.zero();
KeyboardState.zero()
}
show_keyboard :: (self: *AndroidPlatform) { }
hide_keyboard :: (self: *AndroidPlatform) { }

View File

@@ -195,7 +195,7 @@ bundle_main :: () -> bool {
out("bundled: ");
out(bundle);
out("\n");
true;
true
}
// ── Helpers ──────────────────────────────────────────────────────────
@@ -206,7 +206,7 @@ bundle_main :: () -> bool {
str_to_cstr :: (s: string) -> [:0]u8 {
buf := cstring(s.len);
memcpy(buf.ptr, s.ptr, s.len);
buf;
buf
}
// Minimum iOS version baked into the Info.plist — matches what the
@@ -293,7 +293,7 @@ PLIST, xml_escape(bundle_id), xml_escape(exe_name), xml_escape(exe_name), IOS_MI
<string>0.1</string>
</dict>
</plist>
PLIST, xml_escape(bundle_id), xml_escape(exe_name), xml_escape(exe_name));
PLIST, xml_escape(bundle_id), xml_escape(exe_name), xml_escape(exe_name))
}
// Read a `.mobileprovision` and write it to
@@ -313,7 +313,7 @@ embed_provisioning_profile :: (profile: string, bundle: string) -> bool {
out("error: bundle: cannot read provisioning profile: ");
out(profile);
out("\n");
false;
false
}
// Recursive-copy `<src_dir>` (relative to the build CWD) into
@@ -359,7 +359,7 @@ copy_asset_dir :: (src: string, dest: string, bundle: string) -> bool {
return true;
}
out("error: cp -R spawn failed\n");
false;
false
}
// Recursive-copy `<name>.framework` from one of the user's `-F` search
@@ -402,7 +402,7 @@ embed_framework :: (opts: BuildOptions, name: string, dest_dir: string) -> bool
}
i += 1;
}
false;
false
}
// Extract entitlements XML from a `.mobileprovision` and resolve the
@@ -505,7 +505,7 @@ extract_entitlements :: (profile: string, bundle_id: string) -> ?string {
return null;
}
ent_path;
ent_path
}
// Codesign the bundle. Empty `ent_path` means no `--entitlements`
@@ -532,7 +532,7 @@ codesign :: (bundle: string, identity: string, ent_path: string) -> bool {
return true;
}
out("error: codesign spawn failed\n");
false;
false
}
// =====================================================================
@@ -572,7 +572,7 @@ absolutify :: (path: string) -> string {
if cwd.len == 0 { return path; }
return path_join(cwd, path);
}
path;
path
}
android_bundle_main :: (opts: BuildOptions, binary: string, apk_path: string, bundle_id: string) -> bool {
@@ -771,7 +771,7 @@ android_bundle_main :: (opts: BuildOptions, binary: string, apk_path: string, bu
out("apk: ");
out(apk_path);
out("\n");
true;
true
}
// ── Android helpers ──────────────────────────────────────────────────
@@ -795,7 +795,7 @@ run_in_dir :: (dir: string, cmd: string) -> bool {
return true;
}
out("error: shell spawn failed\n");
false;
false
}
// Discover the Android SDK root. Honors $ANDROID_HOME /
@@ -808,7 +808,7 @@ discover_android_sdk :: () -> string {
candidate := path_join(home, "Library/Android/sdk");
if exists(str_to_cstr(candidate)) { return candidate; }
}
"";
""
}
// Pick the lexicographically-highest subdir of `parent`. Equivalent to
@@ -831,7 +831,7 @@ find_highest_subdir :: (parent: string) -> string {
if name.len == 0 { return ""; }
return path_join(parent, name);
}
"";
""
}
// `libfoo.so` → `foo`. Android's `android.app.lib_name` meta-data
@@ -858,7 +858,7 @@ lib_name_from_so_basename :: (basename: string) -> string {
}
}
}
name;
name
}
// AndroidManifest.xml synthesizer. When the program declares a
@@ -922,7 +922,7 @@ MANIFEST, pkg_esc, lib_esc, cls_esc, lib_esc);
</activity>
</application>
</manifest>
MANIFEST, pkg_esc, lib_esc, lib_esc, lib_esc);
MANIFEST, pkg_esc, lib_esc, lib_esc, lib_esc)
}
// `co/swipelab/sxchess/SxApp` → `co.swipelab.sxchess.SxApp`.
@@ -934,7 +934,7 @@ slash_to_dot :: (path: string) -> string {
buf[i] = if c == 47 then 46 else c; // 47 = '/', 46 = '.'
i += 1;
}
buf;
buf
}
// Last `/`-separated component of a forward-slash path (used to split
@@ -947,7 +947,7 @@ last_slash_component :: (path: string) -> string {
if path[i - 1] == 47 { return substr(path, i, path.len - i); }
i -= 1;
}
path;
path
}
dir_part :: (path: string) -> string {
@@ -956,7 +956,7 @@ dir_part :: (path: string) -> string {
if path[i - 1] == 47 { return substr(path, 0, i - 1); }
i -= 1;
}
"";
""
}
// Write each `#jni_main` decl's `.java` source, then compile to
@@ -1054,7 +1054,7 @@ compile_jni_main_sources :: (opts: BuildOptions, stage: string, android_jar: str
out("error: d8 spawn failed\n");
return false;
}
true;
true
}
// Locate `javac`. Honors `$JAVA_HOME/bin/javac` first (Android
@@ -1066,7 +1066,7 @@ discover_javac :: () -> string {
if exists(str_to_cstr(cand)) { return cand; }
}
if path := find_executable("javac") { return path; }
"";
""
}
// Zip the contents of `<src>` into the APK at `<dest>/`. Uses a
@@ -1109,7 +1109,7 @@ zip_asset_dir :: (src: string, dest: string, apk: string) -> bool {
zip_cmd = concat(zip_cmd, dest);
zip_cmd = concat(zip_cmd, "\"");
if !run_in_dir(".sx-tmp/apk-assets", zip_cmd) { return false; }
true;
true
}
// Generate the Android debug keystore on first use. The defaults
@@ -1133,5 +1133,5 @@ ensure_debug_keystore :: (keystore_path: string) -> bool {
return true;
}
out("error: keytool spawn failed\n");
false;
false
}

View File

@@ -122,7 +122,7 @@ impl Platform for SdlPlatform {
g_sdl_plat = self;
self.last_perf = SDL_GetPerformanceCounter();
true;
true
}
run_frame_loop :: (self: *SdlPlatform, frame_fn: Closure()) {
@@ -164,7 +164,7 @@ impl Platform for SdlPlatform {
result : []Event = ---;
result.ptr = self.events.items;
result.len = self.events.len;
result;
result
}
begin_frame :: (self: *SdlPlatform) -> FrameContext {
@@ -195,7 +195,7 @@ impl Platform for SdlPlatform {
pixel_h = self.pixel_h,
dpi_scale = self.dpi_scale,
delta_time = self.delta_time,
};
}
}
end_frame :: (self: *SdlPlatform) {
@@ -203,11 +203,11 @@ impl Platform for SdlPlatform {
}
safe_insets :: (self: *SdlPlatform) -> EdgeInsets {
EdgeInsets.zero();
EdgeInsets.zero()
}
keyboard :: (self: *SdlPlatform) -> KeyboardState {
KeyboardState.zero();
KeyboardState.zero()
}
show_keyboard :: (self: *SdlPlatform) { }
@@ -242,7 +242,7 @@ sdl_event_watch :: (userdata: *void, event: *SDL_Event) -> bool callconv(.c) {
}
}
}
true;
true
}
sdl_wasm_tick :: () callconv(.c) {

View File

@@ -357,7 +357,7 @@ impl Platform for UIKitPlatform {
self.read_screen_scale();
}
}
true;
true
}
run_frame_loop :: (self: *UIKitPlatform, frame_fn: Closure()) {
@@ -380,7 +380,7 @@ impl Platform for UIKitPlatform {
result.ptr = self.events.items;
result.len = self.events.len;
self.events.len = 0;
result;
result
}
begin_frame :: (self: *UIKitPlatform) -> FrameContext {
@@ -395,7 +395,7 @@ impl Platform for UIKitPlatform {
dpi_scale = self.dpi_scale,
delta_time = self.delta_time,
target_present_time = self.last_target_ts,
};
}
}
end_frame :: (self: *UIKitPlatform) {
@@ -417,14 +417,14 @@ impl Platform for UIKitPlatform {
left = self.safe_left,
bottom = bottom,
right = self.safe_right,
};
}
}
keyboard :: (self: *UIKitPlatform) -> KeyboardState {
KeyboardState.{
visible = self.keyboard_visible,
height = self.keyboard_height,
};
}
}
show_keyboard :: (self: *UIKitPlatform) {
@@ -793,7 +793,7 @@ impl Platform for UIKitPlatform {
// callconv(.c) so this is callable from `load_gl`'s C-conv proc-loader slot.
ios_gl_proc :: (name: [*]u8) -> *void callconv(.c) {
rtld_default : *void = xx (0 - 2);
dlsym(rtld_default, name);
dlsym(rtld_default, name)
}
// Read a `extern NSString * const k...` global from the loaded image. The
@@ -804,7 +804,7 @@ uikit_extern_nsstring :: (name: [*]u8) -> *void {
p := dlsym(rtld_default, name);
if p == null { return null; }
pp : **void = xx p;
pp.*;
pp.*
}
// ───────────────────────────────────────────────────────────────────────────
@@ -869,12 +869,12 @@ SxGLView :: #objc_class("SxGLView") {
uikit_touch_location :: (touch: *void, view: *void) -> Point {
p := #objc_call(CGPoint)(touch, "locationInView:", view);
Point.{ x = xx p.x, y = xx p.y };
Point.{ x = xx p.x, y = xx p.y }
}
uikit_first_touch :: (touches: *void) -> *void {
touches_set : *NSSet = xx touches;
touches_set.anyObject();
touches_set.anyObject()
}
// uikit_register_gl_view_class — deleted (M3.3). SxGLView is now