vinivendra / Gryphon

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

Failed to convert float literal expression #100

Closed rlinoz closed 3 years ago

rlinoz commented 3 years ago

Describe the bug Apparently Gryphon is having trouble understanding Doubles and Floats

How to reproduce it If we run this code:

public func foo(_ bar: Double?) -> Double {
    return bar ?? 0.0
}

We get error: Failed to convert float literal expression (failed to translate SwiftSyntax node). return bar ?? 0.0

Also if we assign a variable of a struct to a Double value it will give a similar error:

private struct Foo {
    var balance: Double?
}

public func baz() {
    let f = Foo()
    f.balance = 32.0
}

And we get this error: Failed to convert float literal expression (failed to translate SwiftSyntax node). f.balance = 32.0

Your environment (please complete the following information):

vinivendra commented 3 years ago

Hey, thanks for the bug report.

Your examples show two different issues:

public func foo(_ bar: Double?) -> Double {
    return bar ?? 0.0
}

This one failed because Swift treats the 0.0 as an autoclosure, and Gryphon didn't know how to interpret that. This should be fixed now.

private struct Foo {
    var balance: Double?
}

public func baz() {
    let f = Foo()
    f.balance = 32.0
}

This one actually fails because Swift can't compile it, since we can't mutate the f variable because it's a let. If we change f to be a var, Swift compiles it successfully and Gryphon translates it without errors. That said, Gryphon's error message for this case wasn't helpful, so I also improved the way it handles the unexpected cases.

As usual, you can try this now if you want with brew install vinivendra/gryphon/gryphon --HEAD, or wait for the fix to be publicly released tomorrow night :)