FIR: use default getter in serializer if fir contains none

since we generate the default getter in this case in
Fir2IrDeclarationStorage.createIrProperty, so the serialized metadata
should follow the same behavior.
#KT-57373 fixed
This commit is contained in:
Ilya Chernikov
2023-03-24 15:42:48 +01:00
committed by Space Team
parent 913b55174d
commit a045a0a81c
22 changed files with 227 additions and 2 deletions
@@ -18,6 +18,8 @@ import org.jetbrains.kotlin.fir.*
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.comparators.FirCallableDeclarationComparator
import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyAccessor
import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyGetter
import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertySetter
import org.jetbrains.kotlin.fir.declarations.utils.*
import org.jetbrains.kotlin.fir.deserialization.projection
import org.jetbrains.kotlin.fir.expressions.*
@@ -341,7 +343,14 @@ class FirElementSerializer private constructor(
false, false, false
)
val getter = property.getter
val getter = property.getter ?: with(property) {
if (origin == FirDeclarationOrigin.Delegated) {
// since we generate the default accessor on fir2ir anyway (Fir2IrDeclarationStorage.createIrProperty), we have to
// serialize it accordingly at least for delegates to fix issues like #KT-57373
// TODO: rewrite accordingly after fixing #KT-58233
FirDefaultPropertyGetter(source = null, moduleData, origin, returnTypeRef, visibility, symbol)
} else null
}
if (getter != null) {
hasGetter = true
val accessorFlags = getAccessorFlags(getter, property)
@@ -350,7 +359,14 @@ class FirElementSerializer private constructor(
}
}
val setter = property.setter
val setter = property.setter ?: with(property) {
if (origin == FirDeclarationOrigin.Delegated && isVar) {
// since we generate the default accessor on fir2ir anyway (Fir2IrDeclarationStorage.createIrProperty), we have to
// serialize it accordingly at least for delegates to fix issues like #KT-57373
// TODO: rewrite accordingly after fixing #KT-58233
FirDefaultPropertySetter(source = null, moduleData, origin, returnTypeRef, visibility, symbol)
} else null
}
if (setter != null) {
hasSetter = true
val accessorFlags = getAccessorFlags(setter, property)
@@ -15709,6 +15709,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule.kt");
}
@Test
@TestMetadata("delegationDifferentModule2.kt")
public void testDelegationDifferentModule2() throws Exception {
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule2.kt");
}
@Test
@TestMetadata("delegationToIntersectionType.kt")
public void testDelegationToIntersectionType() throws Exception {
@@ -15709,6 +15709,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule.kt");
}
@Test
@TestMetadata("delegationDifferentModule2.kt")
public void testDelegationDifferentModule2() throws Exception {
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule2.kt");
}
@Test
@TestMetadata("delegationToIntersectionType.kt")
public void testDelegationToIntersectionType() throws Exception {
@@ -0,0 +1,24 @@
// MODULE: lib
// FILE: lib.kt
interface I {
val bar: Int
}
class Impl : I {
override val bar: Int = 42
}
class D1(foo: I) : I by foo
// MODULE: main(lib)
// FILE: main.kt
class D2(foo: I) : I by foo
fun box() : String {
val c = Impl()
if (D1(c).bar != 42) return "FAIL 1"
if (D2(c).bar != 42) return "FAIL 2"
return "OK"
}
@@ -15373,6 +15373,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule.kt");
}
@Test
@TestMetadata("delegationDifferentModule2.kt")
public void testDelegationDifferentModule2() throws Exception {
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule2.kt");
}
@Test
@TestMetadata("delegationToIntersectionType.kt")
public void testDelegationToIntersectionType() throws Exception {
@@ -15709,6 +15709,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule.kt");
}
@Test
@TestMetadata("delegationDifferentModule2.kt")
public void testDelegationDifferentModule2() throws Exception {
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule2.kt");
}
@Test
@TestMetadata("delegationToIntersectionType.kt")
public void testDelegationToIntersectionType() throws Exception {
@@ -15709,6 +15709,12 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule.kt");
}
@Test
@TestMetadata("delegationDifferentModule2.kt")
public void testDelegationDifferentModule2() throws Exception {
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule2.kt");
}
@Test
@TestMetadata("delegationToIntersectionType.kt")
public void testDelegationToIntersectionType() throws Exception {
@@ -12630,6 +12630,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule.kt");
}
@TestMetadata("delegationDifferentModule2.kt")
public void testDelegationDifferentModule2() throws Exception {
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule2.kt");
}
@TestMetadata("delegationToIntersectionType.kt")
public void testDelegationToIntersectionType() throws Exception {
runTest("compiler/testData/codegen/box/delegation/delegationToIntersectionType.kt");
@@ -11785,6 +11785,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule.kt");
}
@Test
@TestMetadata("delegationDifferentModule2.kt")
public void testDelegationDifferentModule2() throws Exception {
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule2.kt");
}
@Test
@TestMetadata("delegationToIntersectionType.kt")
public void testDelegationToIntersectionType() throws Exception {
@@ -11881,6 +11881,12 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule.kt");
}
@Test
@TestMetadata("delegationDifferentModule2.kt")
public void testDelegationDifferentModule2() throws Exception {
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule2.kt");
}
@Test
@TestMetadata("delegationToIntersectionType.kt")
public void testDelegationToIntersectionType() throws Exception {
@@ -11881,6 +11881,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule.kt");
}
@Test
@TestMetadata("delegationDifferentModule2.kt")
public void testDelegationDifferentModule2() throws Exception {
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule2.kt");
}
@Test
@TestMetadata("delegationToIntersectionType.kt")
public void testDelegationToIntersectionType() throws Exception {
@@ -11881,6 +11881,12 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule.kt");
}
@Test
@TestMetadata("delegationDifferentModule2.kt")
public void testDelegationDifferentModule2() throws Exception {
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule2.kt");
}
@Test
@TestMetadata("delegationToIntersectionType.kt")
public void testDelegationToIntersectionType() throws Exception {
@@ -1294,6 +1294,27 @@ __attribute__((swift_name("Kt56521Kt")))
@property (class) int32_t initialized __attribute__((swift_name("initialized")));
@end
__attribute__((swift_name("IKt57373")))
@protocol KtIKt57373
@required
@property (readonly) int32_t bar __attribute__((swift_name("bar")));
@end
__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("DKt57373")))
@interface KtDKt57373 : KtBase <KtIKt57373>
- (instancetype)initWithFoo:(id<KtIKt57373>)foo __attribute__((swift_name("init(foo:)"))) __attribute__((objc_designated_initializer));
@property (readonly) int32_t bar __attribute__((swift_name("bar")));
@end
__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("CKt57373")))
@interface KtCKt57373 : KtBase <KtIKt57373>
- (instancetype)init __attribute__((swift_name("init()"))) __attribute__((objc_designated_initializer));
+ (instancetype)new __attribute__((availability(swift, unavailable, message="use object initializers instead")));
@property (readonly) int32_t bar __attribute__((swift_name("bar")));
@end
__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("LibraryKt")))
@interface KtLibraryKt : KtBase
@@ -1294,6 +1294,27 @@ __attribute__((swift_name("Kt56521Kt")))
@property (class) int32_t initialized __attribute__((swift_name("initialized")));
@end
__attribute__((swift_name("IKt57373")))
@protocol KtIKt57373
@required
@property (readonly) int32_t bar __attribute__((swift_name("bar")));
@end
__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("DKt57373")))
@interface KtDKt57373 : KtBase <KtIKt57373>
- (instancetype)initWithFoo:(id<KtIKt57373>)foo __attribute__((swift_name("init(foo:)"))) __attribute__((objc_designated_initializer));
@property (readonly) int32_t bar __attribute__((swift_name("bar")));
@end
__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("CKt57373")))
@interface KtCKt57373 : KtBase <KtIKt57373>
- (instancetype)init __attribute__((swift_name("init()"))) __attribute__((objc_designated_initializer));
+ (instancetype)new __attribute__((availability(swift, unavailable, message="use object initializers instead")));
@property (readonly) int32_t bar __attribute__((swift_name("bar")));
@end
__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("LibraryKt")))
@interface KtLibraryKt : KtBase
@@ -1294,6 +1294,27 @@ __attribute__((swift_name("Kt56521Kt")))
@property (class) int32_t initialized __attribute__((swift_name("initialized")));
@end
__attribute__((swift_name("IKt57373")))
@protocol KtIKt57373
@required
@property (readonly) int32_t bar __attribute__((swift_name("bar")));
@end
__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("DKt57373")))
@interface KtDKt57373 : KtBase <KtIKt57373>
- (instancetype)initWithFoo:(id<KtIKt57373>)foo __attribute__((swift_name("init(foo:)"))) __attribute__((objc_designated_initializer));
@property (readonly) int32_t bar __attribute__((swift_name("bar")));
@end
__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("CKt57373")))
@interface KtCKt57373 : KtBase <KtIKt57373>
- (instancetype)init __attribute__((swift_name("init()"))) __attribute__((objc_designated_initializer));
+ (instancetype)new __attribute__((availability(swift, unavailable, message="use object initializers instead")));
@property (readonly) int32_t bar __attribute__((swift_name("bar")));
@end
__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("LibraryKt")))
@interface KtLibraryKt : KtBase
@@ -0,0 +1,14 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
interface IKt57373 {
val bar: Int
}
class DKt57373(foo: IKt57373) : IKt57373 by foo
class CKt57373 : IKt57373 {
override val bar: Int = 42
}
@@ -0,0 +1,20 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
import Kt
func testKt57373() throws {
let impl = CKt57373()
let x = DKt57373(foo: impl)
try assertEquals(actual: x.bar, expected: 42)
}
class Kt57373Tests : SimpleTestProvider {
override init() {
super.init()
test("testKt57373", testKt57373)
}
}
@@ -13138,6 +13138,12 @@ public class FirNativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTe
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule.kt");
}
@Test
@TestMetadata("delegationDifferentModule2.kt")
public void testDelegationDifferentModule2() throws Exception {
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule2.kt");
}
@Test
@TestMetadata("delegationToIntersectionType.kt")
public void testDelegationToIntersectionType() throws Exception {
@@ -13436,6 +13436,12 @@ public class FirNativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenB
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule.kt");
}
@Test
@TestMetadata("delegationDifferentModule2.kt")
public void testDelegationDifferentModule2() throws Exception {
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule2.kt");
}
@Test
@TestMetadata("delegationToIntersectionType.kt")
public void testDelegationToIntersectionType() throws Exception {
@@ -12989,6 +12989,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule.kt");
}
@Test
@TestMetadata("delegationDifferentModule2.kt")
public void testDelegationDifferentModule2() throws Exception {
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule2.kt");
}
@Test
@TestMetadata("delegationToIntersectionType.kt")
public void testDelegationToIntersectionType() throws Exception {
@@ -13287,6 +13287,12 @@ public class NativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenBoxT
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule.kt");
}
@Test
@TestMetadata("delegationDifferentModule2.kt")
public void testDelegationDifferentModule2() throws Exception {
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule2.kt");
}
@Test
@TestMetadata("delegationToIntersectionType.kt")
public void testDelegationToIntersectionType() throws Exception {
@@ -10520,6 +10520,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule.kt");
}
@TestMetadata("delegationDifferentModule2.kt")
public void testDelegationDifferentModule2() throws Exception {
runTest("compiler/testData/codegen/box/delegation/delegationDifferentModule2.kt");
}
@TestMetadata("delegationToIntersectionType.kt")
public void testDelegationToIntersectionType() throws Exception {
runTest("compiler/testData/codegen/box/delegation/delegationToIntersectionType.kt");