backport r47392 from ruby/ruby trunk.