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
This commit is contained in:
Svyatoslav Scherbina
2021-11-12 11:45:09 +03:00
committed by Space
parent fb859c0270
commit 2f5706597a
8 changed files with 181 additions and 1 deletions
@@ -45,7 +45,11 @@ fun <T : IrElement> IrStatementsBuilder<T>.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
+3
View File
@@ -0,0 +1,3 @@
fun intN(): Int? = null
fun test() = intN() ?: 1
+20
View File
@@ -0,0 +1,20 @@
@0:0..3:0 FILE fqName:<root> 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 <root>'
@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 <root>'
@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 <root>' 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 <root>.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 <root>.test' type=kotlin.Int? origin=null
@@ -0,0 +1,6 @@
fun test() {
var x = 0
var y = 0
y = x++
y = x--
}
@@ -0,0 +1,23 @@
@0:0..6:0 FILE fqName:<root> 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 <root>.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 <root>.test' type=kotlin.Int origin=POSTFIX_INCR
@3:8..9 SET_VAR 'var x: kotlin.Int [var] declared in <root>.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 <root>.test' type=kotlin.Int origin=null
@3:8..11 GET_VAR 'val tmp0: kotlin.Int [val] declared in <root>.test' type=kotlin.Int origin=null
@4:4..5 SET_VAR 'var y: kotlin.Int [var] declared in <root>.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 <root>.test' type=kotlin.Int origin=POSTFIX_DECR
@4:8..9 SET_VAR 'var x: kotlin.Int [var] declared in <root>.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 <root>.test' type=kotlin.Int origin=null
@4:8..11 GET_VAR 'val tmp1: kotlin.Int [val] declared in <root>.test' type=kotlin.Int origin=null
@@ -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)
@@ -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"
}
@@ -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<in T> {
suspend fun emit(value: T)
}
interface Flow<out T> {
suspend fun collect(collector: FlowCollector<T>)
}
suspend inline fun <T> Flow<T>.collect(crossinline action: suspend (value: T) -> Unit): Unit =
collect(object : FlowCollector<T> {
override suspend fun emit(value: T) = action(value)
})
inline fun <T> unsafeFlow(crossinline block: suspend FlowCollector<T>.() -> Unit): Flow<T> {
return object : Flow<T> {
override suspend fun collect(collector: FlowCollector<T>) {
collector.block()
}
}
}
inline fun <T, R> Flow<T>.unsafeTransform(
crossinline transform: suspend FlowCollector<R>.(value: T) -> Unit
): Flow<R> = unsafeFlow {
collect { value ->
return@collect transform(value)
}
}
inline fun <T, R: Any> Flow<T>.mapNotNull(crossinline transform: suspend (value: T) -> R?): Flow<R> = unsafeTransform { value ->
val transformed = transform(value) ?: return@unsafeTransform
return@unsafeTransform emit(transformed)
}
fun <T> flowOf(value: T): Flow<T> = unsafeFlow {
emit(value)
}
suspend fun <T> Flow<T>.toList(): List<T> {
val result = mutableListOf<T>()
collect {
result.add(it)
}
return result
}
// Close to https://youtrack.jetbrains.com/issue/KT-49360:
fun testWithFlowMapNotNull(flow: Flow<Boolean>): Flow<Block> {
return flow.mapNotNull {
if (it) Block({ 333 }) else null
}
}
@Test
fun testWithFlow() {
lateinit var list1: List<Block>
lateinit var list2: List<Block>
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<Any?> {
companion object : EmptyContinuation()
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}