Project

General

Profile

Bug #21933

Updated by katsyoshi (Katsuyoshi MATSUMOTO) about 2 months ago

Environment: 
 - Ruby 4.0.1 
 - RUBY_BOX=1 
 - OS: Linux x86_64 

 Summary: 
 With Ruby::Box enabled, a named capture local variable from `=~` may become nil, 
 even when the regexp matches. This happens after iterating over non-matching lines first. 

 Reproducer: 

 sample.rb 
 ``` ruby 

 text = <<~ASM 
   .intel_syntax noprefix 
   .globl main 
 main: 
   nop 
 ASM 

 cur=nil 
 text.each_line do |line| 
   if /^(?<label>[_A-Za-z.]\w*):/ =~ line 
     p [:matched, line.inspect, label.inspect] 
     cur = label 
     break 
   end 
 end 
 p [:cur, cur.inspect] 
 ``` 

 Expected: 
 ``` shell 
 Expected: 
 [:matched, "\"main:\\n\"", "\"main\""] 
 [:cur, "\"main\""] 
 ``` 


 Actual (with RUBY_BOX=1): 
 ``` shell 
 [:matched, "\"main:\\n\"", "nil"] 
 [:cur, "nil"] 

 ``` 

 Control: 
 Without RUBY_BOX=1, the same script returns "main" as expected.

Back