From: "mame (Yusuke Endoh)" Date: 2021-12-25T04:37:58+00:00 Subject: [ruby-core:106816] [Ruby master Bug#18432] case ... when bug if add a extra comma on the end of condition. Issue #18432 has been updated by mame (Yusuke Endoh). ``` case type when 'aaa', x = 100 p x end ``` is parsed as ``` case type when 'aaa', (x = 100) p x end ``` which is (almost) equivalent to ``` if 'aaa' === type || ((x = 100) === type) p x end ``` . If `'aaa' === type` evaluates to true, `(x = 100)` is not executed. Thus, `x` is uninitialized, so it is nil by default. ---------------------------------------- Bug #18432: case ... when bug if add a extra comma on the end of condition. https://bugs.ruby-lang.org/issues/18432#change-95627 * Author: zw963 (Wei Zheng) * Status: Open * Priority: Normal * Backport: 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN ---------------------------------------- I will describe how this bug happen for me. 1) I have code like this at first, it works! ```rb type = 'aaa' case type when 'aaa' x = 100 p x when 'bbb' x = 100 p x end # => 100 ``` 2) Because the code in when case 'aaa' or 'bbb', all code same, so, i do some refactor, it still work, like this: ```rb type = 'aaa' case type when 'aaa', 'bbb' x = 100 p x end # => 100 ``` 3) Then, logic is changes, i need split 'aaa' and 'bbb' back to original form, somethings is wrong! ```rb 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. 4) In fact, it even no syntax error raised when i remove the second when. ```rb 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: #@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 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 ``` -- https://bugs.ruby-lang.org/ Unsubscribe: