Skip to content

KaSmartCastSource

Either a symbol for which a smart cast is generated, or one of its receivers.

Properties

dispatchReceiver

The required dispatch receiver for this path component, if any.

The symbol only participates in the smart cast if its dispatch receiver is the same as this one. E.g.:

class Holder(val value: Any)

fun test(holder: Holder) {
    if (holder.value is String) {
        holder.value.length  // Context position
    }
}

In the example above, the holder parameter is a required dispatch receiver for value. If any other receiver is passed, such as Holder().value, the smart cast is not generated.

extensionReceiver

The required extension receiver for this path component, if any.

originalType

abstract val originalType: KaType

The original type of the symbol.

symbol

abstract val symbol: KaDeclarationSymbol

A declaration participating in the smart cast path.

Smart casts to the same declaration can be allowed or prohibited, depending on how that declaration is accessed. In the following example, the symbol will be val value: Any. However, a smart cast will only be produced for a.value.

class Holder(val value: Any)

fun test(a: Holder, b: Holder) {
    if (a.value is String) {
        a.value.length  // OK
        b.value.length  // compilation error
    }
}

For a.value from the example above, a KaSmartCastSource is generated whose symbol points to val value: Any (a property), and whose dispatchReceiver is the a: Holder value parameter.

For this, the symbol may be either a org.jetbrains.kotlin.analysis.api.symbols.KaReceiverParameterSymbol (for an extension receiver parameter) or a org.jetbrains.kotlin.analysis.api.symbols.KaClassSymbol (if the cast occurs right inside a class).

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.