Skip to content

Commit

Permalink
Warn when passing a non-literal block to Kernel#lambda
Browse files Browse the repository at this point in the history
Implements [Feature #15973]
  • Loading branch information
jeremyevans committed Jun 11, 2020
1 parent 5349506 commit 2188d6d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
3 changes: 0 additions & 3 deletions basictest/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1425,9 +1425,6 @@ def marity_test(m)
marity_test(:marity_test)
marity_test(:p)

lambda(&method(:test_ok)).call(true)
lambda(&block_get{|a,n| test_ok(a,n)}).call(true, 2)

class ITER_TEST1
def a
block_given?
Expand Down
13 changes: 13 additions & 0 deletions proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,19 @@ rb_block_lambda(void)
static VALUE
f_lambda(VALUE _)
{
VALUE block_handler = rb_vm_frame_block_handler(GET_EC()->cfp);

if (block_handler != VM_BLOCK_HANDLER_NONE) {
switch (vm_block_handler_type(block_handler)) {
case block_handler_type_proc:
case block_handler_type_symbol:
case block_handler_type_ifunc:
rb_warn_deprecated("lambda without a literal block", "the proc without lambda");
default:
break;
}
}

return rb_block_lambda();
}

Expand Down
3 changes: 1 addition & 2 deletions test/ruby/test_iterator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,7 @@ def test_marity
marity_test(:marity_test)
marity_test(:p)

lambda(&method(:assert)).call(true)
lambda(&get_block{|a,n| assert(a,n)}).call(true, "marity")
get_block{|a,n| assert(a,n)}.call(true, "marity")
end

def foo
Expand Down
6 changes: 6 additions & 0 deletions test/ruby/test_lambda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ def test_call_block_from_lambda
assert_raise(ArgumentError, bug9605) {proc(&plus).call [1,2]}
end

def test_warning_for_non_literal_blocks
assert_warn(/lambda without a literal block/, '[ruby-core:93482] [Feature #15973]') do
lambda(&:symbol)
end
end

def pass_along(&block)
lambda(&block)
end
Expand Down

0 comments on commit 2188d6d

Please sign in to comment.