From 820c541671d9485144d81138bb029f2da8379edd Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Thu, 13 Mar 2025 15:17:26 +0900 Subject: [PATCH] [Bug #21026] no singleton method on pseudo variable literal --- parse.y | 28 ++++++++++++-------- prism/prism.c | 2 +- test/prism/fixtures/keyword_method_names.txt | 9 ------- test/ruby/test_parse.rb | 6 +++++ 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/parse.y b/parse.y index 8a775174fe..16f91a9483 100644 --- a/parse.y +++ b/parse.y @@ -2757,7 +2757,7 @@ rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary) %token tREGEXP_END %token tDUMNY_END "dummy end" -%type singleton strings string string1 xstring regexp +%type singleton singleton_expr strings string string1 xstring regexp %type string_contents xstring_contents regexp_contents string_content %type words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word %type literal numeric simple_numeric ssym dsym symbol cpath @@ -6581,16 +6581,10 @@ opt_f_block_arg : ',' f_block_arg | none ; -singleton : value_expr(var_ref) - | '(' + +singleton : value_expr(singleton_expr) { - SET_LEX_STATE(EXPR_BEG); - p->ctxt.in_argdef = 0; - } - expr rparen - { - p->ctxt.in_argdef = 1; - NODE *expr = last_expr_node($3); + NODE *expr = last_expr_node($1); switch (nd_type(expr)) { case NODE_STR: case NODE_DSTR: @@ -6612,9 +6606,21 @@ singleton : value_expr(var_ref) yyerror1(&expr->nd_loc, "can't define singleton method for literals"); break; default: - value_expr($3); break; } + $$ = $1; + } + ; + +singleton_expr : var_ref + | '(' + { + SET_LEX_STATE(EXPR_BEG); + p->ctxt.in_argdef = 0; + } + expr rparen + { + p->ctxt.in_argdef = 1; $$ = $3; /*% ripper: paren!($:3) %*/ } diff --git a/prism/prism.c b/prism/prism.c index c4cab8f00e..8597aa63fa 100644 --- a/prism/prism.c +++ b/prism/prism.c @@ -3874,7 +3874,7 @@ pm_def_node_create( end = end_keyword->end; } - if ((receiver != NULL) && PM_NODE_TYPE_P(receiver, PM_PARENTHESES_NODE)) { + if (receiver != NULL) { pm_def_node_receiver_check(parser, receiver); } diff --git a/test/prism/fixtures/keyword_method_names.txt b/test/prism/fixtures/keyword_method_names.txt index 9154469441..d3b6eac537 100644 --- a/test/prism/fixtures/keyword_method_names.txt +++ b/test/prism/fixtures/keyword_method_names.txt @@ -12,18 +12,9 @@ end def m(a, **nil) end -def __ENCODING__.a -end - %{abc} %"abc" -def __FILE__.a -end - -def __LINE__.a -end - def nil::a end diff --git a/test/ruby/test_parse.rb b/test/ruby/test_parse.rb index 0c50793549..98e95b98af 100644 --- a/test/ruby/test_parse.rb +++ b/test/ruby/test_parse.rb @@ -475,6 +475,12 @@ class TestParse < Test::Unit::TestCase assert_parse_error(%q[def (:"#{42}").foo; end], msg) assert_parse_error(%q[def ([]).foo; end], msg) assert_parse_error(%q[def ([1]).foo; end], msg) + assert_parse_error(%q[def (__FILE__).foo; end], msg) + assert_parse_error(%q[def (__LINE__).foo; end], msg) + assert_parse_error(%q[def (__ENCODING__).foo; end], msg) + assert_parse_error(%q[def __FILE__.foo; end], msg) + assert_parse_error(%q[def __LINE__.foo; end], msg) + assert_parse_error(%q[def __ENCODING__.foo; end], msg) end def test_flip_flop