Skip to content

Commit 015da2f

Browse files
Evgeny GerashchenkoEvgeny Gerashchenko
authored andcommitted
More proper code to check if lambda is inlined. Parameter of inline function can be annotated with "noInline".
1 parent d24d750 commit 015da2f

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/util/callUtil.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.utils.sure
3232
import org.jetbrains.kotlin.psi.psiUtil.getTextWithLocation
3333
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
3434
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor
35+
import org.jetbrains.kotlin.resolve.InlineDescriptorUtils
3536
import org.jetbrains.kotlin.resolve.calls.context.ResolutionContext
3637

3738
// resolved call
@@ -183,9 +184,6 @@ public fun JetExpression.getFunctionResolvedCallWithAssert(context: BindingConte
183184
public fun JetFunctionLiteral.isInlined(bindingContext: BindingContext): Boolean {
184185
val parent = this.getParent()
185186
assert(parent is JetFunctionLiteralExpression) { "parent of JetFunctionLiteral is " + parent }
186-
val resolvedCall = (parent as JetFunctionLiteralExpression).getParentResolvedCall(bindingContext, true)
187-
if (resolvedCall == null) return false
188187

189-
val callable = resolvedCall.getResultingDescriptor()
190-
return callable is SimpleFunctionDescriptor && callable.getInlineStrategy().isInline()
188+
return InlineDescriptorUtils.isInlineLambda(parent as JetFunctionLiteralExpression, bindingContext, false)
191189
}

idea/testData/checker/infos/CapturedInInlinedClosure.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,15 @@ fun objectExpression() {
8888
val <info descr="Value captured in a closure">u3</info> = ""
8989
object : Throwable(run { <info descr="Value captured in a closure">u3</info> }) {
9090
}
91-
}
91+
}
92+
93+
inline fun withNoInlineParam(noinline task1: () -> Unit, task2: () -> Unit) {
94+
task1()
95+
task2()
96+
}
97+
98+
fun usage(<info descr="Value captured in a closure">param1</info>: Int, param2: Int) {
99+
withNoInlineParam({ println(<info descr="Value captured in a closure">param1</info>) }, { println(param2) })
100+
}
101+
102+
fun println(<warning>a</warning>: Any) {}

idea/testData/codeInsight/lineMarker/recursiveCall/inInlinedLambda.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,13 @@ fun f(a: Int) {
22
run {
33
<lineMarker>f</lineMarker>(a - 1)
44
}
5-
}
5+
}
6+
7+
fun ff(a: Int) {
8+
run1 {
9+
ff(a - 1)
10+
}
11+
}
12+
13+
inline fun <T> run1(noinline f: () -> T): T { }
14+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fun f(a: Int): Int {
2+
if (a < 5) {
3+
run1(fun (): Int {
4+
<caret>return 1
5+
})
6+
}
7+
return 2
8+
}
9+
10+
inline public fun <T> run1(noinline f: () -> T): T { }
11+
12+
//HIGHLIGHTED: return 1

idea/tests/org/jetbrains/kotlin/idea/highlighter/HighlightExitPointsTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ public void testInlineLocalReturn2() throws Exception {
6060
doTest(fileName);
6161
}
6262

63+
@TestMetadata("inlineWithNoInlineParam.kt")
64+
public void testInlineWithNoInlineParam() throws Exception {
65+
String fileName = JetTestUtils.navigationMetadata("idea/testData/exitPoints/inlineWithNoInlineParam.kt");
66+
doTest(fileName);
67+
}
68+
6369
@TestMetadata("localFunction1.kt")
6470
public void testLocalFunction1() throws Exception {
6571
String fileName = JetTestUtils.navigationMetadata("idea/testData/exitPoints/localFunction1.kt");

0 commit comments

Comments
 (0)