Skip to content

Commit e7ea076

Browse files
committed
Do not apply constant folding for non built-in functions
#KT-15872 Fixed
1 parent 935f7b1 commit e7ea076

File tree

9 files changed

+68
-0
lines changed

9 files changed

+68
-0
lines changed

compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,8 @@ private class ConstantExpressionEvaluatorVisitor(
449449
private fun evaluateCall(callExpression: KtExpression, receiverExpression: KtExpression, expectedType: KotlinType?): CompileTimeConstant<*>? {
450450
val resolvedCall = callExpression.getResolvedCall(trace.bindingContext) ?: return null
451451

452+
if (!KotlinBuiltIns.isBuiltIn(resolvedCall.resultingDescriptor)) return null
453+
452454
val resultingDescriptorName = resolvedCall.resultingDescriptor.name
453455

454456
val argumentForReceiver = createOperationArgumentForReceiver(resolvedCall, receiverExpression) ?: return null
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
infix fun Int.rem(other: Int) = 10
2+
infix operator fun Int.minus(other: Int): Int = 20
3+
4+
fun box(): String {
5+
val a = 5 rem 2
6+
if (a != 10) return "fail 1"
7+
8+
val b = 5 minus 3
9+
if (b != 20) return "fail 2"
10+
11+
val a1 = 5.rem(2)
12+
if (a1 != 1) return "fail 3"
13+
14+
val b2 = 5.minus(3)
15+
if (b2 != 2) return "fail 4"
16+
17+
return "OK"
18+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@kotlin.Metadata
2+
public final class InfixFunctionOverBuiltinMemberKt {
3+
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
4+
public final static method minus(p0: int, p1: int): int
5+
public final static method rem(p0: int, p1: int): int
6+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// !DIAGNOSTICS: -UNUSED_VARIABLE, -UNUSED_PARAMETER
2+
3+
infix fun Int.rem(other: Int) = 10
4+
infix operator fun Int.minus(other: Int): Int = 20
5+
6+
const val a1 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>(-5) rem 2<!>
7+
const val a2 = (-5).rem(2)
8+
9+
const val b1 = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>5 minus 3<!>
10+
const val b2 = 5.minus(3)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package
2+
3+
public const val a1: kotlin.Int
4+
public const val a2: kotlin.Int = -1
5+
public const val b1: kotlin.Int
6+
public const val b2: kotlin.Int = 2
7+
public operator infix fun kotlin.Int.minus(/*0*/ other: kotlin.Int): kotlin.Int
8+
public infix fun kotlin.Int.rem(/*0*/ other: kotlin.Int): kotlin.Int

compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11141,6 +11141,12 @@ public void testIncDecOnObject() throws Exception {
1114111141
doTest(fileName);
1114211142
}
1114311143

11144+
@TestMetadata("infixFunctionOverBuiltinMember.kt")
11145+
public void testInfixFunctionOverBuiltinMember() throws Exception {
11146+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/operatorConventions/infixFunctionOverBuiltinMember.kt");
11147+
doTest(fileName);
11148+
}
11149+
1114411150
@TestMetadata("kt14201.kt")
1114511151
public void testKt14201() throws Exception {
1114611152
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/operatorConventions/kt14201.kt");

compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7378,6 +7378,12 @@ public void testFloatLiteralOutOfRange() throws Exception {
73787378
doTest(fileName);
73797379
}
73807380

7381+
@TestMetadata("infixFunOverBuiltinMemberInConst.kt")
7382+
public void testInfixFunOverBuiltinMemberInConst() throws Exception {
7383+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/evaluate/infixFunOverBuiltinMemberInConst.kt");
7384+
doTest(fileName);
7385+
}
7386+
73817387
@TestMetadata("intOverflow.kt")
73827388
public void testIntOverflow() throws Exception {
73837389
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/evaluate/intOverflow.kt");

compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11141,6 +11141,12 @@ public void testIncDecOnObject() throws Exception {
1114111141
doTest(fileName);
1114211142
}
1114311143

11144+
@TestMetadata("infixFunctionOverBuiltinMember.kt")
11145+
public void testInfixFunctionOverBuiltinMember() throws Exception {
11146+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/operatorConventions/infixFunctionOverBuiltinMember.kt");
11147+
doTest(fileName);
11148+
}
11149+
1114411150
@TestMetadata("kt14201.kt")
1114511151
public void testKt14201() throws Exception {
1114611152
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/operatorConventions/kt14201.kt");

js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12888,6 +12888,12 @@ public void testIncDecOnObject() throws Exception {
1288812888
doTest(fileName);
1288912889
}
1289012890

12891+
@TestMetadata("infixFunctionOverBuiltinMember.kt")
12892+
public void testInfixFunctionOverBuiltinMember() throws Exception {
12893+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/operatorConventions/infixFunctionOverBuiltinMember.kt");
12894+
doTest(fileName);
12895+
}
12896+
1289112897
@TestMetadata("kt14201.kt")
1289212898
public void testKt14201() throws Exception {
1289312899
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/operatorConventions/kt14201.kt");

0 commit comments

Comments
 (0)