Commit 5ba5709a

5ba5709af100Merged

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

Description

Multiple assignment with a scalar right-hand side

a, b = 1 previously emitted an array-typed temp from the scalar and failed to compile. Treat a non-array RHS as CRuby does: the first target takes the value and the remaining targets land their typed slot default (CRuby leaves them nil; we keep the zero). A call / super / yield can return a multi-value tuple, so those are excluded and continue to flow to the tuple-destructuring path.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
5ba5709af100
Parent
7143f34b83be

src/analyze.cmodifiedView file

@@ -1528,7 +1528,26 @@
15281528 const int *lefts = nt_arr(nt, id, "lefts", &ln);
15291529 int value = nt_ref(nt, id, "value");
15301530 const char *vty = nt_type(nt, value);
1531- if (!vty || strcmp(vty, "ArrayNode")) continue;
1531+ if (!vty || strcmp(vty, "ArrayNode")) {
1532+ /* scalar RHS (`a, b = 1`): the first target gets the scalar, the rest
1533+ their slot default. Type every target as the scalar's kind. Array /
1534+ hash RHS would splat and is handled elsewhere, so skip those. */
1535+ int multi_src = vty && (!strcmp(vty, "CallNode") || !strcmp(vty, "SuperNode") ||
1536+ !strcmp(vty, "ForwardingSuperNode") || !strcmp(vty, "YieldNode"));
1537+ if (vty && value >= 0 && !multi_src) {
1538+ TyKind st = infer_type(c, value);
1539+ if (st != TY_UNKNOWN && st != TY_NIL && !ty_is_array(st) && !ty_is_hash(st)) {
1540+ for (int i = 0; i < ln; i++) {
1541+ if (strcmp(nt_type(nt, lefts[i]) ? nt_type(nt, lefts[i]) : "", "LocalVariableTargetNode")) continue;
1542+ const char *lnm = nt_str(nt, lefts[i], "name");
1543+ LocalVar *lv = lnm ? scope_local(comp_scope_of(c, id), lnm) : NULL;
1544+ if (!lv || lv->is_param || lv->is_block_param) continue;
1545+ lv->type = ty_unify(lv->type, st);
1546+ }
1547+ }
1548+ }
1549+ continue;
1550+ }
15321551 int en = 0;
15331552 const int *els = nt_arr(nt, value, "elements", &en);
15341553 for (int i = 0; i < ln && i < en; i++) {