Scala and Kotlin have 'tailrec' annotation/modifier, though not as sophisticated as you describe.