backport r46823 from ruby/ruby trunk.