backport r47623 from ruby/ruby trunk.