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

@@ -3332,10 +3332,20 @@
33323332 return;
33333333 }
33343334 if (!strcmp(name, "!") && recv >= 0 && argc == 0) {
3335- /* Ruby truthiness: only nil and false are falsy */
3335+ /* Ruby truthiness: only nil and false are falsy. `!x` negates the same
3336+ per-type truthiness emit_cond uses -- a poly / nullable scalar / nullable
3337+ pointer can be falsy, so the result is not unconditionally false. */
33363338 if (rt == TY_BOOL) { buf_puts(b, "(!"); emit_expr(c, recv, b); buf_puts(b, ")"); }
33373339 else if (rt == TY_NIL) { buf_puts(b, "1"); }
3338- else { buf_puts(b, "(("); emit_expr(c, recv, b); buf_puts(b, "), 0)"); } /* truthy -> false */
3340+ else if (rt == TY_POLY) { buf_puts(b, "(!sp_poly_truthy("); emit_expr(c, recv, b); buf_puts(b, "))"); }
3341+ else if (rt == TY_INT) { buf_puts(b, "(("); emit_expr(c, recv, b); buf_puts(b, ") == SP_INT_NIL)"); }
3342+ else if (rt == TY_FLOAT) { buf_puts(b, "sp_float_is_nil("); emit_expr(c, recv, b); buf_puts(b, ")"); }
3343+ else if (rt == TY_STRING || ty_is_array(rt) || ty_is_hash(rt) || ty_is_object(rt) ||
3344+ rt == TY_PROC || rt == TY_STRINGIO || rt == TY_STRINGSCANNER ||
3345+ rt == TY_MATCHDATA || rt == TY_EXCEPTION || rt == TY_FIBER || rt == TY_IO) {
3346+ buf_puts(b, "(("); emit_expr(c, recv, b); buf_puts(b, ") == 0)"); /* NULL pointer is falsy */
3347+ }
3348+ else { buf_puts(b, "(("); emit_expr(c, recv, b); buf_puts(b, "), 0)"); } /* always-truthy -> false */
33393349 return;
33403350 }
33413351