Skip to content

mruby-regexp: test break out of the Regexp#match block - #6992

Merged
matz merged 1 commit into
mruby:masterfrom
takumin:regexp-match-block-break
Aug 2, 2026
Merged

mruby-regexp: test break out of the Regexp#match block#6992
matz merged 1 commit into
mruby:masterfrom
takumin:regexp-match-block-break

Conversation

@takumin

@takumin takumin commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Regexp#match accepts a block (MRB_ARGS_BLOCK() in regexp.c, yielded in
regexp_match()), but the suite only covered the value the block returns
normally:

assert("Regexp#match - block") do
  result = /bc/.match("abcd") { |md| [md[0], md.begin(0)] }
  assert_equal ["bc", 1], result
  assert_nil(/xyz/.match("abcd") { |md| md[0] })
end

Nothing exercised a non-local exit from that block. The behaviour is correct
today, because mrb_yield() propagates the break:

$ ./build/host/bin/mruby -e 'p(/l+/.match("hello") { break :broke })'
:broke

That is an easy property to lose when regexp_match() is refactored -- for
example if the yield moves behind an error-handling wrapper, or is replaced by
a call that swallows the break state. The regression would be silent: the
method would return the MatchData instead of the break value.

Test-only; no implementation change.

Related to #6991, which adds the same coverage for String#match once the
block is forwarded there. This one stands on its own and touches a different
part of the test file, so the two do not conflict.

rake test passes.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed Regexp#match to return the value provided by a block when it exits early with break.
  • Tests

    • Added regression coverage for early block exits.

`Regexp#match` accepts a block and yields the `MatchData`, but the suite
only covered the value the block returns normally. A non-local exit from
that block was untested, even though it works today because `mrb_yield()`
propagates the break.

That is easy to lose in a refactor of `regexp_match()` -- moving the yield
behind an error-handling wrapper, or replacing it with a call that swallows
the break state, would silently make the method return the `MatchData`
instead of the break value.

Test-only; no implementation change.
@takumin
takumin requested a review from matz as a code owner August 2, 2026 13:01
@coderabbitai

coderabbitai Bot commented Aug 2, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 7273c242-ddc5-4d9f-a845-a2781504ab38

📥 Commits

Reviewing files that changed from the base of the PR and between 11ff450 and 46b995b.

📒 Files selected for processing (1)
  • mrbgems/mruby-regexp/test/regexp.rb

📝 Walkthrough

Walkthrough

The test suite adds regression coverage for Regexp#match returning a block’s break value.

Changes

Regexp match break behavior

Layer / File(s) Summary
Regexp match break return test
mrbgems/mruby-regexp/test/regexp.rb
The test asserts that breaking from a Regexp#match block returns :broke.

Estimated code review effort: 1 (Trivial) | ~2 minutes

Possibly related PRs

Suggested reviewers: matz

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the regression test for breaking out of a block passed to Regexp#match.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@matz
matz merged commit f7be5bf into mruby:master Aug 2, 2026
21 checks passed
@takumin
takumin deleted the regexp-match-block-break branch August 2, 2026 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants