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
return; } /* statements that don't produce a usable tail value: emit normally; the trailing default return covers the method's value. */ /* statements that don't produce a usable tail value: emit normally; the trailing default return covers the method's value. Local/instance operator assignments (`x += 1`, `@x += 1`) are NOT here -- in Ruby they return the updated value, so they fall through to the value path and are returned (matz/spinel#1484, matching the class-variable form that already worked). Plain `x = v` / `||=` / `&&=` stay statements for now: routing a tail ivar write of nil through the value path perturbs nullable-scalar inference. */ if (!strcmp(ty, "LocalVariableWriteNode") || !strcmp(ty, "LocalVariableOperatorWriteNode") || !strcmp(ty, "LocalVariableOrWriteNode") || !strcmp(ty, "LocalVariableAndWriteNode") || !strcmp(ty, "InstanceVariableWriteNode") || !strcmp(ty, "InstanceVariableOperatorWriteNode") || !strcmp(ty, "GlobalVariableWriteNode") || !strcmp(ty, "ConstantWriteNode") || !strcmp(ty, "WhileNode") || !strcmp(ty, "UntilNode") || if (strcmp(ty, "CallNode") != 0) emit_stmt(c, id, b, indent); return; } /* A local operator-assignment whose target is a captured/cell var (inside a proc/block body) has no value form in emit_expr -- cells are int-restricted and statement-only -- so keep emitting it as a statement; the enclosing callable returns its default. Ordinary locals/ivars fall through below. */ if (!strcmp(ty, "LocalVariableOperatorWriteNode")) { const char *nm = nt_str(nt, id, "name"); LocalVar *lv = nm ? scope_local(comp_scope_of(c, id), nm) : NULL; int celled = (lv && lv->is_cell) || (g_cap_struct && g_cap_names && nm && nameset_has(g_cap_names, nm)); if (celled) { emit_stmt(c, id, b, indent); return; } } /* iteration calls with a block are side-effect statements at tail position; emit them without wrapping in a return (the method returns nil implicitly) */ if (!strcmp(ty, "CallNode") && nt_ref(nt, id, "block") >= 0 &&test/op_assign_tail_return.rbaddedView file
Loading diff…
test/op_assign_tail_return.rb.expectedaddedView file
Loading diff…