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
if kk < owner_df.length def_id = owner_df[kk].to_i if def_id >= 0 # Coerce an empty `{}` default to the param's declared # hash variant (sp_SymStrHash_new vs the generic # sp_StrIntHash_new). Same shape as the positional-arg # branch in compile_call_args_with_defaults. def_expr = empty_hash_coerce(def_id, owner_pt[kk]) if def_expr == "" def_expr = compile_expr(def_id) end if ca == "" ca = compile_expr(def_id) ca = def_expr else ca = ca + ", " + compile_expr(def_id) ca = ca + ", " + def_expr end end end if k < defaults.length def_id = defaults[k].to_i if def_id >= 0 # An empty `{}` default whose receiving param has been # body-widened to a specific hash variant (e.g. # `set_cookies:` to sp_SymStrHash via callers passing # symbol-keyed hashes) needs to construct the matching # empty container, not the generic sp_StrIntHash. Mirrors # the positional-arg empty_hash_coerce branch above. if k < ptypes.length coerced_def_h = empty_hash_coerce(def_id, ptypes[k]) if coerced_def_h != "" result = result + coerced_def_h k = k + 1 next end end result = result + compile_expr(def_id) else result = result + "0"test/kwarg_default_empty_hash_kind.rbaddedView file
Loading diff…
test/kwarg_default_empty_hash_kind.rb.expectedaddedView file
Loading diff…