Commit 49690212
496902126914Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
nil.inspect/to_s/nil?, p/puts nil, and p/puts of an empty array literal
- A nil receiver:
nil.inspect-> "nil",nil.to_s-> "",nil.nil?-> true (the receiver is still evaluated for side effects). p nilprints "nil";p []prints "[]";puts []prints nothing.
make test: 184 pass / 6 fail / 654 error (was 183 / 6 / 655); inspect green, no regressions.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 496902126914
- Parent
- 66fc04fe6b6d
src/codegen.cmodifiedView file
@@ -1049,6 +1049,14 @@
10491049
}10501050
}10511051
1052+
/* nil receiver: nil.inspect -> "nil", nil.to_s -> "", nil.nil? -> true.1053+
Evaluate the receiver for side effects, then yield the constant. */1054+
if (recv >= 0 && rt == TY_NIL && argc == 0) {1055+
if (!strcmp(name, "inspect")) { buf_puts(b, "((void)("); emit_expr(c, recv, b); buf_puts(b, "), SPL(\"nil\"))"); return; }1056+
if (!strcmp(name, "to_s")) { buf_puts(b, "((void)("); emit_expr(c, recv, b); buf_puts(b, "), SPL(\"\"))"); return; }1057+
if (!strcmp(name, "nil?")) { buf_puts(b, "((void)("); emit_expr(c, recv, b); buf_puts(b, "), 1)"); return; }1058+
}1059+
10521060
/* poly receiver: nil? / conversions / a few type-agnostic queries */10531061
if (recv >= 0 && rt == TY_POLY && argc == 0) {10541062
if (!strcmp(name, "nil?")) { buf_puts(b, "sp_poly_nil_p("); emit_expr(c, recv, b); buf_puts(b, ")"); return; }@@ -2800,6 +2808,10 @@
28002808
}28012809
buf_puts(b, ")); if (_ps) fputs(_ps, stdout); if (!_ps || !*_ps || _ps[strlen(_ps)-1] != '\\n') putchar('\\n'); }\n");28022810
}2811+
else if (nt_type(c->nt, arg) && !strcmp(nt_type(c->nt, arg), "ArrayNode") &&2812+
({ int _n = 0; nt_arr(c->nt, arg, "elements", &_n); _n == 0; })) {2813+
buf_puts(b, "(void)0; /* puts [] prints nothing */\n");2814+
}28032815
else {28042816
unsupported(c, arg, "puts argument");28052817
}@@ -2872,6 +2884,13 @@
28722884
buf_puts(b, "fputs(sp_poly_inspect("); emit_expr(c, arg, b);28732885
buf_puts(b, "), stdout); putchar('\\n');\n");28742886
}2887+
else if (t == TY_NIL) {2888+
buf_puts(b, "(void)("); emit_expr(c, arg, b); buf_puts(b, "); fputs(\"nil\\n\", stdout);\n");2889+
}2890+
else if (nt_type(c->nt, arg) && !strcmp(nt_type(c->nt, arg), "ArrayNode") &&2891+
({ int _n = 0; nt_arr(c->nt, arg, "elements", &_n); _n == 0; })) {2892+
buf_puts(b, "fputs(\"[]\\n\", stdout);\n"); /* p [] */2893+
}28752894
else {28762895
unsupported(c, arg, "p argument");28772896
}