Skip to content

Commit 392e22d

Browse files
committed
Fix setting $~ for Enumerator::Lazy#grep
1 parent ea3ed76 commit 392e22d

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

spec/tags/core/enumerator/lazy/grep_tags.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/main/ruby/truffleruby/core/enumerator.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -394,24 +394,26 @@ def grep(pattern, &block)
394394
if block_given?
395395
Lazy.new(self, nil) do |yielder, *args|
396396
val = args.length >= 2 ? args : args.first
397-
if pattern === val
398-
Truffle::RegexpOperations.set_last_match($~, block.binding)
397+
matches = pattern === val
398+
Truffle::RegexpOperations.set_last_match($~, block.binding)
399+
400+
if matches
399401
yielder.yield yield(val)
400402
end
401403
end
402404
else
403-
lazy = Lazy.new(self, nil) do |yielder, *args|
405+
# :caller_binding is not optimized in blocks right now, and we want the caller of #grep
406+
caller_binding = Truffle.invoke_primitive(:caller_binding)
407+
408+
Lazy.new(self, nil) do |yielder, *args|
404409
val = args.length >= 2 ? args : args.first
405-
if pattern === val
410+
matches = pattern === val
411+
Truffle::RegexpOperations.set_last_match($~, caller_binding)
412+
413+
if matches
406414
yielder.yield val
407415
end
408-
409-
Truffle::RegexpOperations.set_last_match($~, Truffle.invoke_primitive(:caller_binding))
410416
end
411-
412-
Truffle::RegexpOperations.set_last_match($~, Truffle.invoke_primitive(:caller_binding))
413-
414-
lazy
415417
end
416418
end
417419

0 commit comments

Comments
 (0)