Skip to content

Commit

Permalink
[Bug #19918] Reject block passing in index
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Mar 17, 2024
1 parent 3605d60 commit df5ef28
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 202 deletions.
19 changes: 0 additions & 19 deletions bootstraptest/test_method.rb
Expand Up @@ -340,24 +340,6 @@ def method_missing(mid, *args, &block) block end
assert_equal '1', %q( class C; def m() 1 end; private :m end
C.new.send(:m) )

# with block
assert_equal '[[:ok1, :foo], [:ok2, :foo, :bar]]',
%q{
class C
def [](a)
$ary << [yield, a]
end
def []=(a, b)
$ary << [yield, a, b]
end
end
$ary = []
C.new[:foo, &lambda{:ok1}]
C.new[:foo, &lambda{:ok2}] = :bar
$ary
}

# with
assert_equal '[:ok1, [:ok2, 11]]', %q{
class C
Expand Down Expand Up @@ -404,7 +386,6 @@ def m(*args, &b)

# aset and splat
assert_equal '4', %q{class Foo;def []=(a,b,c,d);end;end;Foo.new[1,*a=[2,3]]=4}
assert_equal '4', %q{class Foo;def []=(a,b,c,d);end;end;def m(&blk)Foo.new[1,*a=[2,3],&blk]=4;end;m{}}

# post test
assert_equal %q{[1, 2, :o1, :o2, [], 3, 4, NilClass, nil, nil]}, %q{
Expand Down
3 changes: 3 additions & 0 deletions parse.y
Expand Up @@ -13875,6 +13875,9 @@ new_bv(struct parser_params *p, ID name)
static NODE *
aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
{
if (idx && nd_type_p(idx, NODE_BLOCK_PASS)) {
compile_error(p, "block arg given in index");
}
return NEW_ATTRASGN(recv, tASET, idx, loc);
}

Expand Down
10 changes: 5 additions & 5 deletions test/prism/fixtures/arrays.txt
Expand Up @@ -81,13 +81,13 @@ foo[bar] = baz

%w[\C:]

foo[&bar] = 1
# foo[&bar] = 1

foo.foo[&bar] = 1
# foo.foo[&bar] = 1

def foo(&)
bar[&] = 1
end
# def foo(&)
# bar[&] = 1
# end

foo[] += 1

Expand Down
147 changes: 1 addition & 146 deletions test/prism/snapshots/arrays.txt
Expand Up @@ -2,7 +2,7 @@
├── locals: []
└── statements:
@ StatementsNode (location: (1,0)-(142,32))
└── body: (length: 59)
└── body: (length: 56)
├── @ ArrayNode (location: (1,0)-(1,4))
│ ├── flags: contains_splat
│ ├── elements: (length: 1)
Expand Down Expand Up @@ -942,151 +942,6 @@
│ │ └── unescaped: "\\C:"
│ ├── opening_loc: (82,0)-(82,3) = "%w["
│ └── closing_loc: (82,6)-(82,7) = "]"
├── @ CallNode (location: (84,0)-(84,13))
│ ├── flags: attribute_write
│ ├── receiver:
│ │ @ CallNode (location: (84,0)-(84,3))
│ │ ├── flags: variable_call, ignore_visibility
│ │ ├── receiver: ∅
│ │ ├── call_operator_loc: ∅
│ │ ├── name: :foo
│ │ ├── message_loc: (84,0)-(84,3) = "foo"
│ │ ├── opening_loc: ∅
│ │ ├── arguments: ∅
│ │ ├── closing_loc: ∅
│ │ └── block: ∅
│ ├── call_operator_loc: ∅
│ ├── name: :[]=
│ ├── message_loc: (84,3)-(84,9) = "[&bar]"
│ ├── opening_loc: (84,3)-(84,4) = "["
│ ├── arguments:
│ │ @ ArgumentsNode (location: (84,12)-(84,13))
│ │ ├── flags: ∅
│ │ └── arguments: (length: 1)
│ │ └── @ IntegerNode (location: (84,12)-(84,13))
│ │ ├── flags: decimal
│ │ └── value: 1
│ ├── closing_loc: (84,8)-(84,9) = "]"
│ └── block:
│ @ BlockArgumentNode (location: (84,4)-(84,8))
│ ├── expression:
│ │ @ CallNode (location: (84,5)-(84,8))
│ │ ├── flags: variable_call, ignore_visibility
│ │ ├── receiver: ∅
│ │ ├── call_operator_loc: ∅
│ │ ├── name: :bar
│ │ ├── message_loc: (84,5)-(84,8) = "bar"
│ │ ├── opening_loc: ∅
│ │ ├── arguments: ∅
│ │ ├── closing_loc: ∅
│ │ └── block: ∅
│ └── operator_loc: (84,4)-(84,5) = "&"
├── @ CallNode (location: (86,0)-(86,17))
│ ├── flags: attribute_write
│ ├── receiver:
│ │ @ CallNode (location: (86,0)-(86,7))
│ │ ├── flags: ∅
│ │ ├── receiver:
│ │ │ @ CallNode (location: (86,0)-(86,3))
│ │ │ ├── flags: variable_call, ignore_visibility
│ │ │ ├── receiver: ∅
│ │ │ ├── call_operator_loc: ∅
│ │ │ ├── name: :foo
│ │ │ ├── message_loc: (86,0)-(86,3) = "foo"
│ │ │ ├── opening_loc: ∅
│ │ │ ├── arguments: ∅
│ │ │ ├── closing_loc: ∅
│ │ │ └── block: ∅
│ │ ├── call_operator_loc: (86,3)-(86,4) = "."
│ │ ├── name: :foo
│ │ ├── message_loc: (86,4)-(86,7) = "foo"
│ │ ├── opening_loc: ∅
│ │ ├── arguments: ∅
│ │ ├── closing_loc: ∅
│ │ └── block: ∅
│ ├── call_operator_loc: ∅
│ ├── name: :[]=
│ ├── message_loc: (86,7)-(86,13) = "[&bar]"
│ ├── opening_loc: (86,7)-(86,8) = "["
│ ├── arguments:
│ │ @ ArgumentsNode (location: (86,16)-(86,17))
│ │ ├── flags: ∅
│ │ └── arguments: (length: 1)
│ │ └── @ IntegerNode (location: (86,16)-(86,17))
│ │ ├── flags: decimal
│ │ └── value: 1
│ ├── closing_loc: (86,12)-(86,13) = "]"
│ └── block:
│ @ BlockArgumentNode (location: (86,8)-(86,12))
│ ├── expression:
│ │ @ CallNode (location: (86,9)-(86,12))
│ │ ├── flags: variable_call, ignore_visibility
│ │ ├── receiver: ∅
│ │ ├── call_operator_loc: ∅
│ │ ├── name: :bar
│ │ ├── message_loc: (86,9)-(86,12) = "bar"
│ │ ├── opening_loc: ∅
│ │ ├── arguments: ∅
│ │ ├── closing_loc: ∅
│ │ └── block: ∅
│ └── operator_loc: (86,8)-(86,9) = "&"
├── @ DefNode (location: (88,0)-(90,3))
│ ├── name: :foo
│ ├── name_loc: (88,4)-(88,7) = "foo"
│ ├── receiver: ∅
│ ├── parameters:
│ │ @ ParametersNode (location: (88,8)-(88,9))
│ │ ├── requireds: (length: 0)
│ │ ├── optionals: (length: 0)
│ │ ├── rest: ∅
│ │ ├── posts: (length: 0)
│ │ ├── keywords: (length: 0)
│ │ ├── keyword_rest: ∅
│ │ └── block:
│ │ @ BlockParameterNode (location: (88,8)-(88,9))
│ │ ├── flags: ∅
│ │ ├── name: ∅
│ │ ├── name_loc: ∅
│ │ └── operator_loc: (88,8)-(88,9) = "&"
│ ├── body:
│ │ @ StatementsNode (location: (89,2)-(89,12))
│ │ └── body: (length: 1)
│ │ └── @ CallNode (location: (89,2)-(89,12))
│ │ ├── flags: attribute_write
│ │ ├── receiver:
│ │ │ @ CallNode (location: (89,2)-(89,5))
│ │ │ ├── flags: variable_call, ignore_visibility
│ │ │ ├── receiver: ∅
│ │ │ ├── call_operator_loc: ∅
│ │ │ ├── name: :bar
│ │ │ ├── message_loc: (89,2)-(89,5) = "bar"
│ │ │ ├── opening_loc: ∅
│ │ │ ├── arguments: ∅
│ │ │ ├── closing_loc: ∅
│ │ │ └── block: ∅
│ │ ├── call_operator_loc: ∅
│ │ ├── name: :[]=
│ │ ├── message_loc: (89,5)-(89,8) = "[&]"
│ │ ├── opening_loc: (89,5)-(89,6) = "["
│ │ ├── arguments:
│ │ │ @ ArgumentsNode (location: (89,11)-(89,12))
│ │ │ ├── flags: ∅
│ │ │ └── arguments: (length: 1)
│ │ │ └── @ IntegerNode (location: (89,11)-(89,12))
│ │ │ ├── flags: decimal
│ │ │ └── value: 1
│ │ ├── closing_loc: (89,7)-(89,8) = "]"
│ │ └── block:
│ │ @ BlockArgumentNode (location: (89,6)-(89,7))
│ │ ├── expression: ∅
│ │ └── operator_loc: (89,6)-(89,7) = "&"
│ ├── locals: []
│ ├── def_keyword_loc: (88,0)-(88,3) = "def"
│ ├── operator_loc: ∅
│ ├── lparen_loc: (88,7)-(88,8) = "("
│ ├── rparen_loc: (88,9)-(88,10) = ")"
│ ├── equal_loc: ∅
│ └── end_keyword_loc: (90,0)-(90,3) = "end"
├── @ IndexOperatorWriteNode (location: (92,0)-(92,10))
│ ├── flags: ∅
│ ├── receiver:
Expand Down
18 changes: 0 additions & 18 deletions test/ruby/test_compile_prism.rb
Expand Up @@ -2012,24 +2012,6 @@ def obj.[]=(a, b); 10; end
obj[*[1]] = 3
RUBY

# Test passing block inside of []=
assert_prism_eval(<<~RUBY)
obj = Object.new
def obj.[]=(a); end
p = proc {}
obj[&p] = 4
RUBY

# Test splat and block inside of []=
assert_prism_eval(<<~RUBY)
obj = Object.new
def obj.[]=(a, b); end
p = proc {}
obj[*[1], &p] = 4
RUBY

assert_prism_eval(<<-CODE)
def self.prism_opt_var_trail_hash(a = nil, *b, c, **d); end
prism_opt_var_trail_hash("a")
Expand Down
14 changes: 0 additions & 14 deletions test/ruby/test_parse.rb
Expand Up @@ -916,20 +916,6 @@ def test_set_backref
assert_syntax_error("$& = 1", /Can't set variable/)
end

def test_arg_concat
o = Object.new
class << o; self; end.instance_eval do
define_method(:[]=) {|*r, &b| b.call(r) }
end
r = nil
assert_nothing_raised do
eval <<-END, nil, __FILE__, __LINE__+1
o[&proc{|x| r = x }] = 1
END
end
assert_equal([1], r)
end

def test_void_expr_stmts_value
x = 1
useless_use = /useless use/
Expand Down

0 comments on commit df5ef28

Please sign in to comment.