Make error messages clear blocks/keywords are disallowed in index assignment

Blocks and keywords are allowed in regular index.

Also update NEWS to make this more clear.

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
This commit is contained in:
Jeremy Evans 2024-05-31 08:22:40 -07:00 коммит произвёл GitHub
Родитель 8e2a6435e4
Коммит 89486c79bb
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
5 изменённых файлов: 13 добавлений и 11 удалений

Просмотреть файл

@ -19,9 +19,11 @@ Note that each entry is kept to a minimum, see links for details.
`**nil` is treated similarly to `**{}`, passing no keywords,
and not calling any conversion methods. [[Bug #20064]]
* Block passing is no longer allowed in index. [[Bug #19918]]
* Block passing is no longer allowed in index assignment
(e.g. `a[0, &b] = 1`). [[Bug #19918]]
* Keyword arguments are no longer allowed in index. [[Bug #20218]]
* Keyword arguments are no longer allowed in index assignment
(e.g. `a[0, kw: 1] = 2`). [[Bug #20218]]
## Core classes updates

Просмотреть файл

@ -13658,10 +13658,10 @@ aryset_check(struct parser_params *p, NODE *args)
}
}
if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
yyerror1(&kwds->nd_loc, "keyword arg given in index");
yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
}
if (block) {
yyerror1(&block->nd_loc, "block arg given in index");
yyerror1(&block->nd_loc, "block arg given in index assignment");
}
}

Просмотреть файл

@ -344,8 +344,8 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
[PM_ERR_UNDEF_ARGUMENT] = { "invalid argument being passed to `undef`; expected a bare word, constant, or symbol argument", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_UNARY_RECEIVER] = { "unexpected %s, expected a receiver for unary `%c`", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_UNEXPECTED_BLOCK_ARGUMENT] = { "block argument should not be given", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_UNEXPECTED_INDEX_BLOCK] = { "unexpected block arg given in index; blocks are not allowed in index expressions", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_UNEXPECTED_INDEX_KEYWORDS] = { "unexpected keyword arg given in index; keywords are not allowed in index expressions", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_UNEXPECTED_INDEX_BLOCK] = { "unexpected block arg given in index assignment; blocks are not allowed in index assignment expressions", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_UNEXPECTED_INDEX_KEYWORDS] = { "unexpected keyword arg given in index assignment; keywords are not allowed in index assignment expressions", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_UNEXPECTED_SAFE_NAVIGATION] = { "&. inside multiple assignment destination", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_UNEXPECTED_TOKEN_CLOSE_CONTEXT] = { "unexpected %s, assuming it is closing the parent %s", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_UNEXPECTED_TOKEN_IGNORE] = { "unexpected %s, ignoring it", PM_ERROR_LEVEL_SYNTAX },

Просмотреть файл

@ -136,7 +136,7 @@ class TestCall < Test::Unit::TestCase
# Prevent "assigned but unused variable" warnings
_ = [h, a, kw, b]
message = /keyword arg given in index/
message = /keyword arg given in index assignment/
# +=, without block, non-popped
assert_syntax_error(%q{h[**kw] += 1}, message)
@ -270,7 +270,7 @@ class TestCall < Test::Unit::TestCase
def o.[](...) 2 end
def o.[]=(...) end
message = /keyword arg given in index/
message = /keyword arg given in index assignment/
assert_syntax_error(%q{o[kw: 1] += 1}, message)
assert_syntax_error(%q{o[**o] += 1}, message)
@ -292,7 +292,7 @@ class TestCall < Test::Unit::TestCase
def []=(*a, **b) @set = [a, b] end
end.new
message = /keyword arg given in index/
message = /keyword arg given in index assignment/
a = []
kw = {}

Просмотреть файл

@ -512,12 +512,12 @@ class TestParse < Test::Unit::TestCase
def t.dummy(_)
end
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /block arg given in index/)
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /block arg given in index assignment/)
begin;
t[42, &blk] ||= 42
end;
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /block arg given in index/)
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /block arg given in index assignment/)
begin;
t[42, &blk] ||= t.dummy 42 # command_asgn test
end;