Commit 9000016d

9000016d8a3fMerged

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

Description

fix(codegen): cast int local to sp_* when passed as obj-typed call arg

A method returning obj_<C> | nil whose union collapses to mrb_int upstream leaves the local catching the call declared mrb_int. Property-access sites on that local (local.attr_reader) get a runtime cast via compile_int_class_fallback_expr — but call args that pass the local to a typed-pointer parameter (f(local) where the param is sp_<C> *) were previously emitted raw. The asymmetry produced gcc errors incompatible integer to pointer conversion on every such call site.

Fix: in compile_call_args_with_defaults' positional-arg branch, when the param's declared ptype is obj_<C> and the arg's inferred type is int, insert a (sp_<C> *) cast — the same shape the property-access path already emits.

Surfaced via Roundhouse's Comment#after_create_commit → Views::Articles.article(parent) shape, where parent was caught from a method returning Article | nil.

Fixes #379

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

Details

Commit
9000016d8a3f
Parent
c38448ced0e7

spinel_codegen.rbmodifiedView file

@@ -29322,6 +29322,26 @@
2932229322 next
2932329323 end
2932429324 end
29325+ # When the param expects `sp_<C> *` but the arg's static
29326+ # type is `int` (typically a local catching a method
29327+ # whose return widened to mrb_int because the union
29328+ # collapsed somewhere upstream — e.g.
29329+ # `parent = article` after a `nil.return` narrows
29330+ # parent's declared type but the call site below still
29331+ # passes `lv_parent` raw), insert the same cast that
29332+ # compile_int_class_fallback_expr already inserts at
29333+ # property-access sites. Without this, gcc errors
29334+ # `incompatible integer to pointer conversion`.
29335+ if is_obj_type(ptypes[k]) == 1
29336+ arg_at = infer_type(positional_ids[k])
29337+ if arg_at == "int"
29338+ bt_arg = base_type(ptypes[k])
29339+ arg_cname = bt_arg[4, bt_arg.length - 4]
29340+ result = result + "(sp_" + arg_cname + " *)" + compile_expr(positional_ids[k])
29341+ k = k + 1
29342+ next
29343+ end
29344+ end
2932529345 end
2932629346 result = result + compile_expr(positional_ids[k])
2932729347 else