Commit cede0837
cede08371e1aMerged
Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
fix(codegen): exclude classes stored in ptr_array from value-type optimization
A class with no attr_writers and no mutating methods is normally
optimized into a value type — sp_Foo_new(x) returns the struct by
value. But when its instances appear in an array literal, the
inferred container is obj_Foo_ptr_array (sp_PtrArray *), whose
_push takes void *. The push call ended up emitting
sp_PtrArray_push(arr, sp_Foo_new(1)), which doesn't compile:
/tmp/_t.c:43:27: error: incompatible type for argument 2 of 'sp_PtrArray_push' 43 | sp_PtrArray_push(_t1, sp_Foo_new(1)); | ^~~~~~~~~~~~~ | sp_Foo {aka struct sp_Foo_s} note: expected 'void *' but argument is of type 'sp_Foo'
The bug fires whenever a value-type-eligible class is referenced via
a literal [Foo.new(...)] anywhere in the program.
Add detect_ptr_array_stored_types: scan the AST for ArrayNodes
whose inferred type is an obj_<C>_ptr_array, and exclude <C>
from the value-type optimization (parallel to the existing
detect_param_mutated_types exclusion). The class then keeps the
heap-allocated sp_<C> * representation, and sp_<C>_new returns a
pointer that fits the void * push signature.
Adds test/ptr_array_of_value_class.rb.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- cede08371e1a
- Parent
- 0c3c5d918fc5
spinel_codegen.rbmodifiedView file
"" end def detect_ptr_array_stored_types # Find object types `obj_<C>` that appear as the element type of an # array literal. Such an array becomes a `sp_PtrArray *` whose # `_push` takes `void *`; if `<C>` were optimized into a value type # then `sp_<C>_new(...)` would return the struct by value and the # generated push call would be a C type error. @ptr_array_stored_types = "".split(",") nid = 0 while nid < @nd_type.length if @nd_type[nid] == "ArrayNode" at = infer_array_elem_type(nid) if is_ptr_array_type(at) == 1 obj_t = ptr_array_elem_type(at) if is_obj_type(obj_t) == 1 if not_in(obj_t, @ptr_array_stored_types) == 1 @ptr_array_stored_types.push(obj_t) end end end end nid = nid + 1 end end def detect_param_mutated_types # Find classes whose instances are mutated when passed as method parameters @param_mutated_types = "".split(",") auto_register_attr_readers auto_register_attr_writers detect_param_mutated_types detect_ptr_array_stored_types # Multiple passes: value type detection depends on other classes 2.times do i = 0 pmi = pmi + 1 end end # Exclude classes whose instances are pushed into a ptr_array # (array literal of `obj_<C>` becomes a `sp_PtrArray *` whose # `_push` takes `void *`; a value-type return from `Foo.new` # is a struct by value and can't be passed through `void *`). if all_val == 1 type_str = "obj_" + @cls_names[i] psi = 0 while psi < @ptr_array_stored_types.length if @ptr_array_stored_types[psi] == type_str all_val = 0 end psi = psi + 1 end end if all_val == 1 @cls_is_value_type[i] = 1 endtest/ptr_array_of_value_class.rbaddedView file
Loading diff…