Skip to content

Commit 7f7a9aa

Browse files
committed
+ builder.rb: disallow kwargs and blocks in index for Ruby 3.4
This tracks upstream commit ruby/ruby@df5ef28 and ruby/ruby@0d5b165
1 parent a7b4ac2 commit 7f7a9aa

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

lib/parser/builders/default.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,15 @@ def index(receiver, lbrack_t, indexes, rbrack_t)
11921192
end
11931193

11941194
def index_asgn(receiver, lbrack_t, indexes, rbrack_t)
1195+
if @parser.version >= 34 && (last = indexes.last)
1196+
if kwargs?(last)
1197+
diagnostic :error, :invalid_arg_in_index_assign, { :type => "keyword"}, last.loc.expression
1198+
end
1199+
if last.type == :block_pass
1200+
diagnostic :error, :invalid_arg_in_index_assign, { :type => "block"}, last.loc.expression
1201+
end
1202+
end
1203+
11951204
if self.class.emit_kwargs
11961205
rewrite_hash_args_to_kwargs(indexes)
11971206
end

lib/parser/messages.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ module Parser
8585
:ambiguous_anonymous_restarg => 'anonymous rest parameter is also used within block',
8686
:ambiguous_anonymous_kwrestarg => 'anonymous keyword rest parameter is also used within block',
8787
:ambiguous_anonymous_blockarg => 'anonymous block parameter is also used within block',
88+
:invalid_arg_in_index_assign => '%{type} argument given in index assignment',
8889

8990
# Parser warnings
9091
:useless_else => 'else without rescue is useless',

test/test_parser.rb

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3634,7 +3634,9 @@ def test_send_index_asgn_kwarg
36343634
s(:sym, :kw),
36353635
s(:send, nil, :arg))),
36363636
s(:int, 3)),
3637-
%q{foo[:kw => arg] = 3})
3637+
%q{foo[:kw => arg] = 3},
3638+
%q{},
3639+
ALL_VERSIONS - SINCE_3_4)
36383640
end
36393641

36403642
def test_send_index_asgn_kwarg_legacy
@@ -3647,11 +3649,49 @@ def test_send_index_asgn_kwarg_legacy
36473649
s(:sym, :kw),
36483650
s(:send, nil, :arg))),
36493651
s(:int, 3)),
3650-
%q{foo[:kw => arg] = 3})
3652+
%q{foo[:kw => arg] = 3},
3653+
%q{},
3654+
ALL_VERSIONS - SINCE_3_4)
36513655
ensure
36523656
Parser::Builders::Default.emit_kwargs = true
36533657
end
36543658

3659+
def test_send_index_asgn_since_34
3660+
[true, false].each do |emit_kwargs|
3661+
Parser::Builders::Default.emit_kwargs = emit_kwargs
3662+
3663+
[
3664+
%q{foo[:kw => arg] = 3},
3665+
%q{foo[**args] = 3},
3666+
%q{def x(**); foo[**] = 3; end},
3667+
].each do |code|
3668+
assert_diagnoses(
3669+
[:error, :invalid_arg_in_index_assign, { :type => "keyword" }],
3670+
code,
3671+
%q{},
3672+
SINCE_3_4)
3673+
end
3674+
3675+
refute_diagnoses(
3676+
%q{foo[{}]},
3677+
ALL_VERSIONS)
3678+
3679+
[
3680+
%q{foo[&blk] = 3},
3681+
%q{def x(&); foo[&] = 3; end},
3682+
%q{foo[&method(:foo)] = 3},
3683+
].each do |code|
3684+
assert_diagnoses(
3685+
[:error, :invalid_arg_in_index_assign, { :type => "block" }],
3686+
code,
3687+
%q{},
3688+
SINCE_3_4)
3689+
end
3690+
ensure
3691+
Parser::Builders::Default.emit_kwargs = true
3692+
end
3693+
end
3694+
36553695
def test_send_lambda
36563696
assert_parses(
36573697
s(:block, s(:lambda),

0 commit comments

Comments
 (0)