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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user