Set receivers for inline class default function stub calls

Default function stubs have dispatch and receiver parameters, but
inline class methods are static by design with receivers as ordinary
parameters. So, take these parameters and set them as receivers during
lowerings.
 #KT-46230: Fixed
This commit is contained in:
Ilmir Usmanov
2021-07-08 04:01:59 +02:00
committed by teamcityserver
parent 31420a934c
commit ac7538a269
15 changed files with 332 additions and 7 deletions
@@ -19778,6 +19778,40 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
public void testKt27416() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/kt27416.kt");
}
@Nested
@TestMetadata("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter")
@TestDataPath("$PROJECT_ROOT")
public class OverrideFunctionWithDefaultParameter {
@Test
@TestMetadata("all.kt")
public void testAll() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter/all.kt");
}
@Test
public void testAllFilesPresentInOverrideFunctionWithDefaultParameter() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("all-compatibility.kt")
public void testAll_compatibility() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter/all-compatibility.kt");
}
@Test
@TestMetadata("compatibility.kt")
public void testCompatibility() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter/compatibility.kt");
}
@Test
@TestMetadata("default.kt")
public void testDefault() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter/default.kt");
}
}
}
@Nested
@@ -303,17 +303,30 @@ open class DefaultParameterInjector(
}
symbol.owner.typeParameters.forEach { log { "${symbol.owner}[${it.index}] : $it" } }
val isStatic = isStatic(expression.symbol.owner)
return builder(symbol).apply {
copyTypeArgumentsFrom(expression)
params.forEachIndexed { i, value ->
var receivers = 0
if (isStatic) {
if (symbol.owner.dispatchReceiverParameter != null) {
dispatchReceiver = params[receivers++]
}
if (symbol.owner.extensionReceiverParameter != null) {
extensionReceiver = params[receivers++]
}
} else {
dispatchReceiver = expression.dispatchReceiver
extensionReceiver = expression.extensionReceiver
}
params.drop(receivers).forEachIndexed { i, value ->
log { "call::params@$i/${symbol.owner.valueParameters[i].name}: ${ir2string(value)}" }
putValueArgument(i, value)
}
dispatchReceiver = expression.dispatchReceiver
extensionReceiver = expression.extensionReceiver
log { "call::extension@: ${ir2string(expression.extensionReceiver)}" }
log { "call::dispatch@: ${ir2string(expression.dispatchReceiver)}" }
}
@@ -393,12 +406,19 @@ open class DefaultParameterInjector(
val realArgumentsNumber = declaration.valueParameters.size
val maskValues = IntArray((declaration.valueParameters.size + 31) / 32)
assert((stubFunction.valueParameters.size - realArgumentsNumber - maskValues.size) in listOf(0, 1)) {
assert(
((if (isStatic(expression.symbol.owner) && stubFunction.extensionReceiverParameter != null) 1 else 0) +
stubFunction.valueParameters.size - realArgumentsNumber - maskValues.size) in listOf(0, 1)
) {
"argument count mismatch: expected $realArgumentsNumber arguments + ${maskValues.size} masks + optional handler/marker, " +
"got ${stubFunction.valueParameters.size} total in ${stubFunction.render()}"
}
var sourceParameterIndex = -1
return stubFunction.symbol to stubFunction.valueParameters.mapIndexed { i, parameter ->
val valueParametersPrefix =
if (isStatic(expression.symbol.owner))
listOfNotNull(stubFunction.dispatchReceiverParameter, stubFunction.extensionReceiverParameter)
else emptyList()
return stubFunction.symbol to (valueParametersPrefix + stubFunction.valueParameters).mapIndexed { i, parameter ->
if (!parameter.isMovedReceiver()) {
++sourceParameterIndex
}
@@ -438,6 +458,8 @@ open class DefaultParameterInjector(
protected open fun useConstructorMarker(function: IrFunction) = function is IrConstructor
protected open fun isStatic(function: IrFunction): Boolean = false
private fun log(msg: () -> String) = context.log { "DEFAULT-INJECTOR: ${msg()}" }
}
@@ -482,7 +482,9 @@ class ExpressionCodegen(
expression.symbol.owner.valueParameters.forEachIndexed { i, irParameter ->
val arg = expression.getValueArgument(i)
val parameterType = callable.valueParameterTypes[i]
require(arg != null) { "Null argument in ExpressionCodegen for parameter ${irParameter.render()}" }
require(arg != null) {
"Null argument in ExpressionCodegen for parameter ${irParameter.render()}"
}
callGenerator.genValueAndPut(irParameter, arg, parameterType, this, data)
}
@@ -32,4 +32,7 @@ class JvmDefaultParameterInjector(context: JvmBackendContext) :
override fun useConstructorMarker(function: IrFunction): Boolean =
function is IrConstructor || function.origin == JvmLoweredDeclarationOrigin.STATIC_INLINE_CLASS_CONSTRUCTOR
override fun isStatic(function: IrFunction): Boolean =
function.origin == JvmLoweredDeclarationOrigin.STATIC_INLINE_CLASS_REPLACEMENT
}
@@ -0,0 +1,28 @@
// !LANGUAGE: +InlineClasses
// !JVM_DEFAULT_MODE: all-compatibility
// TARGET_BACKEND: JVM
// IGNORE_BACKEND: JVM
// JVM_TARGET: 1.8
interface Path {
fun dispatch(maxDepth: Int = 42)
fun Int.extension(maxDepth: Int = 42)
}
inline class RealPath(val x: Int) : Path {
override fun dispatch(maxDepth: Int) = Unit
fun childrenDispatch(recursively: Boolean): Unit =
if (recursively) dispatch() else dispatch()
override fun Int.extension(maxDepth: Int) = Unit
fun Int.childrenExtension(recursively: Boolean): Unit =
if (recursively) extension() else extension()
}
fun box(): String {
RealPath(1)
return "OK"
}
@@ -0,0 +1,28 @@
// !LANGUAGE: +InlineClasses
// !JVM_DEFAULT_MODE: all
// TARGET_BACKEND: JVM
// IGNORE_BACKEND: JVM
// JVM_TARGET: 1.8
interface Path {
fun dispatch(maxDepth: Int = 42)
fun Int.extension(maxDepth: Int = 42)
}
inline class RealPath(val x: Int) : Path {
override fun dispatch(maxDepth: Int) = Unit
fun childrenDispatch(recursively: Boolean): Unit =
if (recursively) dispatch() else dispatch()
override fun Int.extension(maxDepth: Int) = Unit
fun Int.childrenExtension(recursively: Boolean): Unit =
if (recursively) extension() else extension()
}
fun box(): String {
RealPath(1)
return "OK"
}
@@ -0,0 +1,28 @@
// !LANGUAGE: +InlineClasses
// !JVM_DEFAULT_MODE: compatibility
// TARGET_BACKEND: JVM
// IGNORE_BACKEND: JVM
// JVM_TARGET: 1.8
interface Path {
fun dispatch(maxDepth: Int = 42)
fun Int.extension(maxDepth: Int = 42)
}
inline class RealPath(val x: Int) : Path {
override fun dispatch(maxDepth: Int) = Unit
fun childrenDispatch(recursively: Boolean): Unit =
if (recursively) dispatch() else dispatch()
override fun Int.extension(maxDepth: Int) = Unit
fun Int.childrenExtension(recursively: Boolean): Unit =
if (recursively) extension() else extension()
}
fun box(): String {
RealPath(1)
return "OK"
}
@@ -0,0 +1,27 @@
// !LANGUAGE: +InlineClasses
// TARGET_BACKEND: JVM
// IGNORE_BACKEND: JVM
// JVM_TARGET: 1.8
interface Path {
fun dispatch(maxDepth: Int = 42)
fun Int.extension(maxDepth: Int = 42)
}
inline class RealPath(val x: Int) : Path {
override fun dispatch(maxDepth: Int) = Unit
fun childrenDispatch(recursively: Boolean): Unit =
if (recursively) dispatch() else dispatch()
override fun Int.extension(maxDepth: Int) = Unit
fun Int.childrenExtension(recursively: Boolean): Unit =
if (recursively) extension() else extension()
}
fun box(): String {
RealPath(1)
return "OK"
}
@@ -19736,6 +19736,40 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
public void testKt27416() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/kt27416.kt");
}
@Nested
@TestMetadata("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter")
@TestDataPath("$PROJECT_ROOT")
public class OverrideFunctionWithDefaultParameter {
@Test
@TestMetadata("all.kt")
public void testAll() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter/all.kt");
}
@Test
public void testAllFilesPresentInOverrideFunctionWithDefaultParameter() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
@Test
@TestMetadata("all-compatibility.kt")
public void testAll_compatibility() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter/all-compatibility.kt");
}
@Test
@TestMetadata("compatibility.kt")
public void testCompatibility() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter/compatibility.kt");
}
@Test
@TestMetadata("default.kt")
public void testDefault() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter/default.kt");
}
}
}
@Nested
@@ -19778,6 +19778,40 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
public void testKt27416() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/kt27416.kt");
}
@Nested
@TestMetadata("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter")
@TestDataPath("$PROJECT_ROOT")
public class OverrideFunctionWithDefaultParameter {
@Test
@TestMetadata("all.kt")
public void testAll() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter/all.kt");
}
@Test
public void testAllFilesPresentInOverrideFunctionWithDefaultParameter() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("all-compatibility.kt")
public void testAll_compatibility() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter/all-compatibility.kt");
}
@Test
@TestMetadata("compatibility.kt")
public void testCompatibility() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter/compatibility.kt");
}
@Test
@TestMetadata("default.kt")
public void testDefault() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter/default.kt");
}
}
}
@Nested
@@ -16384,6 +16384,39 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
public void testKt27416() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/kt27416.kt");
}
@TestMetadata("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class OverrideFunctionWithDefaultParameter extends AbstractLightAnalysisModeTest {
@TestMetadata("all.kt")
public void ignoreAll() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter/all.kt");
}
@TestMetadata("all-compatibility.kt")
public void ignoreAll_compatibility() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter/all-compatibility.kt");
}
@TestMetadata("compatibility.kt")
public void ignoreCompatibility() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter/compatibility.kt");
}
@TestMetadata("default.kt")
public void ignoreDefault() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter/default.kt");
}
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath);
}
public void testAllFilesPresentInOverrideFunctionWithDefaultParameter() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
}
}
@TestMetadata("compiler/testData/codegen/box/inlineClasses/funInterface")
@@ -14383,6 +14383,19 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
public void testKt27416() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/kt27416.kt");
}
@TestMetadata("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class OverrideFunctionWithDefaultParameter extends AbstractIrJsCodegenBoxES6Test {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS_IR_ES6, testDataFilePath);
}
public void testAllFilesPresentInOverrideFunctionWithDefaultParameter() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR_ES6, true);
}
}
}
@TestMetadata("compiler/testData/codegen/box/inlineClasses/funInterface")
@@ -13789,6 +13789,19 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
public void testKt27416() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/kt27416.kt");
}
@TestMetadata("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class OverrideFunctionWithDefaultParameter extends AbstractIrJsCodegenBoxTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS_IR, testDataFilePath);
}
public void testAllFilesPresentInOverrideFunctionWithDefaultParameter() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true);
}
}
}
@TestMetadata("compiler/testData/codegen/box/inlineClasses/funInterface")
@@ -13854,6 +13854,19 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
public void testKt27416() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/kt27416.kt");
}
@TestMetadata("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class OverrideFunctionWithDefaultParameter extends AbstractJsCodegenBoxTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS, testDataFilePath);
}
public void testAllFilesPresentInOverrideFunctionWithDefaultParameter() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS, true);
}
}
}
@TestMetadata("compiler/testData/codegen/box/inlineClasses/funInterface")
@@ -7779,6 +7779,19 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
public void testKt27416() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/kt27416.kt");
}
@TestMetadata("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class OverrideFunctionWithDefaultParameter extends AbstractIrCodegenBoxWasmTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.WASM, testDataFilePath);
}
public void testAllFilesPresentInOverrideFunctionWithDefaultParameter() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/inlineClasses/defaultParameterValues/overrideFunctionWithDefaultParameter"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true);
}
}
}
@TestMetadata("compiler/testData/codegen/box/inlineClasses/funInterface")