Commit 50106f3c

50106f3cc3dcMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(codegen): bare cls method call inside cls body coerces arg types

def self.exists?(id); _adapter_exists_by_id_p(id); end resolved the bare _adapter_exists_by_id_p call against @cls_cmeth_names but compiled its args through compile_call_args (no type coercion). When the sibling cmeth's param type differed from the caller's arg type (Article#exists? takes sp_RbVal id; the adapter expects mrb_int), clang errored on the raw pass.

Add compile_call_args_coerced_for_cmeth: reads the sibling cmeth's ptypes and routes each arg through compile_expr_for_expected_type when the expected type needs a coerce (poly<->concrete, hash variant, typed-array, etc.).

#685: 3 hard errors -> 2 remaining (sp_ActionResponse_new args 2,3 where sp_RbVal poly-dispatch result flows into typed const char * constructor param).

Tests: default 460/0/0, promote 460/0/0, optcarrot checksum 59662.

Refs #685

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

Details

Commit
50106f3cc3dc
Parent
4e1c20d60507

spinel_codegen.rbmodifiedView file

@@ -16210,7 +16210,7 @@
1621016210 cmidx_r = 0
1621116211 while cmidx_r < cmnames_r.length
1621216212 if cmnames_r[cmidx_r] == mname
16213- ca_r = compile_call_args(nid)
16213+ ca_r = compile_call_args_coerced_for_cmeth(nid, @current_class_idx, cmidx_r)
1621416214 owning_r = @cls_names[@current_class_idx]
1621516215 if ca_r == ""
1621616216 return "sp_" + owning_r + "_cls_" + sanitize_name(mname) + "()"
@@ -26127,6 +26127,55 @@
2612726127 0
2612826128 end
2612926129
26130+ # Like compile_call_args but coerces each arg's type to match the
26131+ # callee class-method's declared param type. Used by the bare cls
26132+ # method call path (`_adapter_exists_by_id_p(id)` inside `def self.X`)
26133+ # which previously emitted raw compile_expr and tripped clang on
26134+ # poly<->concrete mismatches (#685, Article#exists? -> Article#
26135+ # _adapter_exists_by_id_p with sp_RbVal arg into mrb_int param).
26136+ def compile_call_args_coerced_for_cmeth(nid, ci, cmidx)
26137+ args_id = @nd_arguments[nid]
26138+ if args_id < 0
26139+ return ""
26140+ end
26141+ arg_ids = get_args(args_id)
26142+ if arg_ids.length == 0
26143+ return ""
26144+ end
26145+ ptypes = cls_cmeth_ptypes_get(ci, cmidx)
26146+ result = ""
26147+ k = 0
26148+ while k < arg_ids.length
26149+ if k > 0
26150+ result = result + ", "
26151+ end
26152+ pt_arg = k < ptypes.length ? ptypes[k] : ""
26153+ at_arg = infer_type(arg_ids[k])
26154+ pt_base_arg = base_type(pt_arg)
26155+ needs_arg = 0
26156+ if pt_arg != ""
26157+ if pt_base_arg == "bigint" || pt_base_arg == "int"
26158+ needs_arg = 1
26159+ elsif at_arg == "poly" && pt_base_arg != "poly" && pt_base_arg != ""
26160+ needs_arg = 1
26161+ elsif at_arg != "poly" && pt_base_arg == "poly"
26162+ needs_arg = 1
26163+ elsif pt_base_arg == "string" || pt_base_arg == "mutable_str"
26164+ needs_arg = 1
26165+ elsif is_hash_type(pt_base_arg) == 1 || is_array_type(pt_base_arg) == 1
26166+ needs_arg = 1
26167+ end
26168+ end
26169+ if needs_arg == 1
26170+ result = result + compile_expr_for_expected_type(arg_ids[k], pt_arg)
26171+ else
26172+ result = result + cast_away_volatile_arg(arg_ids[k], compile_expr(arg_ids[k]))
26173+ end
26174+ k = k + 1
26175+ end
26176+ result
26177+ end
26178+
2613026179 def compile_call_args(nid)
2613126180 args_id = @nd_arguments[nid]
2613226181 if args_id < 0