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:
-
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 helperswiden_nil_ptypes(per-array) andwiden_nil_defaults_to(top-level table). Applies symmetrically to top-level methods, class instance methods, and class methods. -
codegen (
compile_expr_for_expected_typestring-arm): when the argument isniland the expected slot is nullable (string?), emitNULLinstead 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
end rt = infer_body_return(@meth_body_ids[i]) @meth_return_types[i] = rt # Back-propagate: a `default = nil` param leaves its slot typed # "nil" (= mrb_int at C codegen). If the function's overall return # type is a nullable pointer (e.g. "string?"), an implicit-return # of the param mismatches the function signature (`return mrb_int` # vs declared `const char *`). Widen those params to the return # type's nullable form so the implicit-return and any callers' # default-value sites lower as NULL of the right pointer type. if is_nullable_pointer_type(rt) == 1 widen_nil_defaults_to(@meth_param_types, i, rt) end @current_method_name = saved_meth_ar pop_scope i = i + 1 if j < returns.length returns[j] = rt end # Same back-propagation as the top-level case: widen any "nil"- # typed param (from `default = nil`) to the function's nullable # pointer return type, so `return param` doesn't emit a mrb_int → # const-char* mismatch. if is_nullable_pointer_type(rt) == 1 widened = widen_nil_ptypes(ptypes, rt) if widened != 0 if j < all_ptypes.length all_ptypes[j] = ptypes.join(",") end @cls_meth_ptypes[i] = all_ptypes.join("|") @cls_meth_ptypes_version = @cls_meth_ptypes_version + 1 end end # Save incrementally so later methods can see updated return types @cls_meth_returns[i] = returns.join(";") @cls_meth_return_cache = {} if j < cm_returns.length cm_returns[j] = rt end if is_nullable_pointer_type(rt) == 1 widened = widen_nil_ptypes(cptypes, rt) if widened != 0 if j < cm_ptypes.length cm_ptypes[j] = cptypes.join(",") end @cls_cmeth_ptypes[i] = cm_ptypes.join("|") end end pop_scope j = j + 1 end end end # Widen "nil"-typed entries in `ptypes` to the nullable form of `rt`. # Used by the back-propagation pass in infer_all_returns: if a method # returns a nullable pointer (e.g. "string?"), any param defaulted to # nil should be typed as that nullable pointer too so `return param` # doesn't generate a mrb_int → pointer cast in C. # Returns 1 if any entry was widened, 0 otherwise. def widen_nil_ptypes(ptypes, rt) target = rt if is_nullable_type(target) == 0 target = base_type(target) + "?" end changed = 0 pk = 0 while pk < ptypes.length if ptypes[pk] == "nil" ptypes[pk] = target changed = 1 end pk = pk + 1 end changed end # Top-level method variant of widen_nil_ptypes: reads/writes # @meth_param_types[i] directly. def widen_nil_defaults_to(table, i, rt) ptypes = table[i].split(",") if widen_nil_ptypes(ptypes, rt) == 1 table[i] = ptypes.join(",") end end def infer_init_param_type(ci, pname) # Synthetic Struct.new(...) constructors (body id -2) have no AST # body to scan — the implicit rule is "param pname → @pname = pname",spinel_codegen.rbmodifiedView file
Loading diff…
test/default_nil_param_with_str_return.rbaddedView file
Loading diff…
test/default_nil_param_with_str_return.rb.expectedaddedView file
Loading diff…