Fix for KT-16411, KT-16412: Exception from compiler when try call SAM constructor where argument is callable reference to nested class inside object

#Fixed KT-16411
  #Fixed KT-16412
This commit is contained in:
Mikhael Bogdanov
2017-02-17 17:13:20 +01:00
parent 85f9e2e47b
commit a8625b632d
7 changed files with 69 additions and 0 deletions
@@ -37,6 +37,8 @@ import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter;
import java.util.*;
import static org.jetbrains.kotlin.resolve.DescriptorUtils.isObject;
public class FunctionReferenceGenerationStrategy extends FunctionGenerationStrategy.CodegenBased {
private final ResolvedCall<?> resolvedCall;
private final FunctionDescriptor referencedFunction;
@@ -146,6 +148,12 @@ public class FunctionReferenceGenerationStrategy extends FunctionGenerationStrat
(referencedFunction.getExtensionReceiverParameter() != null ? 1 : 0) -
(receiverType != null ? 1 : 0);
if (receivers < 0 && referencedFunction instanceof ConstructorDescriptor && isObject(referencedFunction.getContainingDeclaration().getContainingDeclaration())) {
//reference to object nested class
//TODO: seems problem should be fixed on frontend side (note that object instance are captured by generated class)
receivers = 0;
}
List<ValueParameterDescriptor> parameters = CollectionsKt.drop(functionDescriptor.getValueParameters(), receivers);
for (int i = 0; i < parameters.size(); i++) {
ValueParameterDescriptor parameter = parameters.get(i);
@@ -0,0 +1,20 @@
// FILE: MFunction.java
public interface MFunction<T, R> {
R invoke(T t);
}
// FILE: 1.kt
object Foo {
class Requester(val dealToBeOffered: String)
}
class Bar {
val foo = MFunction(Foo::Requester)
}
fun box(): String {
return Bar().foo("OK").dealToBeOffered
}
@@ -0,0 +1,17 @@
// FILE: 1.kt
inline fun <R> startFlow(
flowConstructor: (String) -> R
): R {
return flowConstructor("OK")
}
object Foo {
class Requester(val dealToBeOffered: String)
}
// FILE: 2.kt
fun box(): String {
return startFlow(Foo::Requester).dealToBeOffered
}
@@ -152,6 +152,12 @@ public class BlackBoxAgainstJavaCodegenTestGenerated extends AbstractBlackBoxAga
doTest(fileName);
}
@TestMetadata("kt16412.kt")
public void testKt16412() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxAgainstJava/callableReference/kt16412.kt");
doTest(fileName);
}
@TestMetadata("publicFinalField.kt")
public void testPublicFinalField() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxAgainstJava/callableReference/publicFinalField.kt");
@@ -596,6 +596,12 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo
doTest(fileName);
}
@TestMetadata("kt16411.kt")
public void testKt16411() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/kt16411.kt");
doTest(fileName);
}
@TestMetadata("propertyIntrinsic.kt")
public void testPropertyIntrinsic() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/propertyIntrinsic.kt");
@@ -596,6 +596,12 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi
doTest(fileName);
}
@TestMetadata("kt16411.kt")
public void testKt16411() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/kt16411.kt");
doTest(fileName);
}
@TestMetadata("propertyIntrinsic.kt")
public void testPropertyIntrinsic() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/propertyIntrinsic.kt");
@@ -66,6 +66,12 @@ public class CallableReferenceInlineTestsGenerated extends AbstractCallableRefer
doTest(fileName);
}
@TestMetadata("kt16411.kt")
public void testKt16411() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/kt16411.kt");
doTest(fileName);
}
@TestMetadata("propertyIntrinsic.kt")
public void testPropertyIntrinsic() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/propertyIntrinsic.kt");