diff --git a/prism/diagnostic.c b/prism/diagnostic.c index b8b92f97e0..7eafd8b088 100644 --- a/prism/diagnostic.c +++ b/prism/diagnostic.c @@ -92,6 +92,7 @@ static const char* const diagnostic_messages[PM_DIAGNOSTIC_ID_LEN] = { [PM_ERR_CLASS_NAME] = "Expected a constant name after `class`", [PM_ERR_CLASS_SUPERCLASS] = "Expected a superclass after `<`", [PM_ERR_CLASS_TERM] = "Expected an `end` to close the `class` statement", + [PM_ERR_CLASS_UNEXPECTED_END] = "Unexpected `end`, expecting ';' or '\n'", [PM_ERR_CONDITIONAL_ELSIF_PREDICATE] = "Expected a predicate expression for the `elsif` statement", [PM_ERR_CONDITIONAL_IF_PREDICATE] = "Expected a predicate expression for the `if` statement", [PM_ERR_CONDITIONAL_PREDICATE_TERM] = "Expected `then` or `;` or '\n'", diff --git a/prism/diagnostic.h b/prism/diagnostic.h index 33bf381d61..062ba07d42 100644 --- a/prism/diagnostic.h +++ b/prism/diagnostic.h @@ -57,6 +57,7 @@ typedef enum { PM_ERR_CLASS_NAME, PM_ERR_CLASS_SUPERCLASS, PM_ERR_CLASS_TERM, + PM_ERR_CLASS_UNEXPECTED_END, PM_ERR_CONDITIONAL_ELSIF_PREDICATE, PM_ERR_CONDITIONAL_IF_PREDICATE, PM_ERR_CONDITIONAL_PREDICATE_TERM, diff --git a/prism/prism.c b/prism/prism.c index f1438b8132..5fee40351c 100644 --- a/prism/prism.c +++ b/prism/prism.c @@ -12304,7 +12304,11 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power) { } pm_parser_scope_push(parser, true); - accept2(parser, PM_TOKEN_NEWLINE, PM_TOKEN_SEMICOLON); + if (inheritance_operator.type != PM_TOKEN_NOT_PROVIDED) { + expect2(parser, PM_TOKEN_NEWLINE, PM_TOKEN_SEMICOLON, PM_ERR_CLASS_UNEXPECTED_END); + } else { + accept2(parser, PM_TOKEN_NEWLINE, PM_TOKEN_SEMICOLON); + } pm_node_t *statements = NULL; if (!match3(parser, PM_TOKEN_KEYWORD_RESCUE, PM_TOKEN_KEYWORD_ENSURE, PM_TOKEN_KEYWORD_END)) { diff --git a/test/prism/errors_test.rb b/test/prism/errors_test.rb index 1975a17102..372e7791db 100644 --- a/test/prism/errors_test.rb +++ b/test/prism/errors_test.rb @@ -1355,6 +1355,13 @@ module Prism ] end + def test_semicolon_after_inheritance_operator + source = "class Foo < Bar end" + assert_errors expression(source), source, [ + ["Unexpected `end`, expecting ';' or '\n'", 15..15], + ] + end + private def assert_errors(expected, source, errors, compare_ripper: RUBY_ENGINE == "ruby") diff --git a/test/prism/location_test.rb b/test/prism/location_test.rb index b5c108e399..7242bdeca5 100644 --- a/test/prism/location_test.rb +++ b/test/prism/location_test.rb @@ -206,7 +206,7 @@ module Prism def test_ClassNode assert_location(ClassNode, "class Foo end") - assert_location(ClassNode, "class Foo < Bar end") + assert_location(ClassNode, "class Foo < Bar; end") end def test_ClassVariableAndWriteNode