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
/* a value-type object is never a NULL pointer -- it is always truthy */ if (comp_ty_value_obj(c, t)) { buf_puts(b, "(("); emit_expr(c, id, b); buf_puts(b, "), 1)"); return; } if (t == TY_STRING || ty_is_array(t) || ty_is_hash(t) || ty_is_object(t) || t == TY_PROC || t == TY_STRINGIO || t == TY_STRINGSCANNER || t == TY_MATCHDATA || t == TY_EXCEPTION) { t == TY_PROC || t == TY_STRINGIO || t == TY_STRINGSCANNER || t == TY_MATCHDATA || t == TY_EXCEPTION || t == TY_BIGINT || t == TY_REGEX || t == TY_CURRY || t == TY_FIBER || t == TY_RANDOM || t == TY_METHOD || t == TY_IO || t == TY_ARGF) { buf_puts(b, "(("); emit_expr(c, id, b); buf_puts(b, ") != 0)"); return; } if (t == TY_INT) { buf_puts(b, "(("); emit_expr(c, id, b); buf_puts(b, ") != SP_INT_NIL)"); return; } if (t == TY_FLOAT) { buf_puts(b, "(!sp_float_is_nil("); emit_expr(c, id, b); buf_puts(b, "))"); return; } if (t == TY_SYMBOL) { buf_puts(b, "(("); emit_expr(c, id, b); buf_puts(b, "), 1)"); return; } /* Always-truthy concrete value types: a Range / Class / Complex / Rational / Time value is never nil or false, so it is truthy in condition position. Evaluate it for side effects and yield 1. */ if (t == TY_RANGE || t == TY_CLASS || t == TY_COMPLEX || t == TY_RATIONAL || t == TY_TIME) { buf_puts(b, "(("); emit_expr(c, id, b); buf_puts(b, "), 1)"); return; } if (t != TY_BOOL) unsupported(c, id, "condition (non-bool)"); emit_expr(c, id, b);}test/cond_poly_truthy.rbaddedView file
Loading diff…
test/cond_poly_truthy.rb.expectedaddedView file
Loading diff…