[FIR2IR] Set correct endOffset for the IrBranch

We want to use end offset of branch's result as the end for the whole
branch, not just the end of its condition.

#KT-63779 Fixed
This commit is contained in:
Ivan Kylchik
2023-12-21 15:26:05 +01:00
committed by Space Team
parent 582dd1d3c0
commit a56bebe44e
10 changed files with 103 additions and 3 deletions
@@ -1291,13 +1291,13 @@ class Fir2IrVisitor(
}
private fun FirWhenBranch.toIrWhenBranch(whenExpressionType: ConeKotlinType): IrBranch {
return convertWithOffsets { startOffset, endOffset ->
return convertWithOffsets { startOffset, _ ->
val condition = condition
val irResult = convertToIrExpression(result).insertImplicitCast(result, result.resolvedType, whenExpressionType)
if (condition is FirElseIfTrueCondition) {
IrElseBranchImpl(IrConstImpl.boolean(irResult.startOffset, irResult.endOffset, irBuiltIns.booleanType, true), irResult)
} else {
IrBranchImpl(startOffset, endOffset, convertToIrExpression(condition), irResult)
IrBranchImpl(startOffset, irResult.endOffset, convertToIrExpression(condition), irResult)
}
}
}
@@ -67,6 +67,18 @@ public class FirLightTreeJvmIrSourceRangesTestGenerated extends AbstractFirLight
runTest("compiler/testData/ir/sourceRanges/kt24258.kt");
}
@Test
@TestMetadata("kt63779.kt")
public void testKt63779() throws Exception {
runTest("compiler/testData/ir/sourceRanges/kt63779.kt");
}
@Test
@TestMetadata("kt63779_2.kt")
public void testKt63779_2() throws Exception {
runTest("compiler/testData/ir/sourceRanges/kt63779_2.kt");
}
@Test
@TestMetadata("multiLineCall.kt")
public void testMultiLineCall() throws Exception {
@@ -67,6 +67,18 @@ public class FirPsiJvmIrSourceRangesTestGenerated extends AbstractFirPsiJvmIrSou
runTest("compiler/testData/ir/sourceRanges/kt24258.kt");
}
@Test
@TestMetadata("kt63779.kt")
public void testKt63779() throws Exception {
runTest("compiler/testData/ir/sourceRanges/kt63779.kt");
}
@Test
@TestMetadata("kt63779_2.kt")
public void testKt63779_2() throws Exception {
runTest("compiler/testData/ir/sourceRanges/kt63779_2.kt");
}
@Test
@TestMetadata("multiLineCall.kt")
public void testMultiLineCall() throws Exception {
+8
View File
@@ -0,0 +1,8 @@
// FIR_IDENTICAL
fun topLevelMethod(x: Int, y: Int) {
if(x > y) {
} else if(x < y) {
} else {
}
}
+19
View File
@@ -0,0 +1,19 @@
@0:0..8:0 FILE fqName:<root> fileName:/kt63779.kt
@2:0..7:1 FUN name:topLevelMethod visibility:public modality:FINAL <> (x:kotlin.Int, y:kotlin.Int) returnType:kotlin.Unit
@2:19..25 VALUE_PARAMETER name:x index:0 type:kotlin.Int
@2:27..33 VALUE_PARAMETER name:y index:1 type:kotlin.Int
@2:35..7:1 BLOCK_BODY
@3:4..6:5 WHEN type=kotlin.Unit origin=IF
@3:7..4:5 BRANCH
@3:7..12 CALL 'public final fun greater (arg0: kotlin.Int, arg1: kotlin.Int): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=GT
@3:7..8 GET_VAR 'x: kotlin.Int declared in <root>.topLevelMethod' type=kotlin.Int origin=null
@3:11..12 GET_VAR 'y: kotlin.Int declared in <root>.topLevelMethod' type=kotlin.Int origin=null
@3:14..4:5 BLOCK type=kotlin.Unit origin=null
@4:14..5:5 BRANCH
@4:14..19 CALL 'public final fun less (arg0: kotlin.Int, arg1: kotlin.Int): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=LT
@4:14..15 GET_VAR 'x: kotlin.Int declared in <root>.topLevelMethod' type=kotlin.Int origin=null
@4:18..19 GET_VAR 'y: kotlin.Int declared in <root>.topLevelMethod' type=kotlin.Int origin=null
@4:21..5:5 BLOCK type=kotlin.Unit origin=null
@5:11..6:5 BRANCH
@5:11..6:5 CONST Boolean type=kotlin.Boolean value=true
@5:11..6:5 BLOCK type=kotlin.Unit origin=null
+16
View File
@@ -0,0 +1,16 @@
@0:0..5:0 FILE fqName:<root> fileName:/kt63779_2.kt
@1:0..4:1 FUN name:topLevelMethod visibility:public modality:FINAL <> () returnType:kotlin.Unit
@1:21..4:1 BLOCK_BODY
@2:4..13 VAR name:z type:kotlin.Int [var]
@2:12..13 CONST Int type=kotlin.Int value=3
@3:4..37 WHEN type=kotlin.Unit origin=IF
@3:7..22 BRANCH
@3:7..11 CONST Boolean type=kotlin.Boolean value=true
@3:13..22 BLOCK type=kotlin.Unit origin=null
@3:15..20 SET_VAR 'var z: kotlin.Int [var] declared in <root>.topLevelMethod' type=kotlin.Unit origin=EQ
@3:19..20 CONST Int type=kotlin.Int value=4
@3:28..37 BRANCH
@3:28..37 CONST Boolean type=kotlin.Boolean value=true
@3:28..37 BLOCK type=kotlin.Unit origin=null
@3:30..35 SET_VAR 'var z: kotlin.Int [var] declared in <root>.topLevelMethod' type=kotlin.Unit origin=EQ
@3:34..35 CONST Int type=kotlin.Int value=5
+5
View File
@@ -0,0 +1,5 @@
// In this test K2 behaviour is preferable.
fun topLevelMethod() {
var z = 3
if(true) { z = 4 } else { z = 5 }
}
+16
View File
@@ -0,0 +1,16 @@
@0:0..5:0 FILE fqName:<root> fileName:/kt63779_2.kt
@1:0..4:1 FUN name:topLevelMethod visibility:public modality:FINAL <> () returnType:kotlin.Unit
@1:21..4:1 BLOCK_BODY
@2:8..9 VAR name:z type:kotlin.Int [var]
@2:12..13 CONST Int type=kotlin.Int value=3
@3:4..37 WHEN type=kotlin.Unit origin=IF
@3:4..37 BRANCH
@3:7..11 CONST Boolean type=kotlin.Boolean value=true
@3:13..22 BLOCK type=kotlin.Unit origin=null
@3:15..16 SET_VAR 'var z: kotlin.Int [var] declared in <root>.topLevelMethod' type=kotlin.Unit origin=EQ
@3:19..20 CONST Int type=kotlin.Int value=4
@3:4..37 BRANCH
@3:4..37 CONST Boolean type=kotlin.Boolean value=true
@3:28..37 BLOCK type=kotlin.Unit origin=null
@3:30..31 SET_VAR 'var z: kotlin.Int [var] declared in <root>.topLevelMethod' type=kotlin.Unit origin=EQ
@3:34..35 CONST Int type=kotlin.Int value=5
+1 -1
View File
@@ -8,7 +8,7 @@
@3:4..46 FIELD PROPERTY_BACKING_FIELD name:maybeThis type:<root>.Klass? visibility:private [final]
@3:20..46 EXPRESSION_BODY
@3:20..46 WHEN type=<root>.Klass? origin=IF
@3:24..30 BRANCH
@3:24..36 BRANCH
@3:24..30 CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EQEQ
@3:24..25 CONST Int type=kotlin.Int value=1
@3:29..30 CONST Int type=kotlin.Int value=1
@@ -67,6 +67,18 @@ public class ClassicJvmIrSourceRangesTestGenerated extends AbstractClassicJvmIrS
runTest("compiler/testData/ir/sourceRanges/kt24258.kt");
}
@Test
@TestMetadata("kt63779.kt")
public void testKt63779() throws Exception {
runTest("compiler/testData/ir/sourceRanges/kt63779.kt");
}
@Test
@TestMetadata("kt63779_2.kt")
public void testKt63779_2() throws Exception {
runTest("compiler/testData/ir/sourceRanges/kt63779_2.kt");
}
@Test
@TestMetadata("multiLineCall.kt")
public void testMultiLineCall() throws Exception {