JVM: KT-40665 more exact check for intrinsified range 'contains'

This commit is contained in:
Dmitry Petrov
2020-07-29 11:38:32 +03:00
parent 0559e192ee
commit f2493d0950
10 changed files with 61 additions and 5 deletions
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.codegen.range.inExpression.InExpressionGenerator
import org.jetbrains.kotlin.codegen.range.inExpression.InFloatingPointRangeLiteralExpressionGenerator
import org.jetbrains.kotlin.codegen.range.inExpression.InIntegralContinuousRangeExpressionGenerator
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.isTopLevelInPackage
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtForExpression
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
@@ -208,17 +208,25 @@ fun isPrimitiveRangeContains(descriptor: CallableDescriptor): Boolean {
}
fun isUnsignedIntegerRangeContains(descriptor: CallableDescriptor): Boolean {
if (descriptor.name.asString() != "contains") return false
val dispatchReceiverType = descriptor.dispatchReceiverParameter?.type
val extensionReceiverType = descriptor.extensionReceiverParameter?.type
return (dispatchReceiverType != null && isUnsignedRange(dispatchReceiverType)) ||
(extensionReceiverType != null && isUnsignedRange(extensionReceiverType))
when {
dispatchReceiverType != null && extensionReceiverType == null -> {
if (descriptor.name.asString() != "contains") return false
return isUnsignedRange(dispatchReceiverType)
}
extensionReceiverType != null && dispatchReceiverType == null -> {
if (!descriptor.isTopLevelInPackage("contains", "kotlin.ranges")) return false
return isUnsignedRange(extensionReceiverType)
}
else ->
return false
}
}
fun isPrimitiveNumberRangeExtensionContainsPrimitiveNumber(descriptor: CallableDescriptor): Boolean {
if (descriptor.name.asString() != "contains") return false
if (!descriptor.isTopLevelInPackage("contains", "kotlin.ranges")) return false
val extensionReceiverType = descriptor.extensionReceiverParameter?.type ?: return false
@@ -20611,6 +20611,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/ranges/contains/rangeContainsString.kt");
}
@TestMetadata("userDefinedContainsExtension.kt")
public void testUserDefinedContainsExtension() throws Exception {
runTest("compiler/testData/codegen/box/ranges/contains/userDefinedContainsExtension.kt");
}
@TestMetadata("compiler/testData/codegen/box/ranges/contains/generated")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -0,0 +1,12 @@
// WITH_RUNTIME
// KJS_WITH_FULL_RUNTIME
operator fun ClosedRange<Int>.contains(value: Long) = false
operator fun ClosedRange<UInt>.contains(value: ULong) = false
fun box(): String {
if (10L in 1..10) return "Failed: Long in Int..Int"
if (10UL in 1U..10U) return "Failed: ULong in UInt..UInt"
return "OK"
}
@@ -22207,6 +22207,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/ranges/contains/rangeContainsString.kt");
}
@TestMetadata("userDefinedContainsExtension.kt")
public void testUserDefinedContainsExtension() throws Exception {
runTest("compiler/testData/codegen/box/ranges/contains/userDefinedContainsExtension.kt");
}
@TestMetadata("compiler/testData/codegen/box/ranges/contains/generated")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -22207,6 +22207,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/ranges/contains/rangeContainsString.kt");
}
@TestMetadata("userDefinedContainsExtension.kt")
public void testUserDefinedContainsExtension() throws Exception {
runTest("compiler/testData/codegen/box/ranges/contains/userDefinedContainsExtension.kt");
}
@TestMetadata("compiler/testData/codegen/box/ranges/contains/generated")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -20611,6 +20611,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/ranges/contains/rangeContainsString.kt");
}
@TestMetadata("userDefinedContainsExtension.kt")
public void testUserDefinedContainsExtension() throws Exception {
runTest("compiler/testData/codegen/box/ranges/contains/userDefinedContainsExtension.kt");
}
@TestMetadata("compiler/testData/codegen/box/ranges/contains/generated")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -17137,6 +17137,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/ranges/contains/rangeContainsString.kt");
}
@TestMetadata("userDefinedContainsExtension.kt")
public void testUserDefinedContainsExtension() throws Exception {
runTest("compiler/testData/codegen/box/ranges/contains/userDefinedContainsExtension.kt");
}
@TestMetadata("compiler/testData/codegen/box/ranges/contains/generated")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -17137,6 +17137,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/ranges/contains/rangeContainsString.kt");
}
@TestMetadata("userDefinedContainsExtension.kt")
public void testUserDefinedContainsExtension() throws Exception {
runTest("compiler/testData/codegen/box/ranges/contains/userDefinedContainsExtension.kt");
}
@TestMetadata("compiler/testData/codegen/box/ranges/contains/generated")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -17242,6 +17242,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/ranges/contains/rangeContainsString.kt");
}
@TestMetadata("userDefinedContainsExtension.kt")
public void testUserDefinedContainsExtension() throws Exception {
runTest("compiler/testData/codegen/box/ranges/contains/userDefinedContainsExtension.kt");
}
@TestMetadata("compiler/testData/codegen/box/ranges/contains/generated")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)