dotlin-org/dotlin

Smart cast error not raised

Closed this issue · 2 comments

This dart code is illegal because the value of a.s can be modified between null-check and return:

class A
{
    String? s;
}

class B
{
    String f()
    {
        final A a = A();
        if (a.s != null)
            return a.s;

        return '';
    }
}

and this kind of code is generated by dotlin:
in lib/src/kotlin/dev/eggnstone/plugins/jetbrains/dartformat/splitters/iSplitters/text_splitter.dt.g.dart:58:33:

          if (handleResult.splitResult != null) {
            return handleResult.splitResult;
          }

It probably needs a temp variable:

class A
{
    String? s;
}

class B
{
    String f()
    {
        final A a = A();
        final String? temp = a.s;
        if (temp != null)
            return temp;

        return '';
    }
}

Actually you should have received an error on the Dotlin side before Dart code was even generated, because of the same reason you mentioned 🤔 If you try out the Kotlin equivalent in Dotlin you'll see an error:

class A {
    var s: String? = ""
}

class B
{
    fun f(): String {
        val a: A = A()
        if (a.s != null)
            return a.s

        return ""
    }
}
error: smart cast to 'String' is impossible, because 'a.s' is a mutable property that could have been changed by this time
            return a.s

So for some reason in the TextSplitter the error isn't raised, I'll look into it 👍

Closing because of Dirt.