Commit b0d96174

b0d961748de7Merged

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

Description

fix(analyze,codegen): if/else union of Regexp.new lifts to regexp type

pattern = if X then Regexp.new("a") else Regexp.new("b") end was typed as int (the default fallback for an unhandled CallNode return), so the if/else unify collapsed both arms to int and the subsequent pattern.match?(...) had no static pattern to thread through. The codegen warned cannot resolve call to 'match?' on int (emitting 0) and the binary always reported no match. The direct pattern = Regexp.new("a") form had been working only because the .match? site's writer-tracing side-channel (@local_regex_idx) could find the unique writer; the if/else shape has two distinct writers and the side-channel marked the slot ambiguous.

The fix:

  • Analyzer: infer_call_type for Regexp.new / Regexp.compile returns the new regexp type (a mrb_regexp_pattern *). An if/else with both arms producing Regexp.new now unifies to regexp instead of int, so the receiving local is typed correctly and survives the union.

  • Codegen: compile_call_expr gains a Regexp.new arm that emits the runtime pattern expression via the existing regex_pat_c_expr helper — literal sources resolve to sp_re_pat_, dynamic sources to sp_re_dyn_(). The LV write that consumes this stores the pointer in lv_.

  • Codegen: .match? / .match / =~ dispatch now also accepts a regexp-typed local as the receiver; if regex_pat_c_expr's writer-trace returns empty (the multi-writer case) and the recv's static type is regexp, compile_expr(recv) yields the pointer directly.

  • Both files: is_nullable_pointer_type("regexp") -> 1 so a regexp slot inherits the same NULL-init / nil-compare path as the other pointer-shaped types.

The fix preserves the existing single-write inline path — single-writer locals still resolve via @local_regex_idx without the extra local store, so the canonical pattern = Regexp.new("foo"); pattern.match?(s) shape compiles to the same code it did before.

Closes #609

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
b0d961748de7
Parent
02a294eeffcf

spinel_analyze.rbmodifiedView file

@@ -3036,6 +3036,19 @@
30363036 end
30373037 end
30383038
3039+ # `Regexp.new(<arg>)` / `Regexp.compile(<arg>)` returns a compiled
3040+ # pattern. Static type is the new `regexp` (mrb_regexp_pattern *),
3041+ # so an if/else that builds patterns in both arms unifies cleanly
3042+ # instead of collapsing to int and losing the .match? dispatch
3043+ # downstream. Issue #609. The existing single-write side-channel
3044+ # in codegen still inlines literal/dyn references through
3045+ # regex_pat_c_expr; the regexp type is what survives across the
3046+ # multi-write boundary (the new LV write emit assigns the pointer
3047+ # to lv_<name>, the new .match? recv arm uses it).
3048+ if regexp_new_call_arg_id(nid) >= 0
3049+ return "regexp"
3050+ end
3051+
30393052 # Methods on a `rescue => e` bound exception variable. The variable
30403053 # itself is string-typed but .class / .message / .to_s / .inspect /
30413054 # .full_message return strings; .backtrace returns nil for now.