Commit bc738871

bc7388719fe6Merged

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

Description

fix(analyze,codegen): widen nil-default params to nullable-return type

A method like

def fetch(key, default = nil) return @data[key] if @data.key?(key) default end

infers a return type of "string?" (string|nil from the @data[key] / default branches) but the default param stays typed "nil" from the literal default. C emit declared the param mrb_int lv_default while the function returned const char *, producing an int-to-pointer conversion warning at return lv_default. At call sites that omitted the default, the slot was filled with the static "" sentinel instead of NULL, so .nil? checks against the result returned false even when the caller relied on the default-nil path.

Two coordinated fixes:

  1. analyze (back-propagation in infer_all_returns): once a method's return type is known to be a nullable pointer, widen any param still typed "nil" to that nullable type. New helpers widen_nil_ptypes (per-array) and widen_nil_defaults_to (top-level table). Applies symmetrically to top-level methods, class instance methods, and class methods.

  2. codegen (compile_expr_for_expected_type string-arm): when the argument is nil and the expected slot is nullable (string?), emit NULL instead of the static empty-string sentinel.

Result: the param's C type matches the function's declared return type; return default lowers cleanly; call sites without an explicit default pass NULL and .nil? reports true.

Tests: 400 pass (+1 new regression test pinning both the class-method and top-level-method shapes), bench 52, optcarrot checksum 59662.

Fixes #447

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

Details

Commit
bc7388719fe6
Parent
df2850feefa8

spinel_analyze.rbmodifiedView file

@@ -13091,6 +13091,16 @@
1309113091 end
1309213092 rt = infer_body_return(@meth_body_ids[i])
1309313093 @meth_return_types[i] = rt
13094+ # Back-propagate: a `default = nil` param leaves its slot typed
13095+ # "nil" (= mrb_int at C codegen). If the function's overall return
13096+ # type is a nullable pointer (e.g. "string?"), an implicit-return
13097+ # of the param mismatches the function signature (`return mrb_int`
13098+ # vs declared `const char *`). Widen those params to the return
13099+ # type's nullable form so the implicit-return and any callers'
13100+ # default-value sites lower as NULL of the right pointer type.
13101+ if is_nullable_pointer_type(rt) == 1
13102+ widen_nil_defaults_to(@meth_param_types, i, rt)
13103+ end
1309413104 @current_method_name = saved_meth_ar
1309513105 pop_scope
1309613106 i = i + 1
@@ -13232,6 +13242,20 @@
1323213242 if j < returns.length
1323313243 returns[j] = rt
1323413244 end
13245+ # Same back-propagation as the top-level case: widen any "nil"-
13246+ # typed param (from `default = nil`) to the function's nullable
13247+ # pointer return type, so `return param` doesn't emit a mrb_int →
13248+ # const-char* mismatch.
13249+ if is_nullable_pointer_type(rt) == 1
13250+ widened = widen_nil_ptypes(ptypes, rt)
13251+ if widened != 0
13252+ if j < all_ptypes.length
13253+ all_ptypes[j] = ptypes.join(",")
13254+ end
13255+ @cls_meth_ptypes[i] = all_ptypes.join("|")
13256+ @cls_meth_ptypes_version = @cls_meth_ptypes_version + 1
13257+ end
13258+ end
1323513259 # Save incrementally so later methods can see updated return types
1323613260 @cls_meth_returns[i] = returns.join(";")
1323713261 @cls_meth_return_cache = {}
@@ -13291,6 +13315,15 @@
1329113315 if j < cm_returns.length
1329213316 cm_returns[j] = rt
1329313317 end
13318+ if is_nullable_pointer_type(rt) == 1
13319+ widened = widen_nil_ptypes(cptypes, rt)
13320+ if widened != 0
13321+ if j < cm_ptypes.length
13322+ cm_ptypes[j] = cptypes.join(",")
13323+ end
13324+ @cls_cmeth_ptypes[i] = cm_ptypes.join("|")
13325+ end
13326+ end
1329413327 pop_scope
1329513328 j = j + 1
1329613329 end
@@ -13301,6 +13334,38 @@
1330113334 end
1330213335 end
1330313336
13337+ # Widen "nil"-typed entries in `ptypes` to the nullable form of `rt`.
13338+ # Used by the back-propagation pass in infer_all_returns: if a method
13339+ # returns a nullable pointer (e.g. "string?"), any param defaulted to
13340+ # nil should be typed as that nullable pointer too so `return param`
13341+ # doesn't generate a mrb_int → pointer cast in C.
13342+ # Returns 1 if any entry was widened, 0 otherwise.
13343+ def widen_nil_ptypes(ptypes, rt)
13344+ target = rt
13345+ if is_nullable_type(target) == 0
13346+ target = base_type(target) + "?"
13347+ end
13348+ changed = 0
13349+ pk = 0
13350+ while pk < ptypes.length
13351+ if ptypes[pk] == "nil"
13352+ ptypes[pk] = target
13353+ changed = 1
13354+ end
13355+ pk = pk + 1
13356+ end
13357+ changed
13358+ end
13359+
13360+ # Top-level method variant of widen_nil_ptypes: reads/writes
13361+ # @meth_param_types[i] directly.
13362+ def widen_nil_defaults_to(table, i, rt)
13363+ ptypes = table[i].split(",")
13364+ if widen_nil_ptypes(ptypes, rt) == 1
13365+ table[i] = ptypes.join(",")
13366+ end
13367+ end
13368+
1330413369 def infer_init_param_type(ci, pname)
1330513370 # Synthetic Struct.new(...) constructors (body id -2) have no AST
1330613371 # body to scan — the implicit rule is "param pname → @pname = pname",