2022/07/29 今回の気になった bugs.ruby のチケット
今週は CRuby のインデントが全てスペースに展開されるチケットがありました。
[Bug #18883] parse.y: trailing comma cannot coexist with star
- 末尾に
,
がある時に*
付きの多重代入がうまく動作しないというバグ報告
x, y = 1, 2 # OK => x = 1, y = 2 x, y, = 1, 2 # OK => x = 1, y = 2 *x, y = 1, 2 # OK => x = [1], y = 2 *x, y, = 1, 2 # syntax error, unexpected '='
x, = [1, 2, 3]
はx, * = [1, 2, 3]
と同等になっている- したがって
*x, y, = 1, 2
は*x, y, * = 1, 2
と同等となる - Ruby では
*
が2つある場合は代入することができないので報告されているエラー自体は期待する挙動っぽい?
*x, y, * = 1, 2 # => syntax error, unexpected *
- また、以下のコードは全て同じ VM 命令になるらしい
- なので末尾の
,
で違いが発生すべきではないとのこと - https://bugs.ruby-lang.org/issues/18883#note-2
- なので末尾の
x, a = [1, 2, 3] x, a, = [1, 2, 3] x, a, * = [1, 2, 3]
bin = RubyVM::InstructionSequence.compile("x, a = [1, 2, 3]") puts bin.disasm # => == disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(1,16)> (catch: FALSE) # local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1]) # [ 2] x@0 [ 1] a@1 # 0000 duparray [1, 2, 3] ( 1)[Li] # 0002 dup # 0003 expandarray 2, 0 # 0006 setlocal_WC_0 x@0 # 0008 setlocal_WC_0 a@1 # 0010 leave bin = RubyVM::InstructionSequence.compile("x, a, = [1, 2, 3]") puts bin.disasm # => == disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(1,16)> (catch: FALSE) # local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1]) # [ 2] x@0 [ 1] a@1 # 0000 duparray [1, 2, 3] ( 1)[Li] # 0002 dup # 0003 expandarray 2, 0 # 0006 setlocal_WC_0 x@0 # 0008 setlocal_WC_0 a@1 # 0010 leave bin = RubyVM::InstructionSequence.compile("x, a, * = [1, 2, 3]") puts bin.disasm # => == disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(1,16)> (catch: FALSE) # local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1]) # [ 2] x@0 [ 1] a@1 # 0000 duparray [1, 2, 3] ( 1)[Li] # 0002 dup # 0003 expandarray 2, 0 # 0006 setlocal_WC_0 x@0 # 0008 setlocal_WC_0 a@1 # 0010 leave
[Misc #18891] Expand tabs in C code
- 既存の Ruby の実装の C のコードのインデントを全てスペースに統一するというチケット
- インデント幅は 4 で幅が8であればタブ文字、それ以外はスペースというスタイルになっていた
- 新しいコードは基本的にスペースを使うようにはなっていたが古いコードではタブ文字が依然として残っている状態になっていた
- VSCode だと古いスタイルでコードを書くことが難しいというのが起因ぽいですね
- また今回の変更を
git blame
で無視されるように.git-blame-ignore-revs
に今回のコミットを追加しているぽいですね- https://github.com/ruby/ruby/blob/708d06f301df9a703fbee6bd1ae369c5e11de7ad/.git-blame-ignore-revs#L6-L7
--ignore-revs-file
オプションでgit blame
で無視できるようなるらしい- 参照: https://tech.hey.jp/entry/2020/10/23/111200
- これ、長年の問題だったので解消されてめでたい
[Bug #18931] Inconsistent handling of invalid codepoints in String#lstrip and String#rstrip
- 無効なコードポイントが文字列に含まれている場合の
String#lstrip
とString#rstrip
で一貫性がないというバグ報告 String#lstrip
だと以下のような挙動String#lstrip
は先頭の空白を取り除くメソッド
# error: `lstrip': invalid byte sequence in UTF-8 (ArgumentError) p " \x80abc".lstrip
# error: `lstrip': invalid byte sequence in UTF-8 (ArgumentError) p " \x80 abc".lstrip
# error: `lstrip': invalid byte sequence in UTF-8 (ArgumentError) p "\x80".lstrip
# ok p " abc \x80".lstrip # => "abc \x80"
String#rstrip
だと以下のような挙動String#rstrip
は末尾の空白を取り除くメソッド
# ok p "abc\x80 ".rstrip # => "abc\x80"
# ok p "abc\x80".rstrip # => "abc\x80"
- また
String#rstrip
の場合は更に意図しない挙動になっているぽい?
# ok: \x80 が消える p "abc \x80".rstrip # => "abc"
# ok: \x80 が消える p "abc \x80 ".rstrip # => "abc"
# ok: \x80 が消える p " \x80 ".rstrip # => ""
\x80
だけの場合だとエラーになる
# error: `rstrip': invalid byte sequence in UTF-8 (ArgumentError) p "\x80 ".rstrip
# error: `rstrip': invalid byte sequence in UTF-8 (ArgumentError) p "\x80".rstrip
- なにもわからない