Project

General

Profile

Actions

Feature #8953

closed

`str =~ /pattern/` does not call =~ method if (1) str is a String, (2) /pattern/ is a Regexp literal

Added by gfx (Goro FUJI) over 10 years ago. Updated over 2 years ago.

Status:
Closed
Target version:
-
[ruby-core:57385]

Description

The expression a =~ b does not call the =~ method in some cases.
I think it is a bug that results from optimizations.

See the following code for details:

Code that does not work as expected (shows nothing):

$ ruby -e 's = "foo"; class << s; def =~ (rhs); raise "a"; end; end; s =~ /foo/' # does nothing

Code that works as expected (raises errors):

$ ruby -e 's = "foo"; class << s; def =~ (rhs); raise "a"; end; end; s.=~ /foo/' # call =~ as a method
$ ruby -e 's = "foo"; class << s; def =~ (rhs); raise "a"; end; end; s =~ -> { /foo/ }.call' # RHS is not a Regexp literal but a Regexp object
$ ruby -e 's = Objec.new; class << s; def =~ (rhs); raise "a"; end; end; s =~ /foo/' # LHS is not a String

Updated by sorah (Sorah Fukumori) over 10 years ago

hm...

ruby -e'class Regexp; def =~(o); true; end; end; p /a/ =~ "b"' #=> nil

Actions #2

Updated by Anonymous over 10 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r43050.
Goro, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • insns.def (opt_regexpmatch1): check Regexp#=~ is not defined before
    calling rb_reg_match()

  • test/ruby/test_regexp.rb: add test

  • vm.c (ruby_vm_redefined_flag): change type to short[]

  • vm.c (vm_redefinition_check_flag): return REGEXP_REDEFINED_OP_FLAG if
    klass == rb_cRegexp

  • vm.c (vm_init_redefined_flag): setup BOP flag for Regexp#=~

  • vm_insnhelper.h: add REGEXP_REDEFINED_OP_FLAG

[ruby-core:57385] [Bug #8953]

Actions #3

Updated by sorah (Sorah Fukumori) over 10 years ago

  • Status changed from Closed to Open
  • % Done changed from 100 to 50

The problem still exists for opt_regexpmatch1, right?

Actions #4

Updated by sorah (Sorah Fukumori) over 10 years ago

The problem still exists for opt_regexpmatch1, right?

s/opt_regexpmatch1/opt_regexpmatch2/

Actions #5

Updated by Anonymous over 10 years ago

Ah you are right. Thanks for pointing that out sorah.

Actions #6

Updated by sorah (Sorah Fukumori) over 10 years ago

  • Status changed from Open to Assigned
  • Assignee set to sorah (Sorah Fukumori)

Testing my patch on my machine for regexpmatch2

Actions #7

Updated by sorah (Sorah Fukumori) over 10 years ago

  • Status changed from Assigned to Closed
  • % Done changed from 50 to 100

This issue was solved with changeset r43052.
Goro, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • insns.def (opt_regexpmatch2): Check String#=~ hasn't overridden
    before calling rb_reg_match().

  • test/ruby/test_string.rb: Test for above.

  • vm.c (vm_init_redefined_flag): Add BOP flag for String#=~

[ruby-core:57385] [Bug #8953]

Updated by sorah (Sorah Fukumori) over 10 years ago

  • Backport changed from 1.9.3: UNKNOWN, 2.0.0: UNKNOWN to 1.9.3: UNKNOWN, 2.0.0: REQUIRED

Please backport r43050, 43052

Actions #9

Updated by nagachika (Tomoyuki Chikanaga) over 10 years ago

  • Tracker changed from Bug to Backport
  • Project changed from Ruby master to Backport200
  • Status changed from Closed to Assigned
  • Assignee changed from sorah (Sorah Fukumori) to nagachika (Tomoyuki Chikanaga)

Updated by ko1 (Koichi Sasada) over 10 years ago

I checked previous behavior.

versions/install-tags_v1_6_5_/bin/ruby
versions/install-tags_v1_6_6_/bin/ruby
versions/install-tags_v1_6_7_/bin/ruby
versions/install-tags_v1_6_8_/bin/ruby
versions/install-tags_v1_8_5_/bin/ruby
versions/install-tags_v1_8_5_113_/bin/ruby
versions/install-tags_v1_8_5_114_/bin/ruby
versions/install-tags_v1_8_5_115_/bin/ruby
versions/install-tags_v1_8_5_11_/bin/ruby
versions/install-tags_v1_8_5_12_/bin/ruby
versions/install-tags_v1_8_5_231_/bin/ruby
versions/install-tags_v1_8_5_2_/bin/ruby
versions/install-tags_v1_8_5_35_/bin/ruby
versions/install-tags_v1_8_5_52_/bin/ruby
versions/install-tags_v1_8_5_preview1_/bin/ruby
versions/install-tags_v1_8_5_preview2_/bin/ruby
versions/install-tags_v1_8_5_preview3_/bin/ruby
versions/install-tags_v1_8_5_preview4_/bin/ruby
versions/install-tags_v1_8_5_preview5_/bin/ruby
versions/install-tags_v1_8_6_/bin/ruby
versions/install-tags_v1_8_6_110_/bin/ruby
versions/install-tags_v1_8_6_111_/bin/ruby
versions/install-tags_v1_8_6_114_/bin/ruby
versions/install-tags_v1_8_6_230_/bin/ruby
versions/install-tags_v1_8_6_286_/bin/ruby
versions/install-tags_v1_8_6_287_/bin/ruby
versions/install-tags_v1_8_6_368_/bin/ruby
versions/install-tags_v1_8_6_369_/bin/ruby
versions/install-tags_v1_8_6_36_/bin/ruby
versions/install-tags_v1_8_6_383_/bin/ruby
versions/install-tags_v1_8_6_388_/bin/ruby
versions/install-tags_v1_8_6_398_/bin/ruby
versions/install-tags_v1_8_6_399_/bin/ruby
versions/install-tags_v1_8_6_420_/bin/ruby
versions/install-tags_v1_8_6_preview1_/bin/ruby
versions/install-tags_v1_8_6_preview2_/bin/ruby
versions/install-tags_v1_8_6_preview3_/bin/ruby
versions/install-tags_v1_8_7_/bin/ruby
versions/install-tags_v1_8_7_160_/bin/ruby
versions/install-tags_v1_8_7_173_/bin/ruby
versions/install-tags_v1_8_7_174_/bin/ruby
versions/install-tags_v1_8_7_17_/bin/ruby
versions/install-tags_v1_8_7_22_/bin/ruby
versions/install-tags_v1_8_7_248_/bin/ruby
versions/install-tags_v1_8_7_249_/bin/ruby
versions/install-tags_v1_8_7_299_/bin/ruby
versions/install-tags_v1_8_7_301_/bin/ruby
versions/install-tags_v1_8_7_302_/bin/ruby
versions/install-tags_v1_8_7_330_/bin/ruby
versions/install-tags_v1_8_7_334_/bin/ruby
versions/install-tags_v1_8_7_352_/bin/ruby
versions/install-tags_v1_8_7_357_/bin/ruby
versions/install-tags_v1_8_7_358_/bin/ruby
versions/install-tags_v1_8_7_369_/bin/ruby
versions/install-tags_v1_8_7_370_/bin/ruby
versions/install-tags_v1_8_7_371_/bin/ruby
versions/install-tags_v1_8_7_373_/bin/ruby
versions/install-tags_v1_8_7_374_/bin/ruby
versions/install-tags_v1_8_7_71_/bin/ruby
versions/install-tags_v1_8_7_72_/bin/ruby
versions/install-tags_v1_8_7_preview1_/bin/ruby
versions/install-tags_v1_8_7_preview2_/bin/ruby
versions/install-tags_v1_8_7_preview3_/bin/ruby
versions/install-tags_v1_8_7_preview4_/bin/ruby
versions/install-tags_v1_9_0_0_/bin/ruby
versions/install-tags_v1_9_0_1_/bin/ruby
versions/install-tags_v1_9_0_2_/bin/ruby
versions/install-tags_v1_9_0_3_/bin/ruby
versions/install-tags_v1_9_0_4_/bin/ruby
versions/install-tags_v1_9_0_5_/bin/ruby
versions/install-tags_v1_9_1_0_/bin/ruby
versions/install-tags_v1_9_1_126_/bin/ruby
versions/install-tags_v1_9_1_127_/bin/ruby
versions/install-tags_v1_9_1_128_/bin/ruby
versions/install-tags_v1_9_1_129_/bin/ruby
versions/install-tags_v1_9_1_243_/bin/ruby
versions/install-tags_v1_9_1_376_/bin/ruby
versions/install-tags_v1_9_1_378_/bin/ruby
versions/install-tags_v1_9_1_429_/bin/ruby
versions/install-tags_v1_9_1_430_/bin/ruby
versions/install-tags_v1_9_1_431_/bin/ruby
versions/install-tags_v1_9_1_preview1_/bin/ruby
versions/install-tags_v1_9_1_preview2_/bin/ruby
versions/install-tags_v1_9_1_rc1_/bin/ruby
versions/install-tags_v1_9_1_rc2_/bin/ruby
versions/install-tags_v1_9_2_0_/bin/ruby
versions/install-tags_v1_9_2_136_/bin/ruby
versions/install-tags_v1_9_2_180_/bin/ruby
versions/install-tags_v1_9_2_290_/bin/ruby
versions/install-tags_v1_9_2_318_/bin/ruby
versions/install-tags_v1_9_2_320_/bin/ruby
versions/install-tags_v1_9_2_preview1_/bin/ruby
versions/install-tags_v1_9_2_preview2_/bin/ruby
versions/install-tags_v1_9_2_preview3_/bin/ruby
versions/install-tags_v1_9_2_rc1_/bin/ruby
versions/install-tags_v1_9_2_rc2_/bin/ruby
versions/install-tags_v1_9_3_0_/bin/ruby
versions/install-tags_v1_9_3_125_/bin/ruby
versions/install-tags_v1_9_3_194_/bin/ruby
versions/install-tags_v1_9_3_195_/bin/ruby
versions/install-tags_v1_9_3_286_/bin/ruby
versions/install-tags_v1_9_3_327_/bin/ruby
versions/install-tags_v1_9_3_360_/bin/ruby
versions/install-tags_v1_9_3_362_/bin/ruby
versions/install-tags_v1_9_3_374_/bin/ruby
versions/install-tags_v1_9_3_385_/bin/ruby
versions/install-tags_v1_9_3_392_/bin/ruby
versions/install-tags_v1_9_3_429_/bin/ruby
versions/install-tags_v1_9_3_448_/bin/ruby
versions/install-tags_v1_9_3_preview1_/bin/ruby
versions/install-tags_v1_9_3_rc1_/bin/ruby
versions/install-tags_v2_0_0_0_/bin/ruby
versions/install-tags_v2_0_0_195_/bin/ruby
versions/install-tags_v2_0_0_247_/bin/ruby
versions/install-tags_v2_0_0_preview1_/bin/ruby
versions/install-tags_v2_0_0_preview2_/bin/ruby
versions/install-tags_v2_0_0_rc1_/bin/ruby
versions/install-tags_v2_0_0_rc2_/bin/ruby

And all of versions ignore the redefinition.
I implemented 1.9 VM as this behaviour because I think it is spec.

Could you ask matz?

Updated by ko1 (Koichi Sasada) over 10 years ago

(2013/09/27 1:13), ko1 (Koichi Sasada) wrote:

And all of versions ignore the redefinition.
I implemented 1.9 VM as this behaviour because I think it is spec.

Could you ask matz?

I have no objection about that.
However, please be more careful to change the behavior.
(this phrase is also for me, of course)

--
// SASADA Koichi at atdot dot net

Updated by akr (Akira Tanaka) over 10 years ago

2013/9/27 ko1 (Koichi Sasada) :

Issue #8953 has been updated by ko1 (Koichi Sasada).

And all of versions ignore the redefinition.
I implemented 1.9 VM as this behaviour because I think it is spec.

I also think so.
The spec is one of the reason Ruby can assign named capture to local variable.

% ruby -e '/def(?.*)/ =~ "abcdefghi"; p var'
"ghi"

Tanaka Akira

Actions #13

Updated by nagachika (Tomoyuki Chikanaga) over 10 years ago

  • Tracker changed from Backport to Feature
  • Project changed from Backport200 to Ruby master
  • Assignee changed from nagachika (Tomoyuki Chikanaga) to Anonymous

Updated by ko1 (Koichi Sasada) over 10 years ago

  • Category set to core
  • Target version set to 2.1.0
  • Assignee changed from Anonymous to matz (Yukihiro Matsumoto)

Could you check it?

Updated by hsbt (Hiroshi SHIBATA) about 10 years ago

  • Target version changed from 2.1.0 to 2.2.0
Actions #16

Updated by naruse (Yui NARUSE) about 6 years ago

  • Target version deleted (2.2.0)

Updated by jeremyevans0 (Jeremy Evans) over 2 years ago

  • Status changed from Assigned to Closed

This was fixed in Ruby 2.2:

$ ruby22 -ve 's = "foo"; class << s; def =~ (rhs); raise "a"; end; end; s =~ /foo/'
ruby 2.2.10p489 (2018-03-28 revision 63023) [x86_64-openbsd]
-e:1:in `=~': a (RuntimeError)
        from -e:1:in `<main>'
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0