💬
[Bug #21337] 論理演算子の右辺で not を利用すると parse.y と Prism で差異があるバグ報告
[Bug #21337] Using not
on the RHS of a logical operator becomes valid syntax with Prism
- 論理演算子の右辺で
not
を利用するとparse.y
ではシンタックスエラーになるがPrism
で有効な構文になるというバグ報告- これにより
parse.y
がデフォルトの Ruby 3.3 とPrism
がデフォルトの Ruby 3.4 で差異が発生する
- これにより
- なので次のようなコードは
Prism
とparse.y
で実行結果が異なる
if true && not true
end
# Prism => no error
# parse.y => syntax error, unexpected 'true', expecting '(' (SyntaxError)
- matz はコメントで『
not a && b
がnot (a && b)
か(not a) && b
が曖昧になる』という理由でPrism
の挙動は反対みたいですね - ただ、このチケットの話って右辺に
not
がある場合で左辺にnot
がある場合はPrism
もparse.y
も有効な構文になるんですよね
p((not true && false))
# Prism => no error => true
# parse.y => no error => true
-
not a && b
に関しては演算子の優先順位がnot
よりも&&
のほうが高いのでnot a && b
はnot (a && b)
になることが正しそうですかね?- 実際の挙動もそうなっていそう
-
true && not true
の場合だと優先順位が決めれなさそうなので曖昧になりそうですかね…? - また次のようなコードも
Prism
ではエラーになることを期待しているみたいですね
p(not 1)
# Prism => no error
# parse.y => syntax error, unexpected integer literal, expecting '(' (SyntaxError)
- この問題は開発版の Ruby 3.5-dev では Prism でもシンタックスエラーになるように修正された
# Prism の場合
if true && not true
end
# Ruby 3.4 => no error
# Ruby 3.5 -> syntax errors found (SyntaxError)
Discussion