Commit 92163abd

92163abdc968Merged

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

Description

Add Regexp.last_match(n), sleep, define_method, Fiber.current fixes

  • analyze: Regexp.last_match(1..9) returns TY_STRING
  • analyze: walk_scope registers define_method(:sym)/("str") { } as named method scopes at class body level
  • analyze: Fiber.current returns TY_POLY; ConstantPathNode support for Proc.new, Class.new, and builtin constructors (::Array.new etc.)
  • codegen: emit Regexp.last_match(n) as sp_re_captures[n] or sp_re_match_str
  • codegen: emit sleep(n)/Kernel.sleep(n) via sp_sleep()
  • codegen: sp_is_fiber_storage_recv() guard prevents TY_POLY []= handler from intercepting Fiber/Fiber.current storage writes before the dedicated Fiber storage set handler can fire

Fixes: regexp_last_match_capture, compile_time_define_method_direct, bundle_class_23 (kernel_module_function_call), bundle_misc_c_09

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
92163abdc968
Parent
d1ce90c59bb8

src/analyze.cmodifiedView file

@@ -241,6 +241,16 @@
241241
242242 /* 1 if `id` is a proc/lambda literal: `proc {}` / `lambda {}` (CallNode with
243243 no receiver and a block) or `Proc.new {}`. */
244+static int is_proc_constant(const NodeTable *nt, int n) {
245+ if (n < 0) return 0;
246+ const char *ty = nt_type(nt, n);
247+ if (!ty) return 0;
248+ if (!strcmp(ty, "ConstantReadNode") || !strcmp(ty, "ConstantPathNode")) {
249+ const char *nm = nt_str(nt, n, "name");
250+ return nm && !strcmp(nm, "Proc");
251+ }
252+ return 0;
253+}
244254 static int is_proc_literal(Compiler *c, int id) {
245255 const NodeTable *nt = c->nt;
246256 const char *ty = nt_type(nt, id);
@@ -249,9 +259,7 @@
249259 const char *name = nt_str(nt, id, "name");
250260 int recv = nt_ref(nt, id, "receiver");
251261 if (recv < 0 && name && (!strcmp(name, "proc") || !strcmp(name, "lambda"))) return 1;
252- if (recv >= 0 && name && !strcmp(name, "new") && nt_type(nt, recv) &&
253- !strcmp(nt_type(nt, recv), "ConstantReadNode") && nt_str(nt, recv, "name") &&
254- !strcmp(nt_str(nt, recv, "name"), "Proc")) return 1;
262+ if (recv >= 0 && name && !strcmp(name, "new") && is_proc_constant(nt, recv)) return 1;
255263 return 0;
256264 }
257265
@@ -554,7 +562,7 @@
554562 /* Class.new(...) -> an instance of that class; built-in .new constructors */
555563 if (recv >= 0 && !strcmp(name, "new")) {
556564 const char *rty = nt_type(nt, recv);
557- /* a namespaced class (M::Sub) -> resolve by the final path component */
565+ /* a namespaced class (M::Sub) or root-qualified builtin (::Array etc) */
558566 if (rty && !strcmp(rty, "ConstantPathNode")) {
559567 const char *cn = nt_str(nt, recv, "name");
560568 int ci = cn ? comp_class_index(c, cn) : -1;
@@ -565,6 +573,18 @@
565573 return ty_object(ci);
566574 }
567575 if (cn && is_builtin_exception_name(cn)) return TY_EXCEPTION;
576+ /* ::Array.new / ::String.new / ::StringIO.new etc. */
577+ if (cn && !strcmp(cn, "Array") && argc == 2) return ty_array_of(infer_type(c, argv[1]));
578+ if (cn && !strcmp(cn, "Array")) return TY_POLY_ARRAY;
579+ if (cn && !strcmp(cn, "Object")) return TY_POLY;
580+ if (cn && !strcmp(cn, "String")) return TY_STRING;
581+ if (cn && !strcmp(cn, "StringIO")) return TY_STRINGIO;
582+ if (cn && !strcmp(cn, "StringScanner")) return TY_STRINGSCANNER;
583+ if (cn && !strcmp(cn, "Hash")) return TY_UNKNOWN;
584+ if (cn && !strcmp(cn, "Regexp")) return TY_REGEX;
585+ if (cn && (!strcmp(cn, "Fiber") || !strcmp(cn, "Thread") || !strcmp(cn, "Mutex") ||
586+ !strcmp(cn, "Random") || !strcmp(cn, "IO") || !strcmp(cn, "File") ||
587+ !strcmp(cn, "GzipReader") || !strcmp(cn, "GzipWriter"))) return TY_POLY;
568588 }
569589 if (rty && !strcmp(rty, "ConstantReadNode")) {
570590 const char *cn = nt_str(nt, recv, "name");
@@ -627,6 +647,7 @@
627647 if ((!strcmp(name, "escape") || !strcmp(name, "quote")) && argc >= 1) return TY_STRING;
628648 if (!strcmp(name, "union") && argc >= 1) return TY_REGEX;
629649 if (!strcmp(name, "last_match") && argc == 0) return TY_POLY;
650+ if (!strcmp(name, "last_match") && argc == 1) return TY_STRING;
630651 }
631652
632653 /* Regexp instance methods */
@@ -745,13 +766,15 @@
745766 /* IO.pipe -> [r, w] pair; each is TY_POLY; the pair is a str_array */
746767 if (!strcmp(name, "pipe")) return TY_STR_ARRAY;
747768 }
748- /* Fiber.new { } / Thread.new { } -> TY_POLY (builtin object) */
749- if (rty && !strcmp(rty, "ConstantReadNode")) {
769+ /* Fiber.new {} / Thread.new {} / Fiber.current etc. -> TY_POLY (builtin object).
770+ Handles both bare Const and ::Const path forms. */
771+ if (rty && (!strcmp(rty, "ConstantReadNode") || !strcmp(rty, "ConstantPathNode"))) {
750772 const char *cn2 = nt_str(nt, recv, "name");
751773 if (cn2 && !strcmp(name, "new") &&
752774 (!strcmp(cn2, "Fiber") || !strcmp(cn2, "Thread") || !strcmp(cn2, "Mutex") ||
753775 !strcmp(cn2, "Random")))
754776 return TY_POLY;
777+ if (cn2 && !strcmp(cn2, "Fiber") && !strcmp(name, "current")) return TY_POLY;
755778 }
756779 }
757780
@@ -2396,6 +2419,32 @@
23962419 collect_def_params(c, id, s);
23972420 child = new_idx;
23982421 }
2422+ else if (ty && !strcmp(ty, "CallNode") && class_id >= 0) {
2423+ /* define_method(:literal_name) { ... } at class scope: register as method scope */
2424+ const char *dm_cn = nt_str(c->nt, id, "name");
2425+ int dm_recv = nt_ref(c->nt, id, "receiver");
2426+ if (dm_cn && !strcmp(dm_cn, "define_method") && dm_recv < 0) {
2427+ int dm_args = nt_ref(c->nt, id, "arguments");
2428+ int dm_na = 0;
2429+ const int *dm_argv = dm_args >= 0 ? nt_arr(c->nt, dm_args, "arguments", &dm_na) : NULL;
2430+ if (dm_na >= 1) {
2431+ const char *dm_aty = nt_type(c->nt, dm_argv[0]);
2432+ const char *dm_mname = NULL;
2433+ if (dm_aty && !strcmp(dm_aty, "SymbolNode"))
2434+ dm_mname = nt_str(c->nt, dm_argv[0], "value");
2435+ else if (dm_aty && !strcmp(dm_aty, "StringNode"))
2436+ dm_mname = nt_str(c->nt, dm_argv[0], "content");
2437+ int dm_blk = nt_ref(c->nt, id, "block");
2438+ if (dm_mname && dm_blk >= 0) {
2439+ Scope *dm_s = comp_scope_new(c, dm_mname, id);
2440+ int dm_new_idx = c->nscopes - 1;
2441+ dm_s->body = nt_ref(c->nt, dm_blk, "body");
2442+ dm_s->class_id = class_id;
2443+ child = dm_new_idx;
2444+ }
2445+ }
2446+ }
2447+ }
23992448
24002449 int saved_cbody = g_cbody_class_id;
24012450 if (child_class >= 0) g_cbody_class_id = child_class;