Skip to content

KaExpressionTypeProvider

@SubclassOptInRequired(markerClass = [KaSessionComponentImplementationDetail::class])
interface KaExpressionTypeProvider : KaSessionComponent

Inheritors

Properties

expectedType

abstract val PsiElement.expectedType: KaType?

The expected KaType for the given PsiElement if it is an expression, or null if the element does not have an expected type. The expected type represents the type that is expected for an expression at a specific location in the code.

See expressionType for a discussion about the expression type vs. the expected type.

expressionType

The type of the given KtExpression, or null if it does not have a type.

In particular:

  • A not-null type for valued expressions (e.g., a variable, a function call, a lambda expression).

  • Unit for statements (e.g., assignments, loops).

  • null for KtExpressions that are not a part of the expression tree (e.g., expressions in import or package statements).

Expression vs. expected type

The Analysis API distinguishes between an expression's type and its expected type, which represent different aspects of the Kotlin type system.

The expression type represents the actual type of an expression after it has been resolved. It reflects the result of type inference, smart casts, and implicit conversions.

The expected type represents the type that is expected for an expression at a specific location in the code. This is determined by the context in which the expression appears, such as a variable type for its initializer, or a parameter type for a function call.

functionType

The function type of the given KtFunction.

For a regular function, the result is a kotlin.FunctionN<P1, P2, ..., R> type where:

  • N is the number of value parameters in the function.

  • Px is the type of the x-th value parameter.

  • R is the return type of the function.

Depending on the function's attributes, such as suspend or reflective access, a different functional type such as SuspendFunction, KFunction, or KSuspendFunction will be constructed.

isDefinitelyNotNull

Whether this expression is definitely not null.

See also

isDefinitelyNull

Whether this expression is definitely null, based on the declared nullability and smart cast types derived from data-flow analysis facts.

Only nullability from stable smart casts is considered. See the smart cast sink stability section of the Kotlin specification for more information.

Examples

public fun <T : Any> foo(t: T, nt: T?, s: String, ns: String?) {
  t     // t.isDefinitelyNull()  == false && t.isDefinitelyNotNull()  == true
  nt    // nt.isDefinitelyNull() == false && nt.isDefinitelyNotNull() == false
  s     // s.isDefinitelyNull()  == false && s.isDefinitelyNotNull()  == true
  ns    // ns.isDefinitelyNull() == false && ns.isDefinitelyNotNull() == false

  if (ns != null) {
    ns  // ns.isDefinitelyNull() == false && ns.isDefinitelyNotNull() == true
  } else {
    ns  // ns.isDefinitelyNull() == true  && ns.isDefinitelyNotNull() == false
  }

  ns!!  // From this point on: ns.isDefinitelyNull() == false && ns.isDefinitelyNotNull() == true
}

returnType

The return type of the given KtDeclarationWithReturnType.

Note: For a vararg foo: T parameter, the resulting type is the full Array<out T> type (unlike KaValueParameterSymbol.returnType, which is T).

The reasoning behind this is that KaCallableSymbol.returnType sees the parameter from the declaration's semantic perspective, representing the signature of the parameter, which contains just the element type. In this paradigm, vararg arrays are constructed separately under the hood.

At the same time, KtDeclaration.returnType from KaExpressionTypeProvider represents a use-site perspective, which has to desugar vararg parameters because they are consumed as array types.

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.