💬

[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 で差異が発生する
  • なので次のようなコードは Prismparse.y で実行結果が異なる
if true && not true
end
# Prism   => no error
# parse.y => syntax error, unexpected 'true', expecting '(' (SyntaxError)
  • matz はコメントで『 not a && bnot (a && b)(not a) && b が曖昧になる』という理由で Prism の挙動は反対みたいですね
  • ただ、このチケットの話って右辺に not がある場合で左辺に not がある場合は Prismparse.y も有効な構文になるんですよね
p((not true && false))
# Prism   => no error => true
# parse.y => no error => true
  • not a && b に関しては演算子の優先順位が not よりも && のほうが高いので not a && bnot (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)
GitHubで編集を提案

Discussion