backport r47299 from ruby/ruby trunk.