Commit eedc1732

eedc17323ec3Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · +1 added · ~1 modified

Description

Treat a require reaching codegen as a runtime no-op

require / require_relative is resolved by textual splicing before codegen, but the splicer only matches a require at column 0 (start of line or right after a newline). An indented require -- inside an if branch, a module, or a method body -- survived into codegen as an unsupported CallNode and hard-failed the compile, while a top-level require warns and continues.

emit_call now lowers a receiverless require/require_relative to a no-op (yielding nil in value position). Spinel inlines real dependencies at compile time and provides stdlib natively, so a require evaluated at runtime has nothing to do.

Fixes #1400 Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
eedc17323ec3
Parent
289ce84baf6f

src/codegen_call.cmodifiedView file

@@ -151,6 +151,19 @@
151151
152152 void emit_call(Compiler *c, int id, Buf *b) {
153153 const NodeTable *nt = c->nt;
154+ /* `require` / `require_relative` is a compile-time directive: top-level ones
155+ are textually spliced away before codegen, and native libs are provided by
156+ the runtime. One that still reaches codegen -- indented inside an `if`,
157+ module, or method body -- is a runtime no-op (it would otherwise be an
158+ unsupported CallNode). Returns nil in value position via emit_boxed. */
159+ {
160+ int rcv = nt_ref(nt, id, "receiver");
161+ const char *cn = nt_str(nt, id, "name");
162+ if (rcv < 0 && cn && (!strcmp(cn, "require") || !strcmp(cn, "require_relative"))) {
163+ buf_puts(b, "0");
164+ return;
165+ }
166+ }
154167 if (emit_partition_expr(c, id, b)) return;
155168 if (emit_collect_expr(c, id, b)) return;
156169 if (emit_predicate_expr(c, id, b)) return;