From 2953fb3970f822b3c363b7b35c4e25dbe396c2e8 Mon Sep 17 00:00:00 2001 From: Dmitriy Dolovov Date: Tue, 17 Nov 2020 16:09:10 +0300 Subject: [PATCH] Fix lost nullability in C-interop type aliases ^KT-43427 --- .../native/interop/gen/StubIrMetadataEmitter.kt | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrMetadataEmitter.kt b/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrMetadataEmitter.kt index 8f5aa9e76cb..7ec9f8e9c8f 100644 --- a/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrMetadataEmitter.kt +++ b/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrMetadataEmitter.kt @@ -326,6 +326,11 @@ private class MappingExtensions( Flag.Type.IS_NULLABLE.takeIf { nullable } ) + val AbbreviatedType.expandedTypeFlags: Flags + get() = flagsOfNotNull( + Flag.Type.IS_NULLABLE.takeIf { isEffectivelyNullable() } + ) + val TypealiasStub.flags: Flags get() = flagsOfNotNull( Flag.IS_PUBLIC @@ -357,6 +362,13 @@ private class MappingExtensions( Flag.HAS_ANNOTATIONS.takeIf { annotations.isNotEmpty() } ) or visibility.flags + private tailrec fun StubType.isEffectivelyNullable(): Boolean = + when { + nullable -> true + this !is AbbreviatedType -> false + else -> underlyingType.isEffectivelyNullable() + } + fun AnnotationStub.map(): KmAnnotation { fun Pair.asAnnotationArgument() = (first to KmAnnotationArgument.StringValue(second)).takeIf { second.isNotEmpty() } @@ -467,8 +479,7 @@ private class MappingExtensions( km.arguments += typeArguments } if (shouldExpandTypeAliases) { - // Abbreviated and expanded types have the same nullability. - KmType(flags).also { km -> + KmType(expandedTypeFlags).also { km -> km.abbreviatedType = abbreviatedType val kmUnderlyingType = underlyingType.map(true) km.arguments += kmUnderlyingType.arguments