Feature #8953

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

Added by Goro Fuji 7 months ago. Updated 3 months ago.

[ruby-core:57385]
Status:Assigned
Priority:Normal
Assignee:Yukihiro Matsumoto
Category:core
Target version:current: 2.2.0

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

Associated revisions

Revision 43050
Added by Charlie Somerville 7 months ago

  • insns.def (optregexpmatch1): check Regexp#=~ is not defined before
    calling rb
    reg_match()

  • test/ruby/test_regexp.rb: add test

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

  • vm.c (vmredefinitioncheckflag): return REGEXPREDEFINEDOPFLAG if
    klass == rb_cRegexp

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

  • vminsnhelper.h: add REGEXPREDEFINEDOPFLAG

[Bug #8953]

Revision 43052
Added by Shota Fukumori 7 months ago

  • insns.def (optregexpmatch2): Check String#=~ hasn't overridden
    before calling rb
    reg_match().

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

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

[Bug #8953]

History

#1 Updated by Shota Fukumori 7 months ago

hm...

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

#2 Updated by Charlie Somerville 7 months 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 (optregexpmatch1): check Regexp#=~ is not defined before
    calling rb
    reg_match()

  • test/ruby/test_regexp.rb: add test

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

  • vm.c (vmredefinitioncheckflag): return REGEXPREDEFINEDOPFLAG if
    klass == rb_cRegexp

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

  • vminsnhelper.h: add REGEXPREDEFINEDOPFLAG

[Bug #8953]

#3 Updated by Shota Fukumori 7 months ago

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

The problem still exists for opt_regexpmatch1, right?

#4 Updated by Shota Fukumori 7 months ago

The problem still exists for opt_regexpmatch1, right?

s/optregexpmatch1/optregexpmatch2/

#5 Updated by Charlie Somerville 7 months ago

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

#6 Updated by Shota Fukumori 7 months ago

  • Status changed from Open to Assigned
  • Assignee set to Shota Fukumori

Testing my patch on my machine for regexpmatch2

#7 Updated by Shota Fukumori 7 months 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 (optregexpmatch2): Check String#=~ hasn't overridden
    before calling rb
    reg_match().

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

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

[Bug #8953]

#8 Updated by Shota Fukumori 7 months 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

#9 Updated by Tomoyuki Chikanaga 7 months ago

  • Tracker changed from Bug to Backport
  • Project changed from ruby-trunk to Backport200
  • Status changed from Closed to Assigned
  • Assignee changed from Shota Fukumori to Tomoyuki Chikanaga

#10 Updated by Koichi Sasada 7 months ago

I checked previous behavior.

versions/install-tagsv165/bin/ruby
versions/install-tagsv166/bin/ruby
versions/install-tagsv167/bin/ruby
versions/install-tagsv168/bin/ruby
versions/install-tagsv185/bin/ruby
versions/install-tagsv185113/bin/ruby
versions/install-tags
v185114/bin/ruby
versions/install-tagsv185115/bin/ruby
versions/install-tags
v18511/bin/ruby
versions/install-tagsv18512/bin/ruby
versions/install-tags
v185231/bin/ruby
versions/install-tagsv1852/bin/ruby
versions/install-tags
v18535/bin/ruby
versions/install-tagsv18552/bin/ruby
versions/install-tags
v185preview1/bin/ruby
versions/install-tagsv185preview2/bin/ruby
versions/install-tags
v185preview3/bin/ruby
versions/install-tagsv185preview4/bin/ruby
versions/install-tags
v185preview5/bin/ruby
versions/install-tagsv186/bin/ruby
versions/install-tagsv186110/bin/ruby
versions/install-tags
v186111/bin/ruby
versions/install-tagsv186114/bin/ruby
versions/install-tags
v186230/bin/ruby
versions/install-tagsv186286/bin/ruby
versions/install-tags
v186287/bin/ruby
versions/install-tagsv186368/bin/ruby
versions/install-tags
v186369/bin/ruby
versions/install-tagsv18636/bin/ruby
versions/install-tags
v186383/bin/ruby
versions/install-tagsv186388/bin/ruby
versions/install-tags
v186398/bin/ruby
versions/install-tagsv186399/bin/ruby
versions/install-tags
v186420/bin/ruby
versions/install-tagsv186preview1/bin/ruby
versions/install-tags
v186preview2/bin/ruby
versions/install-tagsv186preview3/bin/ruby
versions/install-tags
v187/bin/ruby
versions/install-tags
v187160/bin/ruby
versions/install-tagsv187173/bin/ruby
versions/install-tags
v187174/bin/ruby
versions/install-tagsv18717/bin/ruby
versions/install-tags
v18722/bin/ruby
versions/install-tagsv187248/bin/ruby
versions/install-tags
v187249/bin/ruby
versions/install-tagsv187299/bin/ruby
versions/install-tags
v187301/bin/ruby
versions/install-tagsv187302/bin/ruby
versions/install-tags
v187330/bin/ruby
versions/install-tagsv187334/bin/ruby
versions/install-tags
v187352/bin/ruby
versions/install-tagsv187357/bin/ruby
versions/install-tags
v187358/bin/ruby
versions/install-tagsv187369/bin/ruby
versions/install-tags
v187370/bin/ruby
versions/install-tagsv187371/bin/ruby
versions/install-tags
v187373/bin/ruby
versions/install-tagsv187374/bin/ruby
versions/install-tags
v18771/bin/ruby
versions/install-tagsv18772/bin/ruby
versions/install-tags
v187preview1/bin/ruby
versions/install-tagsv187preview2/bin/ruby
versions/install-tags
v187preview3/bin/ruby
versions/install-tagsv187preview4/bin/ruby
versions/install-tags
v1900/bin/ruby
versions/install-tagsv1901/bin/ruby
versions/install-tags
v1902/bin/ruby
versions/install-tagsv1903/bin/ruby
versions/install-tags
v1904/bin/ruby
versions/install-tagsv1905/bin/ruby
versions/install-tags
v1910/bin/ruby
versions/install-tagsv191126/bin/ruby
versions/install-tags
v191127/bin/ruby
versions/install-tagsv191128/bin/ruby
versions/install-tags
v191129/bin/ruby
versions/install-tagsv191243/bin/ruby
versions/install-tags
v191376/bin/ruby
versions/install-tagsv191378/bin/ruby
versions/install-tags
v191429/bin/ruby
versions/install-tagsv191430/bin/ruby
versions/install-tags
v191431/bin/ruby
versions/install-tagsv191preview1/bin/ruby
versions/install-tags
v191preview2/bin/ruby
versions/install-tagsv191rc1/bin/ruby
versions/install-tags
v191rc2/bin/ruby
versions/install-tagsv1920/bin/ruby
versions/install-tags
v192136/bin/ruby
versions/install-tagsv192180/bin/ruby
versions/install-tags
v192290/bin/ruby
versions/install-tagsv192318/bin/ruby
versions/install-tags
v192320/bin/ruby
versions/install-tagsv192preview1/bin/ruby
versions/install-tags
v192preview2/bin/ruby
versions/install-tagsv192preview3/bin/ruby
versions/install-tags
v192rc1/bin/ruby
versions/install-tagsv192rc2/bin/ruby
versions/install-tags
v1930/bin/ruby
versions/install-tagsv193125/bin/ruby
versions/install-tags
v193194/bin/ruby
versions/install-tagsv193195/bin/ruby
versions/install-tags
v193286/bin/ruby
versions/install-tagsv193327/bin/ruby
versions/install-tags
v193360/bin/ruby
versions/install-tagsv193362/bin/ruby
versions/install-tags
v193374/bin/ruby
versions/install-tagsv193385/bin/ruby
versions/install-tags
v193392/bin/ruby
versions/install-tagsv193429/bin/ruby
versions/install-tags
v193448/bin/ruby
versions/install-tagsv193preview1/bin/ruby
versions/install-tags
v193rc1/bin/ruby
versions/install-tagsv2000/bin/ruby
versions/install-tags
v200195/bin/ruby
versions/install-tagsv200247/bin/ruby
versions/install-tags
v200preview1/bin/ruby
versions/install-tagsv200preview2/bin/ruby
versions/install-tags
v200rc1/bin/ruby
versions/install-tagsv200rc2_/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?

#11 Updated by Koichi Sasada 7 months 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

#12 Updated by Akira Tanaka 7 months ago

2013/9/27 ko1 (Koichi Sasada) redmine@ruby-lang.org:

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

#13 Updated by Tomoyuki Chikanaga 7 months ago

  • Tracker changed from Backport to Feature
  • Project changed from Backport200 to ruby-trunk
  • Assignee changed from Tomoyuki Chikanaga to Charlie Somerville

#14 Updated by Koichi Sasada 7 months ago

  • Category set to core
  • Assignee changed from Charlie Somerville to Yukihiro Matsumoto
  • Target version set to 2.1.0

Could you check it?

#15 Updated by Hiroshi SHIBATA 3 months ago

  • Target version changed from 2.1.0 to current: 2.2.0

Also available in: Atom PDF