Skip to content

Commit

Permalink
Show the name Kernel#proc in the warning message
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Nov 12, 2019
1 parent 3816622 commit bf34ade
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
16 changes: 9 additions & 7 deletions proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ rb_func_lambda_new(rb_block_call_func_t func, VALUE val, int min_argc, int max_a
static const char proc_without_block[] = "tried to create Proc object without a block";

static VALUE
proc_new(VALUE klass, int8_t is_lambda)
proc_new(VALUE klass, int8_t is_lambda, int8_t kernel)
{
VALUE procval;
const rb_execution_context_t *ec = GET_EC();
Expand All @@ -757,11 +757,13 @@ proc_new(VALUE klass, int8_t is_lambda)
rb_raise(rb_eArgError, proc_without_block);
}
else {
rb_warn("Capturing the given block using Proc.new is deprecated; use `&block` instead");
const char *name = kernel ? "Kernel#proc" : "Proc.new";
rb_warn("Capturing the given block using %s is deprecated; "
"use `&block` instead", name);
}
}
#else
if (0)
if (0);
#endif
else {
rb_raise(rb_eArgError, proc_without_block);
Expand Down Expand Up @@ -817,7 +819,7 @@ proc_new(VALUE klass, int8_t is_lambda)
static VALUE
rb_proc_s_new(int argc, VALUE *argv, VALUE klass)
{
VALUE block = proc_new(klass, FALSE);
VALUE block = proc_new(klass, FALSE, FALSE);

rb_obj_call_init_kw(block, argc, argv, RB_PASS_CALLED_KEYWORDS);
return block;
Expand All @@ -826,7 +828,7 @@ rb_proc_s_new(int argc, VALUE *argv, VALUE klass)
VALUE
rb_block_proc(void)
{
return proc_new(rb_cProc, FALSE);
return proc_new(rb_cProc, FALSE, FALSE);
}

/*
Expand All @@ -839,13 +841,13 @@ rb_block_proc(void)
static VALUE
f_proc(VALUE _)
{
return rb_block_proc();
return proc_new(rb_cProc, FALSE, TRUE);
}

VALUE
rb_block_lambda(void)
{
return proc_new(rb_cProc, TRUE);
return proc_new(rb_cProc, TRUE, FALSE);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion spec/ruby/core/kernel/proc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def some_method

-> {
some_method { "hello" }
}.should complain(/Capturing the given block using Proc.new is deprecated/)
}.should complain(/Capturing the given block using Kernel#proc is deprecated/)
end
end
end
9 changes: 7 additions & 2 deletions test/ruby/test_proc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ def self.dyna_var_check

def assert_arity(n)
meta = class << self; self; end
meta.class_eval {define_method(:foo, Proc.new)}
b = assert_warn(/Capturing the given block using Proc\.new is deprecated/) do
Proc.new
end
meta.class_eval {define_method(:foo, b)}
assert_equal(n, method(:foo).arity)
end

Expand Down Expand Up @@ -1413,7 +1416,9 @@ def m(&blk) blk.call; end
end

def method_for_test_proc_without_block_for_symbol
binding.eval('proc')
assert_warn(/Capturing the given block using Kernel#proc is deprecated/) do
binding.eval('proc')
end
end

def test_proc_without_block_for_symbol
Expand Down

0 comments on commit bf34ade

Please sign in to comment.