From ad96c5a72908042489357b73dc936c4bc38d919b Mon Sep 17 00:00:00 2001 From: eileencodes Date: Tue, 7 Jan 2025 14:18:20 -0500 Subject: [PATCH] [ruby/prism] Throw syntax error for endless method with `[]=` Prism shoudld throw a syntax error for endless methods when the method name uses brackets. Previously it would not. This matches the behavior of parse.y. Fixes https://bugs.ruby-lang.org/issues/21010 https://github.com/ruby/prism/commit/43c16a89ef --- prism/prism.c | 5 +++-- ...hod_cannot_be_defined_in_an_endless_method_definition.txt | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/prism/prism.c b/prism/prism.c index 1264dbde52..db1e475f95 100644 --- a/prism/prism.c +++ b/prism/prism.c @@ -1740,9 +1740,10 @@ char_is_global_name_punctuation(const uint8_t b) { static inline bool token_is_setter_name(pm_token_t *token) { return ( - (token->type == PM_TOKEN_IDENTIFIER) && + (token->type == PM_TOKEN_BRACKET_LEFT_RIGHT_EQUAL) || + ((token->type == PM_TOKEN_IDENTIFIER) && (token->end - token->start >= 2) && - (token->end[-1] == '=') + (token->end[-1] == '=')) ); } diff --git a/test/prism/errors/setter_method_cannot_be_defined_in_an_endless_method_definition.txt b/test/prism/errors/setter_method_cannot_be_defined_in_an_endless_method_definition.txt index c4440ccc7e..7927664f3c 100644 --- a/test/prism/errors/setter_method_cannot_be_defined_in_an_endless_method_definition.txt +++ b/test/prism/errors/setter_method_cannot_be_defined_in_an_endless_method_definition.txt @@ -1,3 +1,6 @@ def a=() = 42 ^~ invalid method name; a setter method cannot be defined in an endless method definition +def []=() = 42 + ^~~ invalid method name; a setter method cannot be defined in an endless method definition +