Commit a39745eb

a39745eb113eMerged

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

Description

Return the updated value from a tail operator-assignment

In Ruby an operator assignment (value += 1, @value += 1) evaluates to the updated value, so a method whose last expression is one should return it. Spinel emitted local and instance operator-assignments in tail position as plain statements and fell back to the default return (0), so such methods returned 0 instead of the new value. The class-variable form already worked because it was not in the no-tail-value list.

emit_stmt_tail_inner now lets LocalVariableOperatorWriteNode and InstanceVariableOperatorWriteNode fall through to the value path (the same one the class-variable form uses). A local operator-assignment whose target is a captured/cell var inside a proc/block body stays a statement -- cells have no value form in emit_expr -- so closures returning the default are unaffected.

Scoped to operator-assignment (the reported += case). Plain x = v and ||=/&&= in tail position are a separate latent gap left as statements: routing a tail ivar write of nil through the value path perturbs nullable-scalar inference (regressed bundle_classd_13).

957 test (gcc + clang) / 57 bench / optcarrot 59662; clean under clang -fsanitize=address,undefined.

Closes #1484

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

Details

Commit
a39745eb113e
Parent
4d039962b6ff

src/codegen_stmt.cmodifiedView file

@@ -3708,14 +3708,17 @@
37083708 return;
37093709 }
37103710
3711- /* statements that don't produce a usable tail value: emit normally;
3712- the trailing default return covers the method's value. */
3711+ /* statements that don't produce a usable tail value: emit normally; the
3712+ trailing default return covers the method's value. Local/instance operator
3713+ assignments (`x += 1`, `@x += 1`) are NOT here -- in Ruby they return the
3714+ updated value, so they fall through to the value path and are returned
3715+ (matz/spinel#1484, matching the class-variable form that already worked).
3716+ Plain `x = v` / `||=` / `&&=` stay statements for now: routing a tail ivar
3717+ write of nil through the value path perturbs nullable-scalar inference. */
37133718 if (!strcmp(ty, "LocalVariableWriteNode") ||
3714- !strcmp(ty, "LocalVariableOperatorWriteNode") ||
37153719 !strcmp(ty, "LocalVariableOrWriteNode") ||
37163720 !strcmp(ty, "LocalVariableAndWriteNode") ||
37173721 !strcmp(ty, "InstanceVariableWriteNode") ||
3718- !strcmp(ty, "InstanceVariableOperatorWriteNode") ||
37193722 !strcmp(ty, "GlobalVariableWriteNode") ||
37203723 !strcmp(ty, "ConstantWriteNode") ||
37213724 !strcmp(ty, "WhileNode") || !strcmp(ty, "UntilNode") ||
@@ -3724,6 +3727,17 @@
37243727 if (strcmp(ty, "CallNode") != 0) emit_stmt(c, id, b, indent);
37253728 return;
37263729 }
3730+
3731+ /* A local operator-assignment whose target is a captured/cell var (inside a
3732+ proc/block body) has no value form in emit_expr -- cells are int-restricted
3733+ and statement-only -- so keep emitting it as a statement; the enclosing
3734+ callable returns its default. Ordinary locals/ivars fall through below. */
3735+ if (!strcmp(ty, "LocalVariableOperatorWriteNode")) {
3736+ const char *nm = nt_str(nt, id, "name");
3737+ LocalVar *lv = nm ? scope_local(comp_scope_of(c, id), nm) : NULL;
3738+ int celled = (lv && lv->is_cell) || (g_cap_struct && g_cap_names && nm && nameset_has(g_cap_names, nm));
3739+ if (celled) { emit_stmt(c, id, b, indent); return; }
3740+ }
37273741 /* iteration calls with a block are side-effect statements at tail position;
37283742 emit them without wrapping in a return (the method returns nil implicitly) */
37293743 if (!strcmp(ty, "CallNode") && nt_ref(nt, id, "block") >= 0 &&