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

Kotlin 1.5.0 fixes some type safety problems, need to fix some usages in Corda codebase #6888

Open
petukhovv opened this issue Mar 12, 2021 · 1 comment
Labels

Comments

@petukhovv
Copy link

Background.
Not so long ago, in the new type inference of the Kotlin compiler (which became available by default since Kotlin 1.4), we discovered a type safety problem related with the inference of a type variable by the type parameters' upper bounds and the expected type. The problem is that we successfully inferred the type based on unrelated types from the upper bounds of the type parameters and the expected type. The resulting type was the intersection type of these unrelated types.

This behavior made a "hole" in the type system, due to which it was possible to write code that was completely correct from the point of view of the type system (although such code can only be written using materialization-like functions), but fell at runtime with a ClassCastException. The simple example:

open class Foo
open class Bar

inline fun <reified T: Bar> materialize(): T  = T::class.java.newInstance()

fun main() {
    // T in inferred to {Bar & Foo} and approximated to Any for runtime
    // so ClassCastException happens at runtime in `materialize` during creating instance of T when we check if upper bounds aren't violated (it's actually violated, we have `Object cannot be cast to Bar`)
    val x: Foo = materialize()
}

The corresponding issue in the Kotlin bug tracker: https://youtrack.jetbrains.com/issue/KT-43303

After checking the fix upon the largest and most complex projects on Kotlin, we saw that there are several usages in Corda which are related with the existed problem in the Kotlin compiler's type inference and turn into the errors after the fix. Therefore, we decided to notify you in advance that errors will appear in these places when moving to Kotlin 1.5.0 (the fix is since 1.5.0-M2). Places are related with uncheckedCast usages. For example, see the attached screenshot. To look at all those usages you can try out the fix with future Kotlin 1.5.0-M2 or some of dev builds from our public TeamCity.

The fix in Corda project could be either removing the subtyping relation between the type parameters in the uncheckedCast declaration, or using another function (which does not use subtyping) for cases where an error occurs.

Feel free to ask any questions around it.

Screenshot 2021-03-12 at 18 29 51

@nargas-ritu
Copy link
Contributor

Created CORDA-4190 for internal review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants