backports https://github.com/scala/scala/pull/3659

https://github.com/scala/scala/pull/3659