From 1058922b7bcc0d1719ce937ee6855819ded55be8 Mon Sep 17 00:00:00 2001 From: Sean Garwood Date: Sat, 21 Nov 2026 00:41:59 -0500 Subject: [PATCH 1/3] Report a non-constant class name only once When the name token after `class` is not a constant, parse_class reported the class_name error for the token and again for the constant path node. Only report the node error when the token check did not, matching parse_module. --- src/prism.c | 3 ++- test/prism/errors/class_name_lowercase.txt | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 test/prism/errors/class_name_lowercase.txt diff --git a/src/prism.c b/src/prism.c index dcbd52abe5..c66ed4a479 100644 --- a/src/prism.c +++ b/src/prism.c @@ -18654,7 +18654,8 @@ parse_class(pm_parser_t *parser, uint8_t flags, uint16_t depth) { pm_parser_scope_pop(parser); if (!PM_NODE_TYPE_P(constant_path, PM_CONSTANT_PATH_NODE) && !(PM_NODE_TYPE_P(constant_path, PM_CONSTANT_READ_NODE))) { - pm_parser_err_node(parser, constant_path, PM_ERR_CLASS_NAME); + // The name token check above already reported a non-constant name. + if (name.type == PM_TOKEN_CONSTANT) pm_parser_err_node(parser, constant_path, PM_ERR_CLASS_NAME); if (!PM_NODE_TYPE_P(constant_path, PM_ERROR_RECOVERY_NODE)) { constant_path = UP(pm_error_recovery_node_create_unexpected(parser, constant_path)); } diff --git a/test/prism/errors/class_name_lowercase.txt b/test/prism/errors/class_name_lowercase.txt new file mode 100644 index 0000000000..b5b2d5839f --- /dev/null +++ b/test/prism/errors/class_name_lowercase.txt @@ -0,0 +1,3 @@ +class a; end + ^ unexpected constant path after `class`; class/module name must be CONSTANT + From b102fc66f3684209bd904cbbb725b833b02f3b7b Mon Sep 17 00:00:00 2001 From: Sean Garwood Date: Sat, 21 Nov 2026 00:41:59 -0500 Subject: [PATCH 2/3] Mark non-constant class and module names as non-continuable A lowercase class or module name (`class a; end`) produced a class_name or module_name error that pm_parse_continuable did not treat as fatal, so the input stayed continuable forever. IRB would keep prompting for more input, even after `exit`. Treat these errors as non-continuable once anything other than whitespace follows the name. Input ending at the name stays continuable because more input can still complete it (`class a` + `::B`, or `class a\n` + `.b::C`). --- src/prism.c | 13 +++++++++++++ test/prism/result/continuable_test.rb | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/prism.c b/src/prism.c index c66ed4a479..2c254c02fd 100644 --- a/src/prism.c +++ b/src/prism.c @@ -23385,6 +23385,19 @@ pm_parse_continuable(pm_parser_t *parser) { return; } + // A non-constant class/module name is non-continuable once anything + // other than whitespace follows it. Otherwise more input could still + // complete it (`class a` + `::B`, or `class a\n` + `.b::C`). + if (error->diag_id == PM_ERR_CLASS_NAME || error->diag_id == PM_ERR_MODULE_NAME) { + const uint8_t *cursor = parser->start + error_end; + while (cursor < parser->end && pm_char_is_whitespace(*cursor)) cursor++; + + if (cursor < parser->end) { + parser->continuable = false; + return; + } + } + // Track non-stray, non-fatal error positions in list order. if (error->diag_id != PM_ERR_UNEXPECTED_TOKEN_IGNORE && error->diag_id != PM_ERR_UNEXPECTED_TOKEN_CLOSE_CONTEXT) { diff --git a/test/prism/result/continuable_test.rb b/test/prism/result/continuable_test.rb index 3533552167..251b9ff39d 100644 --- a/test/prism/result/continuable_test.rb +++ b/test/prism/result/continuable_test.rb @@ -4,6 +4,19 @@ module Prism class ContinuableTest < TestCase + def test_invalid_class_and_module_names + # Non-continuable once anything but whitespace follows a non-constant name. + refute_predicate Prism.parse("class a; end\n"), :continuable? + refute_predicate Prism.parse("module a; end\n"), :continuable? + refute_predicate Prism.parse("class Mine\n self << class\n self.class\n end\nend\n"), :continuable? + + # Followed only by whitespace (or EOF), the name may still continue: + # `class a::B` or `class a\n.b::C` are valid. + assert_predicate Prism.parse("class a"), :continuable? + assert_predicate Prism.parse("class a\n"), :continuable? + assert_predicate Prism.parse("module a"), :continuable? + end + def test_valid_input # Valid input is not continuable (nothing to continue). refute_predicate Prism.parse("1 + 1"), :continuable? From 08b14df4fc1dc311fe3b977898e1da1142f85455 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Thu, 3 Dec 2026 10:27:19 -0500 Subject: [PATCH 3/3] Extend PM_ERR_CLASS_NAME continuable checking --- src/prism.c | 21 +++++++++++++++------ test/prism/errors_test.rb | 5 +++++ test/prism/result/continuable_test.rb | 15 ++++++++++++++- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/prism.c b/src/prism.c index 2c254c02fd..5f35c8f641 100644 --- a/src/prism.c +++ b/src/prism.c @@ -18599,7 +18599,8 @@ parse_class(pm_parser_t *parser, uint8_t flags, uint16_t depth) { pm_node_t *constant_path = parse_expression(parser, PM_BINDING_POWER_INDEX, flags & PM_PARSE_ACCEPTS_DO_BLOCK, PM_ERR_CLASS_NAME, (uint16_t) (depth + 1)); pm_token_t name = parser->previous; - if (name.type != PM_TOKEN_CONSTANT) { + /* parse_expression already reported a missing name. */ + if (name.type != PM_TOKEN_CONSTANT && !PM_NODE_TYPE_P(constant_path, PM_ERROR_RECOVERY_NODE)) { pm_parser_err_token(parser, &name, PM_ERR_CLASS_NAME); } @@ -18654,7 +18655,7 @@ parse_class(pm_parser_t *parser, uint8_t flags, uint16_t depth) { pm_parser_scope_pop(parser); if (!PM_NODE_TYPE_P(constant_path, PM_CONSTANT_PATH_NODE) && !(PM_NODE_TYPE_P(constant_path, PM_CONSTANT_READ_NODE))) { - // The name token check above already reported a non-constant name. + /* The name token check above already reported a non-constant name. */ if (name.type == PM_TOKEN_CONSTANT) pm_parser_err_node(parser, constant_path, PM_ERR_CLASS_NAME); if (!PM_NODE_TYPE_P(constant_path, PM_ERROR_RECOVERY_NODE)) { constant_path = UP(pm_error_recovery_node_create_unexpected(parser, constant_path)); @@ -23385,12 +23386,20 @@ pm_parse_continuable(pm_parser_t *parser) { return; } - // A non-constant class/module name is non-continuable once anything - // other than whitespace follows it. Otherwise more input could still - // complete it (`class a` + `::B`, or `class a\n` + `.b::C`). if (error->diag_id == PM_ERR_CLASS_NAME || error->diag_id == PM_ERR_MODULE_NAME) { const uint8_t *cursor = parser->start + error_end; - while (cursor < parser->end && pm_char_is_whitespace(*cursor)) cursor++; + + /* A colon immediately after the name can still become ::. */ + if (parser->end - cursor == 1 && *cursor == ':') cursor++; + + /* A name can continue on the next line, even across line comments. */ + while (cursor < parser->end) { + cursor += pm_strspn_whitespace(cursor, parser->end - cursor); + if (cursor == parser->end || *cursor != '#') break; + + const uint8_t *newline = next_newline(cursor, parser->end - cursor); + cursor = newline == NULL ? parser->end : newline + 1; + } if (cursor < parser->end) { parser->continuable = false; diff --git a/test/prism/errors_test.rb b/test/prism/errors_test.rb index fd3c39e854..4f398c7bf3 100644 --- a/test/prism/errors_test.rb +++ b/test/prism/errors_test.rb @@ -71,6 +71,11 @@ def test_module_name_rejects_symbol_ending_in_constant_token assert_equal ":A", result.errors.first.location.slice end + def test_missing_class_name_is_reported_once + errors = Prism.parse("class # comment\n").errors.select { |error| error.type == :class_name } + assert_equal ["class"], errors.map { |error| error.location.slice } + end + def test_begin_with_rescue_and_ensure_rejects_void_values result = Prism.parse("def f; x = begin; return :body; rescue; return :rescue; ensure; end; end") assert_equal [:void_expression], result.errors.map(&:type) diff --git a/test/prism/result/continuable_test.rb b/test/prism/result/continuable_test.rb index 251b9ff39d..946b7b935e 100644 --- a/test/prism/result/continuable_test.rb +++ b/test/prism/result/continuable_test.rb @@ -5,7 +5,7 @@ module Prism class ContinuableTest < TestCase def test_invalid_class_and_module_names - # Non-continuable once anything but whitespace follows a non-constant name. + # The declaration has moved past the invalid name. refute_predicate Prism.parse("class a; end\n"), :continuable? refute_predicate Prism.parse("module a; end\n"), :continuable? refute_predicate Prism.parse("class Mine\n self << class\n self.class\n end\nend\n"), :continuable? @@ -17,6 +17,19 @@ def test_invalid_class_and_module_names assert_predicate Prism.parse("module a"), :continuable? end + def test_class_and_module_names_with_a_trailing_colon_or_comment + %w[class module].each do |keyword| + { "a:" => ":B; end", "a # comment\n" => ".b::C; end", "# comment\n" => "M; end" }.each do |name, suffix| + source = "#{keyword} #{name}" + assert_predicate Prism.parse(source), :continuable?, source + assert_predicate Prism.parse(source + suffix), :success?, source + suffix + end + + refute_predicate Prism.parse("#{keyword} a: "), :continuable? + refute_predicate Prism.parse("#{keyword} a # comment\nbody"), :continuable? + end + end + def test_valid_input # Valid input is not continuable (nothing to continue). refute_predicate Prism.parse("1 + 1"), :continuable?