Skip to content

Commit

Permalink
Split warning messages for tag-jump
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Aug 31, 2019
1 parent d4eef04 commit 431a99b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
40 changes: 20 additions & 20 deletions test/ruby/test_keyword.rb
Expand Up @@ -22,7 +22,7 @@ def f2(x, str: "foo", num: 424242)

def test_f2
assert_equal([:xyz, "foo", 424242], f2(:xyz))
assert_warn(/The keyword argument for `f2' .* is passed as the last hash parameter/) do
assert_warn(/The keyword argument is passed as the last hash parameter.* for `f2'/m) do
assert_equal([{"bar"=>42}, "foo", 424242], f2("bar"=>42))
end
end
Expand Down Expand Up @@ -206,16 +206,16 @@ def test_lambda_kwsplat_call

f = ->(a, **x) { [a,x] }
assert_raise(ArgumentError) { f[**{}] }
assert_warn(/The keyword argument for `\[\]' .* is passed as the last hash parameter/) do
assert_warn(/The keyword argument is passed as the last hash parameter.* for `\[\]'/m) do
assert_equal([{}, {}], f[**kw])
end
assert_warn(/The keyword argument for `\[\]' .* is passed as the last hash parameter/) do
assert_warn(/The keyword argument is passed as the last hash parameter.* for `\[\]'/m) do
assert_equal([h, {}], f[**h])
end
assert_warn(/The keyword argument for `\[\]' .* is passed as the last hash parameter/) do
assert_warn(/The keyword argument is passed as the last hash parameter.* for `\[\]'/m) do
assert_equal([h2, {}], f[**h2])
end
assert_warn(/The keyword argument for `\[\]' .* is passed as the last hash parameter/) do
assert_warn(/The keyword argument is passed as the last hash parameter.* for `\[\]'/m) do
assert_equal([h3, {}], f[**h3])
end

Expand Down Expand Up @@ -353,22 +353,22 @@ def test_rest_keyrest
bug7665 = '[ruby-core:51278]'
bug8463 = '[ruby-core:55203] [Bug #8463]'
expect = [*%w[foo bar], {zzz: 42}]
assert_warn(/The last argument for `rest_keyrest' .* is used as the keyword parameter/) do
assert_warn(/The last argument is used as the keyword parameter.* for `rest_keyrest'/m) do
assert_equal(expect, rest_keyrest(*expect), bug7665)
end
pr = proc {|*args, **opt| next *args, opt}
assert_warn(/The last argument for `call' .* is used as the keyword parameter/) do
assert_warn(/The last argument is used as the keyword parameter.* for `call'/m) do
assert_equal(expect, pr.call(*expect), bug7665)
end
assert_warn(/The last argument for `call' .* is used as the keyword parameter/) do
assert_warn(/The last argument is used as the keyword parameter.* for `call'/m) do
assert_equal(expect, pr.call(expect), bug8463)
end
pr = proc {|a, *b, **opt| next a, *b, opt}
assert_warn(/The last argument for `call' .* is used as the keyword parameter/) do
assert_warn(/The last argument is used as the keyword parameter.* for `call'/m) do
assert_equal(expect, pr.call(expect), bug8463)
end
pr = proc {|a, **opt| next a, opt}
assert_warn(/The last argument for `call' .* is used as the keyword parameter/) do
assert_warn(/The last argument is used as the keyword parameter.* for `call'/m) do
assert_equal(expect.values_at(0, -1), pr.call(expect), bug8463)
end
end
Expand All @@ -386,13 +386,13 @@ def splat_plus_keyword(*a, **h)
end

def test_keyword_split
assert_warn(/The keyword argument for `req_plus_keyword' .* is passed as the last hash parameter/) do
assert_warn(/The keyword argument is passed as the last hash parameter.* for `req_plus_keyword'/m) do
assert_equal([{:a=>1}, {}], req_plus_keyword(:a=>1))
end
assert_warn(/The keyword argument for `req_plus_keyword' .* is passed as the last hash parameter/) do
assert_warn(/The keyword argument is passed as the last hash parameter.* for `req_plus_keyword'/m) do
assert_equal([{"a"=>1}, {}], req_plus_keyword("a"=>1))
end
assert_warn(/The keyword argument for `req_plus_keyword' .* is passed as the last hash parameter/) do
assert_warn(/The keyword argument is passed as the last hash parameter.* for `req_plus_keyword'/m) do
assert_equal([{"a"=>1, :a=>1}, {}], req_plus_keyword("a"=>1, :a=>1))
end
assert_equal([{:a=>1}, {}], req_plus_keyword({:a=>1}))
Expand All @@ -402,22 +402,22 @@ def test_keyword_split
assert_equal([1, {:a=>1}], opt_plus_keyword(:a=>1))
assert_equal([1, {"a"=>1}], opt_plus_keyword("a"=>1))
assert_equal([1, {"a"=>1, :a=>1}], opt_plus_keyword("a"=>1, :a=>1))
assert_warn(/The last argument for `opt_plus_keyword' .* is used as the keyword parameter/) do
assert_warn(/The last argument is used as the keyword parameter.* for `opt_plus_keyword'/m) do
assert_equal([1, {:a=>1}], opt_plus_keyword({:a=>1}))
end
assert_equal([{"a"=>1}, {}], opt_plus_keyword({"a"=>1}))
assert_warn(/The last argument for `opt_plus_keyword' .* is split into positional and keyword parameters/) do
assert_warn(/The last argument is split into positional and keyword parameters.* for `opt_plus_keyword'/m) do
assert_equal([{"a"=>1}, {:a=>1}], opt_plus_keyword({"a"=>1, :a=>1}))
end

assert_equal([[], {:a=>1}], splat_plus_keyword(:a=>1))
assert_equal([[], {"a"=>1}], splat_plus_keyword("a"=>1))
assert_equal([[], {"a"=>1, :a=>1}], splat_plus_keyword("a"=>1, :a=>1))
assert_warn(/The last argument for `splat_plus_keyword' .* is used as the keyword parameter/) do
assert_warn(/The last argument is used as the keyword parameter.* for `splat_plus_keyword'/m) do
assert_equal([[], {:a=>1}], splat_plus_keyword({:a=>1}))
end
assert_equal([[{"a"=>1}], {}], splat_plus_keyword({"a"=>1}))
assert_warn(/The last argument for `splat_plus_keyword' .* is split into positional and keyword parameters/) do
assert_warn(/The last argument is split into positional and keyword parameters.* for `splat_plus_keyword'/m) do
assert_equal([[{"a"=>1}], {:a=>1}], splat_plus_keyword({"a"=>1, :a=>1}))
end
end
Expand Down Expand Up @@ -604,7 +604,7 @@ def foo(a, b, c=1, *d, e, f:2, **g)
[a, b, c, d, e, f, g]
end
end
assert_warn(/The keyword argument for `foo' .* is passed as the last hash parameter/) do
assert_warn(/The keyword argument is passed as the last hash parameter.* for `foo'/m) do
assert_equal([1, 2, 1, [], {:f=>5}, 2, {}], a.new.foo(1, 2, f:5), bug8993)
end
end
Expand Down Expand Up @@ -639,10 +639,10 @@ def test_implicit_hash_conversion
o = Object.new
def o.to_hash() { k: 9 } end
assert_equal([1, 42, [], o, :key, {}, nil], f9(1, o))
assert_warn(/The last argument for `m1' .* is used as the keyword parameter/) do
assert_warn(/The last argument is used as the keyword parameter.* for `m1'/m) do
assert_equal([1, 9], m1(1, o) {|a, k: 0| break [a, k]}, bug10016)
end
assert_warn(/The last argument for `m1' .* is used as the keyword parameter/) do
assert_warn(/The last argument is used as the keyword parameter.* for `m1'/m) do
assert_equal([1, 9], m1(1, o, &->(a, k: 0) {break [a, k]}), bug10016)
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/ruby/test_syntax.rb
Expand Up @@ -155,7 +155,7 @@ def test_keyword_duplicated_splat
h = {k3: 31}
assert_raise(ArgumentError) {o.kw(**h)}
h = {"k1"=>11, k2: 12}
assert_warn(/The last argument for `kw' .* is split into positional and keyword parameters/) do
assert_warn(/The last argument is split into positional and keyword parameters.* for `kw'/m) do
assert_raise(ArgumentError) {o.kw(**h)}
end
end
Expand Down
27 changes: 18 additions & 9 deletions vm_args.c
Expand Up @@ -585,41 +585,50 @@ static inline void
rb_warn_keyword_to_last_hash(struct rb_calling_info *calling, const struct rb_call_info *ci, const rb_iseq_t * const iseq)
{
if (calling->recv == Qundef) return;
VALUE name = rb_id2str(ci->mid);
VALUE loc = rb_iseq_location(iseq);
if (NIL_P(loc)) {
rb_warn("The keyword argument for `%s' is passed as the last hash parameter", rb_id2name(ci->mid));
rb_warn("The keyword argument for `%"PRIsVALUE"' is passed as the last hash parameter",
name);
}
else {
rb_warn("The keyword argument for `%s' (defined at %s:%d) is passed as the last hash parameter",
rb_id2name(ci->mid), RSTRING_PTR(RARRAY_AREF(loc, 0)), FIX2INT(RARRAY_AREF(loc, 1)));
rb_warn("The keyword argument is passed as the last hash parameter");
rb_compile_warn(RSTRING_PTR(RARRAY_AREF(loc, 0)), FIX2INT(RARRAY_AREF(loc, 1)),
"for `%"PRIsVALUE"' defined here", name);
}
}

static inline void
rb_warn_split_last_hash_to_keyword(struct rb_calling_info *calling, const struct rb_call_info *ci, const rb_iseq_t * const iseq)
{
if (calling->recv == Qundef) return;
VALUE name = rb_id2str(ci->mid);
VALUE loc = rb_iseq_location(iseq);
if (NIL_P(loc)) {
rb_warn("The last argument for `%s' is split into positional and keyword parameters", rb_id2name(ci->mid));
rb_warn("The last argument for `%"PRIsVALUE"' is split into positional and keyword parameters",
name);
}
else {
rb_warn("The last argument for `%s' (defined at %s:%d) is split into positional and keyword parameters",
rb_id2name(ci->mid), RSTRING_PTR(RARRAY_AREF(loc, 0)), FIX2INT(RARRAY_AREF(loc, 1)));
rb_warn("The last argument is split into positional and keyword parameters");
rb_compile_warn(RSTRING_PTR(RARRAY_AREF(loc, 0)), FIX2INT(RARRAY_AREF(loc, 1)),
"for `%"PRIsVALUE"' defined here", name);
}
}

static inline void
rb_warn_last_hash_to_keyword(struct rb_calling_info *calling, const struct rb_call_info *ci, const rb_iseq_t * const iseq)
{
if (calling->recv == Qundef) return;
VALUE name = rb_id2str(ci->mid);
VALUE loc = rb_iseq_location(iseq);
if (NIL_P(loc)) {
rb_warn("The last argument for `%s' is used as the keyword parameter", rb_id2name(ci->mid));
rb_warn("The last argument for `%"PRIsVALUE"' is used as the keyword parameter",
name);
}
else {
rb_warn("The last argument for `%s' (defined at %s:%d) is used as the keyword parameter",
rb_id2name(ci->mid), RSTRING_PTR(RARRAY_AREF(loc, 0)), FIX2INT(RARRAY_AREF(loc, 1)));
rb_warn("The last argument is used as the keyword parameter");
rb_compile_warn(RSTRING_PTR(RARRAY_AREF(loc, 0)), FIX2INT(RARRAY_AREF(loc, 1)),
"for `%"PRIsVALUE"' defined here", name);
}
}

Expand Down

0 comments on commit 431a99b

Please sign in to comment.