Commit 058b8f2a

058b8f2a4450Merged

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

Description

Make string? a first-class string type; StringScanner returns nil on miss

StringScanner's string-returning methods (scan, check, scan_until, getch, matched, pre_match, post_match, []) returned "" instead of nil on a miss / EOS / no-last-match, diverging from CRuby and breaking truthiness, nil?, || default, and inspect. The natural fix -- typing them string? and returning NULL from the runtime -- was blocked because string? was not a first-class string type in the compiler: once a value was string?, ordinary string operations fell off the string dispatch path (many sites gated on vt == "string" rather than base_type(vt) == "string").

Make string? dispatch as string wherever a string operation is performed:

  • codegen compile_operator_expr: the +, *, %, <, >, <=, >=, between?, << gates, and the secondary type checks inside collect_concat_parts (which otherwise failed to decompose a string? + chain and recompiled the whole node -> infinite recursion).
  • codegen compile_eq: string equality routes through sp_str_eq (already NULL-safe), so nil_scan == "x" is false, not a pointer compare.
  • codegen + analyze infer_array_elem_type_from_ids: a string? element types the literal as str_array, not int_array.
  • codegen compile_interpolated: "#{m}" renders the string; a NULL string? renders as "" (CRuby "#{nil}"), not glibc's "(null)".
  • analyze infer_operator_type: a string? operand yields a string result type for + / * / % (mirrors the existing scalar-nullable int?/float? normalization), so the consumer of m + x sees string, not the int fallback.

StringScanner: type the nullable methods string? in strscan_return_type and return NULL on miss/EOS/no-match in lib/sp_strscan.c (gating matched / pre_match / post_match on matched_p). peek / rest / string stay non-null.

Also fixes a pre-existing bug exposed by scan(...).to_sym: the dynamic symbol intern pool (sp_sym_intern) was only emitted when a program had at least one static symbol literal, so any program using only String#to_sym at runtime failed to link. emit_sym_runtime now emits the pool whenever @needs_sym_intern is set, and the to_sym detection recognizes a string? receiver.

Closes #1279

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
058b8f2a4450
Parent
db4ac9684731

lib/sp_strscan.cmodifiedView file

@@ -123,14 +123,14 @@
123123 }
124124
125125 const char *sp_StringScanner_scan(sp_StringScanner *sc, mrb_regexp_pattern *pat) {
126- if (!sc || !pat) return sp_ext_str_empty();
126+ if (!sc || !pat) return NULL;
127127 int64_t slen = (int64_t)sp_ext_str_byte_len(sc->source);
128128 int64_t mlen = sc_match_at_pos(pat, sc->source, slen, sc->pos, sc->caps, &sc->ncaps);
129129 if (mlen < 0) {
130130 sc->matched = sp_ext_str_empty();
131131 sc->matched_p = 0;
132132 sc->ncaps = 0;
133- return sp_ext_str_empty();
133+ return NULL;
134134 }
135135 char *m = sc_substr(sc->source, sc->pos, mlen);
136136 sc->last_pos = sc->pos;
@@ -141,14 +141,14 @@
141141 }
142142
143143 const char *sp_StringScanner_check(sp_StringScanner *sc, mrb_regexp_pattern *pat) {
144- if (!sc || !pat) return sp_ext_str_empty();
144+ if (!sc || !pat) return NULL;
145145 int64_t slen = (int64_t)sp_ext_str_byte_len(sc->source);
146146 int64_t mlen = sc_match_at_pos(pat, sc->source, slen, sc->pos, sc->caps, &sc->ncaps);
147147 if (mlen < 0) {
148148 sc->matched = sp_ext_str_empty();
149149 sc->matched_p = 0;
150150 sc->ncaps = 0;
151- return sp_ext_str_empty();
151+ return NULL;
152152 }
153153 char *m = sc_substr(sc->source, sc->pos, mlen);
154154 sc->last_pos = sc->pos;
@@ -166,7 +166,7 @@
166166 sc->matched = sp_ext_str_empty();
167167 sc->matched_p = 0;
168168 sc->ncaps = 0;
169- return sp_ext_str_empty();
169+ return NULL;
170170 }
171171 int64_t consumed = (mstart + mlen) - sc->pos;
172172 char *taken = sc_substr(sc->source, sc->pos, consumed);
@@ -193,8 +193,8 @@
193193 }
194194
195195 const char *sp_StringScanner_matched(sp_StringScanner *sc) {
196- if (!sc) return sp_ext_str_empty();
197- return sc->matched ? sc->matched : sp_ext_str_empty();
196+ if (!sc || !sc->matched_p) return NULL;
197+ return sc->matched ? sc->matched : NULL;
198198 }
199199
200200 mrb_bool sp_StringScanner_matched_p(sp_StringScanner *sc) {
@@ -225,7 +225,7 @@
225225 if (sc->pos >= slen) {
226226 sc->matched = sp_ext_str_empty();
227227 sc->matched_p = 0;
228- return sp_ext_str_empty();
228+ return NULL;
229229 }
230230 int64_t clen = sc_char_len(sc->source, sc->pos, slen);
231231 char *c = sc_substr(sc->source, sc->pos, clen);
@@ -295,13 +295,13 @@
295295 }
296296
297297 const char *sp_StringScanner_pre_match(sp_StringScanner *sc) {
298- if (!sc) return sp_ext_str_empty();
298+ if (!sc || !sc->matched_p) return NULL;
299299 if (sc->last_pos <= 0) return sp_ext_str_empty();
300300 return sc_substr(sc->source, 0, sc->last_pos);
301301 }
302302
303303 const char *sp_StringScanner_post_match(sp_StringScanner *sc) {
304- if (!sc || !sc->matched_p) return sp_ext_str_empty();
304+ if (!sc || !sc->matched_p) return NULL;
305305 int64_t mlen = (int64_t)sp_ext_str_byte_len(sc->matched);
306306 int64_t start = sc->last_pos + mlen;
307307 int64_t slen = (int64_t)sp_ext_str_byte_len(sc->source);