Skip to content

Commit

Permalink
merge revision(s) 9c94db7,fe4d906f5fbacbe6e9267af3bd3503339bad63a9: […
Browse files Browse the repository at this point in the history
…Backport #19774]

	Add tests for `return` in `BEGIN` and `END` blocks

	---
	 spec/ruby/language/return_spec.rb | 15 +++++++++++++++
	 test/ruby/test_syntax.rb          |  1 +
	 2 files changed, 16 insertions(+)

	[Bug #19774] Fix segfault at `return` in `END`

	---
	 eval_error.c             | 7 ++++++-
	 test/ruby/test_syntax.rb | 5 +++++
	 2 files changed, 11 insertions(+), 1 deletion(-)
  • Loading branch information
nagachika committed Jul 22, 2023
1 parent 65d294a commit b97a744
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
7 changes: 6 additions & 1 deletion eval_error.c
Expand Up @@ -455,7 +455,12 @@ exiting_split(VALUE errinfo, volatile int *exitcode, volatile int *sigstatus)

if (NIL_P(errinfo)) return 0;

if (rb_obj_is_kind_of(errinfo, rb_eSystemExit)) {
if (THROW_DATA_P(errinfo)) {
int throw_state = ((const struct vm_throw_data *)errinfo)->throw_state;
ex = throw_state & VM_THROW_STATE_MASK;
result |= EXITING_WITH_STATUS;
}
else if (rb_obj_is_kind_of(errinfo, rb_eSystemExit)) {
ex = sysexit_status(errinfo);
result |= EXITING_WITH_STATUS;
}
Expand Down
15 changes: 15 additions & 0 deletions spec/ruby/language/return_spec.rb
Expand Up @@ -435,6 +435,21 @@ class ReturnSpecs::A
end
end

describe "within BEGIN" do
it "is allowed" do
File.write(@filename, <<-END_OF_CODE)
BEGIN {
ScratchPad << "before call"
return
ScratchPad << "after call"
}
END_OF_CODE

load @filename
ScratchPad.recorded.should == ["before call"]
end
end

describe "file loading" do
it "stops file loading and execution" do
File.write(@filename, <<-END_OF_CODE)
Expand Down
6 changes: 6 additions & 0 deletions test/ruby/test_syntax.rb
Expand Up @@ -1369,6 +1369,8 @@ def test_return_toplevel
begin raise; ensure return; end and self
nil&defined?0--begin e=no_method_error(); return; 0;end
return puts('ignored') #=> ignored
BEGIN {return}
END {return if false}
end;
.split(/\n/).map {|s|[(line+=1), *s.split(/#=> /, 2)]}
failed = proc do |n, s|
Expand Down Expand Up @@ -1406,6 +1408,10 @@ def test_return_in_proc_in_class
assert_in_out_err(['-e', 'class TestSyntax; proc{ return }.call; end'], "", [], /^-e:1:.*unexpected return \(LocalJumpError\)/)
end

def test_return_in_END
assert_normal_exit('END {return}')
end

def test_syntax_error_in_rescue
bug12613 = '[ruby-core:76531] [Bug #12613]'
assert_syntax_error("#{<<-"begin;"}\n#{<<-"end;"}", /Invalid retry/, bug12613)
Expand Down
2 changes: 1 addition & 1 deletion version.h
Expand Up @@ -11,7 +11,7 @@
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
#define RUBY_VERSION_TEENY 2
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
#define RUBY_PATCHLEVEL 102
#define RUBY_PATCHLEVEL 103

#include "ruby/version.h"
#include "ruby/internal/abi.h"
Expand Down

0 comments on commit b97a744

Please sign in to comment.