summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Zhu <peter@peterzhu.ca>2024-01-17 11:19:54 -0500
committerPeter Zhu <peter@peterzhu.ca>2024-01-17 13:17:44 -0500
commitf43a919be494cf5b0f98f104da1024efda1abba5 (patch)
treefbe9c77aeff9d38490e4d38cce8d5e164113f3c7
parentde9411c0b91010e3a30b86904c7470b724564341 (diff)
[PRISM] Fix fallthrough for PM_ENSURE_NODE
This caused it to fall into PM_ELSE_NODE which caused ensure nodes to be compiled twice. Fixes ruby/prism#2176.
-rw-r--r--prism_compile.c1
-rw-r--r--test/ruby/test_compile_prism.rb15
2 files changed, 16 insertions, 0 deletions
diff --git a/prism_compile.c b/prism_compile.c
index 472d25eb35..368cba7039 100644
--- a/prism_compile.c
+++ b/prism_compile.c
@@ -4236,6 +4236,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
PM_COMPILE((pm_node_t *)ensure_node->statements);
}
ADD_LABEL(ret, end);
+ return;
}
case PM_ELSE_NODE: {
pm_else_node_t *cast = (pm_else_node_t *)node;
diff --git a/test/ruby/test_compile_prism.rb b/test/ruby/test_compile_prism.rb
index c0f85af8b4..d547f82fe3 100644
--- a/test/ruby/test_compile_prism.rb
+++ b/test/ruby/test_compile_prism.rb
@@ -931,6 +931,21 @@ module Prism
end
a
CODE
+
+ # Test that ensure block only evaluated once
+ assert_prism_eval(<<~RUBY)
+ res = []
+ begin
+ begin
+ raise
+ ensure
+ res << $!.to_s
+ end
+ rescue
+ res
+ end
+ RUBY
+
assert_prism_eval(<<-CODE)
a = 1
begin