Commit 7b2fd09e

7b2fd09e6224Merged

Ryan Seys <[email protected]> · 6 months ago · 2 files · ~2 modified

Description

fix: replace silent UnknownNode_* fallback with hard compile error

Previously, any Prism node not handled by spinel_parse.c's flatten() switch was emitted as UnknownNode_<numeric_type>, which compile_expr silently turned into "0". Programs using unsupported syntax compiled and ran but produced wrong output -- the worst possible failure mode.

Concrete example ($counter &&= 5 -- GlobalVariableAndWriteNode is not on master's parser switch):

BEFORE              AFTER
------              -----
CRuby:    5         spinel exits 1 with:
spinel:   1         "Spinel: cannot compile GlobalVariableAndWriteNode
          ^^^         at line 2 (unsupported Ruby syntax)"
          wrong,
          no error

This change emits an UnsupportedNode sentinel in the text AST, carrying the Prism node-type name and the source line. compile_stmt and compile_expr both refuse to compile that sentinel and exit(1) with a precise message.

The error message uses the Ruby-vocabulary node name (GlobalVariableAndWriteNode, FlipFlopNode, etc.) rather than the C macro form (PM_GLOBAL_VARIABLE_AND_WRITE_NODE). The parser does the prefix-strip + PascalCase conversion inline so the message reads as Ruby docs the user can google.

Self-host audit shows spinel_codegen.rb itself uses only nodes the parser handles, so the bootstrap is unaffected.

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

Details

Commit
7b2fd09e6224
Parent
59ff8d7a4f30

spinel_codegen.rbmodifiedView file

@@ -648,6 +648,11 @@
648648 if field == "unescaped"
649649 @nd_unescaped[nid] = val
650650 end
651+ if field == "kind"
652+ # UnsupportedNode carries the Prism node-type name here so
653+ # codegen can surface a precise compile error.
654+ @nd_content[nid] = val
655+ end
651656 end
652657
653658 def set_int_field(nid, field, val)
@@ -666,6 +671,11 @@
666671 if field == "start_line"
667672 @nd_value[nid] = val
668673 end
674+ if field == "source_line"
675+ # UnsupportedNode carries the source line so codegen can cite
676+ # location in the compile error.
677+ @nd_value[nid] = val
678+ end
669679 end
670680
671681 def set_ref_field(nid, field, ref_id)
@@ -17728,6 +17738,13 @@
1772817738 return "0"
1772917739 end
1773017740 t = @nd_type[nid]
17741+ if t == "UnsupportedNode"
17742+ # The parser emitted this sentinel because it hit a Prism node
17743+ # type it doesn't know how to serialize. Refusing to compile is
17744+ # far better than the historical silent "0".
17745+ $stderr.puts "Spinel: cannot compile " + @nd_content[nid] + " at line " + @nd_value[nid].to_s + " (unsupported Ruby syntax)"
17746+ exit(1)
17747+ end
1773117748 if t == "IntegerNode"
1773217749 return @nd_value[nid].to_s
1773317750 end
@@ -25295,6 +25312,11 @@
2529525312 return
2529625313 end
2529725314 t = @nd_type[nid]
25315+ if t == "UnsupportedNode"
25316+ # Same loud-error path as compile_expr's UnsupportedNode arm.
25317+ $stderr.puts "Spinel: cannot compile " + @nd_content[nid] + " at line " + @nd_value[nid].to_s + " (unsupported Ruby syntax)"
25318+ exit(1)
25319+ end
2529825320 if t == "MultiWriteNode"
2529925321 compile_multi_write(nid)
2530025322 return