Commit 1c9f042d

1c9f042df01eMerged

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

Description

Module singleton accessor Stage-2: sentinel cascade over distinct constants

An accessor assigned 2+ distinct module/class constants stores a boxed Class and dispatches its class methods through a cls_id cascade over the slot, rather than the Stage-1 static fold.

  • comp_sg_const_candidates collects the distinct constant class indices assigned to Class.base (Stage-1 binding is now the count==1 case); comp_sg_reader_const returns the single constant, comp_sg_reader_candidates the full set.
  • analyze: the chained call's return type unifies the candidates' class-method returns, and each candidate's params are bound from the call site.
  • codegen: X.acc.m(args) emits ({ int _c = slot.cls_id; if (_c==A) ...A_s_m.. else if (_c==B) ...B_s_m.. }), boxing per-branch when the result is poly and running the branches as plain statements when the value is unused (void).

Fixes module_singleton_accessor_poly and module_dispatch_param_widen.

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

Details

Commit
1c9f042df01e
Parent
0f6780d05783

src/analyze.cmodifiedView file

@@ -1122,6 +1122,17 @@
11221122 int mi = comp_cmethod_in_chain(c, fold_ci, name, NULL);
11231123 if (mi >= 0) return c->scopes[mi].ret;
11241124 }
1125+ /* Stage-2: accessor holds one of several constants; unify their cmethod returns. */
1126+ int cand[32];
1127+ int ncand = comp_sg_reader_candidates(c, recv, cand, 32);
1128+ if (ncand >= 2) {
1129+ TyKind r = TY_UNKNOWN;
1130+ for (int k = 0; k < ncand; k++) {
1131+ int mi = comp_cmethod_in_chain(c, cand[k], name, NULL);
1132+ if (mi >= 0) r = ty_unify(r, c->scopes[mi].ret);
1133+ }
1134+ if (r != TY_UNKNOWN) return r;
1135+ }
11251136 }
11261137
11271138 /* Class.cmethod(...) / M::Sub.cmethod(...) -> the class method's return type */
@@ -5668,6 +5679,16 @@
56685679 int fmi = comp_cmethod_in_chain(c, fold_ci, name, NULL);
56695680 if (fmi >= 0) { changed |= bind_call_params(c, id, fmi); continue; }
56705681 }
5682+ int cand[32];
5683+ int ncand = comp_sg_reader_candidates(c, recv, cand, 32);
5684+ if (ncand >= 2) {
5685+ int bound = 0;
5686+ for (int k = 0; k < ncand; k++) {
5687+ int cmi = comp_cmethod_in_chain(c, cand[k], name, NULL);
5688+ if (cmi >= 0) { changed |= bind_call_params(c, id, cmi); bound = 1; }
5689+ }
5690+ if (bound) continue;
5691+ }
56715692 }
56725693 /* Class.new -> initialize params; Class.cmethod -> cmethod params */
56735694 {