backport r46684 from ruby/ruby trunk