Put reification markers came from super object signature

#KT-44770 Fixed
  #KT-30696 Open
This commit is contained in:
Mikhael Bogdanov
2021-04-29 10:42:45 +02:00
parent 8dd307573a
commit 95d95f9a9b
19 changed files with 209 additions and 14 deletions
@@ -265,13 +265,23 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
); );
} }
private static void addReifiedParametersFromSignature(@NotNull MemberCodegen member, @NotNull ClassDescriptor descriptor) { private static void addReifiedParametersFromSignature(@NotNull MemberCodegen<?> member, @NotNull ClassDescriptor descriptor) {
for (KotlinType type : descriptor.getTypeConstructor().getSupertypes()) { for (KotlinType type : descriptor.getTypeConstructor().getSupertypes()) {
for (TypeProjection supertypeArgument : type.getArguments()) { processTypeArguments(member, type);
TypeParameterDescriptor parameterDescriptor = TypeUtils.getTypeParameterDescriptorOrNull(supertypeArgument.getType()); }
if (parameterDescriptor != null && parameterDescriptor.isReified()) { }
private static void processTypeArguments(@NotNull MemberCodegen<?> member, KotlinType type) {
for (TypeProjection supertypeArgument : type.getArguments()) {
if (supertypeArgument.isStarProjection()) continue;
TypeParameterDescriptor parameterDescriptor = TypeUtils.getTypeParameterDescriptorOrNull(supertypeArgument.getType());
if (parameterDescriptor != null) {
if (parameterDescriptor.isReified()) {
member.getReifiedTypeParametersUsages().addUsedReifiedParameter(parameterDescriptor.getName().asString()); member.getReifiedTypeParametersUsages().addUsedReifiedParameter(parameterDescriptor.getName().asString());
} }
} else {
processTypeArguments(member, supertypeArgument.getType());
} }
} }
} }
@@ -3702,6 +3702,18 @@ public class FirBlackBoxInlineCodegenTestGenerated extends AbstractFirBlackBoxIn
runTest("compiler/testData/codegen/boxInline/reified/kt35511_try_values.kt"); runTest("compiler/testData/codegen/boxInline/reified/kt35511_try_values.kt");
} }
@Test
@TestMetadata("kt44770.kt")
public void testKt44770() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt44770.kt");
}
@Test
@TestMetadata("kt44770_2.kt")
public void testKt44770_2() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt44770_2.kt");
}
@Test @Test
@TestMetadata("kt6988.kt") @TestMetadata("kt6988.kt")
public void testKt6988() throws Exception { public void testKt6988() throws Exception {
@@ -11,11 +11,9 @@ import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin
import org.jetbrains.kotlin.backend.jvm.lower.MultifileFacadeFileEntry import org.jetbrains.kotlin.backend.jvm.lower.MultifileFacadeFileEntry
import org.jetbrains.kotlin.backend.jvm.lower.buildAssertionsDisabledField import org.jetbrains.kotlin.backend.jvm.lower.buildAssertionsDisabledField
import org.jetbrains.kotlin.backend.jvm.lower.hasAssertionsDisabledField import org.jetbrains.kotlin.backend.jvm.lower.hasAssertionsDisabledField
import org.jetbrains.kotlin.codegen.DescriptorAsmUtil import org.jetbrains.kotlin.backend.jvm.lower.isReifiedTypeParameter
import org.jetbrains.kotlin.codegen.VersionIndependentOpcodes import org.jetbrains.kotlin.codegen.*
import org.jetbrains.kotlin.codegen.addRecordComponent
import org.jetbrains.kotlin.codegen.inline.* import org.jetbrains.kotlin.codegen.inline.*
import org.jetbrains.kotlin.codegen.writeKotlinMetadata
import org.jetbrains.kotlin.config.JvmAnalysisFlags import org.jetbrains.kotlin.config.JvmAnalysisFlags
import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.LanguageVersionSettings import org.jetbrains.kotlin.config.LanguageVersionSettings
@@ -31,6 +29,11 @@ import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrFunctionReference import org.jetbrains.kotlin.ir.expressions.IrFunctionReference
import org.jetbrains.kotlin.ir.expressions.impl.IrBlockBodyImpl import org.jetbrains.kotlin.ir.expressions.impl.IrBlockBodyImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrSetFieldImpl import org.jetbrains.kotlin.ir.expressions.impl.IrSetFieldImpl
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.IrSimpleType
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.IrTypeProjection
import org.jetbrains.kotlin.ir.types.classifierOrFail
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.load.java.JvmAnnotationNames import org.jetbrains.kotlin.load.java.JvmAnnotationNames
import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader
@@ -44,6 +47,7 @@ import org.jetbrains.kotlin.resolve.jvm.annotations.VOLATILE_ANNOTATION_FQ_NAME
import org.jetbrains.kotlin.resolve.jvm.checkers.JvmSimpleNameBacktickChecker import org.jetbrains.kotlin.resolve.jvm.checkers.JvmSimpleNameBacktickChecker
import org.jetbrains.kotlin.resolve.jvm.diagnostics.* import org.jetbrains.kotlin.resolve.jvm.diagnostics.*
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmClassSignature import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmClassSignature
import org.jetbrains.kotlin.utils.addToStdlib.cast
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import org.jetbrains.kotlin.utils.addToStdlib.safeAs import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.org.objectweb.asm.* import org.jetbrains.org.objectweb.asm.*
@@ -165,10 +169,32 @@ class ClassCodegen private constructor(
} }
} }
addReifiedParametersFromSignature()
visitor.done() visitor.done()
jvmSignatureClashDetector.reportErrors(classOrigin) jvmSignatureClashDetector.reportErrors(classOrigin)
} }
private fun addReifiedParametersFromSignature() {
for (type in irClass.superTypes) {
processTypeParameters(type)
}
}
private fun processTypeParameters(type: IrType) {
for (supertypeArgument in (type as? IrSimpleType)?.arguments ?: emptyList()) {
if (supertypeArgument is IrTypeProjection) {
val typeArgument = supertypeArgument.type
if (typeArgument.isReifiedTypeParameter) {
reifiedTypeParametersUsages.addUsedReifiedParameter(typeArgument.classifierOrFail.cast<IrTypeParameterSymbol>().owner.name.asString())
} else {
processTypeParameters(typeArgument)
}
}
}
}
fun generateAssertFieldIfNeeded(generatingClInit: Boolean): IrExpression? { fun generateAssertFieldIfNeeded(generatingClInit: Boolean): IrExpression? {
if (irClass.hasAssertionsDisabledField(context)) if (irClass.hasAssertionsDisabledField(context))
return null return null
@@ -65,7 +65,8 @@ class FunctionCodegen(
signature.asmMethod.descriptor, signature.asmMethod.descriptor,
signature.genericsSignature signature.genericsSignature
.takeIf { .takeIf {
!isSynthetic && irFunction.origin != IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA (irFunction.isInline && irFunction.origin != IrDeclarationOrigin.FUNCTION_FOR_DEFAULT_PARAMETER) ||
!isSynthetic && irFunction.origin != IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA
}, },
getThrownExceptions(irFunction)?.toTypedArray() getThrownExceptions(irFunction)?.toTypedArray()
) )
@@ -54,14 +54,14 @@ internal val typeOperatorLowering = makeIrFilePhase(
description = "Lower IrTypeOperatorCalls to (implicit) casts and instanceof checks" description = "Lower IrTypeOperatorCalls to (implicit) casts and instanceof checks"
) )
val IrType.isReifiedTypeParameter: Boolean
get() = classifierOrNull?.safeAs<IrTypeParameterSymbol>()?.owner?.isReified == true
private class TypeOperatorLowering(private val context: JvmBackendContext) : FileLoweringPass, IrBuildingTransformer(context) { private class TypeOperatorLowering(private val context: JvmBackendContext) : FileLoweringPass, IrBuildingTransformer(context) {
override fun lower(irFile: IrFile) = irFile.transformChildrenVoid() override fun lower(irFile: IrFile) = irFile.transformChildrenVoid()
private fun IrExpression.transformVoid() = transform(this@TypeOperatorLowering, null) private fun IrExpression.transformVoid() = transform(this@TypeOperatorLowering, null)
private val IrType.isReifiedTypeParameter: Boolean
get() = classifierOrNull?.safeAs<IrTypeParameterSymbol>()?.owner?.isReified == true
private fun lowerInstanceOf(argument: IrExpression, type: IrType) = with(builder) { private fun lowerInstanceOf(argument: IrExpression, type: IrType) = with(builder) {
when { when {
type.isReifiedTypeParameter -> type.isReifiedTypeParameter ->
@@ -1,7 +1,8 @@
// TARGET_BACKEND: JVM // TARGET_BACKEND: JVM
// NO_CHECK_LAMBDA_INLINING // NO_CHECK_LAMBDA_INLINING
// WITH_RUNTIME // WITH_RUNTIME
// IGNORE_BACKEND: JVM // IGNORE_BACKEND: JVM, JVM_IR
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND_MULTI_MODULE: JVM, JVM_IR, JVM_MULTI_MODULE_OLD_AGAINST_IR, JVM_MULTI_MODULE_IR_AGAINST_OLD // IGNORE_BACKEND_MULTI_MODULE: JVM, JVM_IR, JVM_MULTI_MODULE_OLD_AGAINST_IR, JVM_MULTI_MODULE_IR_AGAINST_OLD
// FILE: 1.kt // FILE: 1.kt
+25
View File
@@ -0,0 +1,25 @@
// TARGET_BACKEND: JVM
// FULL_JDK
// WITH_RUNTIME
// IGNORE_BACKEND: ANDROID
// FILE: 1.kt
package test
import java.lang.reflect.*
class OK
interface S<A>
inline fun <reified T : Any> test(): String {
return (object : S<T> {
}::class.java.genericInterfaces[0] as ParameterizedType).actualTypeArguments[0].getTypeName()
// FILE: 2.kt
import test.*
fun box():String {
val test = test<OK>()
return if (test == OK::class.qualifiedName) "OK" else "fail: $test"
}
@@ -0,0 +1,25 @@
// TARGET_BACKEND: JVM
// FULL_JDK
// WITH_RUNTIME
// IGNORE_BACKEND: ANDROID
// FILE: 1.kt
package test
import java.lang.reflect.*
class OK
interface S<A>
inline fun <reified T : Any> test(): String {
return ((object : S<S<T>> {
}::class.java.genericInterfaces[0] as ParameterizedType).actualTypeArguments[0] as ParameterizedType).actualTypeArguments[0].getTypeName()
// FILE: 2.kt
import test.*
fun box():String {
val test = test<OK>()
return if (test == OK::class.qualifiedName) "OK" else "fail: $test"
}
@@ -0,0 +1,5 @@
// KOTLIN_CONFIGURATION_FLAGS: SAM_CONVERSIONS=CLASS
// WITH_SIGNATURES
// FILE: t.kt
inline fun <reified T> foo(s: String = "123") {}
@@ -0,0 +1,6 @@
@kotlin.Metadata
public final class<null> TKt {
// source: 't.kt'
public synthetic final static <<T:Ljava/lang/Object;>(Ljava/lang/String;)V> method foo(p0: java.lang.String): void
public synthetic static <null> method foo$default(p0: java.lang.String, p1: int, p2: java.lang.Object): void
}
@@ -24,7 +24,7 @@ public final class<null> test/SamAdapterAndInlinedOneKt {
public final static @org.jetbrains.annotations.NotNull <(Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;)Ljava/lang/Runnable;> method makeRunnable2(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function0): java.lang.Runnable public final static @org.jetbrains.annotations.NotNull <(Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;)Ljava/lang/Runnable;> method makeRunnable2(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function0): java.lang.Runnable
public final static @org.jetbrains.annotations.NotNull <(Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;)Ljava/lang/Runnable;> method noInline(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function0): java.lang.Runnable public final static @org.jetbrains.annotations.NotNull <(Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;)Ljava/lang/Runnable;> method noInline(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function0): java.lang.Runnable
public final static @org.jetbrains.annotations.NotNull <(Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;)Ljava/lang/Runnable;> method noInline2(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function0): java.lang.Runnable public final static @org.jetbrains.annotations.NotNull <(Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;)Ljava/lang/Runnable;> method noInline2(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function0): java.lang.Runnable
public synthetic final static <null> method makeRunnable(p0: kotlin.jvm.functions.Function0): java.lang.Runnable public synthetic final static <<T:Ljava/lang/Object;>(Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;)Ljava/lang/Runnable;> method makeRunnable(p0: kotlin.jvm.functions.Function0): java.lang.Runnable
inner (anonymous) class test/SamAdapterAndInlinedOneKt$sam$i$java_lang_Runnable$0 inner (anonymous) class test/SamAdapterAndInlinedOneKt$sam$i$java_lang_Runnable$0
inner (anonymous) class test/SamAdapterAndInlinedOneKt$sam$java_lang_Runnable$0 inner (anonymous) class test/SamAdapterAndInlinedOneKt$sam$java_lang_Runnable$0
} }
@@ -3702,6 +3702,18 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo
runTest("compiler/testData/codegen/boxInline/reified/kt35511_try_values.kt"); runTest("compiler/testData/codegen/boxInline/reified/kt35511_try_values.kt");
} }
@Test
@TestMetadata("kt44770.kt")
public void testKt44770() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt44770.kt");
}
@Test
@TestMetadata("kt44770_2.kt")
public void testKt44770_2() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt44770_2.kt");
}
@Test @Test
@TestMetadata("kt6988.kt") @TestMetadata("kt6988.kt")
public void testKt6988() throws Exception { public void testKt6988() throws Exception {
@@ -1026,6 +1026,12 @@ public class BytecodeListingTestGenerated extends AbstractBytecodeListingTest {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/bytecodeListing/inline"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true); KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/bytecodeListing/inline"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
} }
@Test
@TestMetadata("genericReified.kt")
public void testGenericReified() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/inline/genericReified.kt");
}
@Test @Test
@TestMetadata("inlineOnly.kt") @TestMetadata("inlineOnly.kt")
public void testInlineOnly() throws Exception { public void testInlineOnly() throws Exception {
@@ -3702,6 +3702,18 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi
runTest("compiler/testData/codegen/boxInline/reified/kt35511_try_values.kt"); runTest("compiler/testData/codegen/boxInline/reified/kt35511_try_values.kt");
} }
@Test
@TestMetadata("kt44770.kt")
public void testKt44770() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt44770.kt");
}
@Test
@TestMetadata("kt44770_2.kt")
public void testKt44770_2() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt44770_2.kt");
}
@Test @Test
@TestMetadata("kt6988.kt") @TestMetadata("kt6988.kt")
public void testKt6988() throws Exception { public void testKt6988() throws Exception {
@@ -3702,6 +3702,18 @@ public class IrBlackBoxInlineCodegenTestGenerated extends AbstractIrBlackBoxInli
runTest("compiler/testData/codegen/boxInline/reified/kt35511_try_values.kt"); runTest("compiler/testData/codegen/boxInline/reified/kt35511_try_values.kt");
} }
@Test
@TestMetadata("kt44770.kt")
public void testKt44770() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt44770.kt");
}
@Test
@TestMetadata("kt44770_2.kt")
public void testKt44770_2() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt44770_2.kt");
}
@Test @Test
@TestMetadata("kt6988.kt") @TestMetadata("kt6988.kt")
public void testKt6988() throws Exception { public void testKt6988() throws Exception {
@@ -1026,6 +1026,12 @@ public class IrBytecodeListingTestGenerated extends AbstractIrBytecodeListingTes
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/bytecodeListing/inline"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/bytecodeListing/inline"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
} }
@Test
@TestMetadata("genericReified.kt")
public void testGenericReified() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/inline/genericReified.kt");
}
@Test @Test
@TestMetadata("inlineOnly.kt") @TestMetadata("inlineOnly.kt")
public void testInlineOnly() throws Exception { public void testInlineOnly() throws Exception {
@@ -3702,6 +3702,18 @@ public class IrCompileKotlinAgainstInlineKotlinTestGenerated extends AbstractIrC
runTest("compiler/testData/codegen/boxInline/reified/kt35511_try_values.kt"); runTest("compiler/testData/codegen/boxInline/reified/kt35511_try_values.kt");
} }
@Test
@TestMetadata("kt44770.kt")
public void testKt44770() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt44770.kt");
}
@Test
@TestMetadata("kt44770_2.kt")
public void testKt44770_2() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt44770_2.kt");
}
@Test @Test
@TestMetadata("kt6988.kt") @TestMetadata("kt6988.kt")
public void testKt6988() throws Exception { public void testKt6988() throws Exception {
@@ -3702,6 +3702,18 @@ public class JvmIrAgainstOldBoxInlineTestGenerated extends AbstractJvmIrAgainstO
runTest("compiler/testData/codegen/boxInline/reified/kt35511_try_values.kt"); runTest("compiler/testData/codegen/boxInline/reified/kt35511_try_values.kt");
} }
@Test
@TestMetadata("kt44770.kt")
public void testKt44770() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt44770.kt");
}
@Test
@TestMetadata("kt44770_2.kt")
public void testKt44770_2() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt44770_2.kt");
}
@Test @Test
@TestMetadata("kt6988.kt") @TestMetadata("kt6988.kt")
public void testKt6988() throws Exception { public void testKt6988() throws Exception {
@@ -3702,6 +3702,18 @@ public class JvmOldAgainstIrBoxInlineTestGenerated extends AbstractJvmOldAgainst
runTest("compiler/testData/codegen/boxInline/reified/kt35511_try_values.kt"); runTest("compiler/testData/codegen/boxInline/reified/kt35511_try_values.kt");
} }
@Test
@TestMetadata("kt44770.kt")
public void testKt44770() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt44770.kt");
}
@Test
@TestMetadata("kt44770_2.kt")
public void testKt44770_2() throws Exception {
runTest("compiler/testData/codegen/boxInline/reified/kt44770_2.kt");
}
@Test @Test
@TestMetadata("kt6988.kt") @TestMetadata("kt6988.kt")
public void testKt6988() throws Exception { public void testKt6988() throws Exception {