Commit 7305f810

7305f810dc73Merged

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

Description

fix(codegen): exclude push-promoted ptr_array elements from value-type opt

PR #87 excluded value-type-eligible classes from the value-type optimization when their instances appeared in an [Foo.new(...)] literal. The same root mismatch fired through the push-promotion path — arr = []; arr.push(Foo.new(...)) — where no array literal exists at scan time, so the literal-walk in detect_ptr_array_stored_types missed it. Foo got optimized into a value type and the push call tried to feed sp_PtrArray_push's void * parameter a struct-by-value, with sp_PtrArray_get further on trying to cast void * back to a struct.

Extend the same scan to also walk push-style call sites (push, <<, unshift, prepend) and add obj_<C> to the exclusion whenever a push argument has that type. The existing detect_value_types consumer of @ptr_array_stored_types then applies the same exclusion logic for the push-promoted case as for the literal-promoted case.

Mirrors the fix shape PR #91 suggested — option 1 from the issue's note.

test/ptr_array_of_value_class_push.rb covers the issue's exact reproducer plus the << variant.

Closes #91

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

Details

Commit
7305f810dc73
Parent
2966d2d40985

spinel_codegen.rbmodifiedView file

@@ -10262,6 +10262,31 @@
1026210262 end
1026310263 end
1026410264 end
10265+ # Push-promotion path (issue #91): an empty `[]` grows into an
10266+ # `obj_<C>_ptr_array` via later `push(Foo.new(...))` / `<< Foo.new(...)`
10267+ # calls. The literal-walk above doesn't see this because no
10268+ # `[Foo.new(...)]` literal exists. Inspect every push-style call
10269+ # and, if the argument's inferred type is `obj_<C>`, add `<C>`
10270+ # to the exclusion so it stays heap-allocated.
10271+ if @nd_type[nid] == "CallNode"
10272+ mname = @nd_name[nid]
10273+ if mname == "push" || mname == "<<" || mname == "unshift" || mname == "prepend"
10274+ args_id = @nd_arguments[nid]
10275+ if args_id >= 0
10276+ push_args = get_args(args_id)
10277+ pk = 0
10278+ while pk < push_args.length
10279+ at_push = infer_type(push_args[pk])
10280+ if is_obj_type(at_push) == 1
10281+ if not_in(at_push, @ptr_array_stored_types) == 1
10282+ @ptr_array_stored_types.push(at_push)
10283+ end
10284+ end
10285+ pk = pk + 1
10286+ end
10287+ end
10288+ end
10289+ end
1026510290 nid = nid + 1
1026610291 end
1026710292 end