Commit aa800fe3

aa800fe35c0bMerged

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

Description

fix(codegen): str_str_hash / str_int_hash bracket-assign no longer elides

Root cause for #610: compile_bracket_assign had emit arms for sym_int_hash, sym_str_hash, sym_poly_hash, str_poly_hash, poly_poly_hash, int_str_hash, and every array variant, but the str_str_hash and str_int_hash arms were missing. Plus the nullable suffix wasn't stripped at entry. So <str_str_hash?>[k] = v reached this function, matched no rt branch, and returned with no emit at all — silent elision of the assignment. The whole-block body of a .each { merged[k.to_s] = ... } over a path-params dup vanished.

Fixes:

  • Strip the nullable ? at the top via base_type so a str_str_hash? (the canonical shape returned by an attr reader on an instance variable initialized nil) dispatches through the same emit as str_str_hash.

  • Add the two missing arms: str_int_hash routes the value through compile_expr_as_int and str_str_hash through compile_expr_as_string. The expr-as helpers handle the poly / sym / int RHS shapes that flow into a hash key write, so a merged[k.to_s] = v where v is sp_RbVal unboxes via sp_poly_to_s rather than silently dropping.

Test: test/str_str_hash_nullable_bracket_assign.rb exercises the nullable shape end-to-end. Pre-fix the .h["c"] = "3" assignments disappeared and the hash still held only the two seeded entries; post-fix all four reads come back with the expected values.

The roundhouse comments_controller_test body (the dispatch_request merge loop) no longer elides its body. A separate follow-on type error in stringify_keys(sp_RbVal) surfaces now that the surrounding code is emitted; that's an orthogonal issue on the analyzer's Hash-vs-leaf union and isn't part of this fix.

Refs #610. 586 tests pass / bootstrap fixpoint clean.

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

Details

Commit
aa800fe35c0b
Parent
b0d961748de7

spinel_codegen.rbmodifiedView file

@@ -29198,6 +29198,14 @@
2919829198 def compile_bracket_assign(nid)
2919929199 recv = @nd_receiver[nid]
2920029200 rt = infer_type(recv)
29201+ # Nullable hash receivers (`str_str_hash?` etc.) should dispatch
29202+ # identically to the base hash; NULL-check semantics live at the
29203+ # caller, mirroring compile_call_expr's strip-? handling. Without
29204+ # this, `<str_str_hash?>[k] = v` fell through every arm and
29205+ # emitted nothing — the silent-elision shape reported in #610.
29206+ if is_nullable_pointer_type(rt) == 1 && is_nullable_type(rt) == 1
29207+ rt = base_type(rt)
29208+ end
2920129209 rc = compile_expr_gc_rooted(recv)
2920229210 args_id = @nd_arguments[nid]
2920329211 arg_ids = []
@@ -29268,6 +29276,24 @@
2926829276 emit(" sp_IntStrHash_set(" + rc + ", " + idx + ", " + val + ");")
2926929277 return
2927029278 end
29279+ # str_int_hash / str_str_hash were both missing here previously,
29280+ # so `<str_str_hash>[k] = v` fell through compile_bracket_assign
29281+ # with no emit at all — the silent-elision shape reported in #610
29282+ # where a typed-hash write inside a `.each` block disappeared.
29283+ # The value side coerces through compile_expr_as_int /
29284+ # compile_expr_as_string so a poly-typed RHS (the canonical
29285+ # `v.is_a?(Hash) ? ... : v` ternary shape) unboxes to the cell's
29286+ # stored type rather than dropping silently.
29287+ if rt == "str_int_hash"
29288+ val_int = compile_expr_as_int(arg_ids[1])
29289+ emit(" sp_StrIntHash_set(" + rc + ", " + idx + ", " + val_int + ");")
29290+ return
29291+ end
29292+ if rt == "str_str_hash"
29293+ val_str = compile_expr_as_string(arg_ids[1])
29294+ emit(" sp_StrStrHash_set(" + rc + ", " + idx + ", " + val_str + ");")
29295+ return
29296+ end
2927129297 if rt == "sym_int_hash"
2927229298 emit(" sp_SymIntHash_set(" + rc + ", " + idx + ", " + val + ");")
2927329299 return