diff --git a/apps/tests/macro/if/assign_both_branches.nyash b/apps/tests/macro/if/assign_both_branches.nyash new file mode 100644 index 00000000..bd0c689d --- /dev/null +++ b/apps/tests/macro/if/assign_both_branches.nyash @@ -0,0 +1,8 @@ +local x = 0 +if (1 < 2) { + x = 10 +} else { + x = 20 +} +print(x) + diff --git a/docs/guides/scope-hints.md b/docs/guides/scope-hints.md index d151dcf4..00fec5a5 100644 --- a/docs/guides/scope-hints.md +++ b/docs/guides/scope-hints.md @@ -26,3 +26,15 @@ Next - Wire ScopeEnter/Leave in MirBuilder for function entry/exit and block constructs. - Add a simple debug dump when NYASH_MIR_TRACE_HINTS=1. +Runtime Trace +- Enable: `NYASH_MIR_TRACE_HINTS=1` +- Backend: any path that builds MIR (e.g., `--backend vm`) +- Output (stderr): + - `[mir][hint] ScopeEnter(0)` at function entry + - `[mir][hint] JoinResult(x)` when both branches assign same variable `x` + - `[mir][hint] ScopeLeave(0)` at function exit + +Example +- `apps/tests/macro/if/assign_both_branches.nyash` emits JoinResult(x): + - `if (cond) { x = 10 } else { x = 20 }` + - Both branches assign `x`, builder hints the join. diff --git a/tools/test/smoke/mir/hints_join_result_smoke.sh b/tools/test/smoke/mir/hints_join_result_smoke.sh new file mode 100644 index 00000000..2697185f --- /dev/null +++ b/tools/test/smoke/mir/hints_join_result_smoke.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +set -euo pipefail + +root=$(cd "$(dirname "$0")"/../../../.. && pwd) +bin="$root/target/release/nyash" +src="apps/tests/macro/if/assign_both_branches.nyash" + +if [ ! -x "$bin" ]; then + echo "nyash binary not found at $bin; build first (cargo build --release)" >&2 + exit 1 +fi + +export NYASH_MIR_TRACE_HINTS=1 + +# Capture stderr; VM backend is sufficient (MIR builder runs) +out=$({ "$bin" --backend vm "$src" 1>/dev/null; } 2>&1 || true) + +echo "$out" | rg -q "\[mir\]\[hint\] JoinResult\(x\)" || { + echo "[FAIL] missing JoinResult(x) hint" >&2 + printf '%s\n' "$out" | tail -n 60 >&2 + exit 2 +} + +echo "[OK] MIR hints JoinResult trace smoke passed" +