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

@@ -5039,7 +5039,26 @@
50395039 # mismatch (both widths match on every supported target).
50405040 result = result + "((" + ctype + ")(" + compile_expr(call_args[k]) + ")->data)"
50415041 else
5042- result = result + "((" + ctype + ")(" + compile_expr(call_args[k]) + "))"
5042+ # Numeric FFI param (int / uint32 / float / double / ...) with a
5043+ # poly-typed arg expression: cast the sp_RbVal struct directly
5044+ # would emit `(int)({.tag=...})` which gcc rejects with
5045+ # "aggregate value used where an integer was expected". Extract
5046+ # the unboxed payload first via `.v.i` (integers / bool / ptr-
5047+ # shaped) or `.v.f` (floats). The poly arg arises from a sibling
5048+ # cross-class widening pass — e.g. a `recv.attr = val` on a
5049+ # poly receiver widens every class with an attr_writer of that
5050+ # name, so a Mat#@nrows that's structurally int can carry an
5051+ # sp_RbVal slot type after analyze. Issue #626 sub-issue 1.
5052+ if infer_type(call_args[k]) == "poly"
5053+ @needs_rb_value = 1
5054+ if spec == "float" || spec == "double"
5055+ result = result + "((" + ctype + ")(" + compile_expr(call_args[k]) + ").v.f)"
5056+ else
5057+ result = result + "((" + ctype + ")(" + compile_expr(call_args[k]) + ").v.i)"
5058+ end
5059+ else
5060+ result = result + "((" + ctype + ")(" + compile_expr(call_args[k]) + "))"
5061+ end
50435062 end
50445063 k = k + 1
50455064 end