From 3c0756752c73ca08f366c31e197097b557da23d7 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Fri, 12 Apr 2024 14:11:30 -0400 Subject: [PATCH] [ruby/prism] Better error message on statement inside argument list https://github.com/ruby/prism/commit/3b1a99526a --- prism/prism.c | 9 +++++++-- prism/templates/src/diagnostic.c.erb | 2 +- prism/templates/src/token_type.c.erb | 2 +- test/prism/errors_test.rb | 2 +- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/prism/prism.c b/prism/prism.c index 640adb2e2c..91d27f566d 100644 --- a/prism/prism.c +++ b/prism/prism.c @@ -14335,9 +14335,14 @@ parse_arguments_list(pm_parser_t *parser, pm_arguments_t *arguments, bool accept } else { pm_accepts_block_stack_push(parser, true); parse_arguments(parser, arguments, true, PM_TOKEN_PARENTHESIS_RIGHT); - expect1(parser, PM_TOKEN_PARENTHESIS_RIGHT, PM_ERR_ARGUMENT_TERM_PAREN); - pm_accepts_block_stack_pop(parser); + if (!accept1(parser, PM_TOKEN_PARENTHESIS_RIGHT)) { + PM_PARSER_ERR_TOKEN_FORMAT(parser, parser->current, PM_ERR_ARGUMENT_TERM_PAREN, pm_token_type_human(parser->current.type)); + parser->previous.start = parser->previous.end; + parser->previous.type = PM_TOKEN_MISSING; + } + + pm_accepts_block_stack_pop(parser); arguments->closing_loc = PM_LOCATION_TOKEN_VALUE(&parser->previous); } } else if (accepts_command_call && (token_begins_expression_p(parser->current.type) || match3(parser, PM_TOKEN_USTAR, PM_TOKEN_USTAR_STAR, PM_TOKEN_UAMPERSAND)) && !match1(parser, PM_TOKEN_BRACE_LEFT)) { diff --git a/prism/templates/src/diagnostic.c.erb b/prism/templates/src/diagnostic.c.erb index f56f523ce6..97ad451890 100644 --- a/prism/templates/src/diagnostic.c.erb +++ b/prism/templates/src/diagnostic.c.erb @@ -105,7 +105,7 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = { [PM_ERR_ARGUMENT_NO_FORWARDING_STAR_STAR] = { "unexpected `**`; no anonymous keyword rest parameter", PM_ERROR_LEVEL_SYNTAX }, [PM_ERR_ARGUMENT_SPLAT_AFTER_ASSOC_SPLAT] = { "unexpected `*` splat argument after a `**` keyword splat argument", PM_ERROR_LEVEL_SYNTAX }, [PM_ERR_ARGUMENT_SPLAT_AFTER_SPLAT] = { "unexpected `*` splat argument after a `*` splat argument", PM_ERROR_LEVEL_SYNTAX }, - [PM_ERR_ARGUMENT_TERM_PAREN] = { "expected a `)` to close the arguments", PM_ERROR_LEVEL_SYNTAX }, + [PM_ERR_ARGUMENT_TERM_PAREN] = { "unexpected %s; expected a `)` to close the arguments", PM_ERROR_LEVEL_SYNTAX }, [PM_ERR_ARGUMENT_UNEXPECTED_BLOCK] = { "unexpected `{` after a method call without parenthesis", PM_ERROR_LEVEL_SYNTAX }, [PM_ERR_ARRAY_ELEMENT] = { "expected an element for the array", PM_ERROR_LEVEL_SYNTAX }, [PM_ERR_ARRAY_EXPRESSION] = { "expected an expression for the array element", PM_ERROR_LEVEL_SYNTAX }, diff --git a/prism/templates/src/token_type.c.erb b/prism/templates/src/token_type.c.erb index 5ea9199062..1aeecd72b2 100644 --- a/prism/templates/src/token_type.c.erb +++ b/prism/templates/src/token_type.c.erb @@ -208,7 +208,7 @@ pm_token_type_human(pm_token_type_t token_type) { case PM_TOKEN_KEYWORD_RESCUE: return "'rescue'"; case PM_TOKEN_KEYWORD_RESCUE_MODIFIER: - return "'rescue'"; + return "'rescue' modifier"; case PM_TOKEN_KEYWORD_RETRY: return "'retry'"; case PM_TOKEN_KEYWORD_RETURN: diff --git a/test/prism/errors_test.rb b/test/prism/errors_test.rb index d74c5d54a1..679f6ed0a2 100644 --- a/test/prism/errors_test.rb +++ b/test/prism/errors_test.rb @@ -411,7 +411,7 @@ module Prism def test_arguments_binding_power_for_and assert_error_messages "foo(*bar and baz)", [ - "expected a `)` to close the arguments", + "unexpected 'and'; expected a `)` to close the arguments", "unexpected ')', expecting end-of-input", "unexpected ')', ignoring it" ]