KT-42018 explicitly cast inline class values in safe-as

This commit is contained in:
Dmitry Petrov
2020-11-18 16:57:13 +03:00
parent edd3b457d4
commit 3d2f5f4bc1
10 changed files with 53 additions and 1 deletions
@@ -14182,6 +14182,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/inlineClasses/UIntArraySortExample.kt");
}
@TestMetadata("UIntSafeAsInt.kt")
public void testUIntSafeAsInt() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/UIntSafeAsInt.kt");
}
@TestMetadata("unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt")
public void testUnboxNullableValueOfInlineClassWithNonNullUnderlyingType() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt");
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.isInlined
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
@@ -125,10 +126,15 @@ private class TypeOperatorLowering(private val context: JvmBackendContext) : Fil
IrStatementOrigin.SAFE_CALL,
irType = context.irBuiltIns.anyNType
) { valueSymbol ->
val thenPart =
if (valueSymbol.owner.type.isInlined())
lowerCast(irGet(valueSymbol.owner), expression.typeOperand)
else
irGet(valueSymbol.owner)
irIfThenElse(
expression.type,
lowerInstanceOf(irGet(valueSymbol.owner), expression.typeOperand.makeNotNull()),
irGet(valueSymbol.owner),
thenPart,
irNull(expression.type)
)
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
// KJS_WITH_FULL_RUNTIME
fun testUIntSafeAsInt(x: UInt) = x as? Int
fun box(): String = if (testUIntSafeAsInt(1U) != null) "fail" else "OK"
@@ -15582,6 +15582,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/inlineClasses/UIntArraySortExample.kt");
}
@TestMetadata("UIntSafeAsInt.kt")
public void testUIntSafeAsInt() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/UIntSafeAsInt.kt");
}
@TestMetadata("unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt")
public void testUnboxNullableValueOfInlineClassWithNonNullUnderlyingType() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt");
@@ -15582,6 +15582,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/inlineClasses/UIntArraySortExample.kt");
}
@TestMetadata("UIntSafeAsInt.kt")
public void testUIntSafeAsInt() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/UIntSafeAsInt.kt");
}
@TestMetadata("unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt")
public void testUnboxNullableValueOfInlineClassWithNonNullUnderlyingType() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt");
@@ -14182,6 +14182,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/inlineClasses/UIntArraySortExample.kt");
}
@TestMetadata("UIntSafeAsInt.kt")
public void testUIntSafeAsInt() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/UIntSafeAsInt.kt");
}
@TestMetadata("unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt")
public void testUnboxNullableValueOfInlineClassWithNonNullUnderlyingType() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt");
@@ -12182,6 +12182,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/inlineClasses/UIntArraySortExample.kt");
}
@TestMetadata("UIntSafeAsInt.kt")
public void testUIntSafeAsInt() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/UIntSafeAsInt.kt");
}
@TestMetadata("unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt")
public void testUnboxNullableValueOfInlineClassWithNonNullUnderlyingType() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt");
@@ -12182,6 +12182,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/inlineClasses/UIntArraySortExample.kt");
}
@TestMetadata("UIntSafeAsInt.kt")
public void testUIntSafeAsInt() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/UIntSafeAsInt.kt");
}
@TestMetadata("unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt")
public void testUnboxNullableValueOfInlineClassWithNonNullUnderlyingType() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt");
@@ -12247,6 +12247,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/inlineClasses/UIntArraySortExample.kt");
}
@TestMetadata("UIntSafeAsInt.kt")
public void testUIntSafeAsInt() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/UIntSafeAsInt.kt");
}
@TestMetadata("unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt")
public void testUnboxNullableValueOfInlineClassWithNonNullUnderlyingType() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt");
@@ -6702,6 +6702,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/inlineClasses/typeChecksForInlineClasses.kt");
}
@TestMetadata("UIntSafeAsInt.kt")
public void testUIntSafeAsInt() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/UIntSafeAsInt.kt");
}
@TestMetadata("unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt")
public void testUnboxNullableValueOfInlineClassWithNonNullUnderlyingType() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxNullableValueOfInlineClassWithNonNullUnderlyingType.kt");