Issue #14897 has been updated by shyouhei (Shyouhei Urabe).

Backport changed from 2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN to 2.3: DONTNEED, 2.4: DONTNEED, 2.5: REQUIRED

Seems `obj = obj` is (mistakenly) optimized out.

```
% ./miniruby --dump=i -v tmp.rb
ruby 2.6.0dev (2018-07-06 trunk 63854) [x86_64-darwin15]
== disasm: #<ISeq:<main>@tmp.rb:1 (1,0)-(10,16)> (catch: FALSE)
0000 putspecialobject             1                                   (   1)[Li]
0002 putobject                    :seems_bug
0004 putiseq                      seems_bug
0006 opt_send_without_block       <callinfo!mid:core#define_method, argc:2, ARGS_SIMPLE>, <callcache>
0009 pop
0010 putself                                                          (  10)[Li]
0011 putstring                    "foo"
0013 opt_send_without_block       <callinfo!mid:seems_bug, argc:1, FCALL|ARGS_SIMPLE>, <callcache>
0016 leave

== disasm: #<ISeq:seems_bug / tmp.rb:1 (1,0)-(8,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] obj@0<Arg>
0000 getlocal_WC_0                obj@0                               (   2)[LiCa]
0002 branchif                     8
0004 getlocal_WC_0                obj@0
0006 branchif                     18
0008 putself                                                          (   5)[Li]
0009 getlocal_WC_0                obj@0
0011 opt_send_without_block       <callinfo!mid:inspect, argc:0, ARGS_SIMPLE>, <callcache>
0014 opt_send_without_block       <callinfo!mid:raise, argc:1, FCALL|ARGS_SIMPLE>, <callcache>
0017 pop
0018 getlocal_WC_0                obj@0                               (   7)[Li]
0020 leave                                                            (   8)[Re]
```

----------------------------------------
Bug #14897: Unexpected behavior of `if` in specific code
https://bugs.ruby-lang.org/issues/14897#change-72848

* Author: peitetsu (teppei takeo)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin17]
* Backport: 2.3: DONTNEED, 2.4: DONTNEED, 2.5: REQUIRED
----------------------------------------
I found a strange behavior of `if` in the following code.

~~~ ruby
def seems_bug(obj)
  if obj || obj
    obj = obj
  else
    raise obj.inspect
  end
  obj
end

seems_bug('foo')
#=> RuntimeError: "foo"
~~~

This code is expected to return "foo", but the error on the else clause occurs. 

The same error occurs in the following code.

~~~ ruby
def seems_bug(obj)
  if obj || any1
    any2 = any2
  else
    raise obj.inspect
  end
  obj
end

seems_bug('foo')
#=> RuntimeError: "foo"
~~~





-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>