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:

  1. AST serializer. When the AssocNode's value is a PM_IMPLICIT_NODE, follow (pm_implicit_node_t *)val->value to 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.

  2. Pre-scan locals before hash inference. A new helper simple_literal_type maps StringNode/IntegerNode/FloatNode/ SymbolNode/TrueNode/FalseNode/NilNode to their canonical type names; pre_scan_simple_local_writes walks each statement, checks for a not-yet-declared LocalVariableWriteNode whose value is a leaf literal, and declare_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"}) plus length and has_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

@@ -8176,12 +8176,67 @@
81768176 result
81778177 end
81788178
8179+ # Map a simple-literal AST node to its canonical type name. Returns ""
8180+ # for anything that isn't a leaf-literal (hashes, arrays, calls, etc.).
8181+ # Used by pre_scan_simple_local_writes to seed @scope_names before
8182+ # scan_locals's first pass runs.
8183+ def simple_literal_type(nid)
8184+ if nid < 0
8185+ return ""
8186+ end
8187+ t = @nd_type[nid]
8188+ if t == "StringNode"
8189+ return "string"
8190+ end
8191+ if t == "IntegerNode"
8192+ return "int"
8193+ end
8194+ if t == "FloatNode"
8195+ return "float"
8196+ end
8197+ if t == "SymbolNode"
8198+ return "symbol"
8199+ end
8200+ if t == "TrueNode"
8201+ return "bool"
8202+ end
8203+ if t == "FalseNode"
8204+ return "bool"
8205+ end
8206+ if t == "NilNode"
8207+ return "nil"
8208+ end
8209+ ""
8210+ end
8211+
8212+ # Pre-populate @scope_names with simple-literal local writes so that
8213+ # scan_locals's pass-1 inference can resolve LocalVariableReadNode
8214+ # references during type inference. Without this, hash shorthand
8215+ # `{first:}` whose key resolves to a previously-written string-valued
8216+ # local mis-types because find_var_type runs against an empty scope and
8217+ # falls back to "int". Limited to leaf-literal initializers; method
8218+ # calls and composite literals still go through the regular passes.
8219+ def pre_scan_simple_local_writes(stmts)
8220+ stmts.each { |sid|
8221+ if @nd_type[sid] == "LocalVariableWriteNode"
8222+ lname = @nd_name[sid]
8223+ if find_var_type(lname) == ""
8224+ st = simple_literal_type(@nd_expression[sid])
8225+ if st != ""
8226+ declare_var(lname, st)
8227+ end
8228+ end
8229+ end
8230+ }
8231+ end
8232+
81798233 # ---- Feature detection ----
81808234 def detect_features
81818235 # Set up a temporary scope with main-level locals so feature detection
81828236 # can infer types of local variables correctly
81838237 push_scope
81848238 stmts = get_body_stmts(@root_id)
8239+ pre_scan_simple_local_writes(stmts)
81858240 lnames = "".split(",")
81868241 ltypes = "".split(",")
81878242 empty_p = "".split(",")
@@ -13209,6 +13264,7 @@
1320913264 ltypes = "".split(",")
1321013265
1321113266 empty_params = "".split(",")
13267+ pre_scan_simple_local_writes(stmts)
1321213268 stmts.each { |sid|
1321313269 if @nd_type[sid] != "DefNode"
1321413270 if @nd_type[sid] != "ClassNode"