🎃

[Bug #21014] prism で iseq から意図する node_id が取得できないバグ報告

2025/02/07に公開

[Bug #21014] Prism doesn't set node_id on iseqs correctly

  • prism で意図する node_id が返ってこないバグ報告
  • 以下のように RubyVM::InstructionSequence の情報から node_id を取得するときの話みたいですね
f = proc { <<END }
  heredoc
END

iseq = RubyVM::InstructionSequence.of(f)
p iseq.to_a[4][:node_id]
# prism   => -1
# parse.y => 3
  • prismparse.ynode_id が一致していることを期待しているわけではないんですが、次のような node_id を基準として情報を取得するコードは動くことを期待しているみたいですね
f = proc { <<END }
  heredoc
END

iseq = RubyVM::InstructionSequence.of(f)

require "prism"

# iseq の node_id を元にして Prism のパース結果を探す
node_id = iseq.to_a[4][:node_id]

ast = Prism.parse(File.binread(__FILE__))
p ast.value.breadth_first_search { |node| node.node_id == node_id }
# => nil
  • この不具合は開発版の Ruby 3.5-dev ですでに修正済みです
f = proc { <<END }
  heredoc
END

iseq = RubyVM::InstructionSequence.of(f)

require "prism"

# iseq の node_id を元にして Prism のパース結果を探す
node_id = iseq.to_a[4][:node_id]

ast = Prism.parse(File.binread(__FILE__))
p ast.value.breadth_first_search { |node| node.node_id == node_id }
# Ruby 3.4 => nil
# Ruby 3.5 =>
#   @ BlockNode (location: (1,9)-(1,18))
#   ├── flags: ∅
#   ├── locals: []
#   ├── parameters: ∅
#   ├── body:
#   ...
GitHubで編集を提案

Discussion