Commit 98377fca

98377fcacc87Merged

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

Description

fix(codegen): FFI :str / :ptr arg extracts .v.s / .v.p from sp_RbVal

Closes #502. An FFI function declared with a :str parameter takes const char *. When the call site passed an sp_RbVal value (e.g. an ivar widened to poly via a ternary or branch), compile_ffi_func_call emitted the raw struct as the arg and the C compile failed with "passing 'sp_RbVal' to parameter of incompatible type 'const char *'".

Fix in compile_ffi_func_call's per-arg coercion:

  • spec == "str": when infer_type(arg) is "poly", emit (<arg>).v.s (string payload of the tagged union) instead of the raw expression.

  • spec == "ptr": same shape, emit ((void *)(<arg>).v.p) for poly args.

Non-poly args keep the original direct-forwarding behavior.

The infer_type() call is inlined at each branch rather than bound to a local because adding fresh locals here trips self-host scan_locals (see feedback_self_host_recursive_fn_arm).

Test fixture uses libc's atoi(const char *) (an always- available external) to exercise the :str coercion through a real call; the test asserts the program output under -Werror, which fails the build if the .v.s extraction regresses.

Local: 475 tests pass.

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

Details

Commit
98377fcacc87
Parent
8b564e01c786

spinel_codegen.rbmodifiedView file

@@ -4620,11 +4620,25 @@
46204620 spec = arg_specs[k]
46214621 ctype = ffi_c_type_of(spec)
46224622 # `:str` forwards the raw `const char *` directly — casting would
4623- # discard `const` and trigger a warning.
4623+ # discard `const` and trigger a warning. When the arg's static
4624+ # type is poly (sp_RbVal), extract `.v.s` (issue #502). The
4625+ # ternary on infer_type is inlined rather than bound to a local
4626+ # because adding fresh locals here trips self-host scan_locals;
4627+ # see feedback_self_host_recursive_fn_arm.
46244628 if spec == "str"
4625- result = result + compile_expr(call_args[k])
4629+ if infer_type(call_args[k]) == "poly"
4630+ @needs_rb_value = 1
4631+ result = result + "(" + compile_expr(call_args[k]) + ").v.s"
4632+ else
4633+ result = result + compile_expr(call_args[k])
4634+ end
46264635 elsif spec == "ptr"
4627- result = result + "((void *)" + compile_expr(call_args[k]) + ")"
4636+ if infer_type(call_args[k]) == "poly"
4637+ @needs_rb_value = 1
4638+ result = result + "((void *)(" + compile_expr(call_args[k]) + ").v.p)"
4639+ else
4640+ result = result + "((void *)" + compile_expr(call_args[k]) + ")"
4641+ end
46284642 elsif spec == "float_array" || spec == "int_array"
46294643 # Zero-copy bulk transfer (#474). The Spinel Array's contiguous
46304644 # storage lives at `.data`; we hand the raw pointer to C with