Commit 757ecfc3

757ecfc377bcMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~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
757ecfc377bc
Parent
9dc840407ba1

spinel_codegen.rbmodifiedView file

@@ -1550,12 +1550,21 @@
15501550 # underlying pattern, so `RX = /pat/; RX.match?(s)` and
15511551 # `s =~ RX` dispatch to the engine instead of falling through
15521552 # to the literal-`(-1)` / `sp_str_include` fallbacks.
1553+ # Also unwrap `.freeze` (`PATTERN = /pat/.freeze`) — Prism wraps
1554+ # the literal in a `freeze` CallNode whose receiver is the
1555+ # RegularExpressionNode. Issue #394.
15531556 if @nd_type[nid] == "ConstantReadNode"
15541557 cname = resolve_const_ref_name(nid)
15551558 if cname != ""
15561559 ci = find_const_idx(cname)
15571560 if ci >= 0 && ci < @const_expr_ids.length
15581561 eid = @const_expr_ids[ci]
1562+ if eid >= 0 && @nd_type[eid] == "CallNode" && @nd_name[eid] == "freeze"
1563+ recv_freeze = @nd_receiver[eid]
1564+ if recv_freeze >= 0
1565+ eid = recv_freeze
1566+ end
1567+ end
15591568 if eid >= 0 && @nd_type[eid] == "RegularExpressionNode"
15601569 return find_regexp_index(eid)
15611570 end