[FIR] Add @ExtensionFunction to lambdas with receivers

This commit is contained in:
Dmitriy Novozhilov
2019-10-29 17:29:32 +03:00
parent 5776d115b7
commit d2b895d8c2
15 changed files with 161 additions and 35 deletions
@@ -39,6 +39,7 @@ import org.jetbrains.kotlin.fir.types.FirUserTypeRef
import org.jetbrains.kotlin.fir.types.impl.*
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
import org.jetbrains.kotlin.lexer.KtTokens.*
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames
@@ -1393,7 +1394,12 @@ class DeclarationsConverter(
isNullable,
receiverTypeReference,
returnTypeReference
).apply { valueParameters += valueParametersList.map { it.firValueParameter } }
).apply {
valueParameters += valueParametersList.map { it.firValueParameter }
if (receiverTypeReference != null) {
annotations += extensionFunctionAnnotation
}
}
}
/**
@@ -1444,4 +1450,17 @@ class DeclarationsConverter(
).apply { annotations += modifiers.annotations }
return ValueParameter(isVal, isVar, modifiers, firValueParameter, destructuringDeclaration)
}
private val extensionFunctionAnnotation = FirAnnotationCallImpl(
null,
null,
FirResolvedTypeRefImpl(
null,
ConeClassTypeImpl(
ConeClassLikeLookupTagImpl(ClassId.fromString("kotlin/ExtensionFunctionType")),
emptyArray(),
false
)
)
)
}
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.fir.types.FirTypeProjection
import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.fir.types.impl.*
import org.jetbrains.kotlin.lexer.KtTokens.*
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
@@ -850,6 +851,9 @@ class RawFirBuilder(session: FirSession, val stubMode: Boolean) : BaseFirBuilder
for (valueParameter in unwrappedElement.parameters) {
functionType.valueParameters += valueParameter.convert<FirValueParameter>()
}
if (functionType.receiverTypeRef != null) {
functionType.annotations += extensionFunctionAnnotation
}
functionType
}
null -> FirErrorTypeRefImpl(source, "Unwrapped type is null")
@@ -1356,4 +1360,17 @@ class RawFirBuilder(session: FirSession, val stubMode: Boolean) : BaseFirBuilder
return FirExpressionStub(expression.toFirSourceElement())
}
}
private val extensionFunctionAnnotation = FirAnnotationCallImpl(
null,
null,
FirResolvedTypeRefImpl(
null,
ConeClassTypeImpl(
ConeClassLikeLookupTagImpl(ClassId.fromString("kotlin/ExtensionFunctionType")),
emptyArray(),
false
)
)
)
}
@@ -108,13 +108,16 @@ class FirCallCompleter(
stubsForPostponedVariables: Map<TypeVariableMarker, StubTypeMarker>
): Pair<List<FirExpression>, InferenceSession> {
val needItParam = lambdaArgument.valueParameters.isEmpty() && parameters.size == (if (receiverType != null) 2 else 1)
val itParam = when {
lambdaArgument.valueParameters.isEmpty() && parameters.size == 1 -> {
needItParam -> {
val name = Name.identifier("it")
val itType = if (receiverType != null) parameters[1] else parameters.single()
FirValueParameterImpl(
null,
session,
FirResolvedTypeRefImpl(null, parameters.single()),
FirResolvedTypeRefImpl(null, itType),
name,
FirVariableSymbol(name),
defaultValue = null,
@@ -129,7 +132,7 @@ class FirCallCompleter(
val expectedReturnTypeRef = expectedReturnType?.let { lambdaArgument.returnTypeRef.resolvedTypeFromPrototype(it) }
val newLambdaExpression = lambdaArgument.copy(
receiverTypeRef = receiverType?.let { lambdaArgument.receiverTypeRef!!.resolvedTypeFromPrototype(it) },
receiverTypeRef = receiverType?.let { lambdaArgument.receiverTypeRef?.resolvedTypeFromPrototype(it) },
valueParameters = lambdaArgument.valueParameters.mapIndexed { index, parameter ->
parameter.transformReturnTypeRef(StoreType, parameter.returnTypeRef.resolvedTypeFromPrototype(parameters[index]))
parameter
@@ -30,19 +30,19 @@ FILE: chooseCallableReferenceDependingOnInferredReceiver.kt
^bar R|kotlin/TODO|()
}
public final fun test(): R|kotlin/Unit| {
R|/myWith|<R|A|, R|kotlin/Unit|>(R|/A.A|(), <L> = myWith@fun <anonymous>(it: R|A|): R|kotlin/Unit| <kind=UNKNOWN> {
lval t1: <ERROR TYPE REF: Inapplicable(INAPPLICABLE): [/bar]> = <Inapplicable(INAPPLICABLE): [/bar]>#(::foo#)
lval t2: <ERROR TYPE REF: Inapplicable(INAPPLICABLE): [/bar]> = <Inapplicable(INAPPLICABLE): [/bar]>#(::baz#)
R|/myWith|<R|B|, R|kotlin/Unit|>(R|/B.B|(), <L> = myWith@fun <anonymous>(it: R|B|): R|kotlin/Unit| <kind=UNKNOWN> {
lval a: R|A| = <Inapplicable(INAPPLICABLE): [/bar]>#(::foo#)
lval b: R|B| = <Inapplicable(INAPPLICABLE): [/bar]>#(::foo#)
lval t3: <ERROR TYPE REF: Inapplicable(INAPPLICABLE): [/bar]> = <Inapplicable(INAPPLICABLE): [/bar]>#(::baz#)
<Inapplicable(INAPPLICABLE): [/bar]>#(::foo#)
R|/myWith|<R|A|, R|kotlin/Unit|>(R|/A.A|(), <L> = myWith@fun R|A|.<anonymous>(): R|kotlin/Unit| <kind=UNKNOWN> {
lval t1: R|A| = R|/bar|<R|A|>(::R|/A.foo|)
lval t2: R|A| = R|/bar|<R|A|>(::R|/A.baz|)
R|/myWith|<R|B|, R|kotlin/Unit|>(R|/B.B|(), <L> = myWith@fun R|B|.<anonymous>(): R|kotlin/Unit| <kind=UNKNOWN> {
lval a: R|A| = R|/bar|<R|A|>(::R|/A.foo|)
lval b: R|B| = R|/bar|<R|B|>(::R|/B.foo|)
lval t3: R|B| = R|/bar|<R|B|>(::R|/B.baz|)
R|/bar|<R|kotlin/Unit|>(::<Unresolved reference: foo>#)
}
)
}
)
}
public final inline fun <T, R> myWith(receiver: R|T|, block: R|kotlin/Function1<T, R>|): R|R| {
public final inline fun <T, R> myWith(receiver: R|T|, block: @R|kotlin/ExtensionFunctionType|() R|kotlin/Function1<T, R>|): R|R| {
^myWith R|kotlin/TODO|()
}
@@ -12,9 +12,9 @@ FILE: moreSpecificAmbiguousExtensions.kt
lval extFun2: R|kotlin/reflect/KFunction2<IB, IB, kotlin/Unit>| = Q|IB|::R|/extFun|
}
public final fun testWithExpectedType(): R|kotlin/Unit| {
lval extFun_AB_A: R|kotlin/Function2<IA, IB, kotlin/Unit>| = Q|IA|::R|/extFun|
lval extFun_AA_B: R|kotlin/Function2<IA, IA, kotlin/Unit>| = Q|IB|::R|/extFun|
lval extFun_BB_A: R|kotlin/Function2<IB, IB, kotlin/Unit>| = Q|IA|::R|/extFun|
lval extFun_BA_B: R|kotlin/Function2<IB, IA, kotlin/Unit>| = Q|IB|::R|/extFun|
lval extFun_BB_B: R|kotlin/Function2<IB, IB, kotlin/Unit>| = Q|IB|::R|/extFun|
lval extFun_AB_A: @R|kotlin/ExtensionFunctionType|() R|kotlin/Function2<IA, IB, kotlin/Unit>| = Q|IA|::R|/extFun|
lval extFun_AA_B: @R|kotlin/ExtensionFunctionType|() R|kotlin/Function2<IA, IA, kotlin/Unit>| = Q|IB|::R|/extFun|
lval extFun_BB_A: @R|kotlin/ExtensionFunctionType|() R|kotlin/Function2<IB, IB, kotlin/Unit>| = Q|IA|::R|/extFun|
lval extFun_BA_B: @R|kotlin/ExtensionFunctionType|() R|kotlin/Function2<IB, IA, kotlin/Unit>| = Q|IB|::R|/extFun|
lval extFun_BB_B: @R|kotlin/ExtensionFunctionType|() R|kotlin/Function2<IB, IB, kotlin/Unit>| = Q|IB|::R|/extFun|
}
@@ -0,0 +1,41 @@
interface A {
fun foo()
}
fun <T> myWith(receiver: T, block: T.() -> Unit) {
receiver.block()
}
fun <T> T.myApply(block: T.() -> Unit) {
this.block()
}
fun withA(block: A.() -> Unit) {}
fun test_1() {
withA {
foo()
}
}
fun test_2(a: A) {
myWith(a) {
foo()
}
}
fun test_3(a: A) {
a.myApply {
foo()
}
}
fun complexLambda(block: Int.(String) -> Unit) {}
fun test_4() {
complexLambda {
inc()
this.inc()
it.length
}
}
@@ -0,0 +1,41 @@
FILE: lambdaWithReceiver.kt
public abstract interface A : R|kotlin/Any| {
public abstract fun foo(): R|kotlin/Unit|
}
public final fun <T> myWith(receiver: R|T|, block: @R|kotlin/ExtensionFunctionType|() R|kotlin/Function1<T, kotlin/Unit>|): R|kotlin/Unit| {
R|<local>/receiver|.<Unresolved name: block>#()
}
public final fun <T> R|T|.myApply(block: @R|kotlin/ExtensionFunctionType|() R|kotlin/Function1<T, kotlin/Unit>|): R|kotlin/Unit| {
this@R|/myApply|.<Unresolved name: block>#()
}
public final fun withA(block: @R|kotlin/ExtensionFunctionType|() R|kotlin/Function1<A, kotlin/Unit>|): R|kotlin/Unit| {
}
public final fun test_1(): R|kotlin/Unit| {
R|/withA|(<L> = withA@fun R|A|.<anonymous>(): R|kotlin/Unit| {
this@R|/A|.R|/A.foo|()
}
)
}
public final fun test_2(a: R|A|): R|kotlin/Unit| {
R|/myWith|<R|A|>(R|<local>/a|, <L> = myWith@fun R|A|.<anonymous>(): R|kotlin/Unit| {
this@R|/A|.R|/A.foo|()
}
)
}
public final fun test_3(a: R|A|): R|kotlin/Unit| {
R|<local>/a|.R|/myApply|<R|A|>(<L> = myApply@fun R|A|.<anonymous>(): R|kotlin/Unit| {
this@R|/A|.R|/A.foo|()
}
)
}
public final fun complexLambda(block: @R|kotlin/ExtensionFunctionType|() R|kotlin/Function2<kotlin/Int, kotlin/String, kotlin/Unit>|): R|kotlin/Unit| {
}
public final fun test_4(): R|kotlin/Unit| {
R|/complexLambda|(<L> = complexLambda@fun R|kotlin/Int|.<anonymous>(it: R|kotlin/String|): R|kotlin/Unit| {
this@R|kotlin/Int|.R|kotlin/Int.inc|()
this@R|special/anonymous|.R|kotlin/Int.inc|()
R|<local>/it|.R|kotlin/String.length|
}
)
}
+1 -1
View File
@@ -4,7 +4,7 @@ FILE: functionTypes.kt
}
public final fun <T, R> R|kotlin/collections/List<T>|.simpleMap(f: R|kotlin/Function1<T, R>|): R|R| {
}
public final fun <T> simpleWith(t: R|T|, f: R|kotlin/Function1<T, kotlin/Unit>|): R|kotlin/Unit| {
public final fun <T> simpleWith(t: R|T|, f: @R|kotlin/ExtensionFunctionType|() R|kotlin/Function1<T, kotlin/Unit>|): R|kotlin/Unit| {
^simpleWith R|<local>/t|.<Unresolved name: f>#()
}
public abstract interface KMutableProperty1<T, R> : R|KProperty1<T, R>|, R|KMutableProperty<R>| {
@@ -8,7 +8,7 @@ FILE: implicitReceivers.kt
}
}
public final fun <T> R|T|.with(block: R|kotlin/Function1<T, kotlin/Unit>|): R|kotlin/Unit| {
public final fun <T> R|T|.with(block: @R|kotlin/ExtensionFunctionType|() R|kotlin/Function1<T, kotlin/Unit>|): R|kotlin/Unit| {
}
public final fun R|kotlin/Any?|.test_1(): R|kotlin/Unit| {
when () {
@@ -41,9 +41,9 @@ FILE: implicitReceivers.kt
<Unresolved name: foo>#()
}
public final fun test_3(a: R|kotlin/Any|, b: R|kotlin/Any|, c: R|kotlin/Any|): R|kotlin/Unit| {
R|kotlin/with|<R|kotlin/Any|, R|kotlin/Unit|>(R|<local>/a|, <L> = wa@fun R|kotlin/Any|.<anonymous>(it: R|kotlin/Any|): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
R|kotlin/with|<R|kotlin/Any|, R|kotlin/Unit|>(R|<local>/b|, <L> = wb@fun R|kotlin/Any|.<anonymous>(it: R|kotlin/Any|): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
R|kotlin/with|<R|kotlin/Any|, R|kotlin/Unit|>(R|<local>/c|, <L> = wc@fun R|kotlin/Any|.<anonymous>(it: R|kotlin/Any|): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
R|kotlin/with|<R|kotlin/Any|, R|kotlin/Unit|>(R|<local>/a|, <L> = wa@fun R|kotlin/Any|.<anonymous>(): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
R|kotlin/with|<R|kotlin/Any|, R|kotlin/Unit|>(R|<local>/b|, <L> = wb@fun R|kotlin/Any|.<anonymous>(): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
R|kotlin/with|<R|kotlin/Any|, R|kotlin/Unit|>(R|<local>/c|, <L> = wc@fun R|kotlin/Any|.<anonymous>(): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
(this@R|special/anonymous| as R|A|)
this@R|special/anonymous|.R|/A.foo|()
this@R|/A|.R|/A.foo|()
@@ -28,16 +28,16 @@ FILE: implicitReceiverOrder.kt
}
public final fun test(a: R|A|, b: R|B|): R|kotlin/Unit| {
R|kotlin/with|<R|B|, R|kotlin/Int|>(R|<local>/b|, <L> = with@fun R|B|.<anonymous>(it: R|B|): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
R|kotlin/with|<R|A|, R|kotlin/Int|>(R|<local>/a|, <L> = with@fun R|A|.<anonymous>(it: R|A|): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
R|kotlin/with|<R|B|, R|kotlin/Int|>(R|<local>/b|, <L> = with@fun R|B|.<anonymous>(): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
R|kotlin/with|<R|A|, R|kotlin/Int|>(R|<local>/a|, <L> = with@fun R|A|.<anonymous>(): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
this@R|/A|.R|/A.foo|()
(this@R|/B|, this@R|special/anonymous|).R|/B.bar|()
}
)
}
)
R|kotlin/with|<R|A|, R|kotlin/Int|>(R|<local>/a|, <L> = with@fun R|A|.<anonymous>(it: R|A|): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
R|kotlin/with|<R|B|, R|kotlin/Int|>(R|<local>/b|, <L> = with@fun R|B|.<anonymous>(it: R|B|): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
R|kotlin/with|<R|A|, R|kotlin/Int|>(R|<local>/a|, <L> = with@fun R|A|.<anonymous>(): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
R|kotlin/with|<R|B|, R|kotlin/Int|>(R|<local>/b|, <L> = with@fun R|B|.<anonymous>(): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
this@R|/B|.R|/B.foo|()
(this@R|/A|, this@R|special/anonymous|).R|/A.bar|()
}
+1 -1
View File
@@ -5,7 +5,7 @@ FILE: mapList.kt
R|<local>/it|.R|kotlin/Int.plus|(R|<local>/it|)
}
)
R|<local>/u|.R|/applyX|<R|kotlin/collections/List<kotlin/Int>|>(<L> = applyX@fun R|kotlin/collections/List<kotlin/Int>|.<anonymous>(it: R|kotlin/collections/List<kotlin/Int>|): R|kotlin/Unit| <kind=UNKNOWN> {
R|<local>/u|.R|/applyX|<R|kotlin/collections/List<kotlin/Int>|>(<L> = applyX@fun R|kotlin/collections/List<kotlin/Int>|.<anonymous>(): R|kotlin/Unit| <kind=UNKNOWN> {
this@R|special/anonymous|.R|FakeOverride<kotlin/collections/List.contains: R|kotlin/Boolean|>|(Int(1))
this@R|kotlin/collections/List|.R|FakeOverride<kotlin/collections/List.contains: R|kotlin/Boolean|>|(Int(1))
}
@@ -25,10 +25,10 @@ FILE: multipleImplicitReceivers.kt
}
public final fun test(fooImpl: R|IFoo|, invokeImpl: R|IInvoke|): R|kotlin/Unit| {
R|kotlin/with|<R|A|, R|kotlin/Int|>(Q|A|, <L> = with@fun R|A|.<anonymous>(it: R|A|): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
R|kotlin/with|<R|IFoo|, R|kotlin/Int|>(R|<local>/fooImpl|, <L> = with@fun R|IFoo|.<anonymous>(it: R|IFoo|): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
R|kotlin/with|<R|A|, R|kotlin/Int|>(Q|A|, <L> = with@fun R|A|.<anonymous>(): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
R|kotlin/with|<R|IFoo|, R|kotlin/Int|>(R|<local>/fooImpl|, <L> = with@fun R|IFoo|.<anonymous>(): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
(this@R|/IFoo|, this@R|special/anonymous|).R|/IFoo.foo|
R|kotlin/with|<R|IInvoke|, R|kotlin/Int|>(R|<local>/invokeImpl|, <L> = with@fun R|IInvoke|.<anonymous>(it: R|IInvoke|): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
R|kotlin/with|<R|IInvoke|, R|kotlin/Int|>(R|<local>/invokeImpl|, <L> = with@fun R|IInvoke|.<anonymous>(): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
(this@R|/IInvoke|, (this@R|/IFoo|, this@R|special/anonymous|).R|/IFoo.foo|).R|/IInvoke.invoke|()
}
)
@@ -4,7 +4,7 @@ FILE: recursiveBug.kt
super<R|kotlin/Any|>()
}
public final val result: R|kotlin/String| = this@R|/Foo|.R|kotlin/run|<R|Foo|, R|kotlin/String|>(<L> = run@fun R|Foo|.<anonymous>(it: R|Foo|): R|kotlin/String| <kind=EXACTLY_ONCE> {
public final val result: R|kotlin/String| = this@R|/Foo|.R|kotlin/run|<R|Foo|, R|kotlin/String|>(<L> = run@fun R|Foo|.<anonymous>(): R|kotlin/String| <kind=EXACTLY_ONCE> {
R|<local>/name|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/String|>|()
}
)
@@ -35,7 +35,7 @@ FILE: topLevelResolve.kt
R|<local>/it|.R|kotlin/String.length|
}
)
lval viaWith: R|kotlin/collections/List<kotlin/Int>| = R|kotlin/with|<R|kotlin/collections/List<kotlin/Int>|, R|kotlin/collections/List<kotlin/Int>|>(R|kotlin/collections/listOf|<R|kotlin/Int|>(Int(42)), <L> = with@fun R|kotlin/collections/List<kotlin/Int>|.<anonymous>(it: R|kotlin/collections/List<kotlin/Int>|): R|kotlin/collections/List<kotlin/Int>| <kind=EXACTLY_ONCE> {
lval viaWith: R|kotlin/collections/List<kotlin/Int>| = R|kotlin/with|<R|kotlin/collections/List<kotlin/Int>|, R|kotlin/collections/List<kotlin/Int>|>(R|kotlin/collections/listOf|<R|kotlin/Int|>(Int(42)), <L> = with@fun R|kotlin/collections/List<kotlin/Int>|.<anonymous>(): R|kotlin/collections/List<kotlin/Int>| <kind=EXACTLY_ONCE> {
this@R|special/anonymous|.R|kotlin/collections/map|<R|kotlin/Int|, R|kotlin/Int|>(<L> = map@fun <anonymous>(it: R|kotlin/Int|): R|kotlin/Int| <kind=UNKNOWN> {
R|<local>/it|.R|kotlin/Int.times|(R|<local>/it|)
}
@@ -44,11 +44,11 @@ FILE: topLevelResolve.kt
)
}
public final fun testWith(): R|kotlin/Unit| {
lval length: R|kotlin/Int| = R|kotlin/with|<R|kotlin/String|, R|kotlin/Int|>(String(), <L> = with@fun R|kotlin/String|.<anonymous>(it: R|kotlin/String|): R|kotlin/Int| <kind=EXACTLY_ONCE> {
lval length: R|kotlin/Int| = R|kotlin/with|<R|kotlin/String|, R|kotlin/Int|>(String(), <L> = with@fun R|kotlin/String|.<anonymous>(): R|kotlin/Int| <kind=EXACTLY_ONCE> {
this@R|kotlin/String|.R|kotlin/String.length|
}
)
lval indices: R|kotlin/ranges/IntRange| = R|kotlin/with|<R|kotlin/String|, R|kotlin/ranges/IntRange|>(String(), <L> = with@fun R|kotlin/String|.<anonymous>(it: R|kotlin/String|): R|kotlin/ranges/IntRange| <kind=EXACTLY_ONCE> {
lval indices: R|kotlin/ranges/IntRange| = R|kotlin/with|<R|kotlin/String|, R|kotlin/ranges/IntRange|>(String(), <L> = with@fun R|kotlin/String|.<anonymous>(): R|kotlin/ranges/IntRange| <kind=EXACTLY_ONCE> {
this@R|special/anonymous|.R|kotlin/text/indices|
}
)
@@ -326,6 +326,11 @@ public class FirResolveTestCaseGenerated extends AbstractFirResolveTestCase {
runTest("compiler/fir/resolve/testData/resolve/expresssions/lambda.kt");
}
@TestMetadata("lambdaWithReceiver.kt")
public void testLambdaWithReceiver() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/expresssions/lambdaWithReceiver.kt");
}
@TestMetadata("localConstructor.kt")
public void testLocalConstructor() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/expresssions/localConstructor.kt");