Commit f7ae6beb
f7ae6bebe792Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Fix logical NOT on poly/nullable operands
The unary ! handler treated everything except bool/nil as unconditionally truthy, emitting ((x), 0). For a poly value (and nullable scalars/pointers) that is wrong: a boxed false/nil, an int sentinel, a NULL string, etc. are falsy, so !x must negate the real per-type truthiness emit_cond uses.
This made optcarrot print the fps-history and p95 sections: @options[:print_*] booleans copied from DEFAULT_OPTIONS into the sym-poly hash read back as !!false == true, so save_fps_history? and the p95 branch fired. optcarrot output now matches the reference (fps / checksum / OK only).
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- f7ae6bebe792
- Parent
- 71fd1105f109
src/codegen_call.cmodifiedView file
return; } if (!strcmp(name, "!") && recv >= 0 && argc == 0) { /* Ruby truthiness: only nil and false are falsy */ /* Ruby truthiness: only nil and false are falsy. `!x` negates the same per-type truthiness emit_cond uses -- a poly / nullable scalar / nullable pointer can be falsy, so the result is not unconditionally false. */ if (rt == TY_BOOL) { buf_puts(b, "(!"); emit_expr(c, recv, b); buf_puts(b, ")"); } else if (rt == TY_NIL) { buf_puts(b, "1"); } else { buf_puts(b, "(("); emit_expr(c, recv, b); buf_puts(b, "), 0)"); } /* truthy -> false */ else if (rt == TY_POLY) { buf_puts(b, "(!sp_poly_truthy("); emit_expr(c, recv, b); buf_puts(b, "))"); } else if (rt == TY_INT) { buf_puts(b, "(("); emit_expr(c, recv, b); buf_puts(b, ") == SP_INT_NIL)"); } else if (rt == TY_FLOAT) { buf_puts(b, "sp_float_is_nil("); emit_expr(c, recv, b); buf_puts(b, ")"); } else if (rt == TY_STRING || ty_is_array(rt) || ty_is_hash(rt) || ty_is_object(rt) || rt == TY_PROC || rt == TY_STRINGIO || rt == TY_STRINGSCANNER || rt == TY_MATCHDATA || rt == TY_EXCEPTION || rt == TY_FIBER || rt == TY_IO) { buf_puts(b, "(("); emit_expr(c, recv, b); buf_puts(b, ") == 0)"); /* NULL pointer is falsy */ } else { buf_puts(b, "(("); emit_expr(c, recv, b); buf_puts(b, "), 0)"); } /* always-truthy -> false */ return; }