Skip to content

Commit

Permalink
yield cannot be placed outside methods even in blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Oct 2, 2023
1 parent 63e504d commit 9059dfc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion parse.y
Expand Up @@ -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");
}
;
Expand Down
8 changes: 7 additions & 1 deletion test/prism/parse_test.rb
Expand Up @@ -89,11 +89,17 @@ def test_parse_lex_file
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.
Expand Down
2 changes: 2 additions & 0 deletions test/ruby/test_ast.rb
Expand Up @@ -312,11 +312,13 @@ def test_invalid_yield
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")
Expand Down

0 comments on commit 9059dfc

Please sign in to comment.