Skip to content

KaSmartCastPossibility

Represents a possible smart cast at a fixed context position.

The word "possible" means that the compiler is allowed to produce smart casts for the source expression if there is a need to do so. Note that unless isStable is set, the smart cast will not be applied. In addition, the SMARTCAST_IMPOSSIBLE error is issued in certain cases.

Properties

isStable

abstract val isStable: Boolean

Whether the smart cast source is stable at the context position.

The same source can have different smart cast stability depending on the context position. For example, in the code snippet below, a smart cast will be applied to the first usage of value. However, after the conditional expression, the compiler is not sure anymore that value is still set, so a compilation error is reported.

fun test() {
    var value: String? = System.getProperty("some.property")
    if (value == null) {
        return
    }

    value.length  // Smart cast is applied

    if (Random.nextBoolean()) {
        value = null
    }

    value.length  // UNSAFE_CALL
}

For more information on smart cast stability, check the Kotlin specification: Smart cast sink stability.

smartCastTypes

abstract val smartCastTypes: List<KaType>

Types that source may be automatically cast to.

source

abstract val source: KaSmartCastSource

A declaration to whose references a smart cast may be applied (if isStable is true).

token

abstract val token: KaLifetimeToken

The KaLifetimeToken which determines the lifetime of the lifetime owner.

Functions

assertIsValidAndAccessible

isValid

Whether the lifetime owner is still valid, i.e. we are still in the scope of the lifetime owner's regular lifetime.

withValidityAssertion

inline fun <R> KaLifetimeOwner.withValidityAssertion(action: () -> R): R

Executes action only if the KaLifetimeOwner is still valid and accessible. Otherwise, throws a validity exception based on the concrete violation.