Project

General

Profile

Actions

Bug #18432

closed

case ... when bug if add a extra comma on the end of condition.

Added by zw963 (Wei Zheng) over 2 years ago. Updated over 2 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:106815]

Description

I will describe how this bug happen for me.

  1. I have code like this at first, it works!
type = 'aaa'

case type
when 'aaa'
  x = 100
  p x
when 'bbb'
  x = 100
  p x
end
# => 100
  1. Because the code in when case 'aaa' or 'bbb', all code same, so, i do some refactor, it still work, like this:
type = 'aaa'

case type
when 'aaa', 'bbb'
  x = 100
  p x
end
# => 100
  1. Then, logic is changes, i need split 'aaa' and 'bbb' back to original form, somethings is wrong!
type = 'aaa'

case type
when 'aaa',
  x = 100
  p x
when 'bbb'
  x = 200
  p x
end

# => nil

What i expected is raise syntax error, let me find this issue quickly, but, it just ignore silently.

  1. In fact, it even no syntax error raised when i remove the second when.
type = 'aaa'

case type
when 'aaa',
  x = 100
  p x
 'bbb'
  x = 200
  p x
# nil
# 200
end

Following is disam ouput for case 3

== disasm: #<ISeq:<main>@1.rb:1 (1,0)-(10,3)> (catch: FALSE)
local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 2] type@0     [ 1] x@1
0000 putstring                              "aaa"                     (   1)[Li]
0002 setlocal_WC_0                          type@0
0004 getlocal_WC_0                          type@0                    (   3)[Li]
0006 dup                                                              (   4)
0007 putobject                              "aaa"
0009 checkmatch                             2
0011 branchif                               33
0013 dup                                                              (   5)
0014 putobject                              100
0016 dup
0017 setlocal_WC_0                          x@1
0019 checkmatch                             2
0021 branchif                               33
0023 dup                                                              (   7)
0024 putobject                              "bbb"
0026 checkmatch                             2
0028 branchif                               40
0030 pop                                                              (   3)
0031 putnil
0032 leave                                                            (   9)
0033 pop                                                              (   4)
0034 putself                                                          (   6)[Li]
0035 getlocal_WC_0                          x@1
0037 opt_send_without_block                 <calldata!mid:p, argc:1, FCALL|ARGS_SIMPLE>
0039 leave                                                            (   9)
0040 pop                                                              (   7)
0041 putobject                              200                       (   8)[Li]
0043 setlocal_WC_0                          x@1
0045 putself                                                          (   9)[Li]
0046 getlocal_WC_0                          x@1
0048 opt_send_without_block                 <calldata!mid:p, argc:1, FCALL|ARGS_SIMPLE>
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0