backport r46948 from ruby/ruby trunk.