From f6356199d3c61d8e29a1970b021745745bf98979 Mon Sep 17 00:00:00 2001 From: Sergey Igushkin Date: Wed, 5 Aug 2020 20:40:28 +0400 Subject: [PATCH] Fix typealias usage nullability and annotations lost in deserialization Issue #KT-40824 Fixed --- .../annotationOnTypeUseInTypeAlias.kt | 37 +++++++++++++++++++ ...mpileKotlinAgainstKotlinTestGenerated.java | 5 +++ ...mpileKotlinAgainstKotlinTestGenerated.java | 5 +++ .../deserialization/TypeDeserializer.kt | 8 +++- 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/compileKotlinAgainstKotlin/annotationOnTypeUseInTypeAlias.kt diff --git a/compiler/testData/compileKotlinAgainstKotlin/annotationOnTypeUseInTypeAlias.kt b/compiler/testData/compileKotlinAgainstKotlin/annotationOnTypeUseInTypeAlias.kt new file mode 100644 index 00000000000..e215172a8db --- /dev/null +++ b/compiler/testData/compileKotlinAgainstKotlin/annotationOnTypeUseInTypeAlias.kt @@ -0,0 +1,37 @@ +// TARGET_BACKEND: JVM +// WITH_REFLECT +// FILE: A.kt +@Target(AnnotationTarget.TYPE) +annotation class Anno(val value: String) + +class Foo + +typealias MyFoo = Foo +typealias MyMaybeFoo = Foo? + +class C(val t: T) + +typealias MyCMyFoo = C<@Anno("OK") MyFoo?> +typealias MyCMaybeFoo = C<@Anno("OK") MyMaybeFoo> + +// FILE: B.kt +fun testMyFoo(myc: MyCMyFoo) {} +fun testMyMaybeFoo(mycmyb: MyCMaybeFoo) {} + +fun box(): String { + testMyFoo(C(null)) + testMyMaybeFoo(C(null)) + + for (fn in listOf(::testMyFoo, ::testMyMaybeFoo)) { + val mycType = fn.parameters.single().type + val argumentType = mycType.arguments.single().type!! + if (!argumentType.isMarkedNullable) + return "Fail on $fn: argument type should be seen as nullable" + + val annotations = argumentType.annotations + if (annotations.toString() != "[@Anno(value=OK)]") + return "Fail on $fn: $annotations" + } + + return "OK" +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstKotlinTestGenerated.java index 10b1492161a..c878917801c 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstKotlinTestGenerated.java @@ -38,6 +38,11 @@ public class CompileKotlinAgainstKotlinTestGenerated extends AbstractCompileKotl runTest("compiler/testData/compileKotlinAgainstKotlin/annotationInInterface.kt"); } + @TestMetadata("annotationOnTypeUseInTypeAlias.kt") + public void testAnnotationOnTypeUseInTypeAlias() throws Exception { + runTest("compiler/testData/compileKotlinAgainstKotlin/annotationOnTypeUseInTypeAlias.kt"); + } + @TestMetadata("annotationsOnTypeAliases.kt") public void testAnnotationsOnTypeAliases() throws Exception { runTest("compiler/testData/compileKotlinAgainstKotlin/annotationsOnTypeAliases.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstKotlinTestGenerated.java index b0fe25b73dd..f16ce86ba6b 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstKotlinTestGenerated.java @@ -38,6 +38,11 @@ public class IrCompileKotlinAgainstKotlinTestGenerated extends AbstractIrCompile runTest("compiler/testData/compileKotlinAgainstKotlin/annotationInInterface.kt"); } + @TestMetadata("annotationOnTypeUseInTypeAlias.kt") + public void testAnnotationOnTypeUseInTypeAlias() throws Exception { + runTest("compiler/testData/compileKotlinAgainstKotlin/annotationOnTypeUseInTypeAlias.kt"); + } + @TestMetadata("annotationsOnTypeAliases.kt") public void testAnnotationsOnTypeAliases() throws Exception { runTest("compiler/testData/compileKotlinAgainstKotlin/annotationsOnTypeAliases.kt"); diff --git a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt index a94b2acc996..df563bb603a 100644 --- a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt +++ b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt @@ -92,8 +92,12 @@ class TypeDeserializer( val declarationDescriptor = constructor.declarationDescriptor val simpleType = when { - expandTypeAliases && declarationDescriptor is TypeAliasDescriptor -> - with(KotlinTypeFactory) { declarationDescriptor.computeExpandedType(arguments) } + expandTypeAliases && declarationDescriptor is TypeAliasDescriptor -> { + val expandedType = with(KotlinTypeFactory) { declarationDescriptor.computeExpandedType(arguments) } + expandedType + .makeNullableAsSpecified(expandedType.isNullable() || proto.nullable) + .replaceAnnotations(Annotations.create(annotations + expandedType.annotations)) + } Flags.SUSPEND_TYPE.get(proto.flags) -> createSuspendFunctionType(annotations, constructor, arguments, proto.nullable) else ->