Generate SAM wrappers only if they are required for a given argument

SAM interface wrapper for an argument is required,
if in the function descriptor for SAM adapter
type for the corresponding value parameter
doesn't match type of the corresponding value parameter
in the original (Java) descriptor.

 #KT-19251 Fixed Target versions 1.1.5
This commit is contained in:
Dmitry Petrov
2017-07-27 16:40:22 +03:00
parent c8bc2d9d16
commit 67336653e0
6 changed files with 71 additions and 0 deletions
@@ -56,6 +56,7 @@ import org.jetbrains.kotlin.resolve.constants.EnumValue;
import org.jetbrains.kotlin.resolve.constants.NullValue;
import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt;
import org.jetbrains.kotlin.types.KotlinType;
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker;
import org.jetbrains.org.objectweb.asm.Type;
import java.util.*;
@@ -559,6 +560,9 @@ class CodegenAnnotatingVisitor extends KtVisitorVoid {
if (valueArguments == null) return;
for (ValueParameterDescriptor valueParameter : original.getValueParameters()) {
ValueParameterDescriptor adaptedParameter = descriptor.getValueParameters().get(valueParameter.getIndex());
if (KotlinTypeChecker.DEFAULT.equalTypes(adaptedParameter.getType(), valueParameter.getType())) continue;
SamType samType = SamType.create(TypeMapperUtilsKt.removeExternalProjections(valueParameter.getType()));
if (samType == null) continue;
@@ -0,0 +1,14 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FULL_JDK
// FILE: test.kt
fun box(): String {
val map = mutableMapOf<Fun, String>()
val fn = Fun { TODO() }
return map.computeIfAbsent(fn, { "OK" })
}
// FILE: Fun.java
public interface Fun {
String invoke(String string);
}
@@ -0,0 +1,17 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FULL_JDK
// FILE: test.kt
fun box(): String {
val map = mutableMapOf<Fun, String>()
val fn = DerivedFun { TODO() }
return map.computeIfAbsent(fn, { "OK" })
}
// FILE: Fun.java
public interface Fun {
String invoke(String string);
}
// FILE: DerivedFun.java
public interface DerivedFun extends Fun {}
@@ -17266,6 +17266,18 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
doTest(fileName);
}
@TestMetadata("kt19251.kt")
public void testKt19251() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/constructors/kt19251.kt");
doTest(fileName);
}
@TestMetadata("kt19251_child.kt")
public void testKt19251_child() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/constructors/kt19251_child.kt");
doTest(fileName);
}
@TestMetadata("nonLiteralComparator.kt")
public void testNonLiteralComparator() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/constructors/nonLiteralComparator.kt");
@@ -17266,6 +17266,18 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("kt19251.kt")
public void testKt19251() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/constructors/kt19251.kt");
doTest(fileName);
}
@TestMetadata("kt19251_child.kt")
public void testKt19251_child() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/constructors/kt19251_child.kt");
doTest(fileName);
}
@TestMetadata("nonLiteralComparator.kt")
public void testNonLiteralComparator() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/constructors/nonLiteralComparator.kt");
@@ -17266,6 +17266,18 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
doTest(fileName);
}
@TestMetadata("kt19251.kt")
public void testKt19251() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/constructors/kt19251.kt");
doTest(fileName);
}
@TestMetadata("kt19251_child.kt")
public void testKt19251_child() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/constructors/kt19251_child.kt");
doTest(fileName);
}
@TestMetadata("nonLiteralComparator.kt")
public void testNonLiteralComparator() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/constructors/nonLiteralComparator.kt");