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 + xsees 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
}const char *sp_StringScanner_scan(sp_StringScanner *sc, mrb_regexp_pattern *pat) { if (!sc || !pat) return sp_ext_str_empty(); if (!sc || !pat) return NULL; int64_t slen = (int64_t)sp_ext_str_byte_len(sc->source); int64_t mlen = sc_match_at_pos(pat, sc->source, slen, sc->pos, sc->caps, &sc->ncaps); if (mlen < 0) { sc->matched = sp_ext_str_empty(); sc->matched_p = 0; sc->ncaps = 0; return sp_ext_str_empty(); return NULL; } char *m = sc_substr(sc->source, sc->pos, mlen); sc->last_pos = sc->pos;}const char *sp_StringScanner_check(sp_StringScanner *sc, mrb_regexp_pattern *pat) { if (!sc || !pat) return sp_ext_str_empty(); if (!sc || !pat) return NULL; int64_t slen = (int64_t)sp_ext_str_byte_len(sc->source); int64_t mlen = sc_match_at_pos(pat, sc->source, slen, sc->pos, sc->caps, &sc->ncaps); if (mlen < 0) { sc->matched = sp_ext_str_empty(); sc->matched_p = 0; sc->ncaps = 0; return sp_ext_str_empty(); return NULL; } char *m = sc_substr(sc->source, sc->pos, mlen); sc->last_pos = sc->pos; sc->matched = sp_ext_str_empty(); sc->matched_p = 0; sc->ncaps = 0; return sp_ext_str_empty(); return NULL; } int64_t consumed = (mstart + mlen) - sc->pos; char *taken = sc_substr(sc->source, sc->pos, consumed);}const char *sp_StringScanner_matched(sp_StringScanner *sc) { if (!sc) return sp_ext_str_empty(); return sc->matched ? sc->matched : sp_ext_str_empty(); if (!sc || !sc->matched_p) return NULL; return sc->matched ? sc->matched : NULL;}mrb_bool sp_StringScanner_matched_p(sp_StringScanner *sc) { if (sc->pos >= slen) { sc->matched = sp_ext_str_empty(); sc->matched_p = 0; return sp_ext_str_empty(); return NULL; } int64_t clen = sc_char_len(sc->source, sc->pos, slen); char *c = sc_substr(sc->source, sc->pos, clen);}const char *sp_StringScanner_pre_match(sp_StringScanner *sc) { if (!sc) return sp_ext_str_empty(); if (!sc || !sc->matched_p) return NULL; if (sc->last_pos <= 0) return sp_ext_str_empty(); return sc_substr(sc->source, 0, sc->last_pos);}const char *sp_StringScanner_post_match(sp_StringScanner *sc) { if (!sc || !sc->matched_p) return sp_ext_str_empty(); if (!sc || !sc->matched_p) return NULL; int64_t mlen = (int64_t)sp_ext_str_byte_len(sc->matched); int64_t start = sc->last_pos + mlen; int64_t slen = (int64_t)sp_ext_str_byte_len(sc->source);spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/string_scanner_nullable.rbaddedView file
Loading diff…
test/string_scanner_nullable.rb.expectedaddedView file
Loading diff…