Commit 39cc4429
39cc4429bc19Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Implement instance_eval intrinsic + fix print for poly/symbol/nil
- analyze: infer instance_eval block body type using receiver's class context via g_ie_class_id override; post-fixpoint pass re-infers ivar read nodes inside instance_eval blocks so the codegen type cache reflects the correct types
- codegen: emit instance_eval blocks inline with g_self set to the receiver pointer; guard skips when user defines own instance_eval
- codegen/emit_print_one: handle TY_POLY (via sp_poly_to_s), TY_SYMBOL (via sp_sym_to_s), TY_NIL (no-op), and object types
Fixes bundle_classd_04 (instance_eval + nested array print).
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 39cc4429bc19
- Parent
- f93d1966ac77
src/analyze.cmodifiedView file
@@ -93,6 +93,11 @@
9393
static int g_yvt_mi[MAX_YVT_DEPTH];9494
static int g_yvt_depth = 0;9595
96+
/* Temporary class override for instance_eval block body inference.97+
-1 = no override; set to the receiver's class_id while inferring98+
the block body so that InstanceVariableReadNode uses the right class. */99+
static int g_ie_class_id = -1;100+
96101
/* The value type of `yield` / a `<&block-param>.call` inside method mi: the97102
block-body value type at a (any) call site of mi. Polymorphic, resolved from98103
the first matching caller -- matches how the rewrite inlines per call site. */@@ -1197,6 +1202,21 @@
11971202
return result;11981203
}11991204
}1205+
if (!strcmp(name, "instance_eval")) {1206+
int blk_id = nt_ref(nt, id, "block");1207+
if (blk_id >= 0 && ty_is_object(rt) &&1208+
comp_method_in_chain(c, ty_object_class(rt), "instance_eval", NULL) < 0) {1209+
int bdy = nt_ref(nt, blk_id, "body");1210+
int bbn = 0; const int *bbb = bdy >= 0 ? nt_arr(nt, bdy, "body", &bbn) : NULL;1211+
if (bbn <= 0) return TY_NIL;1212+
int saved_ie = g_ie_class_id;1213+
g_ie_class_id = ty_object_class(rt);1214+
TyKind result = infer_type(c, bbb[bbn - 1]);1215+
g_ie_class_id = saved_ie;1216+
return result;1217+
}1218+
return TY_POLY;1219+
}12001220
12011221
/* safe navigation &. with unresolved type: return poly (receiver may be nil at runtime) */12021222
{@@ -1331,8 +1351,9 @@
13311351
if (!strcmp(ty, "InstanceVariableReadNode")) {13321352
const char *nm = nt_str(nt, id, "name");13331353
Scope *s = comp_scope_of(c, id);1334-
if (s->class_id < 0) return TY_UNKNOWN;1335-
ClassInfo *ci = &c->classes[s->class_id];1354+
int cls_id = (s->class_id >= 0) ? s->class_id : g_ie_class_id;1355+
if (cls_id < 0) return TY_UNKNOWN;1356+
ClassInfo *ci = &c->classes[cls_id];13361357
int iv = nm ? comp_ivar_index(ci, nm) : -1;13371358
return iv >= 0 ? ci->ivar_types[iv] : TY_UNKNOWN;13381359
}@@ -4059,4 +4080,29 @@
40594080
40604081
for (int id = 0; id < c->nt->count; id++)40614082
infer_type(c, id);4083+
4084+
/* Re-infer nodes inside instance_eval block bodies with the receiver's class4085+
context, so ivar reads get correct types in the final c->ntype cache.4086+
Call infer_type on each body statement: it recursively re-infers all4087+
sub-expressions (including ivar reads) and updates c->ntype. */4088+
for (int id = 0; id < c->nt->count; id++) {4089+
const char *ty2 = nt_type(c->nt, id);4090+
if (!ty2 || strcmp(ty2, "CallNode")) continue;4091+
const char *nm2 = nt_str(c->nt, id, "name");4092+
if (!nm2 || strcmp(nm2, "instance_eval")) continue;4093+
int blk2 = nt_ref(c->nt, id, "block");4094+
int recv2 = nt_ref(c->nt, id, "receiver");4095+
if (blk2 < 0 || recv2 < 0) continue;4096+
TyKind rt2 = c->ntype[recv2];4097+
if (!ty_is_object(rt2)) continue;4098+
if (comp_method_in_chain(c, ty_object_class(rt2), "instance_eval", NULL) >= 0) continue;4099+
int bdy2 = nt_ref(c->nt, blk2, "body");4100+
if (bdy2 < 0) continue;4101+
int bn2 = 0; const int *bb2 = nt_arr(c->nt, bdy2, "body", &bn2);4102+
if (bn2 <= 0 || !bb2) continue;4103+
int saved2 = g_ie_class_id;4104+
g_ie_class_id = ty_object_class(rt2);4105+
for (int k2 = 0; k2 < bn2; k2++) infer_type(c, bb2[k2]);4106+
g_ie_class_id = saved2;4107+
}40624108
}src/codegen.cmodifiedView file
Loading diff…