backport r46872 from ruby/ruby trunk