Constrain maximum lifetime of stack closures that capture variables to
be limited by the innermost repeating scope.

Fixes #7336.
Improve test
Improve error message