Commit f019b3e5

f019b3e54c5fMerged

Ryan Seys <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

codegen: handle all concrete types in condition position

emit_cond rejected several concrete types with "condition (non-bool)" even though each is always truthy (or truthy when present) in Ruby. The function already handled strings, arrays, hashes, objects, procs, a few pointer-backed builtins, and the int/float/symbol scalars -- but a number of equally-concrete types fell through to the reject.

Add the remaining always-truthy value types -- Range, Class, Complex, Rational, and Time -- which are never nil or false: evaluate them for side effects and yield 1, the same shape already used for symbols and value objects. TY_UNKNOWN still rejects, since an unresolved type cannot be emitted safely.

Add the remaining pointer-backed builtins -- arbitrary-precision integers, Regexp, a curried proc, Fiber, Random, Method, IO, and ARGF -- to the existing non-NULL check, matching how strings and procs are already treated: a non-NULL pointer is truthy, a NULL one (the nil sentinel) is falsy. A mutable string buffer needs no entry -- it is normalized to a plain string before this dispatch, so the string case already covers it.

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

Details

Commit
f019b3e54c5f
Parent
913a83318df6

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 }