From 82180d040b5ecb126e67b4d0047f4b3b80d7a352 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Thu, 20 Nov 2025 14:08:21 +0900 Subject: [PATCH] codegen.c: fix regexp literal with encoding to pass nil for options; fix #6666 restore correct argument passing for Regexp.compile when encoding is present but flags are not. regexp literals like /a/n should compile to Regexp.compile("a", nil, "n") with 3 arguments, not Regexp.compile("a", "n") with 2 arguments. the bug was introduced during refactoring when the nil-insertion logic for the options parameter was accidentally omitted. now properly inserts OP_LOADNIL when flags are absent but encoding is present. Co-authored-by: Claude --- mrbgems/mruby-compiler/core/codegen.c | 28 ++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/mrbgems/mruby-compiler/core/codegen.c b/mrbgems/mruby-compiler/core/codegen.c index bd402d4c9..1b42c2330 100644 --- a/mrbgems/mruby-compiler/core/codegen.c +++ b/mrbgems/mruby-compiler/core/codegen.c @@ -5153,20 +5153,26 @@ codegen_regx(codegen_scope *s, node *varnode, int val) /* Generate regex pattern using common cons list function */ gen_string(s, n->list, VAL); - /* Add flags if present */ - if (n->flags && *n->flags) { - off = new_lit_cstr(s, n->flags); - genop_2(s, OP_STRING, cursp(), off); + /* Add flags and/or encoding if present */ + if ((n->flags && *n->flags) || (n->encoding && *n->encoding)) { + /* Add flags (or nil if not present but encoding is) */ + if (n->flags && *n->flags) { + off = new_lit_cstr(s, n->flags); + genop_2(s, OP_STRING, cursp(), off); + } + else { + genop_1(s, OP_LOADNIL, cursp()); + } push(); argc++; - } - /* Add encoding if present */ - if (n->encoding && *n->encoding) { - off = new_lit_cstr(s, n->encoding); - genop_2(s, OP_STRING, cursp(), off); - push(); - argc++; + /* Add encoding if present */ + if (n->encoding && *n->encoding) { + off = new_lit_cstr(s, n->encoding); + genop_2(s, OP_STRING, cursp(), off); + push(); + argc++; + } } push(); /* space for a block */