Commit 1a91d107

1a91d107fe2eMerged

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

Description

fix(codegen): empty-{} kwarg default uses param's hash variant

Closes #486. A class method with a kwarg set_cookies: {} whose param has been body-widened to a specific hash variant (e.g. sp_SymStrHash * because other callers pass sym-keyed hashes) emitted the generic sp_StrIntHash_new() at call sites that omitted the kwarg. The C compile then warned with -Wincompatible-pointer-types and the receiving function read through the wrongly-typed pointer.

Root: the class-method dispatch's missing-trailing-default loop (in compile_constant_recv_expr's cmeth branch) called compile_expr(def_id) directly, oblivious to the param's declared type. The companion positional-arg branch in compile_call_args_with_defaults already routed empty {} literals through empty_hash_coerce; this commit applies the same coercion at the class-method missing-default emit.

Coverage: just the missing-kwarg-default case. The orthogonal class-method kwarg pass-through bug (cookies argument lowering to 0 instead of the local for W.write(..., set_cookies: cookies)) is separate and deferred.

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

Details

Commit
1a91d107fe2e
Parent
531e56766672

spinel_codegen.rbmodifiedView file

@@ -17205,10 +17205,18 @@
1720517205 if kk < owner_df.length
1720617206 def_id = owner_df[kk].to_i
1720717207 if def_id >= 0
17208+ # Coerce an empty `{}` default to the param's declared
17209+ # hash variant (sp_SymStrHash_new vs the generic
17210+ # sp_StrIntHash_new). Same shape as the positional-arg
17211+ # branch in compile_call_args_with_defaults.
17212+ def_expr = empty_hash_coerce(def_id, owner_pt[kk])
17213+ if def_expr == ""
17214+ def_expr = compile_expr(def_id)
17215+ end
1720817216 if ca == ""
17209- ca = compile_expr(def_id)
17217+ ca = def_expr
1721017218 else
17211- ca = ca + ", " + compile_expr(def_id)
17219+ ca = ca + ", " + def_expr
1721217220 end
1721317221 end
1721417222 end
@@ -20084,6 +20092,20 @@
2008420092 if k < defaults.length
2008520093 def_id = defaults[k].to_i
2008620094 if def_id >= 0
20095+ # An empty `{}` default whose receiving param has been
20096+ # body-widened to a specific hash variant (e.g.
20097+ # `set_cookies:` to sp_SymStrHash via callers passing
20098+ # symbol-keyed hashes) needs to construct the matching
20099+ # empty container, not the generic sp_StrIntHash. Mirrors
20100+ # the positional-arg empty_hash_coerce branch above.
20101+ if k < ptypes.length
20102+ coerced_def_h = empty_hash_coerce(def_id, ptypes[k])
20103+ if coerced_def_h != ""
20104+ result = result + coerced_def_h
20105+ k = k + 1
20106+ next
20107+ end
20108+ end
2008720109 result = result + compile_expr(def_id)
2008820110 else
2008920111 result = result + "0"