From b9872b76511191ebd3106d06d18e69d78de10e91 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Wed, 13 Sep 2017 12:57:19 +0300 Subject: [PATCH] Render annotations in suspend functions in single modifier list (KT-20185) #KT-20185 Fixed --- .../modifierApplicability.kt | 11 +++++++++ .../modifierApplicability.txt | 19 +++++++++++++++ .../kotlin/renderer/DescriptorRendererImpl.kt | 23 ++++++++++++------- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendFunctionType/modifierApplicability.kt b/compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendFunctionType/modifierApplicability.kt index c2c11605814..4950fc67372 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendFunctionType/modifierApplicability.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendFunctionType/modifierApplicability.kt @@ -16,6 +16,12 @@ typealias Test9 = suspend (() -> Unit) -> Unit typealias Test10 = suspend (suspend () -> Unit) -> Unit typealias Test11 = suspend () -> (suspend () -> Unit) typealias Test12 = suspend (suspend (() -> Unit)) -> Unit +typealias Test13 = @A() suspend () -> Unit +typealias Test14 = @A suspend () -> Unit +typealias Test15 = (@A() suspend () -> Unit)? +typealias Test16 = (@A suspend () -> Unit)? +typealias Test17 = @A suspend RS.() -> Unit +typealias Test18 = (suspend () -> Unit)? interface Supertype1 : suspend () -> Unit { @@ -24,3 +30,8 @@ interface Supertype1 : suspend () -> Unit< interface Supertype2 : suspend String.() -> Unit { } + +@Target(AnnotationTarget.TYPE) +annotation class A + +interface RS diff --git a/compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendFunctionType/modifierApplicability.txt b/compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendFunctionType/modifierApplicability.txt index ec4abc4f4ca..d46a70a00a8 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendFunctionType/modifierApplicability.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendFunctionType/modifierApplicability.txt @@ -1,5 +1,18 @@ package +@kotlin.annotation.Target(allowedTargets = {AnnotationTarget.TYPE}) public final annotation class A : kotlin.Annotation { + public constructor A() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public interface RS { + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + public interface SAM { public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int @@ -25,6 +38,12 @@ public typealias Test1 = suspend () -> kotlin.Unit public typealias Test10 = suspend (suspend () -> kotlin.Unit) -> kotlin.Unit public typealias Test11 = suspend () -> suspend () -> kotlin.Unit public typealias Test12 = suspend (suspend () -> kotlin.Unit) -> kotlin.Unit +public typealias Test13 = @A() suspend () -> kotlin.Unit +public typealias Test14 = @A() suspend () -> kotlin.Unit +public typealias Test15 = (@A() suspend () -> kotlin.Unit)? +public typealias Test16 = (@A() suspend () -> kotlin.Unit)? +public typealias Test17 = (@A() suspend RS.() -> kotlin.Unit) +public typealias Test18 = (suspend () -> kotlin.Unit)? public typealias Test2 = suspend kotlin.Int.() -> kotlin.Unit public typealias Test3 = () -> kotlin.Unit public typealias Test4 = Action diff --git a/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt b/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt index 7d7eeaed00e..fa8c45db956 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt @@ -309,22 +309,29 @@ internal class DescriptorRendererImpl( } val hasAnnotations = length != lengthBefore + val isSuspend = type.isSuspendFunctionType val isNullable = type.isMarkedNullable val receiverType = type.getReceiverTypeFromFunctionType() val needParenthesis = isNullable || (hasAnnotations && receiverType != null) + if (needParenthesis) { + if (isSuspend) { + insert(lengthBefore, '(') + } + else { + if (hasAnnotations) { + assert(last() == ' ') + if (get(lastIndex - 1) != ')') { + // last annotation rendered without parenthesis - need to add them otherwise parsing will be incorrect + insert(lastIndex, "()") + } + } - if (needParenthesis && hasAnnotations) { - assert(last() == ' ') - if (get(lastIndex - 1) != ')') { - // last annotation rendered without parenthesis - need to add them otherwise parsing will be incorrect - insert(lastIndex, "()") + append("(") } } - if (needParenthesis) append("(") - - renderModifier(this, type.isSuspendFunctionType, "suspend") + renderModifier(this, isSuspend, "suspend") if (receiverType != null) { val surroundReceiver = shouldRenderAsPrettyFunctionType(receiverType) && !receiverType.isMarkedNullable ||