Commit f35f2084

f35f2084249bMerged

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

Description

Merge pull request #1486 from ryanseys/rs-cond-poly-truthy

Handle all concrete types in condition position

Details

Commit
f35f2084249b
Parent
a39745eb113e
Merge parent
f019b3e54c5f

src/codegen_stmt.cmodifiedView file

@@ -695,12 +695,20 @@
695695 /* a value-type object is never a NULL pointer -- it is always truthy */
696696 if (comp_ty_value_obj(c, t)) { buf_puts(b, "(("); emit_expr(c, id, b); buf_puts(b, "), 1)"); return; }
697697 if (t == TY_STRING || ty_is_array(t) || ty_is_hash(t) || ty_is_object(t) ||
698- t == TY_PROC || t == TY_STRINGIO || t == TY_STRINGSCANNER || t == TY_MATCHDATA || t == TY_EXCEPTION) {
698+ t == TY_PROC || t == TY_STRINGIO || t == TY_STRINGSCANNER || t == TY_MATCHDATA || t == TY_EXCEPTION ||
699+ t == TY_BIGINT || t == TY_REGEX || t == TY_CURRY || t == TY_FIBER || t == TY_RANDOM ||
700+ t == TY_METHOD || t == TY_IO || t == TY_ARGF) {
699701 buf_puts(b, "(("); emit_expr(c, id, b); buf_puts(b, ") != 0)"); return;
700702 }
701703 if (t == TY_INT) { buf_puts(b, "(("); emit_expr(c, id, b); buf_puts(b, ") != SP_INT_NIL)"); return; }
702704 if (t == TY_FLOAT) { buf_puts(b, "(!sp_float_is_nil("); emit_expr(c, id, b); buf_puts(b, "))"); return; }
703705 if (t == TY_SYMBOL) { buf_puts(b, "(("); emit_expr(c, id, b); buf_puts(b, "), 1)"); return; }
706+ /* Always-truthy concrete value types: a Range / Class / Complex / Rational /
707+ Time value is never nil or false, so it is truthy in condition position.
708+ Evaluate it for side effects and yield 1. */
709+ if (t == TY_RANGE || t == TY_CLASS || t == TY_COMPLEX || t == TY_RATIONAL || t == TY_TIME) {
710+ buf_puts(b, "(("); emit_expr(c, id, b); buf_puts(b, "), 1)"); return;
711+ }
704712 if (t != TY_BOOL) unsupported(c, id, "condition (non-bool)");
705713 emit_expr(c, id, b);
706714 }