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

@@ -20083,6 +20083,13 @@
2008320083 if def_at_f == "string" || def_at_f == "mutable_str"
2008420084 return "(sp_SymIntHash_has_key(" + rc + ", " + key + ") ? sp_int_to_s(sp_SymIntHash_get(" + rc + ", " + key + ")) : " + defval + ")"
2008520085 end
20086+ # Sibling of the str_int_hash coerce below (#671). Int-leaf
20087+ # hash + nil default in an RBS-pinned nullable-pointer return
20088+ # collapses to NULL — the int values can't satisfy the
20089+ # declared pointer return type.
20090+ if def_at_f == "nil" && is_nullable_pointer_type(@current_method_return) == 1
20091+ return "((" + c_type(@current_method_return) + ")NULL)"
20092+ end
2008620093 return "(sp_SymIntHash_has_key(" + rc + ", " + key + ") ? sp_SymIntHash_get(" + rc + ", " + key + ") : " + defval + ")"
2008720094 end
2008820095 return "sp_SymIntHash_get((sp_SymIntHash *)(" + rc + "), " + key + ")"
@@ -20612,6 +20619,17 @@
2061220619 boxed_def = box_value_to_poly(def_at_f, defval)
2061320620 return "(sp_StrIntHash_has_key(" + rc + ", " + key + ") ? sp_box_int(sp_StrIntHash_get(" + rc + ", " + key + ")) : " + boxed_def + ")"
2061420621 end
20622+ # int-leaf + nil default inside a method whose RBS-declared
20623+ # return is a nullable pointer (e.g. `-> String?`): the int
20624+ # hash can never carry pointer values, so the only
20625+ # RBS-consistent outcome is nil (NULL). The literal empty
20626+ # `@slots = {}` shape that motivated this issue has no
20627+ # observed string writes, so we never reach a get that would
20628+ # need to surface a real value. Emit NULL. Sibling of the
20629+ # `hash[k] || rhs` coerce added by #660. Issue #671.
20630+ if def_at_f == "nil" && is_nullable_pointer_type(@current_method_return) == 1
20631+ return "((" + c_type(@current_method_return) + ")NULL)"
20632+ end
2061520633 return "(sp_StrIntHash_has_key(" + rc + ", " + key + ") ? sp_StrIntHash_get(" + rc + ", " + key + ") : " + defval + ")"
2061620634 end
2061720635 return "sp_StrIntHash_get(" + rc + ", " + key + ")"