backport r46146 from ruby/ruby trunk.