💬
[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