Commit 20cfb9ed
20cfb9ed546dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(codegen): int-leaf hash#fetch(k, nil) in nullable-ptr return → NULL (#671)
Sibling of #660. module M; @slots = {}; def self.get(s); @slots.fetch( s, nil); end; end with RBS def self.get: (Symbol s) -> String?
previously lowered the fetch as (has_key ? sp_StrIntHash_get(...) : 0) — both ternary arms mrb_int — then the return site tried to coerce
that into the RBS-declared const char *. clang flagged the resulting
mrb_int → pointer conversion.
The empty {} defaults to str_int_hash (spinel's no-info choice for
empty hash literals). When the user wrote fetch(k, nil) and RBS
pins the surrounding return to a nullable pointer, the int hash can't
satisfy that contract — neither arm of the get/default ternary can
legitimately produce a string. Both arms collapse to NULL (the get arm
because the hash holds no strings; the default arm because nil is
already the contract's null leaf). Mirrors the hash[k] || rhs
coerce that #660 added to compile_or.
Applies to both str_int_hash and sym_int_hash fetch arms. The
emit pattern is ((<ret_type>)NULL) so the cast lands at the call
site, matching whatever pointer type the function returns.
Closes #671.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 20cfb9ed546d
- Parent
- 7ccf8d27ae19
spinel_codegen.rbmodifiedView file
if def_at_f == "string" || def_at_f == "mutable_str" return "(sp_SymIntHash_has_key(" + rc + ", " + key + ") ? sp_int_to_s(sp_SymIntHash_get(" + rc + ", " + key + ")) : " + defval + ")" end # Sibling of the str_int_hash coerce below (#671). Int-leaf # hash + nil default in an RBS-pinned nullable-pointer return # collapses to NULL — the int values can't satisfy the # declared pointer return type. if def_at_f == "nil" && is_nullable_pointer_type(@current_method_return) == 1 return "((" + c_type(@current_method_return) + ")NULL)" end return "(sp_SymIntHash_has_key(" + rc + ", " + key + ") ? sp_SymIntHash_get(" + rc + ", " + key + ") : " + defval + ")" end return "sp_SymIntHash_get((sp_SymIntHash *)(" + rc + "), " + key + ")" boxed_def = box_value_to_poly(def_at_f, defval) return "(sp_StrIntHash_has_key(" + rc + ", " + key + ") ? sp_box_int(sp_StrIntHash_get(" + rc + ", " + key + ")) : " + boxed_def + ")" end # int-leaf + nil default inside a method whose RBS-declared # return is a nullable pointer (e.g. `-> String?`): the int # hash can never carry pointer values, so the only # RBS-consistent outcome is nil (NULL). The literal empty # `@slots = {}` shape that motivated this issue has no # observed string writes, so we never reach a get that would # need to surface a real value. Emit NULL. Sibling of the # `hash[k] || rhs` coerce added by #660. Issue #671. if def_at_f == "nil" && is_nullable_pointer_type(@current_method_return) == 1 return "((" + c_type(@current_method_return) + ")NULL)" end return "(sp_StrIntHash_has_key(" + rc + ", " + key + ") ? sp_StrIntHash_get(" + rc + ", " + key + ") : " + defval + ")" end return "sp_StrIntHash_get(" + rc + ", " + key + ")"