Project

General

Profile

Actions

Bug #14897

closed

Unexpected behavior of `if` in specific code

Added by peitetsu (teppei takeo) over 6 years ago. Updated about 6 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin17]
[ruby-core:87830]

Description

I found a strange behavior of if in the following code.

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.

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

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

Related issues 4 (0 open4 closed)

Related to Ruby master - Bug #14974: "if" statement executes wrong branchClosedActions
Related to Ruby master - Bug #14959: Writing a "link_to" method and a "url_helper" with a request parameter under certain "if else" statement in Rails helper crashes with KERN_INVALID_ADDRESS at 0x0000000000000000ClosedActions
Related to Ruby master - Bug #15385: Ruby process hang in ensureClosedActions
Has duplicate Ruby master - Bug #15021: Segfault when compiling certain code on Ruby 2.5.1ClosedActions

Updated by shyouhei (Shyouhei Urabe) over 6 years ago

  • 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]
Actions #2

Updated by mame (Yusuke Endoh) over 6 years ago

  • Status changed from Open to Closed

Applied in changeset trunk|r63868.


Fix a bug of peephole optimization

  if   L1
L0:
  jump L2
L1:
  ...
L2:

was wrongly optimized to:

  unless L2
L0:
L1:
  ...
L2:

To make it conservative, this optimization is now disabled when there is
any label between if and jump instructions.
Fixes [Bug #14897].

Updated by mame (Yusuke Endoh) over 6 years ago

Good catch. It was a bug of peephole optimization. Fixed. Thank you.

Actions #4

Updated by nobu (Nobuyoshi Nakada) over 6 years ago

  • Has duplicate Bug #14959: Writing a "link_to" method and a "url_helper" with a request parameter under certain "if else" statement in Rails helper crashes with KERN_INVALID_ADDRESS at 0x0000000000000000 added
Actions #5

Updated by znz (Kazuhiro NISHIYAMA) over 6 years ago

  • Related to Bug #14974: "if" statement executes wrong branch added

Updated by nagachika (Tomoyuki Chikanaga) about 6 years ago

  • Backport changed from 2.3: DONTNEED, 2.4: DONTNEED, 2.5: REQUIRED to 2.3: DONTNEED, 2.4: DONTNEED, 2.5: DONE

ruby_2_5 r64893 merged revision(s) 63868,63870.

Actions #7

Updated by nobu (Nobuyoshi Nakada) about 6 years ago

  • Has duplicate Bug #15200: RubyVM::InstructionSequence can not compile to binary from method call with hash in 'if false' expression added
Actions #8

Updated by nobu (Nobuyoshi Nakada) about 6 years ago

  • Has duplicate deleted (Bug #15200: RubyVM::InstructionSequence can not compile to binary from method call with hash in 'if false' expression)
Actions #9

Updated by nobu (Nobuyoshi Nakada) almost 6 years ago

  • Has duplicate deleted (Bug #14959: Writing a "link_to" method and a "url_helper" with a request parameter under certain "if else" statement in Rails helper crashes with KERN_INVALID_ADDRESS at 0x0000000000000000)
Actions #10

Updated by nobu (Nobuyoshi Nakada) almost 6 years ago

  • Related to Bug #14959: Writing a "link_to" method and a "url_helper" with a request parameter under certain "if else" statement in Rails helper crashes with KERN_INVALID_ADDRESS at 0x0000000000000000 added
Actions #11

Updated by shyouhei (Shyouhei Urabe) almost 6 years ago

  • Related to Bug #15385: Ruby process hang in ensure added
Actions #12

Updated by jeremyevans0 (Jeremy Evans) over 5 years ago

  • Has duplicate Bug #15021: Segfault when compiling certain code on Ruby 2.5.1 added
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0