From 4b366ddcf643629defb1984e5ec8a97f13a222dc Mon Sep 17 00:00:00 2001 From: "Anastasia.Shadrina" Date: Fri, 18 Feb 2022 02:48:05 +0700 Subject: [PATCH] [BE JVM] KT-51271 Set proper indices generating contextual inline call [Tests] Test inline call with context receivers and default params --- .../codegen/inline/ParametersBuilder.kt | 4 +- .../kotlin/codegen/inline/PsiInlineCodegen.kt | 3 +- .../FirBlackBoxCodegenTestGenerated.java | 6 + .../runners/ir/Fir2IrTextTestGenerated.java | 6 + .../lower/DefaultArgumentStubGenerator.kt | 1 + .../backend/jvm/codegen/ExpressionCodegen.kt | 2 +- .../backend/jvm/codegen/IrCallGenerator.kt | 2 +- .../backend/jvm/codegen/IrInlineCodegen.kt | 4 +- .../contextReceivers/contextualInlineCall.kt | 39 ++++ .../contextualInlineCall.ir.txt | 175 ++++++++++++++++++ .../contextReceivers/contextualInlineCall.kt | 36 ++++ .../contextualInlineCall.kt.txt | 55 ++++++ .../IrBlackBoxCodegenTestGenerated.java | 6 + .../test/runners/ir/IrTextTestGenerated.java | 6 + 14 files changed, 338 insertions(+), 7 deletions(-) create mode 100644 compiler/testData/codegen/box/extensionFunctions/contextReceivers/contextualInlineCall.kt create mode 100644 compiler/testData/ir/irText/declarations/contextReceivers/contextualInlineCall.ir.txt create mode 100644 compiler/testData/ir/irText/declarations/contextReceivers/contextualInlineCall.kt create mode 100644 compiler/testData/ir/irText/declarations/contextReceivers/contextualInlineCall.kt.txt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/ParametersBuilder.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/ParametersBuilder.kt index 30083564b94..8fed6ac406d 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/ParametersBuilder.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/ParametersBuilder.kt @@ -76,8 +76,8 @@ class ParametersBuilder private constructor() { return info } - fun markValueParametersStart() { - this.valueParamFirstIndex = params.size + fun markValueParametersStart(contextReceiversCount: Int) { + this.valueParamFirstIndex = params.size - contextReceiversCount this.nextValueParameterIndex = valueParamFirstIndex } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiInlineCodegen.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiInlineCodegen.kt index 912dbdf3c9c..03695bbb90b 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiInlineCodegen.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiInlineCodegen.kt @@ -97,7 +97,8 @@ class PsiInlineCodegen( hiddenParameters += invocationParamBuilder.addNextParameter(param.asmType, false) to codegen.frameMap.enterTemp(param.asmType) } - invocationParamBuilder.markValueParametersStart() + // TODO: Add context receivers as hiddenParameters and pass their count + invocationParamBuilder.markValueParametersStart(0) } override fun putHiddenParamsIntoLocals() { diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index 0dcf8831869..5b3fe5a5c22 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -16524,6 +16524,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/extensionFunctions/contextReceivers/contextualFunctionConversion.kt"); } + @Test + @TestMetadata("contextualInlineCall.kt") + public void testContextualInlineCall() throws Exception { + runTest("compiler/testData/codegen/box/extensionFunctions/contextReceivers/contextualInlineCall.kt"); + } + @Test @TestMetadata("delegatedPropertiesOperators.kt") public void testDelegatedPropertiesOperators() throws Exception { diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/Fir2IrTextTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/Fir2IrTextTestGenerated.java index 7049fd3acfc..74e4bbd0604 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/Fir2IrTextTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/Fir2IrTextTestGenerated.java @@ -781,6 +781,12 @@ public class Fir2IrTextTestGenerated extends AbstractFir2IrTextTest { runTest("compiler/testData/ir/irText/declarations/contextReceivers/contextualFunctionConversion.kt"); } + @Test + @TestMetadata("contextualInlineCall.kt") + public void testContextualInlineCall() throws Exception { + runTest("compiler/testData/ir/irText/declarations/contextReceivers/contextualInlineCall.kt"); + } + @Test @TestMetadata("contextualPrimaryConstructorWithParams.kt") public void testContextualPrimaryConstructorWithParams() throws Exception { diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/DefaultArgumentStubGenerator.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/DefaultArgumentStubGenerator.kt index 3bcf63dea73..8c60e0e3081 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/DefaultArgumentStubGenerator.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/DefaultArgumentStubGenerator.kt @@ -595,6 +595,7 @@ private fun IrFunction.generateDefaultsFunctionImpl( newFunction.returnType = returnType.remapTypeParameters(classIfConstructor, newFunction.classIfConstructor) newFunction.dispatchReceiverParameter = dispatchReceiverParameter?.copyTo(newFunction) newFunction.extensionReceiverParameter = extensionReceiverParameter?.copyTo(newFunction) + newFunction.contextReceiverParametersCount = contextReceiverParametersCount newFunction.valueParameters = valueParameters.map { val newType = it.type.remapTypeParameters(classIfConstructor, newFunction.classIfConstructor) diff --git a/compiler/ir/backend.jvm/codegen/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt b/compiler/ir/backend.jvm/codegen/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt index 0a766f0c379..e896e064a91 100644 --- a/compiler/ir/backend.jvm/codegen/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt +++ b/compiler/ir/backend.jvm/codegen/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt @@ -505,7 +505,7 @@ class ExpressionCodegen( callGenerator.genValueAndPut(callee.extensionReceiverParameter!!, receiver, type, this, data) } - callGenerator.beforeValueParametersStart() + callGenerator.beforeValueParametersStart(callee.contextReceiverParametersCount) callee.valueParameters.subList(callee.contextReceiverParametersCount, callee.valueParameters.size) .forEachIndexed { i, valueParameter -> handleValueParameter(i + contextReceivers.size, valueParameter) } diff --git a/compiler/ir/backend.jvm/codegen/src/org/jetbrains/kotlin/backend/jvm/codegen/IrCallGenerator.kt b/compiler/ir/backend.jvm/codegen/src/org/jetbrains/kotlin/backend/jvm/codegen/IrCallGenerator.kt index d04db2f9703..65bbdec2cbc 100644 --- a/compiler/ir/backend.jvm/codegen/src/org/jetbrains/kotlin/backend/jvm/codegen/IrCallGenerator.kt +++ b/compiler/ir/backend.jvm/codegen/src/org/jetbrains/kotlin/backend/jvm/codegen/IrCallGenerator.kt @@ -36,7 +36,7 @@ interface IrCallGenerator { fun beforeCallStart() {} - fun beforeValueParametersStart() {} + fun beforeValueParametersStart(contextReceiversCount: Int) {} fun afterCallEnd() {} diff --git a/compiler/ir/backend.jvm/codegen/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt b/compiler/ir/backend.jvm/codegen/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt index ca71f1830f9..0be5eeca7b9 100644 --- a/compiler/ir/backend.jvm/codegen/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt +++ b/compiler/ir/backend.jvm/codegen/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt @@ -150,8 +150,8 @@ class IrInlineCodegen( } } - override fun beforeValueParametersStart() { - invocationParamBuilder.markValueParametersStart() + override fun beforeValueParametersStart(contextReceiversCount: Int) { + invocationParamBuilder.markValueParametersStart(contextReceiversCount) } override fun genInlineCall( diff --git a/compiler/testData/codegen/box/extensionFunctions/contextReceivers/contextualInlineCall.kt b/compiler/testData/codegen/box/extensionFunctions/contextReceivers/contextualInlineCall.kt new file mode 100644 index 00000000000..8d7c2990aac --- /dev/null +++ b/compiler/testData/codegen/box/extensionFunctions/contextReceivers/contextualInlineCall.kt @@ -0,0 +1,39 @@ +// !LANGUAGE: +ContextReceivers +// TARGET_BACKEND: JVM_IR +// IGNORE_BACKEND_FIR: JVM_IR +// FIR status: context receivers aren't yet supported + +// KT-51271 + +class Context { + fun c() = 1 +} + +context(Context) +inline fun testInline() = c() + +context(Context) +inline fun testInlineWithArg(i: Int) = i + c() + +context(Context) +inline fun Int.testInlineWithExtensionAndArg(i: Int) = this@Int + i + c() + +context(Context) +inline fun Int.testInlineWithExtensionAndMultipleArgs(i1: Int, i2: Int) = this@Int + i1 + i2 + c() + +context(Context, Any) +inline fun Int.testInlineWithExtensionAndMultipleContextsAndArgs(i1: Int = 1, i2: Int = 2) = + this@Int + i1 + i2 + c() + if (this@Any == null) 0 else 1 + +fun box(): String = with(Context()) { + var result = 0 + result += testInline() + result += testInlineWithArg(1) + result += 1.testInlineWithExtensionAndArg(1) + result += 1.testInlineWithExtensionAndMultipleArgs(1, 2) + with(1) { + result += 1.testInlineWithExtensionAndMultipleContextsAndArgs(1, 2) + result += 1.testInlineWithExtensionAndMultipleContextsAndArgs() + } + return if (result == 23) "OK" else "fail" +} \ No newline at end of file diff --git a/compiler/testData/ir/irText/declarations/contextReceivers/contextualInlineCall.ir.txt b/compiler/testData/ir/irText/declarations/contextReceivers/contextualInlineCall.ir.txt new file mode 100644 index 00000000000..dd7d11bfb10 --- /dev/null +++ b/compiler/testData/ir/irText/declarations/contextReceivers/contextualInlineCall.ir.txt @@ -0,0 +1,175 @@ +FILE fqName: fileName:/contextualInlineCall.kt + CLASS CLASS name:Context modality:FINAL visibility:public superTypes:[kotlin.Any] + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Context + CONSTRUCTOR visibility:public <> () returnType:.Context [primary] + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Context modality:FINAL visibility:public superTypes:[kotlin.Any]' + FUN name:c visibility:public modality:FINAL <> ($this:.Context) returnType:kotlin.Int + $this: VALUE_PARAMETER name: type:.Context + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun c (): kotlin.Int declared in .Context' + CONST Int type=kotlin.Int value=1 + FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator] + overridden: + public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any + $this: VALUE_PARAMETER name: type:kotlin.Any + VALUE_PARAMETER name:other index:0 type:kotlin.Any? + FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override] + overridden: + public open fun hashCode (): kotlin.Int declared in kotlin.Any + $this: VALUE_PARAMETER name: type:kotlin.Any + FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override] + overridden: + public open fun toString (): kotlin.String declared in kotlin.Any + $this: VALUE_PARAMETER name: type:kotlin.Any + FUN name:testInline visibility:public modality:FINAL <> (:.Context) returnType:kotlin.Int [inline] + contextReceiverParametersCount: 1 + VALUE_PARAMETER name: index:0 type:.Context + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun testInline (: .Context): kotlin.Int [inline] declared in ' + CALL 'public final fun c (): kotlin.Int declared in .Context' type=kotlin.Int origin=null + $this: GET_VAR ': .Context declared in .testInline' type=.Context origin=null + FUN name:testInlineWithArg visibility:public modality:FINAL <> (:.Context, i:kotlin.Int) returnType:kotlin.Int [inline] + contextReceiverParametersCount: 1 + VALUE_PARAMETER name: index:0 type:.Context + VALUE_PARAMETER name:i index:1 type:kotlin.Int + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun testInlineWithArg (: .Context, i: kotlin.Int): kotlin.Int [inline] declared in ' + CALL 'public final fun plus (other: kotlin.Int): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=PLUS + $this: GET_VAR 'i: kotlin.Int declared in .testInlineWithArg' type=kotlin.Int origin=null + other: CALL 'public final fun c (): kotlin.Int declared in .Context' type=kotlin.Int origin=null + $this: GET_VAR ': .Context declared in .testInlineWithArg' type=.Context origin=null + FUN name:testInlineWithExtensionAndArg visibility:public modality:FINAL <> ($receiver:kotlin.Int, :.Context, i:kotlin.Int) returnType:kotlin.Int [inline] + contextReceiverParametersCount: 1 + $receiver: VALUE_PARAMETER name: type:kotlin.Int + VALUE_PARAMETER name: index:0 type:.Context + VALUE_PARAMETER name:i index:1 type:kotlin.Int + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun testInlineWithExtensionAndArg (: .Context, i: kotlin.Int): kotlin.Int [inline] declared in ' + CALL 'public final fun plus (other: kotlin.Int): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=PLUS + $this: CALL 'public final fun plus (other: kotlin.Int): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=PLUS + $this: GET_VAR ': kotlin.Int declared in .testInlineWithExtensionAndArg' type=kotlin.Int origin=null + other: GET_VAR 'i: kotlin.Int declared in .testInlineWithExtensionAndArg' type=kotlin.Int origin=null + other: CALL 'public final fun c (): kotlin.Int declared in .Context' type=kotlin.Int origin=null + $this: GET_VAR ': .Context declared in .testInlineWithExtensionAndArg' type=.Context origin=null + FUN name:testInlineWithExtensionAndMultipleArgs visibility:public modality:FINAL <> ($receiver:kotlin.Int, :.Context, i1:kotlin.Int, i2:kotlin.Int) returnType:kotlin.Int [inline] + contextReceiverParametersCount: 1 + $receiver: VALUE_PARAMETER name: type:kotlin.Int + VALUE_PARAMETER name: index:0 type:.Context + VALUE_PARAMETER name:i1 index:1 type:kotlin.Int + VALUE_PARAMETER name:i2 index:2 type:kotlin.Int + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun testInlineWithExtensionAndMultipleArgs (: .Context, i1: kotlin.Int, i2: kotlin.Int): kotlin.Int [inline] declared in ' + CALL 'public final fun plus (other: kotlin.Int): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=PLUS + $this: CALL 'public final fun plus (other: kotlin.Int): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=PLUS + $this: CALL 'public final fun plus (other: kotlin.Int): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=PLUS + $this: GET_VAR ': kotlin.Int declared in .testInlineWithExtensionAndMultipleArgs' type=kotlin.Int origin=null + other: GET_VAR 'i1: kotlin.Int declared in .testInlineWithExtensionAndMultipleArgs' type=kotlin.Int origin=null + other: GET_VAR 'i2: kotlin.Int declared in .testInlineWithExtensionAndMultipleArgs' type=kotlin.Int origin=null + other: CALL 'public final fun c (): kotlin.Int declared in .Context' type=kotlin.Int origin=null + $this: GET_VAR ': .Context declared in .testInlineWithExtensionAndMultipleArgs' type=.Context origin=null + FUN name:testInlineWithExtensionAndMultipleContextsAndArgs visibility:public modality:FINAL <> ($receiver:kotlin.Int, :.Context, :kotlin.Any, i1:kotlin.Int, i2:kotlin.Int) returnType:kotlin.Int [inline] + contextReceiverParametersCount: 2 + $receiver: VALUE_PARAMETER name: type:kotlin.Int + VALUE_PARAMETER name: index:0 type:.Context + VALUE_PARAMETER name: index:1 type:kotlin.Any + VALUE_PARAMETER name:i1 index:2 type:kotlin.Int + EXPRESSION_BODY + CONST Int type=kotlin.Int value=1 + VALUE_PARAMETER name:i2 index:3 type:kotlin.Int + EXPRESSION_BODY + CONST Int type=kotlin.Int value=2 + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun testInlineWithExtensionAndMultipleContextsAndArgs (: .Context, : kotlin.Any, i1: kotlin.Int, i2: kotlin.Int): kotlin.Int [inline] declared in ' + CALL 'public final fun plus (other: kotlin.Int): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=PLUS + $this: CALL 'public final fun plus (other: kotlin.Int): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=PLUS + $this: CALL 'public final fun plus (other: kotlin.Int): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=PLUS + $this: CALL 'public final fun plus (other: kotlin.Int): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=PLUS + $this: GET_VAR ': kotlin.Int declared in .testInlineWithExtensionAndMultipleContextsAndArgs' type=kotlin.Int origin=null + other: GET_VAR 'i1: kotlin.Int declared in .testInlineWithExtensionAndMultipleContextsAndArgs' type=kotlin.Int origin=null + other: GET_VAR 'i2: kotlin.Int declared in .testInlineWithExtensionAndMultipleContextsAndArgs' type=kotlin.Int origin=null + other: CALL 'public final fun c (): kotlin.Int declared in .Context' type=kotlin.Int origin=null + $this: GET_VAR ': .Context declared in .testInlineWithExtensionAndMultipleContextsAndArgs' type=.Context origin=null + other: WHEN type=kotlin.Int origin=IF + BRANCH + if: CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EQEQ + arg0: GET_VAR ': kotlin.Any declared in .testInlineWithExtensionAndMultipleContextsAndArgs' type=kotlin.Any origin=null + arg1: CONST Null type=kotlin.Nothing? value=null + then: CONST Int type=kotlin.Int value=0 + BRANCH + if: CONST Boolean type=kotlin.Boolean value=true + then: CONST Int type=kotlin.Int value=1 + FUN name:box visibility:public modality:FINAL <> () returnType:kotlin.String + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in ' + CALL 'public final fun with (receiver: T of kotlin.StandardKt.with, block: @[ExtensionFunctionType] kotlin.Function1): R of kotlin.StandardKt.with [inline] declared in kotlin.StandardKt' type=kotlin.Nothing origin=null + : .Context + : kotlin.Nothing + receiver: CONSTRUCTOR_CALL 'public constructor () [primary] declared in .Context' type=.Context origin=null + block: FUN_EXPR type=@[ExtensionFunctionType] kotlin.Function1<.Context, kotlin.Nothing> origin=LAMBDA + FUN LOCAL_FUNCTION_FOR_LAMBDA name: visibility:local modality:FINAL <> ($receiver:.Context) returnType:kotlin.Nothing + $receiver: VALUE_PARAMETER name:$this$with type:.Context + BLOCK_BODY + VAR name:result type:kotlin.Int [var] + CONST Int type=kotlin.Int value=0 + SET_VAR 'var result: kotlin.Int [var] declared in .box.' type=kotlin.Unit origin=PLUSEQ + CALL 'public final fun plus (other: kotlin.Int): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=PLUSEQ + $this: GET_VAR 'var result: kotlin.Int [var] declared in .box.' type=kotlin.Int origin=PLUSEQ + other: CALL 'public final fun testInline (: .Context): kotlin.Int [inline] declared in ' type=kotlin.Int origin=null + : GET_VAR '$this$with: .Context declared in .box.' type=.Context origin=null + SET_VAR 'var result: kotlin.Int [var] declared in .box.' type=kotlin.Unit origin=PLUSEQ + CALL 'public final fun plus (other: kotlin.Int): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=PLUSEQ + $this: GET_VAR 'var result: kotlin.Int [var] declared in .box.' type=kotlin.Int origin=PLUSEQ + other: CALL 'public final fun testInlineWithArg (: .Context, i: kotlin.Int): kotlin.Int [inline] declared in ' type=kotlin.Int origin=null + : GET_VAR '$this$with: .Context declared in .box.' type=.Context origin=null + i: CONST Int type=kotlin.Int value=1 + SET_VAR 'var result: kotlin.Int [var] declared in .box.' type=kotlin.Unit origin=PLUSEQ + CALL 'public final fun plus (other: kotlin.Int): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=PLUSEQ + $this: GET_VAR 'var result: kotlin.Int [var] declared in .box.' type=kotlin.Int origin=PLUSEQ + other: CALL 'public final fun testInlineWithExtensionAndArg (: .Context, i: kotlin.Int): kotlin.Int [inline] declared in ' type=kotlin.Int origin=null + $receiver: CONST Int type=kotlin.Int value=1 + : GET_VAR '$this$with: .Context declared in .box.' type=.Context origin=null + i: CONST Int type=kotlin.Int value=1 + SET_VAR 'var result: kotlin.Int [var] declared in .box.' type=kotlin.Unit origin=PLUSEQ + CALL 'public final fun plus (other: kotlin.Int): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=PLUSEQ + $this: GET_VAR 'var result: kotlin.Int [var] declared in .box.' type=kotlin.Int origin=PLUSEQ + other: CALL 'public final fun testInlineWithExtensionAndMultipleArgs (: .Context, i1: kotlin.Int, i2: kotlin.Int): kotlin.Int [inline] declared in ' type=kotlin.Int origin=null + $receiver: CONST Int type=kotlin.Int value=1 + : GET_VAR '$this$with: .Context declared in .box.' type=.Context origin=null + i1: CONST Int type=kotlin.Int value=1 + i2: CONST Int type=kotlin.Int value=2 + CALL 'public final fun with (receiver: T of kotlin.StandardKt.with, block: @[ExtensionFunctionType] kotlin.Function1): R of kotlin.StandardKt.with [inline] declared in kotlin.StandardKt' type=kotlin.Unit origin=null + : kotlin.Int + : kotlin.Unit + receiver: CONST Int type=kotlin.Int value=1 + block: FUN_EXPR type=@[ExtensionFunctionType] kotlin.Function1 origin=LAMBDA + FUN LOCAL_FUNCTION_FOR_LAMBDA name: visibility:local modality:FINAL <> ($receiver:kotlin.Int) returnType:kotlin.Unit + $receiver: VALUE_PARAMETER name:$this$with type:kotlin.Int + BLOCK_BODY + SET_VAR 'var result: kotlin.Int [var] declared in .box.' type=kotlin.Unit origin=PLUSEQ + CALL 'public final fun plus (other: kotlin.Int): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=PLUSEQ + $this: GET_VAR 'var result: kotlin.Int [var] declared in .box.' type=kotlin.Int origin=PLUSEQ + other: CALL 'public final fun testInlineWithExtensionAndMultipleContextsAndArgs (: .Context, : kotlin.Any, i1: kotlin.Int, i2: kotlin.Int): kotlin.Int [inline] declared in ' type=kotlin.Int origin=null + $receiver: CONST Int type=kotlin.Int value=1 + : GET_VAR '$this$with: .Context declared in .box.' type=.Context origin=null + : GET_VAR '$this$with: kotlin.Int declared in .box..' type=kotlin.Int origin=null + i1: CONST Int type=kotlin.Int value=1 + i2: CONST Int type=kotlin.Int value=2 + SET_VAR 'var result: kotlin.Int [var] declared in .box.' type=kotlin.Unit origin=PLUSEQ + CALL 'public final fun plus (other: kotlin.Int): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=PLUSEQ + $this: GET_VAR 'var result: kotlin.Int [var] declared in .box.' type=kotlin.Int origin=PLUSEQ + other: CALL 'public final fun testInlineWithExtensionAndMultipleContextsAndArgs (: .Context, : kotlin.Any, i1: kotlin.Int, i2: kotlin.Int): kotlin.Int [inline] declared in ' type=kotlin.Int origin=null + $receiver: CONST Int type=kotlin.Int value=1 + : GET_VAR '$this$with: .Context declared in .box.' type=.Context origin=null + : GET_VAR '$this$with: kotlin.Int declared in .box..' type=kotlin.Int origin=null + RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in ' + WHEN type=kotlin.String origin=IF + BRANCH + if: CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EQEQ + arg0: GET_VAR 'var result: kotlin.Int [var] declared in .box.' type=kotlin.Int origin=null + arg1: CONST Int type=kotlin.Int value=23 + then: CONST String type=kotlin.String value="OK" + BRANCH + if: CONST Boolean type=kotlin.Boolean value=true + then: CONST String type=kotlin.String value="fail" diff --git a/compiler/testData/ir/irText/declarations/contextReceivers/contextualInlineCall.kt b/compiler/testData/ir/irText/declarations/contextReceivers/contextualInlineCall.kt new file mode 100644 index 00000000000..3012f6fc156 --- /dev/null +++ b/compiler/testData/ir/irText/declarations/contextReceivers/contextualInlineCall.kt @@ -0,0 +1,36 @@ +// !LANGUAGE: +ContextReceivers +// TARGET_BACKEND: JVM_IR +// IGNORE_BACKEND_FIR: JVM_IR + +class Context { + fun c() = 1 +} + +context(Context) +inline fun testInline() = c() + +context(Context) +inline fun testInlineWithArg(i: Int) = i + c() + +context(Context) +inline fun Int.testInlineWithExtensionAndArg(i: Int) = this@Int + i + c() + +context(Context) +inline fun Int.testInlineWithExtensionAndMultipleArgs(i1: Int, i2: Int) = this@Int + i1 + i2 + c() + +context(Context, Any) +inline fun Int.testInlineWithExtensionAndMultipleContextsAndArgs(i1: Int = 1, i2: Int = 2) = + this@Int + i1 + i2 + c() + if (this@Any == null) 0 else 1 + +fun box(): String = with(Context()) { + var result = 0 + result += testInline() + result += testInlineWithArg(1) + result += 1.testInlineWithExtensionAndArg(1) + result += 1.testInlineWithExtensionAndMultipleArgs(1, 2) + with(1) { + result += 1.testInlineWithExtensionAndMultipleContextsAndArgs(1, 2) + result += 1.testInlineWithExtensionAndMultipleContextsAndArgs() + } + return if (result == 23) "OK" else "fail" +} diff --git a/compiler/testData/ir/irText/declarations/contextReceivers/contextualInlineCall.kt.txt b/compiler/testData/ir/irText/declarations/contextReceivers/contextualInlineCall.kt.txt new file mode 100644 index 00000000000..df623e8b9b8 --- /dev/null +++ b/compiler/testData/ir/irText/declarations/contextReceivers/contextualInlineCall.kt.txt @@ -0,0 +1,55 @@ +class Context { + constructor() /* primary */ { + super/*Any*/() + /* () */ + + } + + fun c(): Int { + return 1 + } + +} + +inline fun testInline(: Context): Int { + return .c() +} + +inline fun testInlineWithArg(: Context, i: Int): Int { + return i.plus(other = .c()) +} + +inline fun Int.testInlineWithExtensionAndArg(: Context, i: Int): Int { + return .plus(other = i).plus(other = .c()) +} + +inline fun Int.testInlineWithExtensionAndMultipleArgs(: Context, i1: Int, i2: Int): Int { + return .plus(other = i1).plus(other = i2).plus(other = .c()) +} + +inline fun Int.testInlineWithExtensionAndMultipleContextsAndArgs(: Context, : Any, i1: Int = 1, i2: Int = 2): Int { + return .plus(other = i1).plus(other = i2).plus(other = .c()).plus(other = when { + EQEQ(arg0 = , arg1 = null) -> 0 + else -> 1 + }) +} + +fun box(): String { + return with(receiver = Context(), block = local fun Context.(): Nothing { + var result: Int = 0 + result = result.plus(other = testInline( = $this$with)) + result = result.plus(other = testInlineWithArg( = $this$with, i = 1)) + result = result.plus(other = 1.testInlineWithExtensionAndArg( = $this$with, i = 1)) + result = result.plus(other = 1.testInlineWithExtensionAndMultipleArgs( = $this$with, i1 = 1, i2 = 2)) + with(receiver = 1, block = local fun Int.() { + result = result.plus(other = 1.testInlineWithExtensionAndMultipleContextsAndArgs( = $this$with, = $this$with, i1 = 1, i2 = 2)) + result = result.plus(other = 1.testInlineWithExtensionAndMultipleContextsAndArgs( = $this$with, = $this$with)) + } +) + return when { + EQEQ(arg0 = result, arg1 = 23) -> "OK" + else -> "fail" + } + } +) +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index c332a993511..36f135122d3 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -16524,6 +16524,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/extensionFunctions/contextReceivers/contextualFunctionConversion.kt"); } + @Test + @TestMetadata("contextualInlineCall.kt") + public void testContextualInlineCall() throws Exception { + runTest("compiler/testData/codegen/box/extensionFunctions/contextReceivers/contextualInlineCall.kt"); + } + @Test @TestMetadata("delegatedPropertiesOperators.kt") public void testDelegatedPropertiesOperators() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ir/IrTextTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ir/IrTextTestGenerated.java index 99ebd4712b1..b89bcefcb4c 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ir/IrTextTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ir/IrTextTestGenerated.java @@ -781,6 +781,12 @@ public class IrTextTestGenerated extends AbstractIrTextTest { runTest("compiler/testData/ir/irText/declarations/contextReceivers/contextualFunctionConversion.kt"); } + @Test + @TestMetadata("contextualInlineCall.kt") + public void testContextualInlineCall() throws Exception { + runTest("compiler/testData/ir/irText/declarations/contextReceivers/contextualInlineCall.kt"); + } + @Test @TestMetadata("contextualPrimaryConstructorWithParams.kt") public void testContextualPrimaryConstructorWithParams() throws Exception {