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

@@ -657,6 +657,17 @@
657657 if (self && self->class_id >= 0) return ty_object(self->class_id);
658658 }
659659
660+ /* Class#allocate -> a bare instance of that class (no initialize run). */
661+ if (recv >= 0 && !strcmp(name, "allocate") && argc == 0) {
662+ const char *rty = nt_type(nt, recv);
663+ if (rty && (!strcmp(rty, "ConstantReadNode") || !strcmp(rty, "ConstantPathNode"))) {
664+ int ci = comp_class_index(c, nt_str(nt, recv, "name"));
665+ /* Use the same exception-subclass predicate as codegen (class_is_exc_subclass)
666+ so inference and emission agree on which classes take the allocate path. */
667+ if (ci >= 0 && !class_is_exc_subclass(c, ci)) return ty_object(ci);
668+ }
669+ }
670+
660671 /* Class.new(...) -> an instance of that class; built-in .new constructors */
661672 if (recv >= 0 && !strcmp(name, "new")) {
662673 const char *rty = nt_type(nt, recv);