From 9e2271399e7d23279110f11976d2d4d52cf4b041 Mon Sep 17 00:00:00 2001 From: Anastasiya Shadrina Date: Mon, 22 Nov 2021 20:04:50 +0700 Subject: [PATCH] [FE] Render context receivers for functions, properties, and classes --- .../kotlin/psi2ir/generators/CallGenerator.kt | 1 + .../method/contextualFunctions/Signature.txt | 2 +- .../contextReceivers/ambiguityInGroup.txt | 4 +- .../fromKEEP/autoCloseScope.txt | 2 +- .../contextReceivers/fromKEEP/canvas.txt | 4 +- .../contextReceivers/fromKEEP/compareTo.txt | 4 +- .../contextReceivers/fromKEEP/dp.txt | 2 +- .../fromKEEP/functionalType.txt | 4 +- .../fromKEEP/iterableClass.txt | 2 +- .../contextReceivers/fromKEEP/json.txt | 4 +- .../fromKEEP/loggingContext.txt | 2 +- .../contextReceivers/fromKEEP/monoidSum.txt | 2 +- .../contextReceivers/fromKEEP/transaction.txt | 2 +- .../contextReceivers/fromKEEP/userStorage.txt | 2 +- .../contextReceivers/functionalType.txt | 8 ++-- .../contextReceivers/genericOuterClass.txt | 4 +- .../contextReceivers/insideDeclaration.txt | 2 +- .../labelsFromClassNameForbidden.txt | 4 +- .../extensions/contextReceivers/lazy.txt | 6 +-- .../contextReceivers/manyReceivers.txt | 2 +- .../contextReceivers/noBackingField.txt | 6 +-- .../noContextReceiversOnInlineClasses.txt | 2 +- .../contextReceivers/noExplicitReceiver.txt | 6 +-- .../contextReceivers/outerClass.txt | 2 +- .../contextReceivers/overloadPriority.txt | 2 +- .../contextReceivers/overloading.txt | 4 +- .../contextReceivers/plusMatrix.txt | 2 +- .../contextReceivers/superWithContext.txt | 2 +- .../contextReceivers/syntaxError.txt | 2 +- .../contextReceivers/thisIdentifierInfo.txt | 2 +- .../contextReceivers/thisWithCustomLabel.txt | 4 +- .../thisWithReceiverLabelsClasses.txt | 2 +- .../thisWithReceiverLabelsFunctions.txt | 9 ++--- .../thisWithReceiverLabelsProperties.txt | 6 +-- .../typeParameterAsContextReceiver.txt | 2 +- .../contextReceivers/typeParameterized.txt | 2 +- .../typeParameterizedList.txt | 2 +- .../contextReceivers/unsupported.txt | 8 ++-- .../contextReceivers/withExplicitReceiver.txt | 2 +- .../withExplicitReceiverError.txt | 2 +- .../kotlin/renderer/DescriptorRendererImpl.kt | 37 ++++++++++++++++--- 41 files changed, 97 insertions(+), 72 deletions(-) diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/CallGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/CallGenerator.kt index 777cb32f729..76be1cbd365 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/CallGenerator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/CallGenerator.kt @@ -168,6 +168,7 @@ class CallGenerator(statementGenerator: StatementGenerator) : StatementGenerator putTypeArguments(call.typeArguments) { it.toIrType() } this.dispatchReceiver = dispatchReceiver?.load() this.extensionReceiver = extensionReceiver?.load() + contextReceiversCount = contextReceivers.size } addParametersToCall(startOffset, endOffset, call, irCall, context.irBuiltIns.unitType, contextReceivers.map { it.load() }) } diff --git a/compiler/testData/compileJavaAgainstKotlin/method/contextualFunctions/Signature.txt b/compiler/testData/compileJavaAgainstKotlin/method/contextualFunctions/Signature.txt index 37e6a4e5811..2557a8cc7f4 100644 --- a/compiler/testData/compileJavaAgainstKotlin/method/contextualFunctions/Signature.txt +++ b/compiler/testData/compileJavaAgainstKotlin/method/contextualFunctions/Signature.txt @@ -1,6 +1,6 @@ package test -public fun test.R.f(/*0*/ test.P1, /*1*/ test.P2): kotlin.Unit +context(test.C1, test.C2) public fun test.R.f(/*0*/ test.P1, /*1*/ test.P2): kotlin.Unit public interface C1 { } diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/ambiguityInGroup.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/ambiguityInGroup.txt index 6a76d813ae4..9aef99132b0 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/ambiguityInGroup.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/ambiguityInGroup.txt @@ -1,7 +1,7 @@ package -public fun supertypeContextual(): kotlin.Unit -public fun test(): kotlin.Unit +context(Common) public fun supertypeContextual(): kotlin.Unit +context(C1, C2) public fun test(): kotlin.Unit public fun Common.supertypeExtension(): kotlin.Unit public fun T.supertypeExtensionGeneric(): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/autoCloseScope.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/autoCloseScope.txt index dc0fc2f8c01..e3d55125b5d 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/autoCloseScope.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/autoCloseScope.txt @@ -2,7 +2,7 @@ package public fun test(): kotlin.Unit public fun withAutoClose(/*0*/ block: context(AutoCloseScope) () -> kotlin.Unit): kotlin.Unit -public fun File.open(): InputStream +context(AutoCloseScope) public fun File.open(): InputStream public interface AutoCloseScope { public abstract fun close(): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/canvas.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/canvas.txt index 11b4d2dab9b..0998f2ef292 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/canvas.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/canvas.txt @@ -10,7 +10,7 @@ public interface Canvas { public final class Circle : Shape { public constructor Circle() - public open override /*1*/ fun draw(): kotlin.Unit + context(Canvas) public open override /*1*/ fun draw(): kotlin.Unit 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 @@ -24,7 +24,7 @@ public object MyCanvas : Canvas { } public interface Shape { - public abstract fun draw(): kotlin.Unit + context(Canvas) public abstract fun draw(): kotlin.Unit 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 diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/compareTo.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/compareTo.txt index 98ae4788e73..c341303fe5e 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/compareTo.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/compareTo.txt @@ -1,8 +1,8 @@ package -public val Pair.max: T +context(kotlin.Comparator /* = java.util.Comparator */) public val Pair.max: T public fun test(): kotlin.Unit -public infix operator fun T.compareTo(/*0*/ other: T): kotlin.Int +context(kotlin.Comparator /* = java.util.Comparator */) public infix operator fun T.compareTo(/*0*/ other: T): kotlin.Int public final data class Pair { public constructor Pair(/*0*/ first: A, /*1*/ second: B) diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/dp.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/dp.txt index 30bb109131a..86c93e9b54d 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/dp.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/dp.txt @@ -1,6 +1,6 @@ package -public val kotlin.Int.dp: kotlin.Int +context(View) public val kotlin.Int.dp: kotlin.Int public fun h(): kotlin.Unit public fun View.f(): kotlin.Unit public fun kotlin.Int.g(/*0*/ v: View): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/functionalType.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/functionalType.txt index 2049e3313bd..0850c668373 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/functionalType.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/functionalType.txt @@ -1,9 +1,9 @@ package -public fun bar(/*0*/ receiver: Receiver, /*1*/ p: Param): kotlin.Unit +context(Context) public fun bar(/*0*/ receiver: Receiver, /*1*/ p: Param): kotlin.Unit public fun foo(/*0*/ context: Context, /*1*/ receiver: Receiver, /*2*/ p: Param): kotlin.Unit public fun main(): kotlin.Unit -public fun Receiver.baz(/*0*/ p: Param): kotlin.Unit +context(Context) public fun Receiver.baz(/*0*/ p: Param): kotlin.Unit public final class Context { public constructor Context() diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/iterableClass.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/iterableClass.txt index e920c355098..3776256528d 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/iterableClass.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/iterableClass.txt @@ -2,5 +2,5 @@ package public fun listOf(/*0*/ vararg items: T /*kotlin.Array*/): kotlin.collections.List public fun test(): kotlin.Unit -public fun C.iterator(/*0*/ any: kotlin.Any?): kotlin.collections.Iterator +context((IterableClass /* = (C) -> kotlin.collections.Iterator */)) public fun C.iterator(/*0*/ any: kotlin.Any?): kotlin.collections.Iterator public typealias IterableClass = (C) -> kotlin.collections.Iterator diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/json.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/json.txt index f625fa28a58..3f07d047f8e 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/json.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/json.txt @@ -2,8 +2,8 @@ package public fun json(/*0*/ build: JSONObject.() -> kotlin.Unit): JSONObject public fun test(): kotlin.Unit -public infix fun kotlin.String.by(/*0*/ build: JSONObject.() -> kotlin.Unit): kotlin.Unit -public infix fun kotlin.String.by(/*0*/ value: kotlin.Any): kotlin.Unit +context(JSONObject) public infix fun kotlin.String.by(/*0*/ build: JSONObject.() -> kotlin.Unit): kotlin.Unit +context(JSONObject) public infix fun kotlin.String.by(/*0*/ value: kotlin.Any): kotlin.Unit public final class JSONObject { public constructor JSONObject() diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/loggingContext.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/loggingContext.txt index 6515c6caf7c..c48548a0bbc 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/loggingContext.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/loggingContext.txt @@ -1,6 +1,6 @@ package -public fun performSomeBusinessOperation(/*0*/ withParams: Params): kotlin.Unit +context(LoggingContext) public fun performSomeBusinessOperation(/*0*/ withParams: Params): kotlin.Unit public interface Logger { public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/monoidSum.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/monoidSum.txt index 83a8094fbb5..c14f0f94c97 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/monoidSum.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/monoidSum.txt @@ -3,7 +3,7 @@ package public fun listOf(/*0*/ vararg items: T /*kotlin.Array*/): kotlin.collections.List public fun test(): kotlin.Unit public inline fun kotlin.collections.Iterable.fold(/*0*/ initial: R, /*1*/ operation: (acc: R, T) -> R): R -public fun kotlin.collections.List.sum(): T +context(Monoid) public fun kotlin.collections.List.sum(): T public object IntMonoid : Monoid { private constructor IntMonoid() diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/transaction.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/transaction.txt index 85b313cacec..7310e779cf2 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/transaction.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/transaction.txt @@ -1,7 +1,7 @@ package public fun now(): kotlin.Any? -public fun updateUserSession(): kotlin.Unit +context(Transaction) public fun updateUserSession(): kotlin.Unit public final class Session { public constructor Session(/*0*/ lastAccess: kotlin.Any?) diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/userStorage.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/userStorage.txt index bcec22293f4..0f022df5f98 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/userStorage.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/fromKEEP/userStorage.txt @@ -1,6 +1,6 @@ package -public fun userInfo(/*0*/ name: kotlin.String): Storage.Info? +context(Logger, Storage) public fun userInfo(/*0*/ name: kotlin.String): Storage.Info? public final class Logger { public constructor Logger() diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/functionalType.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/functionalType.txt index 3721cf0b860..90cabe6d007 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/functionalType.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/functionalType.txt @@ -1,10 +1,10 @@ package -public fun f2(/*0*/ g: context(C) (Param) -> kotlin.Unit): kotlin.Unit -public fun f4(/*0*/ g: context(C) () -> kotlin.Unit): kotlin.Unit +context(C) public fun f2(/*0*/ g: context(C) (Param) -> kotlin.Unit): kotlin.Unit +context(C) public fun f4(/*0*/ g: context(C) () -> kotlin.Unit): kotlin.Unit public fun test(): kotlin.Unit -public fun R.f1(/*0*/ g: context(C) R.(Param) -> kotlin.Unit): kotlin.Unit -public fun R.f3(/*0*/ g: context(C) R.() -> kotlin.Unit): kotlin.Unit +context(C) public fun R.f1(/*0*/ g: context(C) R.(Param) -> kotlin.Unit): kotlin.Unit +context(C) public fun R.f3(/*0*/ g: context(C) R.() -> kotlin.Unit): kotlin.Unit public final class C { public constructor C() diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/genericOuterClass.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/genericOuterClass.txt index 5f5ef231120..f65eb658769 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/genericOuterClass.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/genericOuterClass.txt @@ -3,14 +3,14 @@ package public fun kotlin.collections.Collection.bar(): kotlin.Unit public fun kotlin.Int.foo(): kotlin.Unit -public final class A { +context(T) public final class A { 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 final class B { +context(kotlin.collections.Collection

) public final class B { public constructor B() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/insideDeclaration.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/insideDeclaration.txt index ae57f37d81e..83150546012 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/insideDeclaration.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/insideDeclaration.txt @@ -1,6 +1,6 @@ package -public fun B.bar(): kotlin.Unit +context(A) public fun B.bar(): kotlin.Unit public fun B.foo(): kotlin.Unit public final class A { diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/labelsFromClassNameForbidden.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/labelsFromClassNameForbidden.txt index bd1f2961a86..19c119020da 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/labelsFromClassNameForbidden.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/labelsFromClassNameForbidden.txt @@ -1,5 +1,5 @@ package -public val p: kotlin.String -public fun kotlin.Int.f(): kotlin.Unit +context(kotlin.String) public val p: kotlin.String +context(kotlin.String) public fun kotlin.Int.f(): kotlin.Unit public fun kotlin.collections.List.f(): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/lazy.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/lazy.txt index 19e33b75be5..63970e11779 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/lazy.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/lazy.txt @@ -1,9 +1,9 @@ package public fun f(/*0*/ lazy1: Lazy, /*1*/ lazy2: Lazy, /*2*/ lazyT: Lazy, /*3*/ lazyLazyT: Lazy>): kotlin.Unit -public fun test1(): kotlin.Unit -public fun Lazy.test2(): kotlin.Unit -public fun Lazy.test3(): kotlin.Unit +context(Lazy, Lazy) public fun test1(): kotlin.Unit +context(Lazy) public fun Lazy.test2(): kotlin.Unit +context(Lazy>) public fun Lazy.test3(): kotlin.Unit public interface Lazy { public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/manyReceivers.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/manyReceivers.txt index cae4526e3d9..e2c11705995 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/manyReceivers.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/manyReceivers.txt @@ -1,7 +1,7 @@ package public fun main(/*0*/ a: A, /*1*/ b: B, /*2*/ c: C): kotlin.Unit -public fun C.f(): kotlin.Unit +context(A, B) public fun C.f(): kotlin.Unit public final class A { public constructor A() diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/noBackingField.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/noBackingField.txt index bc22529e432..faa36702f6d 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/noBackingField.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/noBackingField.txt @@ -1,8 +1,8 @@ package -public val a: kotlin.Int = 1 -public var b: kotlin.Int -public val c: kotlin.Int +context(A) public val a: kotlin.Int = 1 +context(B, A) public var b: kotlin.Int +context(B, A) public val c: kotlin.Int public interface A { public abstract fun a(): kotlin.Int diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/noContextReceiversOnInlineClasses.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/noContextReceiversOnInlineClasses.txt index c2cd50a79b5..e5a64849dc6 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/noContextReceiversOnInlineClasses.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/noContextReceiversOnInlineClasses.txt @@ -7,7 +7,7 @@ public final class A { public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } -public final inline class B { +context(A) public final inline class B { public constructor B(/*0*/ x: kotlin.Int) public final val x: kotlin.Int public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/noExplicitReceiver.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/noExplicitReceiver.txt index c989406b311..878c980fc4c 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/noExplicitReceiver.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/noExplicitReceiver.txt @@ -1,8 +1,8 @@ package -public fun B.f(): kotlin.Unit -public fun B.g(): kotlin.Unit -public fun C.h(): kotlin.Unit +context(A) public fun B.f(): kotlin.Unit +context(A) public fun B.g(): kotlin.Unit +context(A) public fun C.h(): kotlin.Unit public fun A.q(/*0*/ b: B): kotlin.Unit public final class A { diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/outerClass.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/outerClass.txt index 422de668787..9777eaa58d8 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/outerClass.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/outerClass.txt @@ -2,7 +2,7 @@ package public fun f(/*0*/ outer: Outer): kotlin.Unit -public final class Inner { +context(Outer) public final class Inner { public constructor Inner(/*0*/ arg: kotlin.Any) public final fun bar(): kotlin.Int public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/overloadPriority.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/overloadPriority.txt index d288d6860cd..6fe4bffa25f 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/overloadPriority.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/overloadPriority.txt @@ -1,7 +1,7 @@ package +context(Context) public fun f(): kotlin.String public fun f(): kotlin.Any -public fun f(): kotlin.String public fun test(): kotlin.Unit public final class Context { diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/overloading.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/overloading.txt index 115b3584d23..2efacc51f89 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/overloading.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/overloading.txt @@ -1,5 +1,5 @@ package -public fun foo(): kotlin.Int -public fun foo(): kotlin.Int +context(kotlin.Int) public fun foo(): kotlin.Int +context(kotlin.Int, kotlin.String) public fun foo(): kotlin.Int public fun test(): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/plusMatrix.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/plusMatrix.txt index 4503a9541ae..62d7dc86644 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/plusMatrix.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/plusMatrix.txt @@ -1,6 +1,6 @@ package -public fun Matrix.plus(/*0*/ other: Matrix): Matrix +context(NumberOperations) public fun Matrix.plus(/*0*/ other: Matrix): Matrix public fun NumberOperations.plusMatrix(/*0*/ m1: Matrix, /*1*/ m2: Matrix): kotlin.Unit public final class Matrix { diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/superWithContext.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/superWithContext.txt index 6a867ecceba..fee9af80887 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/superWithContext.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/superWithContext.txt @@ -15,7 +15,7 @@ public final class B : A { public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String - public final inner class C { + context(Context) public final inner class C { public constructor C() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean public final fun g(): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/syntaxError.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/syntaxError.txt index 65a6ac47e1f..3921ad4f21c 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/syntaxError.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/syntaxError.txt @@ -1,3 +1,3 @@ package -public fun foo(): kotlin.Unit +context([ERROR : No type element]) public fun foo(): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/thisIdentifierInfo.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/thisIdentifierInfo.txt index 3e8ba698060..4597590a3c6 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/thisIdentifierInfo.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/thisIdentifierInfo.txt @@ -1,6 +1,6 @@ package -public fun f(): kotlin.Unit +context(A) public fun f(): kotlin.Unit public final class A { public constructor A(/*0*/ a: kotlin.String?) diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/thisWithCustomLabel.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/thisWithCustomLabel.txt index 2a33dd98d58..553e558023d 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/thisWithCustomLabel.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/thisWithCustomLabel.txt @@ -1,7 +1,7 @@ package -public val C.p: kotlin.Int -public fun f(): kotlin.Unit +context(B, A, A) public val C.p: kotlin.Int +context(A, A, B) public fun f(): kotlin.Unit public final class A { public constructor A(/*0*/ a: T) diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/thisWithReceiverLabelsClasses.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/thisWithReceiverLabelsClasses.txt index b72e7f125c9..23d255667b4 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/thisWithReceiverLabelsClasses.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/thisWithReceiverLabelsClasses.txt @@ -8,7 +8,7 @@ public final class A { public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } -public final class B { +context(A) public final class B { public constructor B() public final val prop: [ERROR : Type for x + this@A.x] public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/thisWithReceiverLabelsFunctions.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/thisWithReceiverLabelsFunctions.txt index 63bb3195997..4bd2dc92494 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/thisWithReceiverLabelsFunctions.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/thisWithReceiverLabelsFunctions.txt @@ -1,9 +1,9 @@ package -public fun f(): kotlin.Unit -public fun f(): kotlin.Unit -public fun A.f(): kotlin.Unit -public fun C.f(): kotlin.Unit +context(A, A, B) public fun f(): kotlin.Unit +context(A, B) public fun f(): kotlin.Unit +context(A) public fun A.f(): kotlin.Unit +context(A, A, B) public fun C.f(): kotlin.Unit public final class A { public constructor A(/*0*/ a: T) @@ -28,4 +28,3 @@ public final class C { public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } - diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/thisWithReceiverLabelsProperties.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/thisWithReceiverLabelsProperties.txt index 1e76e0bf481..5685f4404f5 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/thisWithReceiverLabelsProperties.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/thisWithReceiverLabelsProperties.txt @@ -1,8 +1,8 @@ package -public var p: kotlin.Int -public var p: kotlin.Int -public val C.p: kotlin.Int +context(B, A) public var p: kotlin.Int +context(B, A, A) public var p: kotlin.Int +context(B, A, A) public val C.p: kotlin.Int public final class A { public constructor A(/*0*/ a: T) diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/typeParameterAsContextReceiver.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/typeParameterAsContextReceiver.txt index d963b71dd23..c5cba484460 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/typeParameterAsContextReceiver.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/typeParameterAsContextReceiver.txt @@ -1,4 +1,4 @@ package public fun test(): kotlin.Unit -public fun useContext(/*0*/ block: (T) -> kotlin.Unit): kotlin.Unit +context(T) public fun useContext(/*0*/ block: (T) -> kotlin.Unit): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/typeParameterized.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/typeParameterized.txt index 2853b3591eb..0d427fc1f7d 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/typeParameterized.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/typeParameterized.txt @@ -1,6 +1,6 @@ package -public fun T.f(/*0*/ t: B): kotlin.Unit +context(T) public fun T.f(/*0*/ t: B): kotlin.Unit public fun kotlin.Int.main(/*0*/ a: A, /*1*/ b: B): kotlin.Unit public final class A { diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/typeParameterizedList.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/typeParameterizedList.txt index aa8cfd66d1e..d071913a9d4 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/typeParameterizedList.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/typeParameterizedList.txt @@ -2,7 +2,7 @@ package public fun listOf(/*0*/ vararg e: T /*kotlin.Array*/): kotlin.collections.List public fun main(): kotlin.Unit -public fun A.f(): kotlin.Unit +context(kotlin.collections.List) public fun A.f(): kotlin.Unit public final class A { public constructor A() diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/unsupported.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/unsupported.txt index e6fede1945e..bf9e7af6539 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/unsupported.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/unsupported.txt @@ -1,12 +1,12 @@ package -public fun f(/*0*/ g: context(kotlin.Any) () -> kotlin.Unit, /*1*/ value: kotlin.Any): context(A) () -> kotlin.Unit +context(kotlin.Any) public fun f(/*0*/ g: context(kotlin.Any) () -> kotlin.Unit, /*1*/ value: kotlin.Any): context(A) () -> kotlin.Unit -public final class A { +context(kotlin.String, kotlin.Int) public final class A { public constructor A() - public final val p: kotlin.Any + context(kotlin.Any) public final val p: kotlin.Any 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 final fun m(): kotlin.Unit + context(kotlin.String, kotlin.Int) public final fun m(): kotlin.Unit public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/withExplicitReceiver.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/withExplicitReceiver.txt index 5233804ec65..6301ed24107 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/withExplicitReceiver.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/withExplicitReceiver.txt @@ -1,7 +1,7 @@ package public fun main(): kotlin.Unit -public fun B.f(): kotlin.Unit +context(A) public fun B.f(): kotlin.Unit public open class A { public constructor A() diff --git a/compiler/testData/diagnostics/tests/extensions/contextReceivers/withExplicitReceiverError.txt b/compiler/testData/diagnostics/tests/extensions/contextReceivers/withExplicitReceiverError.txt index 5c7ae405e8a..703794dcdbe 100644 --- a/compiler/testData/diagnostics/tests/extensions/contextReceivers/withExplicitReceiverError.txt +++ b/compiler/testData/diagnostics/tests/extensions/contextReceivers/withExplicitReceiverError.txt @@ -1,7 +1,7 @@ package public fun main(): kotlin.Unit -public fun B.f(): kotlin.Unit +context(A) public fun B.f(): kotlin.Unit public final class A { public constructor A() diff --git a/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt b/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt index 89938abb52f..ee0e0963fdf 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt @@ -676,6 +676,7 @@ internal class DescriptorRendererImpl( if (!startFromName) { if (!startFromDeclarationKeyword) { builder.renderAnnotations(function) + renderContextReceivers(function.contextReceiverParameters, builder) renderVisibility(function.visibility, builder) renderModalityForCallable(function, builder) @@ -732,17 +733,39 @@ internal class DescriptorRendererImpl( } } + private fun KotlinType.renderForReceiver(): String { + var result = renderType(this) + if (shouldRenderAsPrettyFunctionType(this) && !TypeUtils.isNullableType(this)) { + result = "($result)" + } + return result + } + + private fun renderContextReceivers(contextReceivers: List, builder: StringBuilder) { + if (contextReceivers.isNotEmpty()) { + builder.append("context(") + } else { + return + } + for ((i, contextReceiver) in contextReceivers.withIndex()) { + builder.renderAnnotations(contextReceiver, AnnotationUseSiteTarget.RECEIVER) + val typeString = contextReceiver.type.renderForReceiver() + builder.append(typeString) + if (i == contextReceivers.lastIndex) { + builder.append(") ") + } else { + builder.append(", ") + } + } + } + private fun renderReceiver(callableDescriptor: CallableDescriptor, builder: StringBuilder) { val receiver = callableDescriptor.extensionReceiverParameter if (receiver != null) { builder.renderAnnotations(receiver, AnnotationUseSiteTarget.RECEIVER) - val type = receiver.type - var result = renderType(type) - if (shouldRenderAsPrettyFunctionType(type) && !TypeUtils.isNullableType(type)) { - result = "($result)" - } - builder.append(result).append(".") + val typeString = receiver.type.renderForReceiver() + builder.append(typeString).append(".") } } @@ -904,6 +927,7 @@ internal class DescriptorRendererImpl( if (!startFromName) { if (!startFromDeclarationKeyword) { renderPropertyAnnotations(property, builder) + renderContextReceivers(property.contextReceiverParameters, builder) renderVisibility(property.visibility, builder) renderModifier(builder, DescriptorRendererModifier.CONST in modifiers && property.isConst, "const") renderMemberModifiers(property, builder) @@ -988,6 +1012,7 @@ internal class DescriptorRendererImpl( if (!startFromName) { builder.renderAnnotations(klass) + renderContextReceivers(klass.contextReceivers, builder) if (!isEnumEntry) { renderVisibility(klass.visibility, builder) }