Possibly a backport of https://github.com/ruby/ruby/pull/16801