Skip to content

Commit

Permalink
Sync from ruby/ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Jul 25, 2024
1 parent e012072 commit 280517c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
14 changes: 10 additions & 4 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -15342,6 +15342,7 @@ parse_arguments_list(pm_parser_t *parser, pm_arguments_t *arguments, bool accept
*/
static void
parse_return(pm_parser_t *parser, pm_node_t *node) {
bool in_sclass = false;
for (pm_context_node_t *context_node = parser->current_context; context_node != NULL; context_node = context_node->prev) {
switch (context_node->context) {
case PM_CONTEXT_BEGIN_ELSE:
Expand All @@ -15366,17 +15367,19 @@ parse_return(pm_parser_t *parser, pm_node_t *node) {
case PM_CONTEXT_PREDICATE:
case PM_CONTEXT_PREEXE:
case PM_CONTEXT_RESCUE_MODIFIER:
case PM_CONTEXT_SCLASS_ELSE:
case PM_CONTEXT_SCLASS_ENSURE:
case PM_CONTEXT_SCLASS_RESCUE:
case PM_CONTEXT_SCLASS:
case PM_CONTEXT_TERNARY:
case PM_CONTEXT_UNLESS:
case PM_CONTEXT_UNTIL:
case PM_CONTEXT_WHILE:
// Keep iterating up the lists of contexts, because returns can
// see through these.
continue;
case PM_CONTEXT_SCLASS_ELSE:
case PM_CONTEXT_SCLASS_ENSURE:
case PM_CONTEXT_SCLASS_RESCUE:
case PM_CONTEXT_SCLASS:
in_sclass = true;
continue;
case PM_CONTEXT_CLASS_ELSE:
case PM_CONTEXT_CLASS_ENSURE:
case PM_CONTEXT_CLASS_RESCUE:
Expand Down Expand Up @@ -15411,6 +15414,9 @@ parse_return(pm_parser_t *parser, pm_node_t *node) {
break;
}
}
if (in_sclass) {
pm_parser_err_node(parser, node, PM_ERR_RETURN_INVALID);
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions test/prism/errors/dont_allow_return_inside_sclass_body.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class << A; return; end
^~~~~~ Invalid return in class/module body

8 changes: 5 additions & 3 deletions test/prism/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ class ErrorsTest < TestCase
end

if RUBY_VERSION < "3.4"
filepaths -= [
"it_with_ordinary_parameter.txt"
]
filepaths -= ["it_with_ordinary_parameter.txt"]
end

if RUBY_VERSION < "3.4" || RUBY_RELEASE_DATE < "2024-07-24"
filepaths -= ["dont_allow_return_inside_sclass_body.txt"]
end

filepaths.each do |filepath|
Expand Down

0 comments on commit 280517c

Please sign in to comment.