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
regexptype (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
end end # `Regexp.new(<arg>)` / `Regexp.compile(<arg>)` returns a compiled # pattern. Static type is the new `regexp` (mrb_regexp_pattern *), # so an if/else that builds patterns in both arms unifies cleanly # instead of collapsing to int and losing the .match? dispatch # downstream. Issue #609. The existing single-write side-channel # in codegen still inlines literal/dyn references through # regex_pat_c_expr; the regexp type is what survives across the # multi-write boundary (the new LV write emit assigns the pointer # to lv_<name>, the new .match? recv arm uses it). if regexp_new_call_arg_id(nid) >= 0 return "regexp" end # Methods on a `rescue => e` bound exception variable. The variable # itself is string-typed but .class / .message / .to_s / .inspect / # .full_message return strings; .backtrace returns nil for now.spinel_codegen.rbmodifiedView file
Loading diff…
test/regexp_local_if_else_union.rbaddedView file
Loading diff…
test/regexp_local_if_else_union.rb.expectedaddedView file
Loading diff…