vinivendra / Gryphon

The Swift to Kotlin translator.
https://vinivendra.github.io/Gryphon/
Other
606 stars 46 forks source link

Wrong return call inside lambda with guard #66

Closed rlinoz closed 4 years ago

rlinoz commented 4 years ago

Describe the bug returning from inside a guard after doing something inside the else outputs wrong kotlin code

How to reproduce it Given the swift code:

protocol Test {
    var a: Int { get set }
}

func foo(arg: @escaping (Test?) -> ()) {

}

func baz() {
    var k = 0
    foo(arg: { t in
        guard let td = t else {
            k = 1
            return
        }
    })
}

It outputs

internal interface Test {
    var a: Int
}

internal fun foo(arg: (Test?) -> Unit) {
}

internal fun baz() {
    var k: Int = 0
    foo { t ->
            val td: Test? = t

            if (td == null) {
                k = 1
                return@foo(arg:)
            }
        }
}

The correct thing to do inside the lambda return is return@foo

Your environment (please complete the following information):