Commit 6b9fd5ab

6b9fd5ab1453Merged

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

Description

Restrict block predicates to bool block values

The all?/any?/none?/one? block lowering emits if (block_value), which mishandles Ruby truthiness for non-bool values (0 / 0.0 are truthy in Ruby but false in C). Require a bool block result; other types fall back to the unresolved-call path rather than producing wrong output.

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

Details

Commit
6b9fd5ab1453
Parent
12db4e5b523a

src/codegen.cmodifiedView file

@@ -723,9 +723,10 @@
723723 int bn = 0;
724724 const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;
725725 if (bn < 1) return 0;
726- /* the block's last expression is used as a C condition: require a scalar */
727- TyKind vt = comp_ntype(c, bb[bn - 1]);
728- if (!(vt == TY_BOOL || vt == TY_INT || vt == TY_FLOAT)) return 0;
726+ /* the block's last expression is the C condition: require a bool. A bare
727+ `if (value)` would mis-handle Ruby truthiness for other types (0 / 0.0
728+ are truthy in Ruby but false in C), so leave those unsupported. */
729+ if (comp_ntype(c, bb[bn - 1]) != TY_BOOL) return 0;
729730
730731 const char *p0 = block_param_name(c, block, 0); if (p0) p0 = rename_local(p0);
731732 int trecv = ++g_tmp, tcnt = ++g_tmp, ti = ++g_tmp;