Commit 1ed5df4a
1ed5df4a6fccMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): FFI numeric arg unboxes a poly value before the cast
compile_ffi_func_call's catch-all numeric arm emitted
((ctype)(<expr>)). With an sp_RbVal arg expression — a poly-
typed ivar read, an is_a?-narrowed local, etc. — gcc rejected
the struct-to-int cast with "aggregate value used where an
integer was expected".
Mirror the existing :str / :ptr poly handling: when the arg's
static type is poly, extract .v.i for integer / bool / pointer-
shaped FFI specs and .v.f for :float / :double. The :str
arm at the top of this function (#502) already had the same
shape — this just extends the same idea to the numeric path.
This is option B from the #626 sub-issue 1 analysis: the underlying cross-class attr_writer widening that drives the poly slot type can also be tightened (option A — narrow the poly_dispatch class set in scan_writer_calls's poly-recv arm), but B is localised and unblocks the immediate FFI compile without risking regressions in the existing widening paths. Filing a separate followup for A once the reporter shares the tokenizer source that actually triggers the widening.
Refs #626 (sub-issue 1).
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 1ed5df4a6fcc
- Parent
- 8db227b84426
spinel_codegen.rbmodifiedView file
# mismatch (both widths match on every supported target). result = result + "((" + ctype + ")(" + compile_expr(call_args[k]) + ")->data)" else result = result + "((" + ctype + ")(" + compile_expr(call_args[k]) + "))" # Numeric FFI param (int / uint32 / float / double / ...) with a # poly-typed arg expression: cast the sp_RbVal struct directly # would emit `(int)({.tag=...})` which gcc rejects with # "aggregate value used where an integer was expected". Extract # the unboxed payload first via `.v.i` (integers / bool / ptr- # shaped) or `.v.f` (floats). The poly arg arises from a sibling # cross-class widening pass — e.g. a `recv.attr = val` on a # poly receiver widens every class with an attr_writer of that # name, so a Mat#@nrows that's structurally int can carry an # sp_RbVal slot type after analyze. Issue #626 sub-issue 1. if infer_type(call_args[k]) == "poly" @needs_rb_value = 1 if spec == "float" || spec == "double" result = result + "((" + ctype + ")(" + compile_expr(call_args[k]) + ").v.f)" else result = result + "((" + ctype + ")(" + compile_expr(call_args[k]) + ").v.i)" end else result = result + "((" + ctype + ")(" + compile_expr(call_args[k]) + "))" end end k = k + 1 endtest/ffi_int_arg_poly_value.rbaddedView file
Loading diff…
test/ffi_int_arg_poly_value.rb.expectedaddedView file
Loading diff…