yield cannot be placed outside methods even in blocks

This commit is contained in:
Nobuyoshi Nakada 2023-10-02 22:06:32 +09:00
parent 63e504d6e6
commit 9059dfce12
No known key found for this signature in database
GPG Key ID: 3582D74E1FEE4465
3 changed files with 10 additions and 2 deletions

View File

@ -4349,7 +4349,7 @@ k_return : keyword_return
k_yield : keyword_yield
{
if (!p->ctxt.in_defined && !p->ctxt.in_def && !dyna_in_block(p))
if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
yyerror1(&@1, "Invalid yield");
}
;

View File

@ -89,11 +89,17 @@ module Prism
src = source
case relative
when /break|next|redo|if|unless|rescue|control|keywords|retry|yield|\/args_assocs/
when /break|next|redo|if|unless|rescue|control|keywords|retry/
# Uncaught syntax errors: Invalid break, Invalid next
src = "->do\nrescue\n#{src}\nend"
ripper_should_match = false
end
case src
when /^ *yield/
# Uncaught syntax errors: Invalid yield
src = "def __invalid_yield__\n#{src}\nend"
ripper_should_match = false
end
# Make sure that it can be correctly parsed by Ripper. If it can't, then we have a fixture
# that is invalid Ruby.

View File

@ -312,11 +312,13 @@ class TestAst < Test::Unit::TestCase
assert_invalid_parse(msg, "class C; yield; end")
assert_invalid_parse(msg, "BEGIN {yield}")
assert_invalid_parse(msg, "END {yield}")
assert_invalid_parse(msg, "-> {yield}")
assert_invalid_parse(msg, "yield true")
assert_invalid_parse(msg, "class C; yield true; end")
assert_invalid_parse(msg, "BEGIN {yield true}")
assert_invalid_parse(msg, "END {yield true}")
assert_invalid_parse(msg, "-> {yield true}")
assert_parse("!defined?(yield)")
assert_parse("class C; defined?(yield); end")