Skip to content

Commit a8625b6

Browse files
author
Mikhael Bogdanov
committed
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
1 parent 85f9e2e commit a8625b6

File tree

7 files changed

+69
-0
lines changed

7 files changed

+69
-0
lines changed

compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionReferenceGenerationStrategy.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
import java.util.*;
3939

40+
import static org.jetbrains.kotlin.resolve.DescriptorUtils.isObject;
41+
4042
public class FunctionReferenceGenerationStrategy extends FunctionGenerationStrategy.CodegenBased {
4143
private final ResolvedCall<?> resolvedCall;
4244
private final FunctionDescriptor referencedFunction;
@@ -146,6 +148,12 @@ private void computeAndSaveArguments(@NotNull List<? extends ValueArgument> fake
146148
(referencedFunction.getExtensionReceiverParameter() != null ? 1 : 0) -
147149
(receiverType != null ? 1 : 0);
148150

151+
if (receivers < 0 && referencedFunction instanceof ConstructorDescriptor && isObject(referencedFunction.getContainingDeclaration().getContainingDeclaration())) {
152+
//reference to object nested class
153+
//TODO: seems problem should be fixed on frontend side (note that object instance are captured by generated class)
154+
receivers = 0;
155+
}
156+
149157
List<ValueParameterDescriptor> parameters = CollectionsKt.drop(functionDescriptor.getValueParameters(), receivers);
150158
for (int i = 0; i < parameters.size(); i++) {
151159
ValueParameterDescriptor parameter = parameters.get(i);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// FILE: MFunction.java
2+
3+
public interface MFunction<T, R> {
4+
R invoke(T t);
5+
}
6+
7+
// FILE: 1.kt
8+
9+
10+
object Foo {
11+
class Requester(val dealToBeOffered: String)
12+
}
13+
14+
class Bar {
15+
val foo = MFunction(Foo::Requester)
16+
}
17+
18+
fun box(): String {
19+
return Bar().foo("OK").dealToBeOffered
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// FILE: 1.kt
2+
3+
inline fun <R> startFlow(
4+
flowConstructor: (String) -> R
5+
): R {
6+
return flowConstructor("OK")
7+
}
8+
9+
object Foo {
10+
class Requester(val dealToBeOffered: String)
11+
}
12+
13+
// FILE: 2.kt
14+
15+
fun box(): String {
16+
return startFlow(Foo::Requester).dealToBeOffered
17+
}

compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxAgainstJavaCodegenTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ public void testConstructor() throws Exception {
152152
doTest(fileName);
153153
}
154154

155+
@TestMetadata("kt16412.kt")
156+
public void testKt16412() throws Exception {
157+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxAgainstJava/callableReference/kt16412.kt");
158+
doTest(fileName);
159+
}
160+
155161
@TestMetadata("publicFinalField.kt")
156162
public void testPublicFinalField() throws Exception {
157163
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxAgainstJava/callableReference/publicFinalField.kt");

compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,12 @@ public void testKt15449() throws Exception {
596596
doTest(fileName);
597597
}
598598

599+
@TestMetadata("kt16411.kt")
600+
public void testKt16411() throws Exception {
601+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/kt16411.kt");
602+
doTest(fileName);
603+
}
604+
599605
@TestMetadata("propertyIntrinsic.kt")
600606
public void testPropertyIntrinsic() throws Exception {
601607
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/propertyIntrinsic.kt");

compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,12 @@ public void testKt15449() throws Exception {
596596
doTest(fileName);
597597
}
598598

599+
@TestMetadata("kt16411.kt")
600+
public void testKt16411() throws Exception {
601+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/kt16411.kt");
602+
doTest(fileName);
603+
}
604+
599605
@TestMetadata("propertyIntrinsic.kt")
600606
public void testPropertyIntrinsic() throws Exception {
601607
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/propertyIntrinsic.kt");

js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/CallableReferenceInlineTestsGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ public void testKt15449() throws Exception {
6666
doTest(fileName);
6767
}
6868

69+
@TestMetadata("kt16411.kt")
70+
public void testKt16411() throws Exception {
71+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/kt16411.kt");
72+
doTest(fileName);
73+
}
74+
6975
@TestMetadata("propertyIntrinsic.kt")
7076
public void testPropertyIntrinsic() throws Exception {
7177
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/propertyIntrinsic.kt");

0 commit comments

Comments
 (0)