Commit 0eb0fff5

0eb0fff518a0Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +1 added · ~3 modified

Description

refactor: extract nil-guard helpers + body-definitely-exits to compiler_helpers.rb

Five helpers were byte-for-byte duplicated between spinel_analyze.rb and spinel_codegen.rb after the #550 / #558 landings: parse_nil_predicate, body_definitely_exits?, infer_nil_guard_narrow_type, parse_nil_guard_var, and scan_back_writer_narrow_for. Both call sets depended only on node-table accessors and infer_type / get_stmts, which exist on both passes' Compiler class.

Move them to a new compiler_helpers.rb that reopens class Compiler, require_relative'd from both spinel_analyze.rb and spinel_codegen.rb just after the existing require_relative "node_table_loader" line. Drift between the two passes can't happen any more.

While here, body_definitely_exits? also recognizes BreakNode / NextNode / throw (CallNode "throw") as scope- exiting bodies. Pre-fix only return / raise counted, so patterns like break if h.nil? / next if h.nil? / throw :tag if h.nil? inside a loop didn't push the nil-guard narrow. The narrow continues to apply only to the rest of the enclosing block, matching break / next's unwind-to-loop-boundary semantics.

Makefile gains a COMPILER_HELPERS_STAMP dep on both spinel_analyze$(EXE) and spinel_codegen$(EXE) (plus the test target) so editing the shared file triggers the right rebuild. The generic build/stamps/%.stamp: % rule already covers stamp creation; no new stamp rule needed.

Verified: 527 tests pass, optcarrot checksum 59662 preserved.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
0eb0fff518a0
Parent
34ff4c895448

MakefilemodifiedView file

@@ -122,6 +122,7 @@
122122 CODEGEN_STAMP := build/stamps/spinel_codegen.rb.stamp
123123 ANALYZE_STAMP := build/stamps/spinel_analyze.rb.stamp
124124 NODE_TABLE_LOADER_STAMP := build/stamps/node_table_loader.rb.stamp
125+COMPILER_HELPERS_STAMP := build/stamps/compiler_helpers.rb.stamp
125126 PARSE_STAMP := build/stamps/spinel_parse.c.stamp
126127
127128 .PHONY: all parse bootstrap codegen test retest clean-test-results regen-expected bench optcarrot clean install uninstall deps
@@ -223,13 +224,13 @@
223224
224225 codegen: spinel_analyze$(EXE) spinel_codegen$(EXE)
225226
226-spinel_analyze$(EXE): $(ANALYZE_STAMP) $(NODE_TABLE_LOADER_STAMP) spinel_parse$(EXE)
227+spinel_analyze$(EXE): $(ANALYZE_STAMP) $(NODE_TABLE_LOADER_STAMP) $(COMPILER_HELPERS_STAMP) spinel_parse$(EXE)
227228 ./spinel_parse$(EXE) spinel_analyze.rb build/analyze.ast
228229 ruby spinel_analyze.rb build/analyze.ast build/analyze.ir
229230 ruby spinel_codegen.rb build/analyze.ast build/analyze.ir build/analyze1.c
230231 $(CC) $(BOOTSTRAP_CFLAGS) -Ilib build/analyze1.c $(LDFLAGS) -lm -o spinel_analyze$(EXE)
231232
232-spinel_codegen$(EXE): $(CODEGEN_STAMP) $(NODE_TABLE_LOADER_STAMP) spinel_parse$(EXE)
233+spinel_codegen$(EXE): $(CODEGEN_STAMP) $(NODE_TABLE_LOADER_STAMP) $(COMPILER_HELPERS_STAMP) spinel_parse$(EXE)
233234 ./spinel_parse$(EXE) spinel_codegen.rb build/codegen.ast
234235 ruby spinel_analyze.rb build/codegen.ast build/codegen.ir
235236 ruby spinel_codegen.rb build/codegen.ast build/codegen.ir build/codegen1.c
@@ -319,7 +320,7 @@
319320 # The .ok target is the test's stamp; mtime tracking gives per-test
320321 # caching for free. Order-only spinel_parse$(EXE) / spinel_analyze$(EXE)
321322 # / spinel_codegen$(EXE) stop a bootstrap relink from invalidating every test.
322-build/test-results/%.ok: test/%.rb $(SP_RT_LIB) $(CODEGEN_STAMP) $(ANALYZE_STAMP) $(NODE_TABLE_LOADER_STAMP) $(PARSE_STAMP) | spinel_parse$(EXE) spinel_analyze$(EXE) spinel_codegen$(EXE)
323+build/test-results/%.ok: test/%.rb $(SP_RT_LIB) $(CODEGEN_STAMP) $(ANALYZE_STAMP) $(NODE_TABLE_LOADER_STAMP) $(COMPILER_HELPERS_STAMP) $(PARSE_STAMP) | spinel_parse$(EXE) spinel_analyze$(EXE) spinel_codegen$(EXE)
323324 @mkdir -p build/test-results
324325 @tmpdir=$$(mktemp -d /tmp/spinel-test.XXXXXX); \
325326 ast=$$tmpdir/test.ast; \