New Type Inference Error in 1.3.40

I’m aware that there is a new type inference algorithm in 1.3.40 that is only active in IDE, not in Gradle.

Since going to that version I have a few errors that only show up in IntelliJ but not when compiled with Gradle.

The error is always

Required: Foo<T> Found: Foo<T!!>

What is this “!!” type modifier?

Unfortunately, the examples I have are complex and need a a lot of context but here is one that I can show more of if needed:

When I put at the marked position the compile error goes away.

It seems that you are mixing Java and Kotlin classes in complex ways. ReadWritableFunctionBindingHalf is a Java/platform type. Similarly it is not clear how/where createBinding is defined, but again it looks like a it inadvertently exposes generic parameters of platform type. We would need to see the signature of that function (please copy paste it as code, not screenshot).

Thanks for your reply. It’s all Kotlin actually. The code from JS module of an MPP so if what you say is true it is really unexpected

Here is the signature as you asked:

protected fun <T> createBinding(clientHalf : ReadableBindingHalf<T>, componentHalf : 
ReadableWriteableBindingHalf<T>) : Binding<T> {

Here is the Code for ReadWritableFunctionBindingHalf:

class ReadableWriteableFunctionBindingHalf<T>(
    private val readFunction : ReadFunction<T?>,
    val writeFunction: WriteFunction<T?>,
    override val name : String = "ReadableWriteableFunctionBindingHalf") : ReadableWriteableBindingHalf<T> {

    override fun getValue(): T? = readFunction()
    override fun setValue(t: T?) = writeFunction(t)

    override fun acceptsNull() = true

}

I think @pdvrieze switched up T! with T!!. To me it looks like as if there is a problem with the nullability of generics in the new type inference. If I read the error correctly createBinding expects a generic type parameter that is not nullable, but the type parameter that gets inferred might be.

This looks like a bug to me so maybe you could try to recreate it in a test project and create an issue here.

In the code fore ReadableWriteableFunctionBindingHalf the T is itself actually nullable (for a non-nullable T use T:Any, but then all places where it is used, a nullable variant of T is used. As a result it is not possible to infer whether T should be nullable or not (the type system transforms T?? to T?. I suspect if you constrain T to be not nullable that the problem will go away.

In my view this the type inference is correct in failing to infer the type here (although I’m not sure what the type T!! means - I suspect it means of undetermined nullability). Looking at it from a mathematical perspective, I don’t think it is feasible to detect this on declaration of ReadableWriteableFunctionBindingHalf so usage would be where you will find the problem. The error message can be a lot better though. In particular an occurance of the K!! type should probably be treated specially in terms of generating a better error message (or at least the meaning of K!! should be documented).