Commit 72265ac7

72265ac7a9d8Merged

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

Description

&&/|| with a nil/void left operand

The value form of a && b / a || b declared the left-holding temp with the left's C type; a nil or void left maps to "void", an invalid declaration. Such a left now uses the unified result type for the temp and yields nil (boxed in a poly result), still evaluating the left for side effects.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
72265ac7a9d8
Parent
1e909c640e1a

src/codegen.cmodifiedView file

@@ -3627,12 +3627,20 @@
36273627 /* value form: a || b -> truthy(a) ? a : b ; a && b -> truthy(a) ? b : a.
36283628 Evaluate the left once into a temp; results widen to the unified type. */
36293629 int t = ++g_tmp;
3630+ int lt_falsy_const = (lt == TY_NIL || lt == TY_VOID); /* a nil/void left has no C-typed value */
36303631 buf_puts(b, "({ ");
3631- emit_ctype(c, lt == TY_UNKNOWN ? res : lt, b);
3632- buf_printf(b, " _t%d = ", t); emit_expr(c, left, b); buf_puts(b, "; ");
3632+ emit_ctype(c, (lt == TY_UNKNOWN || lt_falsy_const) ? res : lt, b);
3633+ buf_printf(b, " _t%d = ", t);
3634+ if (lt_falsy_const) {
3635+ buf_puts(b, "("); emit_expr(c, left, b); buf_puts(b, ", ");
3636+ buf_puts(b, res == TY_POLY ? "sp_box_nil()" : default_value(res == TY_UNKNOWN ? TY_INT : res));
3637+ buf_puts(b, ")");
3638+ }
3639+ else emit_expr(c, left, b);
3640+ buf_puts(b, "; ");
36333641 if (lt == TY_POLY) buf_printf(b, "sp_poly_truthy(_t%d)", t);
36343642 else if (lt == TY_BOOL) buf_printf(b, "_t%d", t);
3635- else if (lt == TY_NIL) buf_puts(b, "0");
3643+ else if (lt_falsy_const) buf_puts(b, "0");
36363644 else if (lt == TY_INT) buf_printf(b, "(_t%d != SP_INT_NIL)", t); /* a nullable int reads falsy at the sentinel; a plain int is always truthy */
36373645 else if (lt == TY_FLOAT) buf_printf(b, "(!sp_float_is_nil(_t%d))", t);
36383646 else if (lt == TY_STRING || ty_is_array(lt) || ty_is_hash(lt) || ty_is_object(lt) ||