From 9cf5ac1fbdbb7be2619ecc8de558a4981a91b490 Mon Sep 17 00:00:00 2001 From: Mark Punzalan Date: Wed, 7 Apr 2021 22:11:07 +0000 Subject: [PATCH] FIR: Render "?" on nullable function types. --- .../kotlinSignature/MethodWithFunctionTypes.txt | 2 +- .../testData/loadCompiledKotlin/type/SuspendFunction.txt | 8 ++++++++ .../resolve/callResolution/safeCallOnTypeAlias.fir.txt | 2 +- .../testData/resolve/delegates/kt41982.fir.txt | 2 +- .../expresssions/invoke/propertyWithExtensionType.fir.txt | 8 ++++---- .../testData/resolve/inference/kt41989.fir.txt | 4 ++-- .../testData/resolve/lambdaArgInScopeFunction.fir.txt | 4 ++-- .../resolve/smartcasts/lambdas/smartcastOnLambda.fir.txt | 2 +- .../src/org/jetbrains/kotlin/fir/types/TypeRenderer.kt | 3 ++- .../loadJava/compiledKotlin/type/SuspendFunction.kt | 4 ++++ .../loadJava/compiledKotlin/type/SuspendFunction.txt | 4 ++++ 11 files changed, 30 insertions(+), 13 deletions(-) diff --git a/compiler/fir/analysis-tests/testData/loadCompiledKotlin/fromLoadJava/kotlinSignature/MethodWithFunctionTypes.txt b/compiler/fir/analysis-tests/testData/loadCompiledKotlin/fromLoadJava/kotlinSignature/MethodWithFunctionTypes.txt index cf52d359cce..c69db00847b 100644 --- a/compiler/fir/analysis-tests/testData/loadCompiledKotlin/fromLoadJava/kotlinSignature/MethodWithFunctionTypes.txt +++ b/compiler/fir/analysis-tests/testData/loadCompiledKotlin/fromLoadJava/kotlinSignature/MethodWithFunctionTypes.txt @@ -1,5 +1,5 @@ public open class MethodWithFunctionTypes : R|kotlin/Any| { - public open fun foo(f: R|(kotlin/String?) -> kotlin/String|): R|kotlin/String.() -> kotlin/String?| + public open fun foo(f: R|(kotlin/String?) -> kotlin/String|): R|(kotlin/String.() -> kotlin/String?)?| public constructor(): R|test/MethodWithFunctionTypes| diff --git a/compiler/fir/analysis-tests/testData/loadCompiledKotlin/type/SuspendFunction.txt b/compiler/fir/analysis-tests/testData/loadCompiledKotlin/type/SuspendFunction.txt index 77d7d006d6d..22915426ec5 100644 --- a/compiler/fir/analysis-tests/testData/loadCompiledKotlin/type/SuspendFunction.txt +++ b/compiler/fir/analysis-tests/testData/loadCompiledKotlin/type/SuspendFunction.txt @@ -1,8 +1,16 @@ public final fun test1(): R|suspend () -> kotlin/Unit| +public final fun test1N(): R|(suspend () -> kotlin/Unit)?| + public final fun test2(): R|suspend kotlin/Int.() -> kotlin/Int| +public final fun test2N(): R|(suspend kotlin/Int.() -> kotlin/Int)?| + public final fun test3(): R|kotlin/collections/List>| +public final fun test3N(): R|kotlin/collections/List?>| + public final fun test4(): R|suspend () -> kotlin/coroutines/SuspendFunction0| +public final fun test4N(): R|(suspend () -> kotlin/coroutines/SuspendFunction0?)?| + diff --git a/compiler/fir/analysis-tests/testData/resolve/callResolution/safeCallOnTypeAlias.fir.txt b/compiler/fir/analysis-tests/testData/resolve/callResolution/safeCallOnTypeAlias.fir.txt index af731e93fcb..ce747907fbe 100644 --- a/compiler/fir/analysis-tests/testData/resolve/callResolution/safeCallOnTypeAlias.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolve/callResolution/safeCallOnTypeAlias.fir.txt @@ -1,5 +1,5 @@ FILE: safeCallOnTypeAlias.kt - public final typealias MyTypeAlias = R|() -> kotlin/String?| + public final typealias MyTypeAlias = R|(() -> kotlin/String?)?| public final fun foo(x: R|MyTypeAlias|): R|kotlin/Unit| { R|/x|?.{ $subj$.R|kotlin/let| kotlin/String?|, R|kotlin/String?|>( = let@fun (y: R|() -> kotlin/String?|): R|kotlin/String?| { ^ R|/y|.R|SubstitutionOverride|()?.{ $subj$.R|kotlin/let|( = let@fun (result: R|kotlin/String|): R|kotlin/String| { diff --git a/compiler/fir/analysis-tests/testData/resolve/delegates/kt41982.fir.txt b/compiler/fir/analysis-tests/testData/resolve/delegates/kt41982.fir.txt index cb7c6ae120f..68989da9f2a 100644 --- a/compiler/fir/analysis-tests/testData/resolve/delegates/kt41982.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolve/delegates/kt41982.fir.txt @@ -18,7 +18,7 @@ FILE: kt41982.kt } } - public final fun |> R|Type|.long(initializer: R|() -> kotlin/Long?| = Null(null)): R|Delegate| { + public final fun |> R|Type|.long(initializer: R|(() -> kotlin/Long?)?| = Null(null)): R|Delegate| { ^long Null(null)!! } public final class Test : R|DelegateProvider| { diff --git a/compiler/fir/analysis-tests/testData/resolve/expresssions/invoke/propertyWithExtensionType.fir.txt b/compiler/fir/analysis-tests/testData/resolve/expresssions/invoke/propertyWithExtensionType.fir.txt index 8cdce0b7980..449b274fd06 100644 --- a/compiler/fir/analysis-tests/testData/resolve/expresssions/invoke/propertyWithExtensionType.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolve/expresssions/invoke/propertyWithExtensionType.fir.txt @@ -1,11 +1,11 @@ FILE: propertyWithExtensionType.kt public final class A : R|kotlin/Any| { - public constructor(x: R|kotlin/String.() -> kotlin/Unit|, y: R|kotlin/String.() -> kotlin/Int|): R|A| { + public constructor(x: R|(kotlin/String.() -> kotlin/Unit)?|, y: R|kotlin/String.() -> kotlin/Int|): R|A| { super() } - public final val x: R|kotlin/String.() -> kotlin/Unit| = R|/x| - public get(): R|kotlin/String.() -> kotlin/Unit| + public final val x: R|(kotlin/String.() -> kotlin/Unit)?| = R|/x| + public get(): R|(kotlin/String.() -> kotlin/Unit)?| public final val y: R|kotlin/String.() -> kotlin/Int| = R|/y| public get(): R|kotlin/String.() -> kotlin/Int| @@ -14,7 +14,7 @@ FILE: propertyWithExtensionType.kt public final fun test(a: R|A|): R|kotlin/Unit| { when () { !=(R|/a|.R|/A.x|, Null(null)) -> { - lval b: R|kotlin/String.() -> kotlin/Unit| = R|/a|.R|/A.x| + lval b: R|(kotlin/String.() -> kotlin/Unit)?| = R|/a|.R|/A.x| R|/b|.R|SubstitutionOverride|(String()) } } diff --git a/compiler/fir/analysis-tests/testData/resolve/inference/kt41989.fir.txt b/compiler/fir/analysis-tests/testData/resolve/inference/kt41989.fir.txt index 7621d6ec34d..1bedc25202f 100644 --- a/compiler/fir/analysis-tests/testData/resolve/inference/kt41989.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolve/inference/kt41989.fir.txt @@ -8,8 +8,8 @@ FILE: kt41989.kt } public abstract interface C : R|B| { - public open val lineCellStyle: R|A.() -> kotlin/Unit| - public get(): R|A.() -> kotlin/Unit| { + public open val lineCellStyle: R|(A.() -> kotlin/Unit)?| + public get(): R|(A.() -> kotlin/Unit)?| { ^ when () { R|/cond|() -> { fun R|A|.(): R|kotlin/Unit| { diff --git a/compiler/fir/analysis-tests/testData/resolve/lambdaArgInScopeFunction.fir.txt b/compiler/fir/analysis-tests/testData/resolve/lambdaArgInScopeFunction.fir.txt index 658873e3976..c202a84bbf1 100644 --- a/compiler/fir/analysis-tests/testData/resolve/lambdaArgInScopeFunction.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolve/lambdaArgInScopeFunction.fir.txt @@ -21,14 +21,14 @@ FILE: lambdaArgInScopeFunction.kt this@R|special/anonymous|.R|/_|() } ) - lval lambda: R|() -> KotlinClass| = R|/kotlinClass|?.{ $subj$.R|kotlin/let| KotlinClass|>( = let@fun (it: R|KotlinClass|): R|() -> KotlinClass| { + lval lambda: R|(() -> KotlinClass)?| = R|/kotlinClass|?.{ $subj$.R|kotlin/let| KotlinClass|>( = let@fun (it: R|KotlinClass|): R|() -> KotlinClass| { ^ let@fun (): R|KotlinClass| { ^ R|/it| } } ) } - R|/lambda|.R|/checkType| KotlinClass|>( = checkType@fun R|CheckTypeInv?>|.(): R|kotlin/Unit| { + R|/lambda|.R|/checkType| KotlinClass)?|>( = checkType@fun R|CheckTypeInv?>|.(): R|kotlin/Unit| { this@R|special/anonymous|.# KotlinClass?|>() } ) diff --git a/compiler/fir/analysis-tests/testData/resolve/smartcasts/lambdas/smartcastOnLambda.fir.txt b/compiler/fir/analysis-tests/testData/resolve/smartcasts/lambdas/smartcastOnLambda.fir.txt index 7dcfd77ab23..6fb794e4ca0 100644 --- a/compiler/fir/analysis-tests/testData/resolve/smartcasts/lambdas/smartcastOnLambda.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolve/smartcasts/lambdas/smartcastOnLambda.fir.txt @@ -1,5 +1,5 @@ FILE: smartcastOnLambda.kt - public final fun test(func: R|() -> kotlin/Unit|): R|kotlin/Unit| { + public final fun test(func: R|(() -> kotlin/Unit)?|): R|kotlin/Unit| { when () { !=(R|/func|, Null(null)) -> { R|/func|.R|SubstitutionOverride|() diff --git a/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/TypeRenderer.kt b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/TypeRenderer.kt index ab949b819b4..6698a77c771 100644 --- a/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/TypeRenderer.kt +++ b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/TypeRenderer.kt @@ -69,7 +69,7 @@ fun ConeKotlinType.renderFunctionType( kind: FunctionClassKind?, isExtension: Boolean, renderType: ConeTypeProjection.() -> String = { render() } ): String { if (!kind.withPrettyRender()) return renderType() - return buildString { + val renderedType = buildString { if (kind == FunctionClassKind.SuspendFunction) { append("suspend ") } @@ -88,6 +88,7 @@ fun ConeKotlinType.renderFunctionType( append(" -> ") append(returnType.renderType()) } + return if (isMarkedNullable) "($renderedType)?" else renderedType } @OptIn(ExperimentalContracts::class) diff --git a/compiler/testData/loadJava/compiledKotlin/type/SuspendFunction.kt b/compiler/testData/loadJava/compiledKotlin/type/SuspendFunction.kt index bc8c8916f2f..d85d991990b 100644 --- a/compiler/testData/loadJava/compiledKotlin/type/SuspendFunction.kt +++ b/compiler/testData/loadJava/compiledKotlin/type/SuspendFunction.kt @@ -3,6 +3,10 @@ package test fun test1(): suspend () -> Unit = null!! +fun test1N(): (suspend () -> Unit)? = null fun test2(): suspend Int.() -> Int = null!! +fun test2N(): (suspend Int.() -> Int)? = null fun test3(): List Unit> = null!! +fun test3N(): List<(suspend () -> Unit)?> = null!! fun test4(): suspend () -> (suspend () -> Unit) = null!! +fun test4N(): (suspend () -> (suspend () -> Unit)?)? = null diff --git a/compiler/testData/loadJava/compiledKotlin/type/SuspendFunction.txt b/compiler/testData/loadJava/compiledKotlin/type/SuspendFunction.txt index ea19fcf51b8..ac1d8ac0168 100644 --- a/compiler/testData/loadJava/compiledKotlin/type/SuspendFunction.txt +++ b/compiler/testData/loadJava/compiledKotlin/type/SuspendFunction.txt @@ -1,6 +1,10 @@ package test public fun test1(): suspend () -> kotlin.Unit +public fun test1N(): (suspend () -> kotlin.Unit)? public fun test2(): suspend kotlin.Int.() -> kotlin.Int +public fun test2N(): (suspend kotlin.Int.() -> kotlin.Int)? public fun test3(): kotlin.collections.List kotlin.Unit> +public fun test3N(): kotlin.collections.List<(suspend () -> kotlin.Unit)?> public fun test4(): suspend () -> suspend () -> kotlin.Unit +public fun test4N(): (suspend () -> (suspend () -> kotlin.Unit)?)?