Commit d9c51f33

d9c51f33f282Merged

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

Description

Nullable via poly: return-site boxing, empty-method nil, poly reader dispatch

  • Box the value at return / tail positions when the method's return type is poly (the third boxing site, after args and assignment). A bare return in a poly method returns sp_box_nil().
  • An empty method body returns poly nil (a void C function yields nothing for f.nil?/f.inspect to read).
  • Poly method dispatch also resolves attr readers (field access per cls_id) and infers their ivar type.
  • poly.is_a?(Hash) and a corrected Array cls_id range (-1..-12).

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

Details

Commit
d9c51f33f282
Parent
285ceacf79cf

src/analyze.cmodifiedView file

@@ -181,7 +181,14 @@
181181 TyKind r = TY_UNKNOWN; int found = 0;
182182 for (int k = 0; k < c->nclasses; k++) {
183183 int mi = comp_method_in_chain(c, k, name, NULL);
184- if (mi >= 0) { r = found ? ty_unify(r, c->scopes[mi].ret) : c->scopes[mi].ret; found = 1; }
184+ if (mi >= 0) { r = found ? ty_unify(r, c->scopes[mi].ret) : c->scopes[mi].ret; found = 1; continue; }
185+ int rdcls = -1;
186+ if (comp_reader_in_chain(c, k, name, &rdcls)) {
187+ char ivn[256]; snprintf(ivn, sizeof ivn, "@%s", name);
188+ int iv = comp_ivar_index(&c->classes[rdcls], ivn);
189+ TyKind rt2 = iv >= 0 ? c->classes[rdcls].ivar_types[iv] : TY_UNKNOWN;
190+ r = found ? ty_unify(r, rt2) : rt2; found = 1;
191+ }
185192 }
186193 if (found) return r;
187194 }
@@ -1200,7 +1207,13 @@
12001207 /* implicit return: the body's value */
12011208 for (int s = 1; s < c->nscopes; s++) {
12021209 Scope *sc = &c->scopes[s];
1203- TyKind r = sc->body >= 0 ? infer_type(c, sc->body) : TY_NIL;
1210+ /* An empty method body returns nil; if its value is used at all it must
1211+ be poly (a void C function yields nothing to read). */
1212+ int empty_body = sc->body < 0;
1213+ if (sc->body >= 0 && nt_type(nt, sc->body) && !strcmp(nt_type(nt, sc->body), "StatementsNode")) {
1214+ int bn = 0; nt_arr(nt, sc->body, "body", &bn); if (bn == 0) empty_body = 1;
1215+ }
1216+ TyKind r = empty_body ? TY_POLY : infer_type(c, sc->body);
12041217 /* explicit returns within this scope */
12051218 for (int id = 0; id < nt->count; id++) {
12061219 const char *ty = nt_type(nt, id);