[ruby-core:88327] [Ruby trunk Bug#14974] "if" statement executes wrong branch

From: tobias.kraze@...
Date: 2018-08-07 18:32:13 UTC
List: ruby-core #88327
Issue #14974 has been reported by kratob (Tobias Kraze).

----------------------------------------
Bug #14974: "if" statement executes wrong branch
https://bugs.ruby-lang.org/issues/14974

* Author: kratob (Tobias Kraze)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
* Backport: 2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
I've stumbled over a rather weird bug where Ruby 2.5+ executes an `else` branch of a condition when it should not. The best minimal example I could come up with is this:

~~~
def broken
  foo = 1
  if is_true or is_false
    foo = foo
  else
    raise "FAIL"
  end
  puts "PASS"
end

def is_true
  true
end

def is_false
  false
end

broken()
~~~

For me this script raises "FAIL" instead of printing "PASS".

Note that this is actually pretty much minimal; any of the following will "fix" the issue:
* inlining `is_true` or `is_false`
* removing the `foo = foo` or adding any other code to the `if` branch
* replacing `raise "FAIL"` with  `puts "FAIL"`
* removing the `puts "PASS"`



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

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

In This Thread

Prev Next