'async' reserved in front of function literals
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package org.jetbrains.kotlin.types.expressions
|
||||
|
||||
import com.google.common.collect.Lists
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor
|
||||
@@ -27,6 +28,7 @@ import org.jetbrains.kotlin.diagnostics.DiagnosticUtils
|
||||
import org.jetbrains.kotlin.diagnostics.Errors.*
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.checkReservedPrefixWord
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getAnnotationEntries
|
||||
import org.jetbrains.kotlin.resolve.*
|
||||
import org.jetbrains.kotlin.resolve.BindingContext.EXPECTED_RETURN_TYPE
|
||||
@@ -53,6 +55,7 @@ internal class FunctionsTypingVisitor(facade: ExpressionTypingInternals) : Expre
|
||||
isStatement: Boolean,
|
||||
statementScope: LexicalWritableScope? // must be not null if isStatement
|
||||
): KotlinTypeInfo {
|
||||
checkReservedAsync(context, function)
|
||||
if (!isStatement) {
|
||||
// function expression
|
||||
if (!function.getTypeParameters().isEmpty()) {
|
||||
@@ -131,6 +134,8 @@ internal class FunctionsTypingVisitor(facade: ExpressionTypingInternals) : Expre
|
||||
}
|
||||
|
||||
override fun visitLambdaExpression(expression: KtLambdaExpression, context: ExpressionTypingContext): KotlinTypeInfo? {
|
||||
checkReservedAsync(context, expression)
|
||||
|
||||
if (!expression.getFunctionLiteral().hasBody()) return null
|
||||
|
||||
val expectedType = context.expectedType
|
||||
@@ -153,6 +158,10 @@ internal class FunctionsTypingVisitor(facade: ExpressionTypingInternals) : Expre
|
||||
return components.dataFlowAnalyzer.createCheckedTypeInfo(resultType, context, expression)
|
||||
}
|
||||
|
||||
private fun checkReservedAsync(context: ExpressionTypingContext, expression: PsiElement) {
|
||||
checkReservedPrefixWord(context.trace, expression, "async", "async block/lambda. Use 'async() { ... }' or 'async(fun...)'")
|
||||
}
|
||||
|
||||
private fun createFunctionLiteralDescriptor(
|
||||
expression: KtLambdaExpression,
|
||||
context: ExpressionTypingContext
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
fun async(f: () -> Unit) {
|
||||
f()
|
||||
}
|
||||
|
||||
infix fun Any.async(f: () -> Unit) = f()
|
||||
|
||||
fun test(foo: Any) {
|
||||
<!UNSUPPORTED!>async<!> { }
|
||||
<!UNSUPPORTED!>async<!> /**/ { }
|
||||
foo <!UNSUPPORTED!>async<!> { }
|
||||
|
||||
async() { }
|
||||
|
||||
async({ })
|
||||
foo async ({ })
|
||||
|
||||
foo <!UNSUPPORTED!>async<!> fun () {}
|
||||
foo async (fun () {})
|
||||
|
||||
async (fun () {})
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package
|
||||
|
||||
public fun async(/*0*/ f: () -> kotlin.Unit): kotlin.Unit
|
||||
public fun test(/*0*/ foo: kotlin.Any): kotlin.Unit
|
||||
public infix fun kotlin.Any.async(/*0*/ f: () -> kotlin.Unit): kotlin.Unit
|
||||
@@ -547,6 +547,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("ReservedAsync.kt")
|
||||
public void testReservedAsync() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/ReservedAsync.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("ResolveOfJavaGenerics.kt")
|
||||
public void testResolveOfJavaGenerics() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/ResolveOfJavaGenerics.kt");
|
||||
|
||||
Reference in New Issue
Block a user