Commit 01be5097

01be50974ab7Merged

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

Description

Multi-write poly RHS: type and codegen for local/ivar targets

When a multi-write a, b = rhs has a poly-typed RHS, the analysis fixpoint could not infer types for the LHS locals because the receiver ivar (@h = {}) stays TY_UNKNOWN through the fixpoint and only gets promoted to TY_POLY by the post-fixpoint backstop.

Two analysis fixes:

  • infer_write_types: when RHS infers to TY_POLY or TY_POLY_ARRAY, assign TY_POLY to each LocalVariableTargetNode on the LHS.
  • analyze_program: run one extra infer_write_types pass immediately after the backstop that promotes unknown ivars to TY_POLY, so that multi-write locals chaining through those ivars get typed correctly.

One codegen fix:

  • emit_stmt_inner MultiWriteNode: new TY_POLY arm uses sp_poly_arr_get to destructure each target from the poly RHS.

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
01be50974ab7
Parent
39cc4429bc19

src/analyze.cmodifiedView file

@@ -2370,6 +2370,20 @@
23702370 /* any expression returning a typed array: assign element types to targets */
23712371 if (value >= 0) {
23722372 TyKind st = infer_type(c, value);
2373+ /* poly RHS: destructure gives poly elements */
2374+ if (st == TY_POLY || st == TY_POLY_ARRAY) {
2375+ Scope *ms_poly = comp_scope_of(c, id);
2376+ for (int i = 0; i < ln; i++) {
2377+ const char *lty_p = nt_type(nt, lefts[i]) ? nt_type(nt, lefts[i]) : "";
2378+ if (!strcmp(lty_p, "LocalVariableTargetNode")) {
2379+ const char *lnm_p = nt_str(nt, lefts[i], "name");
2380+ LocalVar *lv_p = lnm_p ? scope_local(ms_poly, lnm_p) : NULL;
2381+ if (!lv_p || lv_p->is_param || lv_p->is_block_param) continue;
2382+ TyKind mg_p = ty_unify(lv_p->type, TY_POLY);
2383+ if (mg_p != lv_p->type) { lv_p->type = mg_p; changed = 1; }
2384+ }
2385+ }
2386+ }
23732387 if (ty_is_array(st)) {
23742388 TyKind elem = ty_array_elem(st);
23752389 int rn2 = 0;
@@ -4056,6 +4070,10 @@
40564070 for (int iv = 0; iv < cl->nivars; iv++)
40574071 if (cl->ivar_types[iv] == TY_UNKNOWN) cl->ivar_types[iv] = TY_POLY;
40584072 }
4073+ /* Post-backstop: re-run write type inference so multi-write locals whose
4074+ RHS chains through a now-typed ivar (e.g. @h[bank][idx] where @h was
4075+ just promoted from UNKNOWN to POLY) get their types resolved. */
4076+ infer_write_types(c);
40594077 /* recompute returns: a method returning such a param is now poly */
40604078 for (int iter = 0; iter < 8; iter++) if (!infer_return_types(c)) break;
40614079