backport r47395 from ruby/ruby trunk.