summaryrefslogtreecommitdiff
path: root/test/ruby
diff options
context:
space:
mode:
authorMatt Valentine-House <matt@eightbitraptor.com>2023-10-02 17:51:14 +0100
committerKevin Newton <kddnewton@gmail.com>2023-10-04 13:53:31 -0400
commit05b9b58d554042aef68462c727ae97325f986961 (patch)
tree250513b55184df11036e188e941c0a4d3c8c903b /test/ruby
parentf834b1a40db3ae70103ba78c64be2aa18bb9bc79 (diff)
[Prism] Fix IfNode and ElseNode
ElseNode looks to have been implemented at the same time as IfNode, but was resulting in a stack underflow error. The following is from the test code ``` if foo bar end ``` ``` ❯ make run compiling compile.c linking miniruby ./miniruby -I./lib -I. -I.ext/common -r./arm64-darwin22-fake ./test.rb CRUBY: ************************************************** == disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(2,19)> 0000 putself ( 2)[Li] 0001 opt_send_without_block <calldata!mid:foo, argc:0, FCALL|VCALL|ARGS_SIMPLE> 0003 branchunless 9 0005 putself 0006 opt_send_without_block <calldata!mid:bar, argc:0, FCALL|VCALL|ARGS_SIMPLE> 0008 pop 0009 putobject_INT2FIX_1_ 0010 leave PRISM: ************************************************** -- raw disasm-------- 0000 putself ( 2) 0001 send <calldata:foo, 0>, nil ( 2) 0004 branchunless <L001> ( 2) 0006 jump <L000> ( 2) <L000> [sp: 0] * 0008 pop ( 1) 0009 putself ( 2) 0010 send <calldata:bar, 0>, nil ( 2) 0013 pop ( 2) 0014 jump <L002> ( 1) <L001> [sp: 0] <L002> [sp: -1] 0016 putobject 1 ( 2) 0018 leave ( 1) --------------------- <compiled>: <compiled>:1: argument stack underflow (-1) (SyntaxError) make: *** [run] Error 1 ``` This commit fixes the stack underflow error for both IfNode and ElseNode and introduces tests for them.
Diffstat (limited to 'test/ruby')
-rw-r--r--test/ruby/test_compile_prism.rb12
1 files changed, 12 insertions, 0 deletions
diff --git a/test/ruby/test_compile_prism.rb b/test/ruby/test_compile_prism.rb
index 9c2f232396..f0e389d30a 100644
--- a/test/ruby/test_compile_prism.rb
+++ b/test/ruby/test_compile_prism.rb
@@ -338,6 +338,18 @@ module Prism
test_prism_eval("false || 1")
end
+ def test_IfNode
+ test_prism_eval("if true; 1; end")
+ test_prism_eval("1 if true")
+ end
+
+ def test_ElseNode
+ test_prism_eval("if false; 0; else; 1; end")
+ test_prism_eval("if true; 0; else; 1; end")
+ test_prism_eval("true ? 1 : 0")
+ test_prism_eval("false ? 0 : 1")
+ end
+
############################################################################
# Calls / arugments #
############################################################################