Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove tailrec as this is not a tailrec function #8727

Merged
merged 1 commit into from
Jan 10, 2025

Conversation

wmontwe
Copy link
Member

@wmontwe wmontwe commented Jan 9, 2025

Fixes a warning that: A function is marked as tail-recursive but no tail calls are found.

@wmontwe wmontwe requested a review from cketti as a code owner January 9, 2025 15:47
@cketti
Copy link
Member

cketti commented Jan 9, 2025

How/where are you getting this warning? The function is tail-recursive and "Show Kotlin Bytecode" in my IDE shows the compiler has converted the code to a loop as is expected when using tailrec.

@wmontwe
Copy link
Member Author

wmontwe commented Jan 9, 2025

How/where are you getting this warning? The function is tail-recursive and "Show Kotlin Bytecode" in my IDE shows the compiler has converted the code to a loop as is expected when using tailrec.

The build reports this and my IDE too. AS 2024.2.1 patch 3

Task :core:android:common:compileDebugKotlin
w: file:///Users/user/Code/mzla/thunderbird-android/core/android/common/src/main/kotlin/app/k9mail/core/android/common/activity/ContextExtensions.kt:10:1 A function is marked as tail-recursive but no tail calls are found.
w: file:///Users/user/Code/mzla/thunderbird-android/core/android/common/src/main/kotlin/app/k9mail/core/android/common/activity/ContextExtensions.kt:11:48 Recursive call is not a tail call.

@cketti
Copy link
Member

cketti commented Jan 9, 2025

Might be this bug: https://youtrack.jetbrains.com/issue/KT-73420

To avoid a StackOverflowException we shouldn't use recursion when the recursion depth is unknown.

The following code should be roughly equivalent to what the compiler generates when tailrec is working.

fun Context.findActivity(): Activity? {
    var context: Context? = this
    while (context != null && context !is Activity) {
        context = (context as? ContextWrapper)?.baseContext
    }

    return context as? Activity
}

Update:

Converting the code to not use the elvis operator seems to also fix the warning.

tailrec fun Context.findActivity(): Activity? {
    return if (this is Activity) {
        this
    } else {
        (this as? ContextWrapper)?.baseContext?.findActivity()
    }
}

@wmontwe
Copy link
Member Author

wmontwe commented Jan 10, 2025

I like the updated version and update the pull-request.

@wmontwe wmontwe force-pushed the fix-tailrec-warning branch from 6fd8261 to 8b373bd Compare January 10, 2025 11:18
@wmontwe wmontwe force-pushed the fix-tailrec-warning branch from 8b373bd to a952b73 Compare January 10, 2025 11:19
@wmontwe wmontwe merged commit 32a9866 into thunderbird:main Jan 10, 2025
3 checks passed
@wmontwe wmontwe deleted the fix-tailrec-warning branch January 10, 2025 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants