diff options
| author | Benoit Daloze <eregontp@gmail.com> | 2023-08-30 22:55:43 +0200 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2023-09-01 13:18:30 +0000 |
| commit | 7f6407c356789db9039a0e45fbb8792236601956 (patch) | |
| tree | d5962ccca5dee320a773f0f3f28faa3a81912d46 | |
| parent | a21b5a943fe20892168eb76ecd1723191650da30 (diff) | |
[ruby/yarp] Ensure node are present only once in the desugared AST
https://github.com/ruby/yarp/commit/7b090bc23d
| -rw-r--r-- | test/yarp/desugar_visitor_test.rb | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/test/yarp/desugar_visitor_test.rb b/test/yarp/desugar_visitor_test.rb index 1ef2710c8e..3966d7bfcb 100644 --- a/test/yarp/desugar_visitor_test.rb +++ b/test/yarp/desugar_visitor_test.rb @@ -51,14 +51,36 @@ module YARP "(#{parts.join(" ")})" end + # Ensure every node is only present once in the AST. + # If the same node is present twice it would most likely indicate it is executed twice, which is invalid semantically. + # This also acts as a sanity check that Node#child_nodes returns only nodes or nil (which caught a couple bugs). + class EnsureEveryNodeOnceInAST < Visitor + def initialize + @all_nodes = {}.compare_by_identity + end + + def visit(node) + if node + if @all_nodes.include?(node) + raise "#{node.inspect} is present multiple times in the desugared AST and likely executed multiple times" + else + @all_nodes[node] = true + end + end + super(node) + end + end + def assert_desugars(expected, source) ast = YARP.parse(source).value.accept(DesugarVisitor.new) assert_equal expected, ast_inspect(ast.statements.body.last) + + ast.accept(EnsureEveryNodeOnceInAST.new) end def assert_not_desugared(source, reason) ast = YARP.parse(source).value - assert_equal_nodes(ast, ast.accept(YARP::DesugarVisitor.new)) + assert_equal_nodes(ast, ast.accept(DesugarVisitor.new)) end end end |
