Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync from ruby/ruby #2958

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading