Commit 0064f376

0064f376f097Merged

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

Description

fix(codegen): move fetch-nil-default NULL coerce to return site (#674)

Regression from #671. The fetch arm for str_int_hash / sym_int_hash emitted ((const char *)NULL) whenever the surrounding method's declared return type was a nullable pointer -- including when the fetch was assigned to an int-typed LV (v = opts.fetch(:k, nil)). The pointer-NULL form into an mrb_int slot trips -Wint-conversion (and -Werror, which roundhouse uses, hard-errors).

Revert the fetch arms back to the original (has_key ? get : 0) ternary. Add a narrow return-site coerce in compile_body_return_inner's last-expression arm: when the body's tail is a typed-hash fetch(k, nil) and the method's declared return type is a nullable pointer, emit return NULL;. Anything else (LV writes, conditionals, computed expressions) keeps the int form. Predicate is fetch_with_nil_default?(last) == 1 -- the explicit == 1 matters because the helper returns Ruby 0 for the no-match case and 0 is truthy in Ruby (unlike in C); a bare && fetch_with_nil_default?(last) would fire on every tail.

Verified:

  • Issue #674 repro compiles cleanly under -Werror (was the regression). Runtime output is "found" because spinel's int-leaf hash uses int 0 as the missing-key sentinel; that's a separate pre-existing quirk unrelated to the compile failure.
  • Issue #671 repro still outputs "nil" (the original case).
  • Default tests 650/0/0 (was 649; the new regression test adds one). Promote 648/0/0 unchanged. optcarrot 59662 unchanged.

Closes #674.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
0064f376f097
Parent
6ec47505bad4

spinel_codegen.rbmodifiedView file

@@ -20163,13 +20163,6 @@
2016320163 if def_at_f == "string" || def_at_f == "mutable_str"
2016420164 return "(sp_SymIntHash_has_key(" + rc + ", " + key + ") ? sp_int_to_s(sp_SymIntHash_get(" + rc + ", " + key + ")) : " + defval + ")"
2016520165 end
20166- # Sibling of the str_int_hash coerce below (#671). Int-leaf
20167- # hash + nil default in an RBS-pinned nullable-pointer return
20168- # collapses to NULL — the int values can't satisfy the
20169- # declared pointer return type.
20170- if def_at_f == "nil" && is_nullable_pointer_type(@current_method_return) == 1
20171- return "((" + c_type(@current_method_return) + ")NULL)"
20172- end
2017320166 return "(sp_SymIntHash_has_key(" + rc + ", " + key + ") ? sp_SymIntHash_get(" + rc + ", " + key + ") : " + defval + ")"
2017420167 end
2017520168 return "sp_SymIntHash_get((sp_SymIntHash *)(" + rc + "), " + key + ")"
@@ -20699,17 +20692,6 @@
2069920692 boxed_def = box_value_to_poly(def_at_f, defval)
2070020693 return "(sp_StrIntHash_has_key(" + rc + ", " + key + ") ? sp_box_int(sp_StrIntHash_get(" + rc + ", " + key + ")) : " + boxed_def + ")"
2070120694 end
20702- # int-leaf + nil default inside a method whose RBS-declared
20703- # return is a nullable pointer (e.g. `-> String?`): the int
20704- # hash can never carry pointer values, so the only
20705- # RBS-consistent outcome is nil (NULL). The literal empty
20706- # `@slots = {}` shape that motivated this issue has no
20707- # observed string writes, so we never reach a get that would
20708- # need to surface a real value. Emit NULL. Sibling of the
20709- # `hash[k] || rhs` coerce added by #660. Issue #671.
20710- if def_at_f == "nil" && is_nullable_pointer_type(@current_method_return) == 1
20711- return "((" + c_type(@current_method_return) + ")NULL)"
20712- end
2071320695 return "(sp_StrIntHash_has_key(" + rc + ", " + key + ") ? sp_StrIntHash_get(" + rc + ", " + key + ") : " + defval + ")"
2071420696 end
2071520697 return "sp_StrIntHash_get(" + rc + ", " + key + ")"
@@ -39035,6 +39017,28 @@
3903539017 c_expr
3903639018 end
3903739019
39020+ # Returns 1 if `nid` is a `<typed_hash>.fetch(k, nil)` CallNode --
39021+ # the shape that motivates the issue #671 return-site coerce. Only
39022+ # fires for int-leaf hash variants (str_int_hash / sym_int_hash):
39023+ # for those, the ternary's get arm returns mrb_int and a nil default
39024+ # also lowers to 0, so the whole expression is "int" but the
39025+ # surrounding method may have an RBS-pinned nullable-pointer return.
39026+ def fetch_with_nil_default?(nid)
39027+ if nid < 0 || @nd_type[nid] != "CallNode"
39028+ return 0
39029+ end
39030+ return 0 if @nd_name[nid] != "fetch"
39031+ recv_fn = @nd_receiver[nid]
39032+ return 0 if recv_fn < 0
39033+ rt_fn = infer_type(recv_fn)
39034+ return 0 if rt_fn != "str_int_hash" && rt_fn != "sym_int_hash"
39035+ args_id_fn = @nd_arguments[nid]
39036+ return 0 if args_id_fn < 0
39037+ aargs_fn = get_args(args_id_fn)
39038+ return 0 if aargs_fn.length < 2
39039+ @nd_type[aargs_fn[1]] == "NilNode" ? 1 : 0
39040+ end
39041+
3903839042 def compile_body_return_inner(body_id, return_type)
3903939043 if body_id < 0
3904039044 if return_type != "void"
@@ -39464,6 +39468,16 @@
3946439468 # const-char-*-typed return signature matches.
3946539469 @needs_rb_value = 1
3946639470 emit(" return (" + val + ").v.s;")
39471+ elsif (expr_type == "int" || expr_type == "nil") && is_nullable_pointer_type(return_type) == 1 && fetch_with_nil_default?(last) == 1
39472+ # Issue #671: `hash.fetch(k, nil)` against an int-leaf hash
39473+ # (typically empty `@slots = {}`) returns int but the
39474+ # surrounding method's RBS-pinned return type is a nullable
39475+ # pointer (`String?`). The int hash can't carry pointer
39476+ # values, so the only RBS-consistent outcome is NULL. Limit to
39477+ # the actual return site (not every assignment site) so an
39478+ # int-typed LV using the same fetch doesn't get the pointer
39479+ # NULL emit -- the #674 regression.
39480+ emit(" return NULL;")
3946739481 else
3946839482 # `--int-overflow=promote` promotes int slots (incl. return
3946939483 # type) to bigint at analyze time, so a literal/int-typed