Commit 2b18d643

2b18d64321b2Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified

Description

codegen: diagnose eval in puts/print/p argument position

The eval(string)/Kernel.eval(string) diagnostic added in PR #1507 only fired when eval was emitted as a full expression through emit_call. As an argument to an output builtin (puts eval(s)), the type-driven argument emitter reaches eval with an unresolved type and rejects it one level up with the generic "puts argument" dump, so the specific message did not reach the common embedded form.

Factor the eval detection into a shared diagnose_eval_call helper and invoke it from emit_call and from the puts/print/p argument fallthroughs. A hard AOT boundary now produces the same intentional message regardless of syntactic position: x = eval(s), bare eval(s), and puts/print/p eval(s) all report "eval of a runtime string is not supported by AOT compilation (define the code statically)". A non-Kernel receiver (obj.eval) is not matched.

Verified manually (reject diagnostics have no in-suite PASS harness); make gate green (985 pass, optcarrot 59662, bench 57).

Refs #1507

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
2b18d64321b2
Parent
346dbf87980d

src/codegen_call.cmodifiedView file

@@ -3319,6 +3319,34 @@
33193319 return 0;
33203320 }
33213321
3322+/* eval(string) / Kernel.eval(string) compiling an arbitrary runtime string is a
3323+ hard AOT boundary, not a missing feature. If node `id` is such a call, emit
3324+ the intentional diagnostic and return 1; otherwise return 0. Shared by
3325+ emit_call and the output builtins (puts/print/p) so `puts eval(s)` gets the
3326+ same specific message as `x = eval(s)` rather than a generic argument dump.
3327+ The instance_eval/class_eval/module_eval block forms carry a literal block,
3328+ not a string, and are handled separately -- they never reach here. */
3329+int diagnose_eval_call(Compiler *c, int id) {
3330+ const NodeTable *nt = c->nt;
3331+ const char *nty = nt_type(nt, id);
3332+ if (!nty || strcmp(nty, "CallNode")) return 0;
3333+ const char *name = nt_str(nt, id, "name");
3334+ if (!name || strcmp(name, "eval")) return 0;
3335+ int args = nt_ref(nt, id, "arguments");
3336+ int argc = 0;
3337+ if (args >= 0) nt_arr(nt, args, "arguments", &argc);
3338+ if (argc < 1) return 0;
3339+ int recv = nt_ref(nt, id, "receiver");
3340+ if (recv >= 0) {
3341+ const char *rty = nt_type(nt, recv);
3342+ if (!rty || (strcmp(rty, "ConstantReadNode") && strcmp(rty, "ConstantPathNode"))) return 0;
3343+ const char *rnm = nt_str(nt, recv, "name");
3344+ if (!rnm || strcmp(rnm, "Kernel")) return 0;
3345+ }
3346+ unsupported(c, id, "eval of a runtime string is not supported by AOT compilation (define the code statically)");
3347+ return 1;
3348+}
3349+
33223350 void emit_call(Compiler *c, int id, Buf *b) {
33233351 const NodeTable *nt = c->nt;
33243352 /* `require` / `require_relative` is a compile-time directive: top-level ones
@@ -3456,17 +3484,8 @@
34563484 buf_puts(b, ")");
34573485 return;
34583486 }
3459- /* eval(string) / Kernel.eval(string): compiling an arbitrary runtime string is
3460- a hard AOT boundary, not a missing feature. Emit an intentional diagnostic.
3461- (The instance_eval/class_eval/module_eval block forms are handled separately
3462- and are unaffected -- those carry a literal block, not a string.) */
3463- if (!strcmp(name, "eval") && argc >= 1 &&
3464- (recv < 0 || (nt_type(nt, recv) &&
3465- (!strcmp(nt_type(nt, recv), "ConstantReadNode") || !strcmp(nt_type(nt, recv), "ConstantPathNode")) &&
3466- nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "Kernel")))) {
3467- unsupported(c, id, "eval of a runtime string is not supported by AOT compilation (define the code statically)");
3468- return;
3469- }
3487+ /* eval(string) / Kernel.eval(string): a hard AOT boundary (see helper). */
3488+ if (diagnose_eval_call(c, id)) return;
34703489 if (recv < 0 && !strcmp(name, "loop") && argc == 0) {
34713490 int blk = nt_ref(nt, id, "block");
34723491 if (blk >= 0) {