Commit eaefc161

eaefc1614fdaMerged

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

Description

fix(codegen): regex const initialised with .freeze resolves through to the pattern

Fixes #394.

PATTERN = /pat/.freeze wraps the literal in a .freeze CallNode in Prism. find_regexp_index walked @const_expr_ids looking for a RegularExpressionNode and missed the wrapper, so gsub(PATTERN, ...) fell through to sp_str_gsub(... cst_PATTERN ...) (a mrb_int arg into a const char * slot — fails C-compile under -Werror).

Fix: peel the .freeze CallNode wrapper off @const_expr_ids[ci] before checking for RegularExpressionNode. The RX = /pat/ form without .freeze already worked.

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

Details

Commit
eaefc1614fda
Parent
f5f0efa6cd70

spinel_codegen.rbmodifiedView file

@@ -1771,12 +1771,21 @@
17711771 # underlying pattern, so `RX = /pat/; RX.match?(s)` and
17721772 # `s =~ RX` dispatch to the engine instead of falling through
17731773 # to the literal-`(-1)` / `sp_str_include` fallbacks.
1774+ # Also unwrap `.freeze` (`PATTERN = /pat/.freeze`) — Prism wraps
1775+ # the literal in a `freeze` CallNode whose receiver is the
1776+ # RegularExpressionNode. Issue #394.
17741777 if @nd_type[nid] == "ConstantReadNode"
17751778 cname = resolve_const_ref_name(nid)
17761779 if cname != ""
17771780 ci = find_const_idx(cname)
17781781 if ci >= 0 && ci < @const_expr_ids.length
17791782 eid = @const_expr_ids[ci]
1783+ if eid >= 0 && @nd_type[eid] == "CallNode" && @nd_name[eid] == "freeze"
1784+ recv_freeze = @nd_receiver[eid]
1785+ if recv_freeze >= 0
1786+ eid = recv_freeze
1787+ end
1788+ end
17801789 if eid >= 0 && @nd_type[eid] == "RegularExpressionNode"
17811790 return find_regexp_index(eid)
17821791 end