Commit 01011f53

01011f532228Merged

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

Description

fix(analyze,codegen): reject Regexp.new flag-arg form so divergence is loud

Tighten regexp_new_literal_pattern to require exactly one argument (no block, no kwargs) before folding into the static pattern table. Without this, Regexp.new("foo", Regexp::IGNORECASE) would have matched the 1-arg literal arm and silently dropped the flag -- spinel positions itself as a Ruby subset, so a supported method diverging from CRuby semantics on the same input is a bug, not a quirk to document.

The 2-arg form now falls through to warn_unresolved_call (loud) exactly as it did before the literal-arg fix landed, so callers get the prior diagnostic instead of a silent wrong answer.

Refs #586

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

Details

Commit
01011f532228
Parent
dbcaa07220c3

spinel_analyze.rbmodifiedView file

@@ -1541,11 +1541,18 @@
15411541 end
15421542
15431543 # Returns the literal source string when `nid` is a `Regexp.new("...")`
1544- # (or `Regexp.compile("...")`) call with a single StringNode argument,
1545- # "" otherwise. Lets `pattern = Regexp.new("foo")` fold into the same
1546- # static pattern table as `pattern = /foo/`, so `pattern.match?(s)`
1547- # dispatches through `sp_re_pat_<i>` instead of falling through to
1548- # unresolved-call. Dynamic-arg forms return "" and fall back.
1544+ # (or `Regexp.compile("...")`) call with exactly one StringNode
1545+ # argument, "" otherwise. Lets `pattern = Regexp.new("foo")` fold
1546+ # into the same static pattern table as `pattern = /foo/`, so
1547+ # `pattern.match?(s)` dispatches through `sp_re_pat_<i>` instead of
1548+ # falling through to unresolved-call. Dynamic-arg forms return ""
1549+ # and fall back.
1550+ #
1551+ # The arity check is strict (exactly 1 arg, no kwargs, no block) so
1552+ # `Regexp.new("foo", Regexp::IGNORECASE)` falls through to the loud
1553+ # `warn_unresolved_call` path instead of silently dropping the flag
1554+ # -- silent CRuby divergence on a supported method belongs in the
1555+ # bug bucket, not the quirk bucket.
15491556 def regexp_new_literal_pattern(nid)
15501557 if @nd_type[nid] != "CallNode"
15511558 return ""
@@ -1554,6 +1561,9 @@
15541561 if mn != "new" && mn != "compile"
15551562 return ""
15561563 end
1564+ if @nd_block[nid] >= 0
1565+ return ""
1566+ end
15571567 recv = @nd_receiver[nid]
15581568 if recv < 0
15591569 return ""
@@ -1569,7 +1579,7 @@
15691579 return ""
15701580 end
15711581 arg_ids = get_args(args_id)
1572- if arg_ids.length == 0
1582+ if arg_ids.length != 1
15731583 return ""
15741584 end
15751585 a0 = arg_ids[0]