Commit 947d6fbe
947d6fbe502bMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
Fill optional-arg defaults at call sites for reopened-builtin methods
A method added to a reopened builtin class (class String; def pad(n=3); ...) that has an optional parameter, when called without that argument, emitted a C call with too few arguments -- "too few arguments to function 'sp___oc_String_pad'" -- and cc failed. Reopening a user class worked because that routes through the normal typed-call path, which fills omitted trailing optionals from the recorded defaults; the open-class (_oc_) path did not.
The cause was two-part. In analyze, register_open_class_def recorded a hardcoded "0" placeholder in @meth_has_defaults instead of the actual default expressions, so the defaults were never available. In codegen, compile_open_class_dispatch_expr built the call from the supplied args only (compile_call_args) and never padded omitted slots.
Record the real defaults via collect_defaults_str, and route the open-class call args through compile_call_args_with_defaults so omitted optionals are filled (and supplied args are cast to the slot type). This covers the direct-def path, the bool (TrueClass/FalseClass) path, and the include-into-builtin path (which shares register_open_class_def).
The compiler contains no builtin reopens, so register_open_class_def and the _oc codegen branches never execute when compiling the compiler; neither change can affect self-host inference or codegen.
Closes #1348
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 947d6fbe502b
- Parent
- 18ea9a96c426
spinel_analyze.rbmodifiedView file
@meth_return_types.push("int") @meth_body_ids.push(@nd_body[sid]) @meth_has_yield.push(0) @meth_has_defaults.push("0") # Record the actual default expressions (parallel to params) rather # than a "0" placeholder, so a call site that omits an optional arg # gets its default filled in instead of emitting too-few arguments. @meth_has_defaults.push(collect_defaults_str(sid)) @meth_rest_index.push(collect_rest_index(sid)) @meth_kwrest_index.push(collect_kwrest_index(sid)) endspinel_codegen.rbmodifiedView file
Loading diff…
test/open_class_default_arg.rbaddedView file
Loading diff…
test/open_class_default_arg.rb.expectedaddedView file
Loading diff…