Commit 66b013c4

66b013c463c5Merged

Ori Pekelman <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

codegen: resolve a regex held in a namespaced constant (M::RE)

find_regexp_index's constant arm gated on ConstantReadNode only, so a namespaced constant regex (M::RE) fell through to the fallbacks: a s.gsub(M::RE, ...) silently no-op'd (returned the receiver unchanged) and s.scan(M::RE) raised undefined method 'scan' for string.

resolve_const_ref_name already maps M::RE to the registered constant name M_RE, so widening the gate to also accept ConstantPathNode forwards it through the same static pattern table as a top-level constant. =~ / match / sub / split share the path via regex_pat_c_expr.

Adds test/regex_namespaced_const.rb (incl. a nested A::B::WS.freeze case).

Closes #1008

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

Details

Commit
66b013c463c5
Parent
7780fff2c6e8

spinel_codegen.rbmodifiedView file

@@ -1998,7 +1998,11 @@
19981998 # Also unwrap `.freeze` (`PATTERN = /pat/.freeze`) — Prism wraps
19991999 # the literal in a `freeze` CallNode whose receiver is the
20002000 # RegularExpressionNode. .
2001- if @nd_type[nid] == "ConstantReadNode"
2001+ # ConstantPathNode (`M::RX`) resolves the same way — resolve_const_ref_name
2002+ # already maps `M::RX` to the registered name `M_RX`. Without this, a
2003+ # namespaced constant regex fell through, so `s.gsub(M::RX, ...)` silently
2004+ # no-op'd and `s.scan(M::RX)` raised undefined-method (e.g. emoji_regex).
2005+ if @nd_type[nid] == "ConstantReadNode" || @nd_type[nid] == "ConstantPathNode"
20022006 cname = resolve_const_ref_name(nid)
20032007 if cname != ""
20042008 ci = find_const_idx(cname)