Commit e8de2dd2

e8de2dd237fdMerged

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

Description

analyze+codegen: String#bytesize and String/Regexp#match with MatchData

Add TY_MATCHDATA type (sp_MatchData *) backed by the existing sp_re_matchdata / sp_re_matchdata_at runtime functions.

  • String#bytesize emits (mrb_int)sp_str_byte_len(s); infers TY_INT.
  • String#match(/re/) and String#match(/re/, pos) return TY_MATCHDATA.
  • /re/.match(str) and /re/.match(str, pos) likewise.
  • MatchData methods: [], begin, end, offset, pre_match, post_match, captures, to_a, to_s, length, nil?.
  • nil comparison for TY_MATCHDATA emits (m == 0) like TY_STRING.
  • Truthy/falsy tests treat TY_MATCHDATA as a nullable pointer.

Fixes str_match_position, i974, regexp_match_data, regexp_capture_ivar_hash, string_with_embedded_nul (461 pass, gate FAIL set unchanged).

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

Details

Commit
e8de2dd237fd
Parent
b16098e3994d

src/analyze.cmodifiedView file

@@ -341,6 +341,16 @@
341341 if (!strcmp(name, "reset") || !strcmp(name, "terminate") || !strcmp(name, "unscan")) return TY_STRINGSCANNER;
342342 }
343343
344+ /* MatchData instance methods */
345+ if (recv >= 0 && rt == TY_MATCHDATA) {
346+ if (!strcmp(name, "[]") && argc == 1) return TY_STRING;
347+ if (!strcmp(name, "pre_match") || !strcmp(name, "post_match") || !strcmp(name, "to_s")) return TY_STRING;
348+ if (!strcmp(name, "begin") || !strcmp(name, "end") || !strcmp(name, "length") || !strcmp(name, "size")) return TY_INT;
349+ if (!strcmp(name, "offset")) return TY_INT_ARRAY;
350+ if (!strcmp(name, "captures") || !strcmp(name, "to_a") || !strcmp(name, "named_captures")) return TY_POLY_ARRAY;
351+ if (!strcmp(name, "nil?")) return TY_BOOL;
352+ }
353+
344354 /* StringIO.open(args) { |io| body } -> the block body's value */
345355 if (recv >= 0 && !strcmp(name, "open") && nt_type(nt, recv) &&
346356 !strcmp(nt_type(nt, recv), "ConstantReadNode") && nt_str(nt, recv, "name") &&
@@ -784,7 +794,8 @@
784794 if (aty && !strcmp(aty, "RegularExpressionNode")) return TY_POLY; /* nil on no match */
785795 }
786796 if (!strcmp(name, "index") || !strcmp(name, "to_i") || !strcmp(name, "count") ||
787- !strcmp(name, "oct") || !strcmp(name, "ord") || !strcmp(name, "casecmp")) return TY_INT;
797+ !strcmp(name, "oct") || !strcmp(name, "ord") || !strcmp(name, "casecmp") ||
798+ !strcmp(name, "bytesize")) return TY_INT;
788799 if (!strcmp(name, "scrub") || !strcmp(name, "crypt")) return TY_STRING;
789800 if (!strcmp(name, "rindex")) return TY_INT;
790801 if (!strcmp(name, "partition") || !strcmp(name, "rpartition")) return TY_STR_ARRAY;
@@ -875,6 +886,11 @@
875886 if (!strcmp(name, "object_id") && recv >= 0 && argc == 0) return TY_INT;
876887 if (!strcmp(name, "between?") && argc == 2 && (rt == TY_STRING || ty_is_numeric(rt))) return TY_BOOL;
877888 if ((!strcmp(name, "match?") || !strcmp(name, "!~")) && recv >= 0) return TY_BOOL;
889+ if (!strcmp(name, "match") && recv >= 0 && (argc == 1 || argc == 2)) {
890+ const char *rrt = nt_type(nt, recv), *art = argc > 0 ? nt_type(nt, argv[0]) : NULL;
891+ if ((rrt && !strcmp(rrt, "RegularExpressionNode")) ||
892+ (art && !strcmp(art, "RegularExpressionNode"))) return TY_MATCHDATA;
893+ }
878894 if (!strcmp(name, "=~") && recv >= 0 && argc == 1) {
879895 const char *rrt = nt_type(nt, recv), *art = nt_type(nt, argv[0]);
880896 if ((rrt && !strcmp(rrt, "RegularExpressionNode")) ||