Skip to content

Commit

Permalink
+ builder.rb: disallow kwargs and blocks in index for Ruby 3.4
Browse files Browse the repository at this point in the history
This tracks upstream commit ruby/ruby@df5ef28 and ruby/ruby@0d5b165
  • Loading branch information
Earlopain committed Dec 21, 2024
1 parent a7b4ac2 commit 7f7a9aa
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lib/parser/builders/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,15 @@ def index(receiver, lbrack_t, indexes, rbrack_t)
end

def index_asgn(receiver, lbrack_t, indexes, rbrack_t)
if @parser.version >= 34 && (last = indexes.last)
if kwargs?(last)
diagnostic :error, :invalid_arg_in_index_assign, { :type => "keyword"}, last.loc.expression
end
if last.type == :block_pass
diagnostic :error, :invalid_arg_in_index_assign, { :type => "block"}, last.loc.expression
end
end

if self.class.emit_kwargs
rewrite_hash_args_to_kwargs(indexes)
end
Expand Down
1 change: 1 addition & 0 deletions lib/parser/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ module Parser
:ambiguous_anonymous_restarg => 'anonymous rest parameter is also used within block',
:ambiguous_anonymous_kwrestarg => 'anonymous keyword rest parameter is also used within block',
:ambiguous_anonymous_blockarg => 'anonymous block parameter is also used within block',
:invalid_arg_in_index_assign => '%{type} argument given in index assignment',

# Parser warnings
:useless_else => 'else without rescue is useless',
Expand Down
44 changes: 42 additions & 2 deletions test/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3634,7 +3634,9 @@ def test_send_index_asgn_kwarg
s(:sym, :kw),
s(:send, nil, :arg))),
s(:int, 3)),
%q{foo[:kw => arg] = 3})
%q{foo[:kw => arg] = 3},
%q{},
ALL_VERSIONS - SINCE_3_4)
end

def test_send_index_asgn_kwarg_legacy
Expand All @@ -3647,11 +3649,49 @@ def test_send_index_asgn_kwarg_legacy
s(:sym, :kw),
s(:send, nil, :arg))),
s(:int, 3)),
%q{foo[:kw => arg] = 3})
%q{foo[:kw => arg] = 3},
%q{},
ALL_VERSIONS - SINCE_3_4)
ensure
Parser::Builders::Default.emit_kwargs = true
end

def test_send_index_asgn_since_34
[true, false].each do |emit_kwargs|
Parser::Builders::Default.emit_kwargs = emit_kwargs

[
%q{foo[:kw => arg] = 3},
%q{foo[**args] = 3},
%q{def x(**); foo[**] = 3; end},
].each do |code|
assert_diagnoses(
[:error, :invalid_arg_in_index_assign, { :type => "keyword" }],
code,
%q{},
SINCE_3_4)
end

refute_diagnoses(
%q{foo[{}]},
ALL_VERSIONS)

[
%q{foo[&blk] = 3},
%q{def x(&); foo[&] = 3; end},
%q{foo[&method(:foo)] = 3},
].each do |code|
assert_diagnoses(
[:error, :invalid_arg_in_index_assign, { :type => "block" }],
code,
%q{},
SINCE_3_4)
end
ensure
Parser::Builders::Default.emit_kwargs = true
end
end

def test_send_lambda
assert_parses(
s(:block, s(:lambda),
Expand Down

0 comments on commit 7f7a9aa

Please sign in to comment.