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¶
abstract val KtExpression.expressionType: KaType?
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).
-
Unitfor statements (e.g., assignments, loops). -
nullforKtExpressions 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¶
abstract val KtFunction.functionType: KaType
The function type of the given KtFunction.
For a regular function, the result is a kotlin.FunctionN<P1, P2, ..., R> type where:
-
Nis the number of value parameters in the function. -
Pxis the type of the x-th value parameter. -
Ris 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¶
abstract val KtExpression.isDefinitelyNotNull: Boolean
isDefinitelyNull¶
abstract val KtExpression.isDefinitelyNull: Boolean
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¶
abstract val KtDeclarationWithReturnType.returnType: KaType
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¶
inline fun KaLifetimeOwner.assertIsValidAndAccessible()
isValid¶
fun KaLifetimeOwner.isValid(): Boolean
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.