[FE] Render context receivers for functions, properties, and classes

This commit is contained in:
Anastasiya Shadrina
2021-11-22 20:04:50 +07:00
committed by TeamCityServer
parent 2954c5cc9c
commit 9e2271399e
41 changed files with 97 additions and 72 deletions
@@ -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() })
}
@@ -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 {
}
@@ -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 </*0*/ T : Common> T.supertypeExtensionGeneric(): kotlin.Unit
@@ -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
@@ -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
@@ -1,8 +1,8 @@
package
public val </*0*/ T> Pair<T, T>.max: T
context(kotlin.Comparator<T> /* = java.util.Comparator<T> */) public val </*0*/ T> Pair<T, T>.max: T
public fun test(): kotlin.Unit
public infix operator fun </*0*/ T> T.compareTo(/*0*/ other: T): kotlin.Int
context(kotlin.Comparator<T> /* = java.util.Comparator<T> */) public infix operator fun </*0*/ T> T.compareTo(/*0*/ other: T): kotlin.Int
public final data class Pair</*0*/ A, /*1*/ B> {
public constructor Pair</*0*/ A, /*1*/ B>(/*0*/ first: A, /*1*/ second: B)
@@ -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
@@ -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()
@@ -2,5 +2,5 @@ package
public fun </*0*/ T> listOf(/*0*/ vararg items: T /*kotlin.Array<out T>*/): kotlin.collections.List<T>
public fun test(): kotlin.Unit
public fun </*0*/ C, /*1*/ T> C.iterator(/*0*/ any: kotlin.Any?): kotlin.collections.Iterator<T>
context((IterableClass<C, T> /* = (C) -> kotlin.collections.Iterator<T> */)) public fun </*0*/ C, /*1*/ T> C.iterator(/*0*/ any: kotlin.Any?): kotlin.collections.Iterator<T>
public typealias IterableClass</*0*/ C, /*1*/ T> = (C) -> kotlin.collections.Iterator<T>
@@ -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()
@@ -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
@@ -3,7 +3,7 @@ package
public fun </*0*/ T> listOf(/*0*/ vararg items: T /*kotlin.Array<out T>*/): kotlin.collections.List<T>
public fun test(): kotlin.Unit
public inline fun </*0*/ T, /*1*/ R> kotlin.collections.Iterable<T>.fold(/*0*/ initial: R, /*1*/ operation: (acc: R, T) -> R): R
public fun </*0*/ T> kotlin.collections.List<T>.sum(): T
context(Monoid<T>) public fun </*0*/ T> kotlin.collections.List<T>.sum(): T
public object IntMonoid : Monoid<kotlin.Int> {
private constructor IntMonoid()
@@ -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?)
@@ -1,6 +1,6 @@
package
public fun userInfo(/*0*/ name: kotlin.String): Storage<User>.Info?
context(Logger, Storage<User>) public fun userInfo(/*0*/ name: kotlin.String): Storage<User>.Info?
public final class Logger {
public constructor Logger()
@@ -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()
@@ -3,14 +3,14 @@ package
public fun kotlin.collections.Collection<kotlin.Int>.bar(): kotlin.Unit
public fun kotlin.Int.foo(): kotlin.Unit
public final class A</*0*/ T> {
context(T) public final class A</*0*/ T> {
public constructor A</*0*/ T>()
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</*0*/ P> {
context(kotlin.collections.Collection<P>) public final class B</*0*/ P> {
public constructor B</*0*/ P>()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
@@ -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 {
@@ -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<kotlin.Int>.f(): kotlin.Unit
@@ -1,9 +1,9 @@
package
public fun </*0*/ T> f(/*0*/ lazy1: Lazy<kotlin.Int>, /*1*/ lazy2: Lazy<kotlin.CharSequence>, /*2*/ lazyT: Lazy<T>, /*3*/ lazyLazyT: Lazy<Lazy<T>>): kotlin.Unit
public fun test1(): kotlin.Unit
public fun </*0*/ T> Lazy<kotlin.Int>.test2(): kotlin.Unit
public fun </*0*/ T> Lazy<kotlin.Int>.test3(): kotlin.Unit
context(Lazy<kotlin.Int>, Lazy<kotlin.CharSequence>) public fun test1(): kotlin.Unit
context(Lazy<T>) public fun </*0*/ T> Lazy<kotlin.Int>.test2(): kotlin.Unit
context(Lazy<Lazy<T>>) public fun </*0*/ T> Lazy<kotlin.Int>.test3(): kotlin.Unit
public interface Lazy</*0*/ T> {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
@@ -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()
@@ -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
@@ -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
@@ -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 {
@@ -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
@@ -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 {
@@ -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
@@ -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 {
@@ -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
@@ -1,3 +1,3 @@
package
public fun foo(): kotlin.Unit
context([ERROR : No type element]) public fun foo(): kotlin.Unit
@@ -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?)
@@ -1,7 +1,7 @@
package
public val C.p: kotlin.Int
public fun f(): kotlin.Unit
context(B, A<kotlin.String>, A<kotlin.Int>) public val C.p: kotlin.Int
context(A<kotlin.Int>, A<kotlin.String>, B) public fun f(): kotlin.Unit
public final class A</*0*/ T> {
public constructor A</*0*/ T>(/*0*/ a: T)
@@ -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
@@ -1,9 +1,9 @@
package
public fun f(): kotlin.Unit
public fun f(): kotlin.Unit
public fun A<kotlin.Int>.f(): kotlin.Unit
public fun C.f(): kotlin.Unit
context(A<kotlin.Int>, A<kotlin.String>, B) public fun f(): kotlin.Unit
context(A<kotlin.String>, B) public fun f(): kotlin.Unit
context(A<kotlin.String>) public fun A<kotlin.Int>.f(): kotlin.Unit
context(A<kotlin.Int>, A<kotlin.String>, B) public fun C.f(): kotlin.Unit
public final class A</*0*/ T> {
public constructor A</*0*/ T>(/*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
}
@@ -1,8 +1,8 @@
package
public var p: kotlin.Int
public var p: kotlin.Int
public val C.p: kotlin.Int
context(B, A<kotlin.String>) public var p: kotlin.Int
context(B, A<kotlin.String>, A<kotlin.Int>) public var p: kotlin.Int
context(B, A<kotlin.String>, A<kotlin.Int>) public val C.p: kotlin.Int
public final class A</*0*/ T> {
public constructor A</*0*/ T>(/*0*/ a: T)
@@ -1,4 +1,4 @@
package
public fun test(): kotlin.Unit
public fun </*0*/ T> useContext(/*0*/ block: (T) -> kotlin.Unit): kotlin.Unit
context(T) public fun </*0*/ T> useContext(/*0*/ block: (T) -> kotlin.Unit): kotlin.Unit
@@ -1,6 +1,6 @@
package
public fun </*0*/ T> T.f(/*0*/ t: B<T>): kotlin.Unit
context(T) public fun </*0*/ T> T.f(/*0*/ t: B<T>): kotlin.Unit
public fun kotlin.Int.main(/*0*/ a: A, /*1*/ b: B<kotlin.String>): kotlin.Unit
public final class A {
@@ -2,7 +2,7 @@ package
public fun </*0*/ T> listOf(/*0*/ vararg e: T /*kotlin.Array<out T>*/): kotlin.collections.List<T>
public fun main(): kotlin.Unit
public fun </*0*/ T> A<T>.f(): kotlin.Unit
context(kotlin.collections.List<T>) public fun </*0*/ T> A<T>.f(): kotlin.Unit
public final class A</*0*/ T> {
public constructor A</*0*/ T>()
@@ -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
}
@@ -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()
@@ -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()
@@ -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<ReceiverParameterDescriptor>, 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)
}