1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Fix pp when passed a empty ruby2_keywords-flagged hash as array element (#2966)

This causes problems because the hash is passed to a block not
accepting keywords.  Because the hash is empty and keyword flagged,
it is removed before calling the block.  This doesn't cause an
ArgumentError because it is a block and not a lambda.  Just like
any other block not passed required arguments, arguments not
passed are set to nil.

Issues like this are a strong reason not to have ruby2_keywords
by default.

Fixes [Bug #16519]

This backports 28d31ead34 and
0ea759eac9, but needed to be modified
for 2.7 as 2.7 will perform empty keyword to positional hash
conversion for required arguments, which will happen if "v" in the
seplist method is empty when yielded.

Co-authored-by: NARUSE, Yui <nurse@users.noreply.github.com>
This commit is contained in:
Jeremy Evans 2020-03-31 00:10:57 -07:00 committed by GitHub
parent d04856bdc5
commit bb93659fef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View file

@ -223,7 +223,16 @@ class PP < PrettyPrint
else
sep.call
end
yield(*v)
case v.last
when Hash
if Hash.ruby2_keywords_hash?(v.last)
yield(*v, **{})
else
yield(*v)
end
else
yield(*v)
end
}
end

View file

@ -176,6 +176,11 @@ class PPSingleLineTest < Test::Unit::TestCase
assert_equal("{1=>1}", PP.singleline_pp({ 1 => 1}, ''.dup)) # [ruby-core:02699]
assert_equal("[1#{', 1'*99}]", PP.singleline_pp([1]*100, ''.dup))
end
def test_hash_in_array
assert_equal("[{}]", PP.singleline_pp([->(*a){a.last}.ruby2_keywords.call(**{})], ''.dup))
assert_equal("[{}]", PP.singleline_pp([Hash.ruby2_keywords_hash({})], ''.dup))
end
end
class PPDelegateTest < Test::Unit::TestCase