Skip to content

When Thread::Backtrace::Location#base_label and #label are #== on MRI they are also #equal? #5163

@ivoanjo

Description

@ivoanjo

Hello again!

I'm using Thread#backtrace_locations and noticed several differences between MRI and JRuby.

I'll report them separately because they may have different fixes, but feel free to mark any as duplicate if it makes sense to do so.


Environment

  • JRuby: jruby 9.1.17.0 (2.3.3) 2018-04-20 d8b1ff9 Java HotSpot(TM) 64-Bit Server VM 25.171-b11 on 1.8.0_171-b11 +jit [linux-x86_64]
  • Kernel: Linux u186024434db159d25c92 4.13.0-39-generic #44~16.04.1-Ubuntu SMP Thu Apr 5 16:43:10 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
  • Distro: Ubuntu 16.04.4 LTS

Expected Behavior

When looking at a Thread::Backtrace::Location, MRI provides both #base_label and #label.

For most methods, these are exactly the same, but for blocks it allows the caller to get the name of the method separate from the "block in" text without resorting to parsing the result.

An additional detail from MRI is that whenever #base_label and #label match, the underlying String is reused and thus both are #equal?. This allows a very fast check if a Location is a block or not, as we can use equal? instead of a heavier ==.

Testcase:

puts RUBY_DESCRIPTION

def test
  location = Thread.current.backtrace_locations[1]
  puts "#base_label '#{location.base_label}', #label '#{location.label}', equal? #{location.base_label.equal?(location.label)}"
end

test

Output on MRI:

ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
#base_label 'test', #label 'test', equal? true

Actual Behavior

JRuby creates different Strings for both methods, so they end up not being equal?.

jruby 9.1.17.0 (2.3.3) 2018-04-20 d8b1ff9 Java HotSpot(TM) 64-Bit Server VM 25.171-b11 on 1.8.0_171-b11 +jit [linux-x86_64]
#base_label 'test', #label 'test', equal? false

If it would be helpful I can also submit a testcase to RubySpec.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions