Commit c360ff73

c360ff731f28Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +2 added

Description

test: regression for #647 deferred const init

Pin the repro from PR #648 (merged in 874ef05) — top-level CFG_VOCAB = cfg.nested.v where cfg is a main local. Before the fix this printed 0 because the const-init pre-loop fired before cfg = Cfg.new(inner); after the fix the body-loop's deferred emit lands at source order and prints 42.

Refs #647, #648.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
c360ff731f28
Parent
b801924b7c1d

test/const_init_defers_for_main_local.rbaddedView file

1+# Issue #647: a top-level `CONST = recv.method` initializer whose
2+# RHS reads a main-scope local used to emit BEFORE the body
3+# statements ran. Codegen's const-init pre-loop planted
4+# `cst_X = lv_y.iv_z;` at the top of `main`, before `lv_y =
5+# sp_Y_new()` ever ran, so the chain read from a zero-initialized
6+# struct and the const ended up as 0 (or SIGSEGV when the call
7+# reached FFI). Reporter hit the FFI crash variant in tinynn
8+# config loading.
9+#
10+# Fix (PR #648): expr_reads_main_local detects the dependency,
11+# the pre-loop skips flagged consts, and the body-statement loop
12+# emits them in source order — by then the local has been
13+# assigned.
14+
15+class Cfg
16+ attr_reader :nested
17+ def initialize(n); @nested = n; end
18+end
19+
20+class Inner
21+ attr_reader :v
22+ def initialize; @v = 42; end
23+end
24+
25+inner = Inner.new
26+cfg = Cfg.new(inner)
27+CFG_VOCAB = cfg.nested.v # ← used to read zero-init lv_cfg, returns 0
28+puts CFG_VOCAB.to_s