Minor: add test to call callable references from Java
This commit is contained in:
@@ -1124,7 +1124,9 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
|
||||
if (!isCrossinlineLambda) {
|
||||
v.aconst(null);
|
||||
}
|
||||
} else if (DescriptorUtilsKt.isCallableReferenceToSuspend(classDescriptor, state.getJvmRuntimeTypes().getFunctionReference())) {
|
||||
}
|
||||
else if (classDescriptor instanceof SyntheticClassDescriptorForLambda &&
|
||||
((SyntheticClassDescriptorForLambda) classDescriptor).isCallableReference()) {
|
||||
// Constructor of callable reference to suspend function does not accept continuation:
|
||||
// do nothing.
|
||||
}
|
||||
|
||||
+6
-13
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.codegen
|
||||
@@ -21,8 +10,10 @@ import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
import org.jetbrains.kotlin.descriptors.impl.ClassDescriptorImpl
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.psi.KtCallableReferenceExpression
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
||||
import org.jetbrains.kotlin.resolve.source.getPsi
|
||||
import org.jetbrains.kotlin.resolve.source.toSourceElement
|
||||
import org.jetbrains.kotlin.storage.LockBasedStorageManager
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
@@ -37,4 +28,6 @@ class SyntheticClassDescriptorForLambda(
|
||||
init {
|
||||
initialize(MemberScope.Empty, emptySet(), null)
|
||||
}
|
||||
|
||||
fun isCallableReference(): Boolean = source.getPsi() is KtCallableReferenceExpression
|
||||
}
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
// IGNORE_BACKEND: JS, JS_IR, NATIVE
|
||||
// COMMON_COROUTINES_TEST
|
||||
// WITH_COROUTINES
|
||||
// WITH_REFLECT
|
||||
|
||||
// FILE: test.kt
|
||||
import kotlin.reflect.KSuspendFunction0
|
||||
|
||||
class Test {
|
||||
suspend fun o() = "O"
|
||||
fun forCall(): KSuspendFunction0<String> {
|
||||
return ::o
|
||||
}
|
||||
|
||||
suspend fun k() = "K"
|
||||
fun forInvoke(): SuspendFunction0<String> {
|
||||
return ::k
|
||||
}
|
||||
}
|
||||
|
||||
// FILE: Checker.java
|
||||
import helpers.*;
|
||||
|
||||
class Checker {
|
||||
public static String check() {
|
||||
Test test = new Test();
|
||||
return ((String) test.forCall().call(new EmptyContinuation())) +
|
||||
((String) test.forInvoke().invoke(new EmptyContinuation()));
|
||||
}
|
||||
}
|
||||
|
||||
// FILE: box.kt
|
||||
fun box() = Checker.check()
|
||||
@@ -12,8 +12,7 @@ inline fun inline() {}
|
||||
class External { external fun external() }
|
||||
operator fun Unit.invoke() {}
|
||||
infix fun Unit.infix(unit: Unit) {}
|
||||
// TODO: support or prohibit references to suspend functions
|
||||
// class Suspend { suspend fun suspend(c: Continuation<Unit>) {} }
|
||||
class Suspend { suspend fun suspend() {} }
|
||||
|
||||
val externalGetter = Unit
|
||||
external get
|
||||
@@ -47,11 +46,11 @@ fun box(): String {
|
||||
assertTrue(Unit::infix.isInfix)
|
||||
assertFalse(Unit::infix.isSuspend)
|
||||
|
||||
// assertFalse(Suspend::suspend.isInline)
|
||||
// assertFalse(Suspend::suspend.isExternal)
|
||||
// assertFalse(Suspend::suspend.isOperator)
|
||||
// assertFalse(Suspend::suspend.isInfix)
|
||||
// assertTrue(Suspend::suspend.isSuspend)
|
||||
assertFalse(Suspend::suspend.isInline)
|
||||
assertFalse(Suspend::suspend.isExternal)
|
||||
assertFalse(Suspend::suspend.isOperator)
|
||||
assertFalse(Suspend::suspend.isInfix)
|
||||
assertTrue(Suspend::suspend.isSuspend)
|
||||
|
||||
assertTrue(::externalGetter.getter.isExternal)
|
||||
assertFalse(::externalGetter.getter.isInline)
|
||||
|
||||
+2
-2
@@ -1,11 +1,11 @@
|
||||
// !CHECK_TYPE
|
||||
// !LANGUAGE: +Coroutines
|
||||
// SKIP_TXT
|
||||
// !CHECK_TYPE
|
||||
|
||||
import kotlin.reflect.KSuspendFunction0
|
||||
|
||||
suspend fun foo() {}
|
||||
|
||||
fun test() {
|
||||
::foo checkType { _<KSuspendFunction0>() }
|
||||
::foo checkType { _<KSuspendFunction0<Unit>>() }
|
||||
}
|
||||
|
||||
Generated
+12
@@ -6298,6 +6298,18 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/featureIntersection/callableReference"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true);
|
||||
}
|
||||
|
||||
@TestMetadata("fromJava.kt")
|
||||
public void testFromJava_1_2() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/featureIntersection/callableReference/fromJava.kt");
|
||||
doTestWithCoroutinesPackageReplacement(fileName, "kotlin.coroutines.experimental");
|
||||
}
|
||||
|
||||
@TestMetadata("fromJava.kt")
|
||||
public void testFromJava_1_3() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/featureIntersection/callableReference/fromJava.kt");
|
||||
doTestWithCoroutinesPackageReplacement(fileName, "kotlin.coroutines");
|
||||
}
|
||||
|
||||
@TestMetadata("longArgs.kt")
|
||||
public void testLongArgs_1_2() throws Exception {
|
||||
runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/featureIntersection/callableReference/longArgs.kt", "kotlin.coroutines.experimental");
|
||||
|
||||
+12
@@ -6298,6 +6298,18 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/featureIntersection/callableReference"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
|
||||
}
|
||||
|
||||
@TestMetadata("fromJava.kt")
|
||||
public void testFromJava_1_2() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/featureIntersection/callableReference/fromJava.kt");
|
||||
doTestWithCoroutinesPackageReplacement(fileName, "kotlin.coroutines.experimental");
|
||||
}
|
||||
|
||||
@TestMetadata("fromJava.kt")
|
||||
public void testFromJava_1_3() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/featureIntersection/callableReference/fromJava.kt");
|
||||
doTestWithCoroutinesPackageReplacement(fileName, "kotlin.coroutines");
|
||||
}
|
||||
|
||||
@TestMetadata("longArgs.kt")
|
||||
public void testLongArgs_1_2() throws Exception {
|
||||
runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/featureIntersection/callableReference/longArgs.kt", "kotlin.coroutines.experimental");
|
||||
|
||||
+12
@@ -6298,6 +6298,18 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/featureIntersection/callableReference"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
|
||||
}
|
||||
|
||||
@TestMetadata("fromJava.kt")
|
||||
public void testFromJava_1_2() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/featureIntersection/callableReference/fromJava.kt");
|
||||
doTestWithCoroutinesPackageReplacement(fileName, "kotlin.coroutines.experimental");
|
||||
}
|
||||
|
||||
@TestMetadata("fromJava.kt")
|
||||
public void testFromJava_1_3() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/featureIntersection/callableReference/fromJava.kt");
|
||||
doTestWithCoroutinesPackageReplacement(fileName, "kotlin.coroutines");
|
||||
}
|
||||
|
||||
@TestMetadata("longArgs.kt")
|
||||
public void testLongArgs_1_2() throws Exception {
|
||||
runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/featureIntersection/callableReference/longArgs.kt", "kotlin.coroutines.experimental");
|
||||
|
||||
@@ -5,8 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.resolve.descriptorUtil
|
||||
|
||||
import org.jetbrains.kotlin.builtins.*
|
||||
import org.jetbrains.kotlin.builtins.functions.FunctionClassDescriptor
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind.*
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotated
|
||||
@@ -149,10 +148,6 @@ fun ClassDescriptor.getSuperInterfaces(): List<ClassDescriptor> =
|
||||
else null
|
||||
}
|
||||
|
||||
fun ClassDescriptor.isCallableReferenceToSuspend(functionReference: ClassDescriptor) =
|
||||
getSuperClassNotAny() == functionReference &&
|
||||
getSuperInterfaces().singleOrNull()?.getFunctionalClassKind() == FunctionClassDescriptor.Kind.SuspendFunction
|
||||
|
||||
val ClassDescriptor.secondaryConstructors: List<ClassConstructorDescriptor>
|
||||
get() = constructors.filterNot { it.isPrimary }
|
||||
|
||||
|
||||
+12
@@ -5503,6 +5503,18 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/featureIntersection/callableReference"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS_IR, true);
|
||||
}
|
||||
|
||||
@TestMetadata("fromJava.kt")
|
||||
public void testFromJava_1_2() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/featureIntersection/callableReference/fromJava.kt");
|
||||
try {
|
||||
doTestWithCoroutinesPackageReplacement(fileName, "kotlin.coroutines.experimental");
|
||||
}
|
||||
catch (Throwable ignore) {
|
||||
return;
|
||||
}
|
||||
throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that.");
|
||||
}
|
||||
|
||||
@TestMetadata("longArgs.kt")
|
||||
public void testLongArgs_1_2() throws Exception {
|
||||
runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/featureIntersection/callableReference/longArgs.kt", "kotlin.coroutines.experimental");
|
||||
|
||||
+12
@@ -5503,6 +5503,18 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/featureIntersection/callableReference"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true);
|
||||
}
|
||||
|
||||
@TestMetadata("fromJava.kt")
|
||||
public void testFromJava_1_2() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/featureIntersection/callableReference/fromJava.kt");
|
||||
try {
|
||||
doTestWithCoroutinesPackageReplacement(fileName, "kotlin.coroutines.experimental");
|
||||
}
|
||||
catch (Throwable ignore) {
|
||||
return;
|
||||
}
|
||||
throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that.");
|
||||
}
|
||||
|
||||
@TestMetadata("longArgs.kt")
|
||||
public void testLongArgs_1_2() throws Exception {
|
||||
runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/featureIntersection/callableReference/longArgs.kt", "kotlin.coroutines.experimental");
|
||||
|
||||
Reference in New Issue
Block a user