Commit 912e2671
912e2671aba9Merged
Ryan Seys <[email protected]> · 5 months ago · 6 files · +2 added · ~4 modified
Description
analyze,codegen: implement Class#allocate
Class#allocate on a class constant was unsupported. Add an
emit_obj_alloc_expr helper that allocates an instance with its ivars
zero/nil-initialized and cls_id stamped but WITHOUT running
initialize -- the allocation half of the constructor. It handles both
value-type objects (returned by value) and pointer objects.
The call site lowers Const.allocate to that expression; inference
returns the object type. Exception subclasses, whose constructor wires
up raise/message state, are excluded. As with any uninitialized object
under a typed layout, reading an ivar that initialize would have set
yields the field's type default rather than nil; allocate's purpose is
to build an instance whose state is assigned afterward.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 912e2671aba9
- Parent
- ed6b75edcd21
src/analyze_infer.cmodifiedView file
if (self && self->class_id >= 0) return ty_object(self->class_id); } /* Class#allocate -> a bare instance of that class (no initialize run). */ if (recv >= 0 && !strcmp(name, "allocate") && argc == 0) { const char *rty = nt_type(nt, recv); if (rty && (!strcmp(rty, "ConstantReadNode") || !strcmp(rty, "ConstantPathNode"))) { int ci = comp_class_index(c, nt_str(nt, recv, "name")); /* Use the same exception-subclass predicate as codegen (class_is_exc_subclass) so inference and emission agree on which classes take the allocate path. */ if (ci >= 0 && !class_is_exc_subclass(c, ci)) return ty_object(ci); } } /* Class.new(...) -> an instance of that class; built-in .new constructors */ if (recv >= 0 && !strcmp(name, "new")) { const char *rty = nt_type(nt, recv);src/codegen.cmodifiedView file
Loading diff…
src/codegen_call.cmodifiedView file
Loading diff…
src/codegen_internal.hmodifiedView file
Loading diff…
test/class_allocate.rbaddedView file
Loading diff…
test/class_allocate.rb.expectedaddedView file
Loading diff…