Skip to content

Commit 89486c7

Browse files
jeremyevansnobu
andauthored
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>
1 parent 8e2a643 commit 89486c7

File tree

5 files changed

+13
-11
lines changed

5 files changed

+13
-11
lines changed

NEWS.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ Note that each entry is kept to a minimum, see links for details.
1919
`**nil` is treated similarly to `**{}`, passing no keywords,
2020
and not calling any conversion methods. [[Bug #20064]]
2121

22-
* Block passing is no longer allowed in index. [[Bug #19918]]
22+
* Block passing is no longer allowed in index assignment
23+
(e.g. `a[0, &b] = 1`). [[Bug #19918]]
2324

24-
* Keyword arguments are no longer allowed in index. [[Bug #20218]]
25+
* Keyword arguments are no longer allowed in index assignment
26+
(e.g. `a[0, kw: 1] = 2`). [[Bug #20218]]
2527

2628
## Core classes updates
2729

parse.y

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13658,10 +13658,10 @@ aryset_check(struct parser_params *p, NODE *args)
1365813658
}
1365913659
}
1366013660
if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13661-
yyerror1(&kwds->nd_loc, "keyword arg given in index");
13661+
yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
1366213662
}
1366313663
if (block) {
13664-
yyerror1(&block->nd_loc, "block arg given in index");
13664+
yyerror1(&block->nd_loc, "block arg given in index assignment");
1366513665
}
1366613666
}
1366713667

prism/templates/src/diagnostic.c.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
344344
[PM_ERR_UNDEF_ARGUMENT] = { "invalid argument being passed to `undef`; expected a bare word, constant, or symbol argument", PM_ERROR_LEVEL_SYNTAX },
345345
[PM_ERR_UNARY_RECEIVER] = { "unexpected %s, expected a receiver for unary `%c`", PM_ERROR_LEVEL_SYNTAX },
346346
[PM_ERR_UNEXPECTED_BLOCK_ARGUMENT] = { "block argument should not be given", PM_ERROR_LEVEL_SYNTAX },
347-
[PM_ERR_UNEXPECTED_INDEX_BLOCK] = { "unexpected block arg given in index; blocks are not allowed in index expressions", PM_ERROR_LEVEL_SYNTAX },
348-
[PM_ERR_UNEXPECTED_INDEX_KEYWORDS] = { "unexpected keyword arg given in index; keywords are not allowed in index expressions", PM_ERROR_LEVEL_SYNTAX },
347+
[PM_ERR_UNEXPECTED_INDEX_BLOCK] = { "unexpected block arg given in index assignment; blocks are not allowed in index assignment expressions", PM_ERROR_LEVEL_SYNTAX },
348+
[PM_ERR_UNEXPECTED_INDEX_KEYWORDS] = { "unexpected keyword arg given in index assignment; keywords are not allowed in index assignment expressions", PM_ERROR_LEVEL_SYNTAX },
349349
[PM_ERR_UNEXPECTED_SAFE_NAVIGATION] = { "&. inside multiple assignment destination", PM_ERROR_LEVEL_SYNTAX },
350350
[PM_ERR_UNEXPECTED_TOKEN_CLOSE_CONTEXT] = { "unexpected %s, assuming it is closing the parent %s", PM_ERROR_LEVEL_SYNTAX },
351351
[PM_ERR_UNEXPECTED_TOKEN_IGNORE] = { "unexpected %s, ignoring it", PM_ERROR_LEVEL_SYNTAX },

test/ruby/test_call.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def []=(*a, **b, &c) @set = [a, b, c] end
136136
# Prevent "assigned but unused variable" warnings
137137
_ = [h, a, kw, b]
138138

139-
message = /keyword arg given in index/
139+
message = /keyword arg given in index assignment/
140140

141141
# +=, without block, non-popped
142142
assert_syntax_error(%q{h[**kw] += 1}, message)
@@ -270,7 +270,7 @@ def test_kwsplat_block_order_op_asgn
270270
def o.[](...) 2 end
271271
def o.[]=(...) end
272272

273-
message = /keyword arg given in index/
273+
message = /keyword arg given in index assignment/
274274

275275
assert_syntax_error(%q{o[kw: 1] += 1}, message)
276276
assert_syntax_error(%q{o[**o] += 1}, message)
@@ -292,7 +292,7 @@ def [](*a, **b)
292292
def []=(*a, **b) @set = [a, b] end
293293
end.new
294294

295-
message = /keyword arg given in index/
295+
message = /keyword arg given in index assignment/
296296

297297
a = []
298298
kw = {}

test/ruby/test_parse.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,12 +512,12 @@ def t.[]=(_, _)
512512
def t.dummy(_)
513513
end
514514

515-
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /block arg given in index/)
515+
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /block arg given in index assignment/)
516516
begin;
517517
t[42, &blk] ||= 42
518518
end;
519519

520-
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /block arg given in index/)
520+
assert_syntax_error("#{<<~"begin;"}\n#{<<~'end;'}", /block arg given in index assignment/)
521521
begin;
522522
t[42, &blk] ||= t.dummy 42 # command_asgn test
523523
end;

0 commit comments

Comments
 (0)