Fir2Ir: simplify getCachedIrTypeParameter

I could not figure out the reason for the special case in that function.
Tests do not show anything useful.
This commit is contained in:
Georgy Bronnikov
2022-04-26 09:13:25 +03:00
committed by Alexander Udalov
parent e504d9faed
commit 3a4b8962f9
8 changed files with 57 additions and 23 deletions
@@ -96,11 +96,11 @@ class Fir2IrClassifierStorage(
internal fun preCacheTypeParameters(owner: FirTypeParameterRefsOwner, irOwnerSymbol: IrSymbol) { internal fun preCacheTypeParameters(owner: FirTypeParameterRefsOwner, irOwnerSymbol: IrSymbol) {
for ((index, typeParameter) in owner.typeParameters.withIndex()) { for ((index, typeParameter) in owner.typeParameters.withIndex()) {
val original = typeParameter.symbol.fir val original = typeParameter.symbol.fir
getCachedIrTypeParameter(original, index) getCachedIrTypeParameter(original)
?: createIrTypeParameterWithoutBounds(original, index, irOwnerSymbol) ?: createIrTypeParameterWithoutBounds(original, index, irOwnerSymbol)
if (owner is FirProperty && owner.isVar) { if (owner is FirProperty && owner.isVar) {
val context = ConversionTypeContext.DEFAULT.inSetter() val context = ConversionTypeContext.DEFAULT.inSetter()
getCachedIrTypeParameter(original, index, context) getCachedIrTypeParameter(original, context)
?: createIrTypeParameterWithoutBounds(original, index, irOwnerSymbol, context) ?: createIrTypeParameterWithoutBounds(original, index, irOwnerSymbol, context)
} }
} }
@@ -429,27 +429,12 @@ class Fir2IrClassifierStorage(
internal fun getCachedIrTypeParameter( internal fun getCachedIrTypeParameter(
typeParameter: FirTypeParameter, typeParameter: FirTypeParameter,
index: Int = UNDEFINED_PARAMETER_INDEX,
typeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT typeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT
): IrTypeParameter? { ): IrTypeParameter? {
// Here transformation is a bit difficult because one FIR property type parameter return if (typeContext.origin == ConversionTypeOrigin.SETTER)
// can be transformed to two different type parameters: one for getter and another one for setter typeParameterCacheForSetter[typeParameter]
val simpleCachedParameter = typeParameterCache[typeParameter] else
if (simpleCachedParameter != null) { typeParameterCache[typeParameter]
if (typeContext.origin != ConversionTypeOrigin.SETTER) {
return simpleCachedParameter
}
if (index < 0) {
val parent = simpleCachedParameter.parent
if (parent !is IrSimpleFunction || parent.returnType == irBuiltIns.unitType) {
return simpleCachedParameter
}
}
}
if (typeContext.origin == ConversionTypeOrigin.SETTER) {
typeParameterCacheForSetter[typeParameter]?.let { return it }
}
return null
} }
internal fun getIrTypeParameter( internal fun getIrTypeParameter(
@@ -458,7 +443,7 @@ class Fir2IrClassifierStorage(
ownerSymbol: IrSymbol, ownerSymbol: IrSymbol,
typeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT typeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT
): IrTypeParameter { ): IrTypeParameter {
getCachedIrTypeParameter(typeParameter, index, typeContext)?.let { return it } getCachedIrTypeParameter(typeParameter, typeContext)?.let { return it }
return typeParameter.run { return typeParameter.run {
val irTypeParameter = createIrTypeParameterWithoutBounds(typeParameter, index, ownerSymbol, typeContext) val irTypeParameter = createIrTypeParameterWithoutBounds(typeParameter, index, ownerSymbol, typeContext)
irTypeParameter.superTypes = bounds.map { it.toIrType() } irTypeParameter.superTypes = bounds.map { it.toIrType() }
@@ -592,7 +577,7 @@ class Fir2IrClassifierStorage(
typeContext: ConversionTypeContext typeContext: ConversionTypeContext
): IrTypeParameterSymbol { ): IrTypeParameterSymbol {
val firTypeParameter = firTypeParameterSymbol.fir val firTypeParameter = firTypeParameterSymbol.fir
return getCachedIrTypeParameter(firTypeParameter, typeContext = typeContext)?.symbol return getCachedIrTypeParameter(firTypeParameter, typeContext)?.symbol
// We can try to use default cache because setter can use parent type parameters // We can try to use default cache because setter can use parent type parameters
?: typeParameterCache[firTypeParameter]?.symbol ?: typeParameterCache[firTypeParameter]?.symbol
?: error("Cannot find cached type parameter by FIR symbol: ${firTypeParameterSymbol.name}") ?: error("Cannot find cached type parameter by FIR symbol: ${firTypeParameterSymbol.name}")
@@ -7181,6 +7181,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/fakeOverridesForIntersectionTypes.kt"); runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/fakeOverridesForIntersectionTypes.kt");
} }
@Test
@TestMetadata("genericSetter.kt")
public void testGenericSetter() throws Exception {
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/genericSetter.kt");
}
@Test @Test
@TestMetadata("importCompanion.kt") @TestMetadata("importCompanion.kt")
public void testImportCompanion() throws Exception { public void testImportCompanion() throws Exception {
@@ -0,0 +1,13 @@
// MODULE: lib
// FILE: 1.kt
var <T> T.prop: Int
get() = 33
set(value) {}
// MODULE: main(lib)
// FILE: 2.kt
fun <T> setProp(t: T, v: Int) {
t.prop = v
}
fun box() = "OK"
@@ -7067,6 +7067,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/fakeOverridesForIntersectionTypes.kt"); runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/fakeOverridesForIntersectionTypes.kt");
} }
@Test
@TestMetadata("genericSetter.kt")
public void testGenericSetter() throws Exception {
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/genericSetter.kt");
}
@Test @Test
@TestMetadata("importCompanion.kt") @TestMetadata("importCompanion.kt")
public void testImportCompanion() throws Exception { public void testImportCompanion() throws Exception {
@@ -7181,6 +7181,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/fakeOverridesForIntersectionTypes.kt"); runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/fakeOverridesForIntersectionTypes.kt");
} }
@Test
@TestMetadata("genericSetter.kt")
public void testGenericSetter() throws Exception {
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/genericSetter.kt");
}
@Test @Test
@TestMetadata("importCompanion.kt") @TestMetadata("importCompanion.kt")
public void testImportCompanion() throws Exception { public void testImportCompanion() throws Exception {
@@ -163,6 +163,12 @@ public class JvmIrAgainstOldBoxTestGenerated extends AbstractJvmIrAgainstOldBoxT
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/fakeOverridesForIntersectionTypes.kt"); runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/fakeOverridesForIntersectionTypes.kt");
} }
@Test
@TestMetadata("genericSetter.kt")
public void testGenericSetter() throws Exception {
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/genericSetter.kt");
}
@Test @Test
@TestMetadata("importCompanion.kt") @TestMetadata("importCompanion.kt")
public void testImportCompanion() throws Exception { public void testImportCompanion() throws Exception {
@@ -163,6 +163,12 @@ public class JvmOldAgainstIrBoxTestGenerated extends AbstractJvmOldAgainstIrBoxT
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/fakeOverridesForIntersectionTypes.kt"); runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/fakeOverridesForIntersectionTypes.kt");
} }
@Test
@TestMetadata("genericSetter.kt")
public void testGenericSetter() throws Exception {
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/genericSetter.kt");
}
@Test @Test
@TestMetadata("importCompanion.kt") @TestMetadata("importCompanion.kt")
public void testImportCompanion() throws Exception { public void testImportCompanion() throws Exception {
@@ -5515,6 +5515,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/expectClassActualTypeAlias.kt"); runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/expectClassActualTypeAlias.kt");
} }
@Test
@TestMetadata("genericSetter.kt")
public void testGenericSetter() throws Exception {
runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/genericSetter.kt");
}
@Test @Test
@TestMetadata("innerClassConstructor.kt") @TestMetadata("innerClassConstructor.kt")
public void testInnerClassConstructor() throws Exception { public void testInnerClassConstructor() throws Exception {