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
void walk_scope(Compiler *c, int id, int scope_idx, int class_id) { if (id < 0 || id >= c->nt->count) return; c->nscope[id] = scope_idx; c->node_cbody[id] = g_cbody_class_id; const char *ty = nt_type(c->nt, id); int child = scope_idx; int child_class = class_id; Scope *s = comp_scope_of(c, id); int cls_id2 = s->class_id; if (!nm) continue; /* A `@ivar = v` directly in a class/module body (not in a method) belongs to that class/module object, like its class methods see it -- attribute it to the enclosing class-body rather than the Toplevel pseudo-class. */ if (cls_id2 < 0 && c->node_cbody[id] >= 0) cls_id2 = c->node_cbody[id]; if (cls_id2 < 0) { /* Top-level method: track ivars in the Toplevel pseudo-class */ int old_nc = c->nclasses;src/compiler.cmodifiedView file
Loading diff…
src/compiler.hmodifiedView file
Loading diff…