SI-6932 StackOverflowError in chained Future.flatMap calls

After discussion on commit 089805510ee79c389ff2a870355c8496c723b8cc, submitting a self-contained PR for the bug fix taken from:
https://github.com/viktorklang/scala/pull/5/commits

I agree with the assessment of @jsuereth, @retronym, and @adriaanm that package-private visibility should enable these changes for 2.10.x.

Note: already reviewed and discussed, but maybe we can have another completing review by @heathermiller