Commit 50f7f928

50f7f92849e7Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified

Description

Type class/module-body ivars by their enclosing class

A @x = v written directly in a class/module body (not in a method) belongs to that class/module object -- its class methods read the same slot. infer_ivar_types attributed such writes to the Toplevel pseudo-class (the body statement's scope has no class_id), so the real class's ivar never saw a typed write and defaulted to poly; codegen meanwhile emitted the access against the class (civ__), leaving every read boxed.

Record each node's enclosing class-body (node_cbody, populated in walk_scope where g_cbody_class_id is live) and, when a body-level ivar write has no method scope, attribute it to that class-body. The module ivar is now typed from its assignment.

bm_getivar_module 5.3x -> 1x vs legacy (@levar types as int, the read loop is int arithmetic instead of boxed poly). make test 845, make bench 57, optcarrot checksum 59662.

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

Details

Commit
50f7f92849e7
Parent
b21ad532afaf

src/analyze_scope.cmodifiedView file

@@ -334,6 +334,7 @@
334334 void walk_scope(Compiler *c, int id, int scope_idx, int class_id) {
335335 if (id < 0 || id >= c->nt->count) return;
336336 c->nscope[id] = scope_idx;
337+ c->node_cbody[id] = g_cbody_class_id;
337338 const char *ty = nt_type(c->nt, id);
338339 int child = scope_idx;
339340 int child_class = class_id;
@@ -1834,6 +1835,10 @@
18341835 Scope *s = comp_scope_of(c, id);
18351836 int cls_id2 = s->class_id;
18361837 if (!nm) continue;
1838+ /* A `@ivar = v` directly in a class/module body (not in a method) belongs
1839+ to that class/module object, like its class methods see it -- attribute
1840+ it to the enclosing class-body rather than the Toplevel pseudo-class. */
1841+ if (cls_id2 < 0 && c->node_cbody[id] >= 0) cls_id2 = c->node_cbody[id];
18371842 if (cls_id2 < 0) {
18381843 /* Top-level method: track ivars in the Toplevel pseudo-class */
18391844 int old_nc = c->nclasses;