[FIR] KT-57104: Preserve @JvmName on fake-override accessors
Otherwise, when we come to `ClassCodegen.kt:173 for `GradleActionTest` and check `FUN FAKE_OVERRIDE name:<get-project>`, we then go to `JvmSignatureClashDetector.kt:37`, and call `mapRawSignature(overriddenFunction)` which ignores the original `@JvmName`. It does so because it relies on the property copy which forgets to copy the getter, but `@get:JvmName` is stored in it. Extra when-branches for `FAKE_OVERRIDE` are needed, because otherwise the annotations would not be copied in `Fir2IrDeclarationStorage.kt:723`. Extra when-branches for `DELEGATED_MEMBER` are needed, because otherwise the generated IR changes in some tests. For example, see: `FirLightTreeIrTextTestGenerated.Declarations.testKt35550`. The `assumesBackingField`-related change is backed by the `FirLightTreeIrTextTestGenerated.Stubs.testJavaEnum` test. `this.body = null` ensures the resulting IR matches K1. The change in `FirImplicitBodyResolve.kt` is needed, because otherwise the bootstrap compiler fails at `:compiler:frontend:compileKotlin`, but I didn't come up with a smaller test for it. If we don't make an explicit accessor copy, then when we later create a `Fir2IrLazyPropertyAccessor` for the fake override getter, it's `fir` will be a reference to the `FirProperty`, not `FirPropertyAccessor`. That's why `DumpIrTreeVisitor` will render `IntrinsicConstEvaluation` as a getter annotation as well. `FirPsiIrTextTestGenerated.testDelegatedGenericImplementation` renders type parameters from `<get-x>`, because when assigning `extensionReceiverParameter` of the setter `<set-x>` we come to `Fir2IrClassifierStorage.kt:638`, and in this cache there's the parameter with the `<get-x>` parent. Note that `typeContext.origin == DEFAULT` in `getCachedIrTypeParameter`. It's `DEFAULT` because at the line `Fir2IrDeclarationStorage.kt:335` the `function` variable is `null` (because there's no setter). The change in `declarationAttributes.kt` is backed by the `FirPsiIrTextTestGenerated.testKt45853` test. ^KT-57104 Fixed ^KT-57432 Fixed Merge-request: KT-MR-9210 Merged-by: Nikolay Lunyak <Nikolay.Lunyak@jetbrains.com>
This commit is contained in:
committed by
Space Team
parent
9a4a3d1f49
commit
7b04201e77
+11
-4
@@ -323,6 +323,7 @@ class Fir2IrDeclarationStorage(
|
||||
function: FirFunction?,
|
||||
containingClass: IrClass?,
|
||||
isStatic: Boolean,
|
||||
forSetter: Boolean,
|
||||
// Can be not-null only for property accessors
|
||||
parentPropertyReceiver: FirReceiverParameter?
|
||||
) {
|
||||
@@ -332,7 +333,6 @@ class Fir2IrDeclarationStorage(
|
||||
setTypeParameters(function)
|
||||
}
|
||||
}
|
||||
val forSetter = function is FirPropertyAccessor && function.isSetter
|
||||
val typeContext = ConversionTypeContext(
|
||||
origin = if (forSetter) ConversionTypeOrigin.SETTER else ConversionTypeOrigin.DEFAULT
|
||||
)
|
||||
@@ -417,10 +417,11 @@ class Fir2IrDeclarationStorage(
|
||||
irParent: IrDeclarationParent?,
|
||||
thisReceiverOwner: IrClass? = irParent as? IrClass,
|
||||
isStatic: Boolean,
|
||||
forSetter: Boolean,
|
||||
parentPropertyReceiver: FirReceiverParameter? = null
|
||||
): T {
|
||||
setAndModifyParent(irParent)
|
||||
declareParameters(function, thisReceiverOwner, isStatic, parentPropertyReceiver)
|
||||
declareParameters(function, thisReceiverOwner, isStatic, forSetter, parentPropertyReceiver)
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -563,7 +564,8 @@ class Fir2IrDeclarationStorage(
|
||||
enterScope(this)
|
||||
bindAndDeclareParameters(
|
||||
function, irParent,
|
||||
thisReceiverOwner, isStatic = simpleFunction?.isStatic == true
|
||||
thisReceiverOwner, isStatic = simpleFunction?.isStatic == true,
|
||||
forSetter = false,
|
||||
)
|
||||
convertAnnotationsForNonDeclaredMembers(function, origin)
|
||||
leaveScope(this)
|
||||
@@ -645,7 +647,7 @@ class Fir2IrDeclarationStorage(
|
||||
).apply {
|
||||
metadata = FirMetadataSource.Function(constructor)
|
||||
enterScope(this)
|
||||
bindAndDeclareParameters(constructor, irParent, isStatic = false)
|
||||
bindAndDeclareParameters(constructor, irParent, isStatic = false, forSetter = false)
|
||||
leaveScope(this)
|
||||
}
|
||||
}
|
||||
@@ -745,6 +747,7 @@ class Fir2IrDeclarationStorage(
|
||||
bindAndDeclareParameters(
|
||||
propertyAccessor, irParent,
|
||||
thisReceiverOwner, isStatic = irParent !is IrClass || propertyAccessor?.isStatic == true,
|
||||
forSetter = isSetter,
|
||||
parentPropertyReceiver = property.receiverParameter,
|
||||
)
|
||||
leaveScope(this)
|
||||
@@ -940,6 +943,8 @@ class Fir2IrDeclarationStorage(
|
||||
origin == IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB -> origin
|
||||
origin == IrDeclarationOrigin.ENUM_CLASS_SPECIAL_MEMBER -> origin
|
||||
delegate != null -> IrDeclarationOrigin.DELEGATED_PROPERTY_ACCESSOR
|
||||
origin == IrDeclarationOrigin.FAKE_OVERRIDE -> origin
|
||||
origin == IrDeclarationOrigin.DELEGATED_MEMBER -> origin
|
||||
getter is FirDefaultPropertyGetter -> IrDeclarationOrigin.DEFAULT_PROPERTY_ACCESSOR
|
||||
else -> origin
|
||||
},
|
||||
@@ -953,6 +958,8 @@ class Fir2IrDeclarationStorage(
|
||||
setter, property, this, type, irParent, thisReceiverOwner, true,
|
||||
when {
|
||||
delegate != null -> IrDeclarationOrigin.DELEGATED_PROPERTY_ACCESSOR
|
||||
origin == IrDeclarationOrigin.FAKE_OVERRIDE -> origin
|
||||
origin == IrDeclarationOrigin.DELEGATED_MEMBER -> origin
|
||||
setter is FirDefaultPropertySetter -> IrDeclarationOrigin.DEFAULT_PROPERTY_ACCESSOR
|
||||
else -> origin
|
||||
},
|
||||
|
||||
@@ -129,7 +129,7 @@ class Fir2IrLazyProperty(
|
||||
}
|
||||
}
|
||||
}
|
||||
fir.initializer != null || fir.getter is FirDefaultPropertyGetter || fir.isVar && fir.setter is FirDefaultPropertySetter -> {
|
||||
fir.hasBackingField && origin != IrDeclarationOrigin.FAKE_OVERRIDE -> {
|
||||
with(declarationStorage) {
|
||||
createBackingField(
|
||||
fir, IrDeclarationOrigin.PROPERTY_BACKING_FIELD,
|
||||
@@ -170,6 +170,8 @@ class Fir2IrLazyProperty(
|
||||
when {
|
||||
origin == IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB -> origin
|
||||
fir.delegate != null -> IrDeclarationOrigin.DELEGATED_PROPERTY_ACCESSOR
|
||||
origin == IrDeclarationOrigin.FAKE_OVERRIDE -> origin
|
||||
origin == IrDeclarationOrigin.DELEGATED_MEMBER -> origin
|
||||
fir.getter is FirDefaultPropertyGetter -> IrDeclarationOrigin.DEFAULT_PROPERTY_ACCESSOR
|
||||
else -> origin
|
||||
},
|
||||
@@ -203,6 +205,8 @@ class Fir2IrLazyProperty(
|
||||
when {
|
||||
origin == IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB -> origin
|
||||
fir.delegate != null -> IrDeclarationOrigin.DELEGATED_PROPERTY_ACCESSOR
|
||||
origin == IrDeclarationOrigin.FAKE_OVERRIDE -> origin
|
||||
origin == IrDeclarationOrigin.DELEGATED_MEMBER -> origin
|
||||
fir.setter is FirDefaultPropertySetter -> IrDeclarationOrigin.DEFAULT_PROPERTY_ACCESSOR
|
||||
else -> origin
|
||||
},
|
||||
|
||||
+12
@@ -48,6 +48,18 @@ public class FirPsiOldDiagnosticsTestWithJvmIrBackendGenerated extends AbstractF
|
||||
runTest("compiler/testData/diagnostics/testsWithJvmBackend/inlineCycle_ir.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt57104.kt")
|
||||
public void testKt57104() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/testsWithJvmBackend/kt57104.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt57104_2.kt")
|
||||
public void testKt57104_2() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/testsWithJvmBackend/kt57104_2.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("multipleBigArityFunsImplemented.kt")
|
||||
public void testMultipleBigArityFunsImplemented() throws Exception {
|
||||
|
||||
+108
-2
@@ -10,10 +10,12 @@ import org.jetbrains.kotlin.descriptors.Visibility
|
||||
import org.jetbrains.kotlin.fir.*
|
||||
import org.jetbrains.kotlin.fir.declarations.*
|
||||
import org.jetbrains.kotlin.fir.declarations.builder.*
|
||||
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.synthetic.FirSyntheticProperty
|
||||
import org.jetbrains.kotlin.fir.declarations.synthetic.buildSyntheticProperty
|
||||
import org.jetbrains.kotlin.fir.declarations.utils.isExpect
|
||||
import org.jetbrains.kotlin.fir.declarations.utils.isStatic
|
||||
import org.jetbrains.kotlin.fir.declarations.utils.*
|
||||
import org.jetbrains.kotlin.fir.resolve.substitution.ChainedSubstitutor
|
||||
import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor
|
||||
import org.jetbrains.kotlin.fir.resolve.substitution.substitutorByMap
|
||||
@@ -374,11 +376,115 @@ object FirFakeOverrideGenerator {
|
||||
fakeOverrideSubstitution
|
||||
)
|
||||
deprecationsProvider = baseProperty.deprecationsProvider
|
||||
|
||||
getter = baseProperty.getter?.buildCopyIfNeeded(
|
||||
moduleData = session.moduleData,
|
||||
origin = origin,
|
||||
propertyReturnTypeRef = this@buildProperty.returnTypeRef,
|
||||
propertySymbol = newSymbol,
|
||||
dispatchReceiverType = dispatchReceiverType,
|
||||
derivedClassLookupTag = derivedClassLookupTag,
|
||||
baseProperty = baseProperty,
|
||||
)
|
||||
|
||||
setter = baseProperty.setter?.buildCopyIfNeeded(
|
||||
moduleData = session.moduleData,
|
||||
origin = origin,
|
||||
propertyReturnTypeRef = this@buildProperty.returnTypeRef,
|
||||
propertySymbol = newSymbol,
|
||||
dispatchReceiverType = dispatchReceiverType,
|
||||
derivedClassLookupTag = derivedClassLookupTag,
|
||||
baseProperty = baseProperty,
|
||||
)
|
||||
}.apply {
|
||||
containingClassForStaticMemberAttr = derivedClassLookupTag.takeIf { shouldOverrideSetContainingClass(baseProperty) }
|
||||
}
|
||||
}
|
||||
|
||||
private fun FirPropertyAccessor.buildCopyIfNeeded(
|
||||
moduleData: FirModuleData,
|
||||
origin: FirDeclarationOrigin,
|
||||
propertyReturnTypeRef: FirTypeRef,
|
||||
propertySymbol: FirPropertySymbol,
|
||||
dispatchReceiverType: ConeSimpleKotlinType?,
|
||||
derivedClassLookupTag: ConeClassLikeLookupTag?,
|
||||
baseProperty: FirProperty,
|
||||
) = when {
|
||||
annotations.isNotEmpty() || visibility != baseProperty.visibility -> buildCopy(
|
||||
moduleData,
|
||||
origin,
|
||||
propertyReturnTypeRef,
|
||||
propertySymbol,
|
||||
dispatchReceiverType,
|
||||
derivedClassLookupTag,
|
||||
baseProperty,
|
||||
)
|
||||
else -> null
|
||||
}
|
||||
|
||||
private fun FirPropertyAccessor.buildCopy(
|
||||
moduleData: FirModuleData,
|
||||
origin: FirDeclarationOrigin,
|
||||
propertyReturnTypeRef: FirTypeRef,
|
||||
propertySymbol: FirPropertySymbol,
|
||||
dispatchReceiverType: ConeSimpleKotlinType?,
|
||||
derivedClassLookupTag: ConeClassLikeLookupTag?,
|
||||
baseProperty: FirProperty,
|
||||
) = when (this) {
|
||||
is FirDefaultPropertyGetter -> FirDefaultPropertyGetter(
|
||||
source = source,
|
||||
moduleData = moduleData,
|
||||
origin = origin,
|
||||
propertyTypeRef = propertyReturnTypeRef,
|
||||
visibility = visibility,
|
||||
propertySymbol = propertySymbol,
|
||||
modality = modality ?: Modality.FINAL,
|
||||
effectiveVisibility = effectiveVisibility,
|
||||
).apply {
|
||||
replaceAnnotations(annotations)
|
||||
}
|
||||
is FirDefaultPropertySetter -> FirDefaultPropertySetter(
|
||||
source = source,
|
||||
moduleData = moduleData,
|
||||
origin = origin,
|
||||
propertyTypeRef = propertyReturnTypeRef,
|
||||
visibility = visibility,
|
||||
propertySymbol = propertySymbol,
|
||||
modality = modality ?: Modality.FINAL,
|
||||
effectiveVisibility = effectiveVisibility,
|
||||
).apply {
|
||||
replaceAnnotations(annotations)
|
||||
}
|
||||
else -> buildPropertyAccessorCopy(this) {
|
||||
this.symbol = FirPropertyAccessorSymbol()
|
||||
this.moduleData = moduleData
|
||||
this.origin = origin
|
||||
this.propertySymbol = propertySymbol
|
||||
this.dispatchReceiverType = dispatchReceiverType
|
||||
this.body = null
|
||||
}.also {
|
||||
if (it.isSetter) {
|
||||
val newParameter = buildValueParameterCopy(it.valueParameters.first()) {
|
||||
this.symbol = FirValueParameterSymbol(symbol.name)
|
||||
this.returnTypeRef = propertyReturnTypeRef
|
||||
}
|
||||
it.replaceValueParameters(listOf(newParameter))
|
||||
} else {
|
||||
it.replaceReturnTypeRef(propertyReturnTypeRef)
|
||||
}
|
||||
}
|
||||
}.also { accessor ->
|
||||
when (accessor.origin) {
|
||||
FirDeclarationOrigin.IntersectionOverride -> accessor.originalForIntersectionOverrideAttr = this
|
||||
FirDeclarationOrigin.SubstitutionOverride -> accessor.originalForSubstitutionOverrideAttr = this
|
||||
else -> {}
|
||||
}
|
||||
|
||||
accessor.containingClassForStaticMemberAttr = derivedClassLookupTag.takeIf {
|
||||
accessor is FirDefaultPropertyAccessor || shouldOverrideSetContainingClass(baseProperty)
|
||||
}
|
||||
}
|
||||
|
||||
fun createCopyForFirField(
|
||||
newSymbol: FirFieldSymbol,
|
||||
baseField: FirField,
|
||||
|
||||
+4
@@ -257,6 +257,10 @@ private class ReturnTypeCalculatorWithJump(
|
||||
val coneType = substitutor.substituteOrSelf(baseReturnType)
|
||||
val returnType = declaration.returnTypeRef.resolvedTypeFromPrototype(coneType)
|
||||
declaration.replaceReturnTypeRef(returnType)
|
||||
if (declaration is FirProperty) {
|
||||
declaration.getter?.replaceReturnTypeRef(returnType)
|
||||
declaration.setter?.valueParameters?.firstOrNull()?.replaceReturnTypeRef(returnType)
|
||||
}
|
||||
return returnType
|
||||
}
|
||||
}
|
||||
|
||||
+2
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.fir.expressions.FirPropertyAccessExpression
|
||||
import org.jetbrains.kotlin.fir.references.impl.FirPropertyFromParameterResolvedNamedReference
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirClassLikeSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirSyntheticPropertySymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirValueParameterSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.lazyResolveToPhase
|
||||
import org.jetbrains.kotlin.fir.types.coneType
|
||||
@@ -88,6 +89,7 @@ val FirProperty.hasBackingField: Boolean
|
||||
if (isAbstract) return false
|
||||
if (delegate != null) return false
|
||||
if (hasExplicitBackingField) return true
|
||||
if (symbol is FirSyntheticPropertySymbol) return false
|
||||
if (isStatic) return false // For Enum.entries
|
||||
when (origin) {
|
||||
FirDeclarationOrigin.SubstitutionOverride -> return false
|
||||
|
||||
Reference in New Issue
Block a user