[Bug #21026] no singleton method on pseudo variable literal

This commit is contained in:
Nobuyoshi Nakada 2025-03-13 15:17:26 +09:00 committed by Nobuyoshi Nakada
parent 641f15b1c6
commit 820c541671
Notes: git 2025-03-20 08:32:44 +00:00
4 changed files with 24 additions and 21 deletions

28
parse.y
View File

@ -2757,7 +2757,7 @@ rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
%token <num> tREGEXP_END
%token <num> tDUMNY_END "dummy end"
%type <node> singleton strings string string1 xstring regexp
%type <node> singleton singleton_expr strings string string1 xstring regexp
%type <node> string_contents xstring_contents regexp_contents string_content
%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
%type <node> 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) %*/
}

View File

@ -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);
}

View File

@ -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

View File

@ -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