[FIR2IR] Treat delegated functions as fake-overrides

Delegated callables in FIR are session-dependant (as fake-overrides),
  so it's incorrect to use their FIR as a key for declaration storage.
  Pair of original function and owner lookup tag should be used instead

^KT-62671 Fixed
This commit is contained in:
Dmitriy Novozhilov
2023-10-19 11:50:50 +03:00
committed by Space Team
parent 3c68b27280
commit 940567978d
19 changed files with 131 additions and 6 deletions
@@ -13,7 +13,6 @@ import org.jetbrains.kotlin.fir.*
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.builder.buildProperty
import org.jetbrains.kotlin.fir.declarations.synthetic.FirSyntheticProperty
import org.jetbrains.kotlin.fir.declarations.utils.isExpect
import org.jetbrains.kotlin.fir.declarations.utils.isStatic
import org.jetbrains.kotlin.fir.declarations.utils.nameOrSpecialName
import org.jetbrains.kotlin.fir.declarations.utils.visibility
@@ -36,11 +35,12 @@ import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.builders.declarations.UNDEFINED_PARAMETER_INDEX
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrClassImpl
import org.jetbrains.kotlin.ir.declarations.lazy.IrLazyClass
import org.jetbrains.kotlin.ir.expressions.IrSyntheticBodyKind
import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.symbols.impl.IrClassSymbolImpl
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.util.IdSignature
import org.jetbrains.kotlin.ir.util.classId
import org.jetbrains.kotlin.ir.util.createParameterDeclarations
import org.jetbrains.kotlin.load.kotlin.FacadeClassSource
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
@@ -321,7 +321,7 @@ class Fir2IrDeclarationStorage(
}
function.isFakeOverride(fakeOverrideOwnerLookupTag) -> {
val originalFunction = function.unwrapFakeOverrides()
val originalFunction = function.unwrapFakeOverridesOrDelegated()
val key = FakeOverrideIdentifier(
originalFunction.symbol,
fakeOverrideOwnerLookupTag ?: function.containingClassLookupTag()!!
@@ -516,7 +516,7 @@ class Fir2IrDeclarationStorage(
setterForPropertyCache[irPropertySymbol] = it.symbol
}
if (property.isFakeOverride(fakeOverrideOwnerLookupTag)) {
val originalProperty = property.unwrapFakeOverrides()
val originalProperty = property.unwrapFakeOverridesOrDelegated()
val key = FakeOverrideIdentifier(
originalProperty.symbol,
fakeOverrideOwnerLookupTag ?: property.containingClassLookupTag()!!
@@ -1155,7 +1155,7 @@ class Fir2IrDeclarationStorage(
}
internal fun FirCallableDeclaration.isFakeOverride(fakeOverrideOwnerLookupTag: ConeClassLikeLookupTag?): Boolean {
if (isSubstitutionOrIntersectionOverride) return true
if (isCopyCreatedInScope) return true
if (fakeOverrideOwnerLookupTag == null) return false
// this condition is true for all places when we are trying to create "fake" fake overrides in IR
// "fake" fake overrides are f/o which are presented in IR but have no corresponding FIR f/o
@@ -34521,6 +34521,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/constructor.kt");
}
@Test
@TestMetadata("defaultArgumentInDelegatedFunction.kt")
public void testDefaultArgumentInDelegatedFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/defaultArgumentInDelegatedFunction.kt");
}
@Test
@TestMetadata("delegatedExpectedInterface.kt")
public void testDelegatedExpectedInterface() throws Exception {
@@ -34521,6 +34521,12 @@ public class FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/constructor.kt");
}
@Test
@TestMetadata("defaultArgumentInDelegatedFunction.kt")
public void testDefaultArgumentInDelegatedFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/defaultArgumentInDelegatedFunction.kt");
}
@Test
@TestMetadata("delegatedExpectedInterface.kt")
public void testDelegatedExpectedInterface() throws Exception {
@@ -34521,6 +34521,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/constructor.kt");
}
@Test
@TestMetadata("defaultArgumentInDelegatedFunction.kt")
public void testDefaultArgumentInDelegatedFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/defaultArgumentInDelegatedFunction.kt");
}
@Test
@TestMetadata("delegatedExpectedInterface.kt")
public void testDelegatedExpectedInterface() throws Exception {
@@ -0,0 +1,24 @@
// IGNORE_BACKEND_K1: JS, JS_IR, JS_IR_ES6, NATIVE, WASM
// LANGUAGE: +MultiPlatformProjects
// ISSUE: KT-62671
// MODULE: common
// FILE: common.kt
interface A {
fun foo(x: Int = 1): String
}
class B : A {
override fun foo(x: Int): String {
return if (x == 1) "OK" else "Fail: $x"
}
}
class X(val delegate: A = B()) : A by delegate
// MODULE: platform()()(common)
// FILE: platform.kt
fun box(): String {
val x = X()
return x.foo()
}
@@ -32757,6 +32757,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/constructor.kt");
}
@Test
@TestMetadata("defaultArgumentInDelegatedFunction.kt")
public void testDefaultArgumentInDelegatedFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/defaultArgumentInDelegatedFunction.kt");
}
@Test
@TestMetadata("delegatedExpectedInterface.kt")
public void testDelegatedExpectedInterface() throws Exception {
@@ -34521,6 +34521,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/constructor.kt");
}
@Test
@TestMetadata("defaultArgumentInDelegatedFunction.kt")
public void testDefaultArgumentInDelegatedFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/defaultArgumentInDelegatedFunction.kt");
}
@Test
@TestMetadata("delegatedExpectedInterface.kt")
public void testDelegatedExpectedInterface() throws Exception {
@@ -34521,6 +34521,12 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/constructor.kt");
}
@Test
@TestMetadata("defaultArgumentInDelegatedFunction.kt")
public void testDefaultArgumentInDelegatedFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/defaultArgumentInDelegatedFunction.kt");
}
@Test
@TestMetadata("delegatedExpectedInterface.kt")
public void testDelegatedExpectedInterface() throws Exception {
@@ -29532,6 +29532,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/annotations.kt");
}
@TestMetadata("defaultArgumentInDelegatedFunction.kt")
public void testDefaultArgumentInDelegatedFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/defaultArgumentInDelegatedFunction.kt");
}
@TestMetadata("expectPropertyAsDefaultArgument.kt")
public void testExpectPropertyAsDefaultArgument() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/expectPropertyAsDefaultArgument.kt");
@@ -24147,6 +24147,12 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/constructor.kt");
}
@Test
@TestMetadata("defaultArgumentInDelegatedFunction.kt")
public void testDefaultArgumentInDelegatedFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/defaultArgumentInDelegatedFunction.kt");
}
@Test
@TestMetadata("delegatedExpectedInterface.kt")
public void testDelegatedExpectedInterface() throws Exception {
@@ -24147,6 +24147,12 @@ public class FirJsES6CodegenBoxTestGenerated extends AbstractFirJsES6CodegenBoxT
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/constructor.kt");
}
@Test
@TestMetadata("defaultArgumentInDelegatedFunction.kt")
public void testDefaultArgumentInDelegatedFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/defaultArgumentInDelegatedFunction.kt");
}
@Test
@TestMetadata("delegatedExpectedInterface.kt")
public void testDelegatedExpectedInterface() throws Exception {
@@ -24147,6 +24147,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/constructor.kt");
}
@Test
@TestMetadata("defaultArgumentInDelegatedFunction.kt")
public void testDefaultArgumentInDelegatedFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/defaultArgumentInDelegatedFunction.kt");
}
@Test
@TestMetadata("delegatedExpectedInterface.kt")
public void testDelegatedExpectedInterface() throws Exception {
@@ -24147,6 +24147,12 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/constructor.kt");
}
@Test
@TestMetadata("defaultArgumentInDelegatedFunction.kt")
public void testDefaultArgumentInDelegatedFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/defaultArgumentInDelegatedFunction.kt");
}
@Test
@TestMetadata("delegatedExpectedInterface.kt")
public void testDelegatedExpectedInterface() throws Exception {
@@ -27134,6 +27134,12 @@ public class FirNativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTe
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/constructor.kt");
}
@Test
@TestMetadata("defaultArgumentInDelegatedFunction.kt")
public void testDefaultArgumentInDelegatedFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/defaultArgumentInDelegatedFunction.kt");
}
@Test
@TestMetadata("delegatedExpectedInterface.kt")
public void testDelegatedExpectedInterface() throws Exception {
@@ -27758,6 +27758,12 @@ public class FirNativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenB
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/constructor.kt");
}
@Test
@TestMetadata("defaultArgumentInDelegatedFunction.kt")
public void testDefaultArgumentInDelegatedFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/defaultArgumentInDelegatedFunction.kt");
}
@Test
@TestMetadata("delegatedExpectedInterface.kt")
public void testDelegatedExpectedInterface() throws Exception {
@@ -26823,6 +26823,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/constructor.kt");
}
@Test
@TestMetadata("defaultArgumentInDelegatedFunction.kt")
public void testDefaultArgumentInDelegatedFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/defaultArgumentInDelegatedFunction.kt");
}
@Test
@TestMetadata("delegatedExpectedInterface.kt")
public void testDelegatedExpectedInterface() throws Exception {
@@ -27135,6 +27135,12 @@ public class NativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenBoxT
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/constructor.kt");
}
@Test
@TestMetadata("defaultArgumentInDelegatedFunction.kt")
public void testDefaultArgumentInDelegatedFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/defaultArgumentInDelegatedFunction.kt");
}
@Test
@TestMetadata("delegatedExpectedInterface.kt")
public void testDelegatedExpectedInterface() throws Exception {
@@ -23895,6 +23895,12 @@ public class FirWasmCodegenBoxTestGenerated extends AbstractFirWasmCodegenBoxTes
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/constructor.kt");
}
@Test
@TestMetadata("defaultArgumentInDelegatedFunction.kt")
public void testDefaultArgumentInDelegatedFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/defaultArgumentInDelegatedFunction.kt");
}
@Test
@TestMetadata("delegatedExpectedInterface.kt")
public void testDelegatedExpectedInterface() throws Exception {
@@ -23895,6 +23895,12 @@ public class K1WasmCodegenBoxTestGenerated extends AbstractK1WasmCodegenBoxTest
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/constructor.kt");
}
@Test
@TestMetadata("defaultArgumentInDelegatedFunction.kt")
public void testDefaultArgumentInDelegatedFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/k2/defaultArguments/defaultArgumentInDelegatedFunction.kt");
}
@Test
@TestMetadata("delegatedExpectedInterface.kt")
public void testDelegatedExpectedInterface() throws Exception {