Tracking of implicit receiver from enclosing lambda

This commit is contained in:
Valentin Kipyatkov
2020-04-11 12:40:36 +03:00
parent 4e88e2697c
commit 52ff1c62dd
5 changed files with 44 additions and 10 deletions
@@ -390,14 +390,24 @@ class InflowSlicer(
val resolvedCall = refElement.resolveToCall() ?: return emptyList()
when (val receiver = resolvedCall.extensionReceiver) {
is ExpressionReceiver -> {
return listOf(KotlinSliceUsage(receiver.expression, parent, behaviour, true))
return listOf(KotlinSliceUsage(receiver.expression, parent, behaviour, forcedExpressionMode = true))
}
is ImplicitReceiver -> {
val callableDeclaration = (receiver.declarationDescriptor as? CallableDescriptor)?.originalSource?.getPsi()
val receiverTypeReference = (callableDeclaration as? KtCallableDeclaration)?.receiverTypeReference
?: return emptyList()
return listOf(KotlinSliceUsage(receiverTypeReference, parent, behaviour, false))
val callableDescriptor = receiver.declarationDescriptor as? CallableDescriptor ?: return emptyList()
when (val callableDeclaration = callableDescriptor.originalSource.getPsi()) {
is KtFunctionLiteral -> {
val newBehaviour = LambdaCallsBehaviour(ReceiverSliceProducer, behaviour)
return listOf(KotlinSliceUsage(callableDeclaration, parent, newBehaviour, forcedExpressionMode = true))
}
is KtCallableDeclaration -> {
val receiverTypeReference = callableDeclaration.receiverTypeReference ?: return emptyList()
return listOf(KotlinSliceUsage(receiverTypeReference, parent, behaviour, false))
}
else -> return emptyList()
}
}
else -> return emptyList()
+8
View File
@@ -14,8 +14,16 @@ fun String.foo() {
1.extensionFun()
extensionFun()
with(123) {
extensionFun()
}
}
fun main() {
"A".foo()
}
inline fun <T, R> with(receiver: T, block: T.() -> R): R {
return receiver.block()
}
@@ -2,10 +2,10 @@
8 fun <bold>Any</bold>.extensionFun() {
12 <bold>""</bold>.extensionFun()
20 <bold>"A"</bold>.foo()
24 <bold>"A"</bold>.foo()
8 fun <bold>Any</bold>.extensionFun() {
11 fun <bold>String</bold>.foo() {
20 <bold>"A"</bold>.foo()
24 <bold>"A"</bold>.foo()
3 KotlinUtil.extensionFun(<bold>"x"</bold>);
8 fun <bold>Any</bold>.extensionFun() {
@@ -15,3 +15,11 @@
8 fun <bold>Any</bold>.extensionFun() {
14 <bold>1</bold>.extensionFun()
18 with(<bold>123</bold>) {
8 fun <bold>Any</bold>.extensionFun() {
18 [LAMBDA CALLS] with(123) <bold>{</bold>
27 [LAMBDA CALLS] inline fun <T, R> with(receiver: T, <bold>block: T.() -> R</bold>): R {
28 return <bold>receiver</bold>.block()
27 inline fun <T, R> with(<bold>receiver: T</bold>, block: T.() -> R): R {
18 with(<bold>123</bold>) {
@@ -5,10 +5,13 @@
14 <bold>1</bold>.extensionFun()
8 fun <bold>Any</bold>.extensionFun() {
14 <bold>1</bold>.extensionFun()
20 <bold>"A"</bold>.foo()
18 with(123) <bold>{</bold>
8 fun <bold>Any</bold>.extensionFun() {
18 [LAMBDA CALLS] with(123) <bold>{</bold>
24 <bold>"A"</bold>.foo()
8 fun <bold>Any</bold>.extensionFun() {
11 fun <bold>String</bold>.foo() {
20 <bold>"A"</bold>.foo()
24 <bold>"A"</bold>.foo()
[Other Values]
3 KotlinUtil.extensionFun(<bold>"x"</bold>);
@@ -1,6 +1,11 @@
8 fun <bold>Any</bold>.extensionFun() {
3 KotlinUtil.extensionFun(<bold>"x"</bold>);
11 fun <bold>String</bold>.foo() {
20 <bold>"A"</bold>.foo()
24 <bold>"A"</bold>.foo()
12 <bold>""</bold>.extensionFun()
14 <bold>1</bold>.extensionFun()
18 [LAMBDA CALLS] with(123) <bold>{</bold>
27 [LAMBDA CALLS] inline fun <T, R> with(receiver: T, <bold>block: T.() -> R</bold>): R {
28 return <bold>receiver</bold>.block()
27 inline fun <T, R> with(<bold>receiver: T</bold>, block: T.() -> R): R {
18 with(<bold>123</bold>) {