From 2f5706597aad4a6ec36b43c44396438b3b2d7d05 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Fri, 12 Nov 2021 11:45:09 +0300 Subject: [PATCH] IR: fix offsets in irTemporary The variable generated by IrStatementBuilder.irTemporary doesn't inherit startOffset and endOffset from the builder. In particular, as a result, temporary variables generated for elvis operator left operand have UNDEFINED_OFFSET. Additionally, ProvisionalFunctionExpressionLowering copies the offsets of a variable to lowered lambda in the variable initializer. With the problem described above, this causes invalid debug information in Kotlin/Native, see KT-49360. Fix irTemporary by using builder's offsets for the variable. ^KT-49360 Fixed --- .../kotlin/ir/builders/ExpressionHelpers.kt | 6 +- compiler/testData/ir/sourceRanges/elvis.kt | 3 + compiler/testData/ir/sourceRanges/elvis.txt | 20 ++++ .../sourceRanges/postfixIncrementDecrement.kt | 6 + .../postfixIncrementDecrement.txt | 23 ++++ .../ir/IrSourceRangesTestCaseGenerated.java | 10 ++ .../backend.native/tests/build.gradle | 4 + .../tests/codegen/lambda/lambda_kt49360.kt | 110 ++++++++++++++++++ 8 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/ir/sourceRanges/elvis.kt create mode 100644 compiler/testData/ir/sourceRanges/elvis.txt create mode 100644 compiler/testData/ir/sourceRanges/postfixIncrementDecrement.kt create mode 100644 compiler/testData/ir/sourceRanges/postfixIncrementDecrement.txt create mode 100644 kotlin-native/backend.native/tests/codegen/lambda/lambda_kt49360.kt diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/builders/ExpressionHelpers.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/builders/ExpressionHelpers.kt index 54d3fa46e3e..4c4afc47874 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/builders/ExpressionHelpers.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/builders/ExpressionHelpers.kt @@ -45,7 +45,11 @@ fun IrStatementsBuilder.irTemporary( irType: IrType = value?.type!!, // either value or irType should be supplied at callsite isMutable: Boolean = false, ): IrVariable { - val temporary = scope.createTemporaryVariableDeclaration(irType, nameHint, isMutable) + val temporary = scope.createTemporaryVariableDeclaration( + irType, nameHint, isMutable, + startOffset = startOffset, + endOffset = endOffset + ) value?.let { temporary.initializer = it } +temporary return temporary diff --git a/compiler/testData/ir/sourceRanges/elvis.kt b/compiler/testData/ir/sourceRanges/elvis.kt new file mode 100644 index 00000000000..1841486b9e3 --- /dev/null +++ b/compiler/testData/ir/sourceRanges/elvis.kt @@ -0,0 +1,3 @@ +fun intN(): Int? = null + +fun test() = intN() ?: 1 diff --git a/compiler/testData/ir/sourceRanges/elvis.txt b/compiler/testData/ir/sourceRanges/elvis.txt new file mode 100644 index 00000000000..2903bffebdb --- /dev/null +++ b/compiler/testData/ir/sourceRanges/elvis.txt @@ -0,0 +1,20 @@ +@0:0..3:0 FILE fqName: fileName:/elvis.kt + @0:0..23 FUN name:intN visibility:public modality:FINAL <> () returnType:kotlin.Int? + @0:19..23 BLOCK_BODY + @0:23..23 RETURN type=kotlin.Nothing from='public final fun intN (): kotlin.Int? declared in ' + @0:19..23 CONST Null type=kotlin.Nothing? value=null + @2:0..24 FUN name:test visibility:public modality:FINAL <> () returnType:kotlin.Int + @2:13..24 BLOCK_BODY + @2:24..24 RETURN type=kotlin.Nothing from='public final fun test (): kotlin.Int declared in ' + @2:13..24 BLOCK type=kotlin.Int origin=ELVIS + @2:13..24 VAR IR_TEMPORARY_VARIABLE name:tmp0_elvis_lhs type:kotlin.Int? [val] + @2:13..19 CALL 'public final fun intN (): kotlin.Int? declared in ' type=kotlin.Int? origin=null + @2:13..24 WHEN type=kotlin.Int origin=null + @2:13..24 BRANCH + @2:13..24 CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EQEQ + @2:13..24 GET_VAR 'val tmp0_elvis_lhs: kotlin.Int? [val] declared in .test' type=kotlin.Int? origin=null + @2:13..24 CONST Null type=kotlin.Nothing? value=null + @2:23..24 CONST Int type=kotlin.Int value=1 + @2:13..24 BRANCH + @2:13..24 CONST Boolean type=kotlin.Boolean value=true + @2:13..24 GET_VAR 'val tmp0_elvis_lhs: kotlin.Int? [val] declared in .test' type=kotlin.Int? origin=null diff --git a/compiler/testData/ir/sourceRanges/postfixIncrementDecrement.kt b/compiler/testData/ir/sourceRanges/postfixIncrementDecrement.kt new file mode 100644 index 00000000000..cea5814f2bc --- /dev/null +++ b/compiler/testData/ir/sourceRanges/postfixIncrementDecrement.kt @@ -0,0 +1,6 @@ +fun test() { + var x = 0 + var y = 0 + y = x++ + y = x-- +} diff --git a/compiler/testData/ir/sourceRanges/postfixIncrementDecrement.txt b/compiler/testData/ir/sourceRanges/postfixIncrementDecrement.txt new file mode 100644 index 00000000000..58387d5c1b9 --- /dev/null +++ b/compiler/testData/ir/sourceRanges/postfixIncrementDecrement.txt @@ -0,0 +1,23 @@ +@0:0..6:0 FILE fqName: fileName:/postfixIncrementDecrement.kt + @0:0..5:1 FUN name:test visibility:public modality:FINAL <> () returnType:kotlin.Unit + @0:11..5:1 BLOCK_BODY + @1:4..13 VAR name:x type:kotlin.Int [var] + @1:12..13 CONST Int type=kotlin.Int value=0 + @2:4..13 VAR name:y type:kotlin.Int [var] + @2:12..13 CONST Int type=kotlin.Int value=0 + @3:4..5 SET_VAR 'var y: kotlin.Int [var] declared in .test' type=kotlin.Unit origin=EQ + @3:8..11 BLOCK type=kotlin.Int origin=POSTFIX_INCR + @3:8..11 VAR IR_TEMPORARY_VARIABLE name:tmp0 type:kotlin.Int [val] + @3:8..9 GET_VAR 'var x: kotlin.Int [var] declared in .test' type=kotlin.Int origin=POSTFIX_INCR + @3:8..9 SET_VAR 'var x: kotlin.Int [var] declared in .test' type=kotlin.Unit origin=POSTFIX_INCR + @3:8..11 CALL 'public final fun inc (): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=POSTFIX_INCR + @3:8..11 GET_VAR 'val tmp0: kotlin.Int [val] declared in .test' type=kotlin.Int origin=null + @3:8..11 GET_VAR 'val tmp0: kotlin.Int [val] declared in .test' type=kotlin.Int origin=null + @4:4..5 SET_VAR 'var y: kotlin.Int [var] declared in .test' type=kotlin.Unit origin=EQ + @4:8..11 BLOCK type=kotlin.Int origin=POSTFIX_DECR + @4:8..11 VAR IR_TEMPORARY_VARIABLE name:tmp1 type:kotlin.Int [val] + @4:8..9 GET_VAR 'var x: kotlin.Int [var] declared in .test' type=kotlin.Int origin=POSTFIX_DECR + @4:8..9 SET_VAR 'var x: kotlin.Int [var] declared in .test' type=kotlin.Unit origin=POSTFIX_DECR + @4:8..11 CALL 'public final fun dec (): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=POSTFIX_DECR + @4:8..11 GET_VAR 'val tmp1: kotlin.Int [val] declared in .test' type=kotlin.Int origin=null + @4:8..11 GET_VAR 'val tmp1: kotlin.Int [val] declared in .test' type=kotlin.Int origin=null diff --git a/compiler/tests-gen/org/jetbrains/kotlin/ir/IrSourceRangesTestCaseGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/ir/IrSourceRangesTestCaseGenerated.java index 2e680dc42f4..4ddbfae78d2 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/ir/IrSourceRangesTestCaseGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/ir/IrSourceRangesTestCaseGenerated.java @@ -39,6 +39,11 @@ public class IrSourceRangesTestCaseGenerated extends AbstractIrSourceRangesTestC runTest("compiler/testData/ir/sourceRanges/comments.kt"); } + @TestMetadata("elvis.kt") + public void testElvis() throws Exception { + runTest("compiler/testData/ir/sourceRanges/elvis.kt"); + } + @TestMetadata("kt17108.kt") public void testKt17108() throws Exception { runTest("compiler/testData/ir/sourceRanges/kt17108.kt"); @@ -49,6 +54,11 @@ public class IrSourceRangesTestCaseGenerated extends AbstractIrSourceRangesTestC runTest("compiler/testData/ir/sourceRanges/kt24258.kt"); } + @TestMetadata("postfixIncrementDecrement.kt") + public void testPostfixIncrementDecrement() throws Exception { + runTest("compiler/testData/ir/sourceRanges/postfixIncrementDecrement.kt"); + } + @TestMetadata("compiler/testData/ir/sourceRanges/declarations") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) diff --git a/kotlin-native/backend.native/tests/build.gradle b/kotlin-native/backend.native/tests/build.gradle index 61963e3df31..b582514e5f3 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -3180,6 +3180,10 @@ standaloneTest("lambda14") { flags = ['-tr'] } +task lambda_kt49360(type: KonanLocalTest) { + source = "codegen/lambda/lambda_kt49360.kt" +} + task funInterface_implIsNotFunction(type: KonanLocalTest) { source = "codegen/funInterface/implIsNotFunction.kt" } diff --git a/kotlin-native/backend.native/tests/codegen/lambda/lambda_kt49360.kt b/kotlin-native/backend.native/tests/codegen/lambda/lambda_kt49360.kt new file mode 100644 index 00000000000..849fbf407c9 --- /dev/null +++ b/kotlin-native/backend.native/tests/codegen/lambda/lambda_kt49360.kt @@ -0,0 +1,110 @@ +package codegen.lambda.lambda_kt49360 + +import kotlin.test.* +import kotlin.coroutines.* + +// To be tested with -g. + +// https://youtrack.jetbrains.com/issue/KT-49360 + +fun testTrivialCreateBlock(result: Int): () -> Int { + return (if (result == 0) { { 0 } } else null) ?: { result } +} + +@Test +fun testTrivial() { + assertEquals(0, testTrivialCreateBlock(0)()) + assertEquals(1, testTrivialCreateBlock(1)()) +} + +class Block(val block: () -> Int) + +fun testWrapBlockCreate(flag: Boolean): Block { + return (if (flag) Block { 11 } else null) ?: Block { 22 } +} + +@Test +fun testWrapBlock() { + assertEquals(11, testWrapBlockCreate(true).block()) + assertEquals(22, testWrapBlockCreate(false).block()) +} + +// The Flow code below is taken from kotlinx.coroutines (some unrelated details removed). + +interface FlowCollector { + suspend fun emit(value: T) +} + +interface Flow { + suspend fun collect(collector: FlowCollector) +} + +suspend inline fun Flow.collect(crossinline action: suspend (value: T) -> Unit): Unit = + collect(object : FlowCollector { + override suspend fun emit(value: T) = action(value) + }) + +inline fun unsafeFlow(crossinline block: suspend FlowCollector.() -> Unit): Flow { + return object : Flow { + override suspend fun collect(collector: FlowCollector) { + collector.block() + } + } +} + +inline fun Flow.unsafeTransform( + crossinline transform: suspend FlowCollector.(value: T) -> Unit +): Flow = unsafeFlow { + collect { value -> + return@collect transform(value) + } +} + +inline fun Flow.mapNotNull(crossinline transform: suspend (value: T) -> R?): Flow = unsafeTransform { value -> + val transformed = transform(value) ?: return@unsafeTransform + return@unsafeTransform emit(transformed) +} + +fun flowOf(value: T): Flow = unsafeFlow { + emit(value) +} + +suspend fun Flow.toList(): List { + val result = mutableListOf() + collect { + result.add(it) + } + return result +} + +// Close to https://youtrack.jetbrains.com/issue/KT-49360: +fun testWithFlowMapNotNull(flow: Flow): Flow { + return flow.mapNotNull { + if (it) Block({ 333 }) else null + } +} + +@Test +fun testWithFlow() { + lateinit var list1: List + lateinit var list2: List + + builder { + list1 = testWithFlowMapNotNull(flowOf(true)).toList() + list2 = testWithFlowMapNotNull(flowOf(false)).toList() + } + + assertEquals(1, list1.size) + assertEquals(333, list1.single().block()) + + assertEquals(0, list2.size) +} + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resumeWith(result: Result) { result.getOrThrow() } +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +}