diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java index aea0ef7918d..b6f5a8c9db3 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java @@ -2988,6 +2988,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/callableReference/referenceAdaptationCompatibility.kt"); } + @Test + @TestMetadata("referenceInCycleInProperties.kt") + public void testReferenceInCycleInProperties() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/referenceInCycleInProperties.kt"); + } + @Test @TestMetadata("referenceToCompanionObjectMemberViaClassName.kt") public void testReferenceToCompanionObjectMemberViaClassName() throws Exception { @@ -22260,6 +22266,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/numbers/intValuesOutOfRange.kt"); } + @Test + @TestMetadata("integerLiteralWillChangeResolveInFunctionReturnPosition.kt") + public void testIntegerLiteralWillChangeResolveInFunctionReturnPosition() throws Exception { + runTest("compiler/testData/diagnostics/tests/numbers/integerLiteralWillChangeResolveInFunctionReturnPosition.kt"); + } + @Test @TestMetadata("kt41679.kt") public void testKt41679() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index 743dada2515..8f08bbf8c4a 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -2994,6 +2994,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/callableReference/referenceAdaptationCompatibility.kt"); } + @Test + @TestMetadata("referenceInCycleInProperties.kt") + public void testReferenceInCycleInProperties() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/referenceInCycleInProperties.kt"); + } + @Test @TestMetadata("referenceToCompanionObjectMemberViaClassName.kt") public void testReferenceToCompanionObjectMemberViaClassName() throws Exception { @@ -22266,6 +22272,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/numbers/intValuesOutOfRange.kt"); } + @Test + @TestMetadata("integerLiteralWillChangeResolveInFunctionReturnPosition.kt") + public void testIntegerLiteralWillChangeResolveInFunctionReturnPosition() throws Exception { + runTest("compiler/testData/diagnostics/tests/numbers/integerLiteralWillChangeResolveInFunctionReturnPosition.kt"); + } + @Test @TestMetadata("kt41679.kt") public void testKt41679() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java index 408e3a671a7..5e2e05e0075 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java @@ -2988,6 +2988,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/callableReference/referenceAdaptationCompatibility.kt"); } + @Test + @TestMetadata("referenceInCycleInProperties.kt") + public void testReferenceInCycleInProperties() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/referenceInCycleInProperties.kt"); + } + @Test @TestMetadata("referenceToCompanionObjectMemberViaClassName.kt") public void testReferenceToCompanionObjectMemberViaClassName() throws Exception { @@ -22260,6 +22266,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/numbers/intValuesOutOfRange.kt"); } + @Test + @TestMetadata("integerLiteralWillChangeResolveInFunctionReturnPosition.kt") + public void testIntegerLiteralWillChangeResolveInFunctionReturnPosition() throws Exception { + runTest("compiler/testData/diagnostics/tests/numbers/integerLiteralWillChangeResolveInFunctionReturnPosition.kt"); + } + @Test @TestMetadata("kt41679.kt") public void testKt41679() throws Exception { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/VariableTypeAndInitializerResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/VariableTypeAndInitializerResolver.kt index 8495a3ac64f..41b4699a00b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/VariableTypeAndInitializerResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/VariableTypeAndInitializerResolver.kt @@ -14,7 +14,6 @@ import org.jetbrains.kotlin.psi.KtExpression import org.jetbrains.kotlin.psi.KtProperty import org.jetbrains.kotlin.psi.KtVariableDeclaration import org.jetbrains.kotlin.resolve.DescriptorResolver.transformAnonymousTypeIfNeeded -import org.jetbrains.kotlin.resolve.calls.checkers.NewSchemeOfIntegerOperatorResolutionChecker import org.jetbrains.kotlin.resolve.calls.components.InferenceSession import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator @@ -128,12 +127,6 @@ class VariableTypeAndInitializerResolver( val initializer = variable.initializer val initializerType = expressionTypingServices.safeGetType(scope, initializer!!, variableType, dataFlowInfo, inferenceSession, trace) - NewSchemeOfIntegerOperatorResolutionChecker.checkArgument( - variableType, - initializer, - trace, - constantExpressionEvaluator.module - ) val constant = constantExpressionEvaluator.evaluateExpression(initializer, trace, initializerType) ?: return@computeInitializer null diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/NewSchemeOfIntegerOperatorResolutionChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/NewSchemeOfIntegerOperatorResolutionChecker.kt index 8b5833f8e36..94f2d007964 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/NewSchemeOfIntegerOperatorResolutionChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/NewSchemeOfIntegerOperatorResolutionChecker.kt @@ -36,7 +36,7 @@ object NewSchemeOfIntegerOperatorResolutionChecker : CallChecker { } else { valueParameter.type }.unwrap().lowerIfFlexible() - if (!expectedType.isPrimitiveNumberOrNullableType()) { + if (!needToCheck(expectedType)) { continue } for (argument in arguments.arguments) { @@ -53,12 +53,16 @@ object NewSchemeOfIntegerOperatorResolutionChecker : CallChecker { trace: BindingTrace, moduleDescriptor: ModuleDescriptor ) { - val type = expectedType.lowerIfFlexible() - if (type.isPrimitiveNumberOrNullableType()) { - checkArgumentImpl(type, KtPsiUtil.deparenthesize(argument)!!, trace, moduleDescriptor) + if (needToCheck(expectedType)) { + checkArgumentImpl(expectedType.lowerIfFlexible(), KtPsiUtil.deparenthesize(argument)!!, trace, moduleDescriptor) } } + fun needToCheck(expectedType: KotlinType): Boolean { + if (TypeUtils.noExpectedType(expectedType)) return false + return expectedType.lowerIfFlexible().isPrimitiveNumberOrNullableType() + } + private fun checkArgumentImpl( expectedType: SimpleType, argumentExpression: KtExpression, diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt index 8f78b735689..aecf9ffb689 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt @@ -19,6 +19,7 @@ import org.jetbrains.kotlin.resolve.* import org.jetbrains.kotlin.resolve.calls.ArgumentTypeResolver import org.jetbrains.kotlin.resolve.calls.NewCommonSuperTypeCalculator import org.jetbrains.kotlin.resolve.calls.checkers.CallCheckerContext +import org.jetbrains.kotlin.resolve.calls.checkers.NewSchemeOfIntegerOperatorResolutionChecker import org.jetbrains.kotlin.resolve.calls.commonSuperType import org.jetbrains.kotlin.resolve.calls.components.* import org.jetbrains.kotlin.resolve.calls.components.candidate.CallableReferenceResolutionCandidate @@ -365,13 +366,17 @@ class ResolvedAtomCompleter( .replaceBindingTrace(topLevelTrace) val argumentExpression = resultValueArgument.valueArgument.getArgumentExpression() ?: continue - kotlinToResolvedCallTransformer.updateRecordedType( + val updatedType = kotlinToResolvedCallTransformer.updateRecordedType( argumentExpression, parameter = null, context = newContext, reportErrorForTypeMismatch = true, convertedArgumentType = null ) + + if (updatedType != null) { + NewSchemeOfIntegerOperatorResolutionChecker.checkArgument(updatedType, argumentExpression, topLevelTrace, moduleDescriptor) + } } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DataFlowAnalyzer.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DataFlowAnalyzer.java index fa2f1c8cdce..ddc0fa88612 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DataFlowAnalyzer.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DataFlowAnalyzer.java @@ -34,6 +34,7 @@ import org.jetbrains.kotlin.resolve.BindingContext; import org.jetbrains.kotlin.resolve.BindingTrace; import org.jetbrains.kotlin.resolve.DescriptorUtils; import org.jetbrains.kotlin.resolve.calls.checkers.AdditionalTypeChecker; +import org.jetbrains.kotlin.resolve.calls.checkers.NewSchemeOfIntegerOperatorResolutionChecker; import org.jetbrains.kotlin.resolve.calls.context.ResolutionContext; import org.jetbrains.kotlin.resolve.calls.inference.BuilderInferenceSession; import org.jetbrains.kotlin.resolve.calls.smartcasts.*; @@ -447,6 +448,8 @@ public class DataFlowAnalyzer { expressionType = ((TypedCompileTimeConstant) value).getType(); } + NewSchemeOfIntegerOperatorResolutionChecker.checkArgument(context.expectedType, expression, context.trace, module); + return createCheckedTypeInfo(expressionType, context, expression); } } diff --git a/compiler/testData/diagnostics/tests/callableReference/referenceInCycleInProperties.kt b/compiler/testData/diagnostics/tests/callableReference/referenceInCycleInProperties.kt new file mode 100644 index 00000000000..c1c9333fc42 --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/referenceInCycleInProperties.kt @@ -0,0 +1,16 @@ +// FIR_IDENTICAL +// WITH_STDLIB +abstract class Parser { + open fun parseString(x: String): List = null!! + + fun parse(name: String): Int = null!! + fun parse(name: String, content: String): Int = null!! +} + +class Some(strings: List) { + val parser = object : Parser() { + override fun parseString(x: String) = listOfInt + } + private val listOfString = strings + private val listOfInt: List = listOfString.map(parser::parse) +} diff --git a/compiler/testData/diagnostics/tests/callableReference/referenceInCycleInProperties.txt b/compiler/testData/diagnostics/tests/callableReference/referenceInCycleInProperties.txt new file mode 100644 index 00000000000..4259e1a4a73 --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/referenceInCycleInProperties.txt @@ -0,0 +1,22 @@ +package + +public abstract class Parser { + public constructor Parser() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public final fun parse(/*0*/ name: kotlin.String): kotlin.Int + public final fun parse(/*0*/ name: kotlin.String, /*1*/ content: kotlin.String): kotlin.Int + public open fun parseString(/*0*/ x: kotlin.String): kotlin.collections.List + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public final class Some { + public constructor Some(/*0*/ strings: kotlin.collections.List) + private final val listOfInt: kotlin.collections.List + private final val listOfString: kotlin.collections.List + public final val parser: Parser + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + diff --git a/compiler/testData/diagnostics/tests/evaluate/divisionByZero.kt b/compiler/testData/diagnostics/tests/evaluate/divisionByZero.kt index ab83e4bba4b..cbf3914065e 100644 --- a/compiler/testData/diagnostics/tests/evaluate/divisionByZero.kt +++ b/compiler/testData/diagnostics/tests/evaluate/divisionByZero.kt @@ -15,7 +15,7 @@ val a10 = 1 / 0.0f val a11 = 1 / 0.0 val a12 = 1L / 0 -val b1: Byte = 1 / 0 +val b1: Byte = 1 / 0 @Ann(1 / 0) val b2 = 1 annotation class Ann(val i : Int) diff --git a/compiler/testData/diagnostics/tests/numbers/integerLiteralWillChangeResolveInFunctionReturnPosition.fir.kt b/compiler/testData/diagnostics/tests/numbers/integerLiteralWillChangeResolveInFunctionReturnPosition.fir.kt new file mode 100644 index 00000000000..211f7318aec --- /dev/null +++ b/compiler/testData/diagnostics/tests/numbers/integerLiteralWillChangeResolveInFunctionReturnPosition.fir.kt @@ -0,0 +1,19 @@ +// ISSUE: KT-55358 + +fun test_0() = 1 + 2 + +fun test_1(): Byte = 1 + 2 + +fun test_2(b: Boolean): Byte { + if (b) return 1 + 2 + return 3 + 4 +} + +fun test_4(): Byte = run { 1 + 2 } + +fun test_5() = run { 1 + 2 } + +fun test_6() = runWithByte { 1 + 2 } + +fun run(block: () -> R): R = block() +fun runWithByte(block: () -> Byte): Byte = block() diff --git a/compiler/testData/diagnostics/tests/numbers/integerLiteralWillChangeResolveInFunctionReturnPosition.kt b/compiler/testData/diagnostics/tests/numbers/integerLiteralWillChangeResolveInFunctionReturnPosition.kt new file mode 100644 index 00000000000..37b268477c8 --- /dev/null +++ b/compiler/testData/diagnostics/tests/numbers/integerLiteralWillChangeResolveInFunctionReturnPosition.kt @@ -0,0 +1,19 @@ +// ISSUE: KT-55358 + +fun test_0() = 1 + 2 + +fun test_1(): Byte = 1 + 2 + +fun test_2(b: Boolean): Byte { + if (b) return 1 + 2 + return 3 + 4 +} + +fun test_4(): Byte = run { 1 + 2 } + +fun test_5() = run { 1 + 2 } + +fun test_6() = runWithByte { 1 + 2 } + +fun run(block: () -> R): R = block() +fun runWithByte(block: () -> Byte): Byte = block() diff --git a/compiler/testData/diagnostics/tests/numbers/integerLiteralWillChangeResolveInFunctionReturnPosition.txt b/compiler/testData/diagnostics/tests/numbers/integerLiteralWillChangeResolveInFunctionReturnPosition.txt new file mode 100644 index 00000000000..8cad5f12466 --- /dev/null +++ b/compiler/testData/diagnostics/tests/numbers/integerLiteralWillChangeResolveInFunctionReturnPosition.txt @@ -0,0 +1,11 @@ +package + +public fun run(/*0*/ block: () -> R): R +public fun runWithByte(/*0*/ block: () -> kotlin.Byte): kotlin.Byte +public fun test_0(): kotlin.Int +public fun test_1(): kotlin.Byte +public fun test_2(/*0*/ b: kotlin.Boolean): kotlin.Byte +public fun test_4(): kotlin.Byte +public fun test_5(): kotlin.Byte +public fun test_6(): kotlin.Byte + diff --git a/compiler/testData/diagnostics/tests/numbers/kt45970.kt b/compiler/testData/diagnostics/tests/numbers/kt45970.kt index e3e1aa7418e..b95e2edcbab 100644 --- a/compiler/testData/diagnostics/tests/numbers/kt45970.kt +++ b/compiler/testData/diagnostics/tests/numbers/kt45970.kt @@ -10,9 +10,9 @@ val d_1: Long = 1 var d_2: Long = 1 val e_1: Byte = 1 + 2 -var e_2: Byte = 1 + 2 +var e_2: Byte = 1 + 2 val f_1: Short = 1 + 2 -var f_2: Short = 1 + 2 +var f_2: Short = 1 + 2 val g_1: Int = 1 + 2 var g_2: Int = 1 + 2 val h_1: Long = 1 + 2 @@ -28,10 +28,10 @@ fun local() { val d_1: Long = 1 var d_2: Long = 1 - val e_1: Byte = 1 + 2 - var e_2: Byte = 1 + 2 - val f_1: Short = 1 + 2 - var f_2: Short = 1 + 2 + val e_1: Byte = 1 + 2 + var e_2: Byte = 1 + 2 + val f_1: Short = 1 + 2 + var f_2: Short = 1 + 2 val g_1: Int = 1 + 2 var g_2: Int = 1 + 2 val h_1: Long = 1 + 2 @@ -49,9 +49,9 @@ class Member { var d_2: Long = 1 val e_1: Byte = 1 + 2 - var e_2: Byte = 1 + 2 + var e_2: Byte = 1 + 2 val f_1: Short = 1 + 2 - var f_2: Short = 1 + 2 + var f_2: Short = 1 + 2 val g_1: Int = 1 + 2 var g_2: Int = 1 + 2 val h_1: Long = 1 + 2 diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index a272530a1c6..1803be91b5c 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -2994,6 +2994,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/callableReference/referenceAdaptationCompatibility.kt"); } + @Test + @TestMetadata("referenceInCycleInProperties.kt") + public void testReferenceInCycleInProperties() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/referenceInCycleInProperties.kt"); + } + @Test @TestMetadata("referenceToCompanionObjectMemberViaClassName.kt") public void testReferenceToCompanionObjectMemberViaClassName() throws Exception { @@ -22266,6 +22272,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/numbers/intValuesOutOfRange.kt"); } + @Test + @TestMetadata("integerLiteralWillChangeResolveInFunctionReturnPosition.kt") + public void testIntegerLiteralWillChangeResolveInFunctionReturnPosition() throws Exception { + runTest("compiler/testData/diagnostics/tests/numbers/integerLiteralWillChangeResolveInFunctionReturnPosition.kt"); + } + @Test @TestMetadata("kt41679.kt") public void testKt41679() throws Exception {