Commit c942dc50
c942dc5086b3Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(codegen): wider-hash-variant return coerces to declared narrower variant
When the function body widens a hash slot through the empty-default
[]=path (e.g.result = {}; result["k"] = @noticewhere @notice is String?) the slot type lands at str_poly_hash, but the RBS-declared return type is Hash[String, String] -> sp_StrStrHash *. The implicit return tripped clang on the variant mismatch.
Add a hash-variant return arm: when body expr_type and declared
return type are both concrete hash variants and differ, try
hash_variant_convert; otherwise fall through to a defensive cast.
The user-level if [email protected]? guard ensures the wider variant's
contents satisfy the narrower variant's element type at runtime,
so the cast is sound for the str_poly -> str_str shape.
Tests: default 460/0/0, promote 460/0/0, optcarrot checksum 59662.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- c942dc5086b3
- Parent
- b9e53f28bcfc
spinel_codegen.rbmodifiedView file
@@ -40036,6 +40036,20 @@
4003640036
# type is `const char *`. Read the underlying buffer via4003740037
# `->data` so the return value matches the sig.4003840038
emit(" return (" + val + ")->data;")40039+
elsif is_hash_type(expr_type) == 1 && is_hash_type(base_type(return_type)) == 1 && expr_type != base_type(return_type)40040+
# Body builds a wider hash variant (e.g. str_poly_hash from40041+
# `result["k"] = nullable_str` where the slot widens to poly)40042+
# but the RBS-declared return is a narrower variant40043+
# (`Hash[String, String]` -> sp_StrStrHash *). The user-level40044+
# `if [email protected]?` guard ensures only non-nil values reach the40045+
# hash, so the wider variant's runtime contents satisfy the40046+
# narrower variant's element type. Convert at the boundary.40047+
conv_ret = hash_variant_convert(expr_type, base_type(return_type), val)40048+
if conv_ret != ""40049+
emit(" return " + conv_ret + ";")40050+
else40051+
emit(" return (" + c_type(return_type) + ")(" + val + ");")40052+
end4003940053
elsif (expr_type == "int" || expr_type == "nil") && is_nullable_pointer_type(return_type) == 1 && fetch_with_nil_default?(last) == 14004040054
# Issue #671: `hash.fetch(k, nil)` against an int-leaf hash4004140055
# (typically empty `@slots = {}`) returns int but the