Commit fd34ce47

fd34ce478a74Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

diag(codegen): warn on uninitialized constants instead of hard error

Pairs with the warn-and-emit-0 fallback at unresolved method call sites (b17ec47). Hard error here (issue #75) bails on programs whose unsupported idioms — e.g. optcarrot's

CLK_1, CLK_2, ..., CLK_8 = (1..8).map { |i| i * RP2A03_CC }

multi-assign destructuring from a Range#map block, neither of which spinel detects yet — would otherwise prevent codegen from completing, even though the rest of the program could compile and surface useful warnings about its other gaps.

Same dedupe scheme as warn_unresolved_call (one warning per name). The warning surface is now uniform: every unresolved name (call or constant) produces one stderr line plus a 0 placeholder. Real typos still get flagged loudly; the difference is that codegen finishes and the user gets the full punch list at once.

Bootstrap (gen2 == gen3) clean; 199 tests + 56 benches pass.

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

Details

Commit
fd34ce478a74
Parent
02005f26deba

spinel_codegen.rbmodifiedView file

@@ -4203,6 +4203,23 @@
42034203 $stderr.puts "warning: cannot resolve call to '" + mname + "' on " + recv_tag + " (emitting 0)"
42044204 end
42054205
4206+ # Same dedupe pattern as warn_unresolved_call but for unknown
4207+ # ConstantReadNode names. Reuses @unresolved_call_warnings so a
4208+ # single program with both an undefined method and an undefined
4209+ # constant produces two distinct warnings, not interleaved noise.
4210+ def warn_unresolved_const(rname)
4211+ key = "_const_:" + rname
4212+ i = 0
4213+ while i < @unresolved_call_warnings.length
4214+ if @unresolved_call_warnings[i] == key
4215+ return
4216+ end
4217+ i = i + 1
4218+ end
4219+ @unresolved_call_warnings.push(key)
4220+ $stderr.puts "warning: uninitialized constant '" + rname + "' (emitting 0)"
4221+ end
4222+
42064223 # Walk every class's parent chain. A cycle anywhere on the chain is
42074224 # a fatal program error: bail with a clear message instead of letting
42084225 # the recursive helpers loop forever. Self-inheritance (`class A < A`)
@@ -13549,13 +13566,19 @@
1354913566 # Built-in module-like constants (Math, File, ENV, …) and
1355013567 # registered classes / modules legitimately reach here as a
1355113568 # method-call receiver and don't need their own value at the
13552- # use site. Any other unresolved constant means the user wrote
13553- # a name we don't know about — reject it now with a clear
13554- # NameError-style message instead of emitting a bare C
13555- # identifier that the C compiler later trips over (issue #75).
13569+ # use site. Any other unresolved constant: warn and emit 0,
13570+ # paired with the warn-and-emit-0 fallback at unresolved method
13571+ # call sites (b17ec47). Hard error here used to be the design
13572+ # (issue #75) but it bails on programs whose unsupported
13573+ # idioms (e.g. `CLK_1, ..., CLK_8 = (1..8).map { ... }` —
13574+ # constants registered by a multi-assign-from-Range#map shape
13575+ # spinel doesn't yet detect) would otherwise compile silently
13576+ # to wrong-but-running C. Warn keeps the diagnostic surface
13577+ # consistent: every unresolved name produces one stderr line
13578+ # plus a `0` placeholder, leaving the user a clear punch list.
1355613579 if is_known_constant_name(rname) == 0
13557- $stderr.puts "Error: uninitialized constant " + rname
13558- exit(1)
13580+ warn_unresolved_const(rname)
13581+ return "0"
1355913582 end
1356013583 return rname
1356113584 end