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
return 0;}/* eval(string) / Kernel.eval(string) compiling an arbitrary runtime string is a hard AOT boundary, not a missing feature. If node `id` is such a call, emit the intentional diagnostic and return 1; otherwise return 0. Shared by emit_call and the output builtins (puts/print/p) so `puts eval(s)` gets the same specific message as `x = eval(s)` rather than a generic argument dump. The instance_eval/class_eval/module_eval block forms carry a literal block, not a string, and are handled separately -- they never reach here. */int diagnose_eval_call(Compiler *c, int id) { const NodeTable *nt = c->nt; const char *nty = nt_type(nt, id); if (!nty || strcmp(nty, "CallNode")) return 0; const char *name = nt_str(nt, id, "name"); if (!name || strcmp(name, "eval")) return 0; int args = nt_ref(nt, id, "arguments"); int argc = 0; if (args >= 0) nt_arr(nt, args, "arguments", &argc); if (argc < 1) return 0; int recv = nt_ref(nt, id, "receiver"); if (recv >= 0) { const char *rty = nt_type(nt, recv); if (!rty || (strcmp(rty, "ConstantReadNode") && strcmp(rty, "ConstantPathNode"))) return 0; const char *rnm = nt_str(nt, recv, "name"); if (!rnm || strcmp(rnm, "Kernel")) return 0; } unsupported(c, id, "eval of a runtime string is not supported by AOT compilation (define the code statically)"); return 1;}void emit_call(Compiler *c, int id, Buf *b) { const NodeTable *nt = c->nt; /* `require` / `require_relative` is a compile-time directive: top-level ones buf_puts(b, ")"); return; } /* eval(string) / Kernel.eval(string): compiling an arbitrary runtime string is a hard AOT boundary, not a missing feature. Emit an intentional diagnostic. (The instance_eval/class_eval/module_eval block forms are handled separately and are unaffected -- those carry a literal block, not a string.) */ if (!strcmp(name, "eval") && argc >= 1 && (recv < 0 || (nt_type(nt, recv) && (!strcmp(nt_type(nt, recv), "ConstantReadNode") || !strcmp(nt_type(nt, recv), "ConstantPathNode")) && nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "Kernel")))) { unsupported(c, id, "eval of a runtime string is not supported by AOT compilation (define the code statically)"); return; } /* eval(string) / Kernel.eval(string): a hard AOT boundary (see helper). */ if (diagnose_eval_call(c, id)) return; if (recv < 0 && !strcmp(name, "loop") && argc == 0) { int blk = nt_ref(nt, id, "block"); if (blk >= 0) {src/codegen_internal.hmodifiedView file
Loading diff…
src/codegen_stmt.cmodifiedView file
Loading diff…