Commit b12c4fab
b12c4fabe3f8Merged
Ryan Seys <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
feat(parse,codegen): support hash shorthand ({ x:, y: })
Ruby 3.1+ hash shorthand { x:, y: } lowers to an AssocNode whose
value is a PM_IMPLICIT_NODE that wraps the actual LocalVariableReadNode
(or MethodCallNode for an undeclared name). The codegen previously
emitted UnknownNode_69 for the implicit wrapper, leaving the value
slot uncompiled and producing zero-valued output. A separate
inference-ordering bug also surfaced: infer_hash_val_type ran
before scan_locals populated @scope_names, so {first:} over a
string-valued local mis-typed the hash as sp_SymIntHash /
sp_SymPolyHash rather than sp_SymStrHash.
Fix has two coordinated halves:
-
AST serializer. When the AssocNode's value is a PM_IMPLICIT_NODE, follow
(pm_implicit_node_t *)val->valueto get at the real expression. The codegen never sees the wrapper, so the existing AssocNode + LocalVariableReadNode compile path handles the shorthand with zero codegen change. -
Pre-scan locals before hash inference. A new helper
simple_literal_typemaps StringNode/IntegerNode/FloatNode/ SymbolNode/TrueNode/FalseNode/NilNode to their canonical type names;pre_scan_simple_local_writeswalks each statement, checks for a not-yet-declared LocalVariableWriteNode whose value is a leaf literal, anddeclare_vars it. Invoked at the top of both detect_features and emit_main, immediately after the matching push_scope. Composite initializers (hashes, arrays, method calls) still flow through the regular passes — only the leaf-literal-then-shorthand-reference case is covered. With the pre-scan in place,{first:}over a string-valued local correctly types as sp_SymStrHash.
Tests exercise eight shapes total:
- two-key shorthand from int-valued locals (
{x:, y:}) - single-key shorthand inside a method body (
{weight:}) - mixing shorthand and explicit pair (
{n:, m:, sum: 18}) - single shorthand
{name:}over a string-valued local - mixed shorthand and explicit pair (
{first:, last: "lovelace"}) - the explicit equivalent for byte-equal output
- three-key all-shorthand (
{a:, b:, c:}) - shorthand + explicit + shorthand mixed (
{x:, y:, z: "three"}) pluslengthandhas_key?reads to confirm the chosen hash variant holds up under the runtime API.
Out of scope: method-body scopes (scan_locals at method-emission sites) are not pre-scanned. The bug pattern can manifest there too, but the documented reproducer is at top level; the method-body case will land separately if it surfaces.
Bootstrap: gen2.c == gen3.c. Tests: 201 pass.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- b12c4fabe3f8
- Parent
- 02005f26deba
spinel_codegen.rbmodifiedView file
result end # Map a simple-literal AST node to its canonical type name. Returns "" # for anything that isn't a leaf-literal (hashes, arrays, calls, etc.). # Used by pre_scan_simple_local_writes to seed @scope_names before # scan_locals's first pass runs. def simple_literal_type(nid) if nid < 0 return "" end t = @nd_type[nid] if t == "StringNode" return "string" end if t == "IntegerNode" return "int" end if t == "FloatNode" return "float" end if t == "SymbolNode" return "symbol" end if t == "TrueNode" return "bool" end if t == "FalseNode" return "bool" end if t == "NilNode" return "nil" end "" end # Pre-populate @scope_names with simple-literal local writes so that # scan_locals's pass-1 inference can resolve LocalVariableReadNode # references during type inference. Without this, hash shorthand # `{first:}` whose key resolves to a previously-written string-valued # local mis-types because find_var_type runs against an empty scope and # falls back to "int". Limited to leaf-literal initializers; method # calls and composite literals still go through the regular passes. def pre_scan_simple_local_writes(stmts) stmts.each { |sid| if @nd_type[sid] == "LocalVariableWriteNode" lname = @nd_name[sid] if find_var_type(lname) == "" st = simple_literal_type(@nd_expression[sid]) if st != "" declare_var(lname, st) end end end } end # ---- Feature detection ---- def detect_features # Set up a temporary scope with main-level locals so feature detection # can infer types of local variables correctly push_scope stmts = get_body_stmts(@root_id) pre_scan_simple_local_writes(stmts) lnames = "".split(",") ltypes = "".split(",") empty_p = "".split(",") ltypes = "".split(",") empty_params = "".split(",") pre_scan_simple_local_writes(stmts) stmts.each { |sid| if @nd_type[sid] != "DefNode" if @nd_type[sid] != "ClassNode"spinel_parse.cmodifiedView file
Loading diff…
test/hash_shorthand.rbaddedView file
Loading diff…
test/hash_shorthand_str.rbaddedView file
Loading diff…