Skip to content

Strange error in implicit return of match variable #3104

@gavin-scott

Description

@gavin-scott

In some cases implicitly returned match variables ($1) are being incorrectly returned as nil. Using an explicit return fixes the issue. This was seen on jruby 9.0.0.0.rc1.

Here is a simple script that reproduces the issue:

[gscott@dev tmp]$ jruby --version
jruby 9.0.0.0.rc1 (2.2.2) 2015-06-10 a0bf3b3 Java HotSpot(TM) 64-Bit Server VM 25.45-b02 on 1.8.0_45-b14 +jit [linux-amd64]
[gscott@dev tmp]$ cat test.rb
def parse_char_with_return(string)
  if string =~ /Prefix (\w)/
    puts "++> parse_char_with_return(#{string}) = #{$1}"
    return $1
  end
end

def parse_char_wo_return(string)
  if string =~ /Prefix (\w)/
    puts "++> parse_char_wo_return(#{string}) = #{$1}"
    $1
  end
end

ret = parse_char_with_return("Prefix A")
puts "==> parse_char_with_return(Prefix A) = #{ret.inspect}"
raise(Exception, 'Failed to return correct value') if ret.nil?

puts ''
ret = parse_char_wo_return("Prefix A")
puts "==> parse_char_wo_return(Prefix A) = #{ret.inspect}"
[gscott@dev tmp]$ jruby test.rb
++> parse_char_with_return(Prefix A) = A
==> parse_char_with_return(Prefix A) = "A"

++> parse_char_wo_return(Prefix A) = A
==> parse_char_wo_return(Prefix A) = nil

Both parse_char_with_return and parse_char_wo_return should return "A" but the latter returns nil as shown above.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions