Backport code from mainline scala