Backport of SI-6932 to 2.9.x

Already reviewed and discussed, see https://github.com/scala/scala/pull/1941

Review by @adriaanm