From afb7625d0c28989f0d93bbe11b7e2aa2a29e707c Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Thu, 15 Jul 2021 13:07:43 +0300 Subject: [PATCH] [FE 1.0] Fix false positive INTEGER_OPERATOR_RESOLVE_WILL_CHANGE ^KT-47729 In progress --- ...irOldFrontendDiagnosticsTestGenerated.java | 6 +++++ ...DiagnosticsWithLightTreeTestGenerated.java | 6 +++++ ...chemeOfIntegerOperatorResolutionChecker.kt | 7 +++++- .../parameters/expressions/maxValueInt.kt | 4 ++-- .../diagnostics/tests/numbers/kt47729.fir.kt | 22 +++++++++++++++++++ .../diagnostics/tests/numbers/kt47729.kt | 22 +++++++++++++++++++ .../diagnostics/tests/numbers/kt47729.txt | 11 ++++++++++ ...nedToUnsignedConversionWithExpectedType.kt | 2 +- .../jsCode/argumentIsLiteral.kt | 2 +- .../test/runners/DiagnosticTestGenerated.java | 6 +++++ ...CompilerTestFE10TestdataTestGenerated.java | 6 +++++ 11 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/numbers/kt47729.fir.kt create mode 100644 compiler/testData/diagnostics/tests/numbers/kt47729.kt create mode 100644 compiler/testData/diagnostics/tests/numbers/kt47729.txt 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 b564e8deb34..6a3d0fd71fc 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 @@ -19877,6 +19877,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/numbers/kt47447.kt"); } + @Test + @TestMetadata("kt47729.kt") + public void testKt47729() throws Exception { + runTest("compiler/testData/diagnostics/tests/numbers/kt47729.kt"); + } + @Test @TestMetadata("kt47729_parenthesis.kt") public void testKt47729_parenthesis() 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 66000e29c9c..e83a65d0414 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 @@ -19877,6 +19877,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/numbers/kt47447.kt"); } + @Test + @TestMetadata("kt47729.kt") + public void testKt47729() throws Exception { + runTest("compiler/testData/diagnostics/tests/numbers/kt47729.kt"); + } + @Test @TestMetadata("kt47729_parenthesis.kt") public void testKt47729_parenthesis() throws Exception { 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 1867ce4b3fd..0e7a5808028 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 @@ -23,6 +23,8 @@ import org.jetbrains.kotlin.resolve.constants.IntegerLiteralTypeConstructor import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstant import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe import org.jetbrains.kotlin.types.lowerIfFlexible +import org.jetbrains.kotlin.types.typeUtil.isPrimitiveNumberOrNullableType +import org.jetbrains.kotlin.types.typeUtil.makeNotNullable object NewSchemeOfIntegerOperatorResolutionChecker : CallChecker { override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) { @@ -34,6 +36,9 @@ object NewSchemeOfIntegerOperatorResolutionChecker : CallChecker { } else { valueParameter.type }.unwrap().lowerIfFlexible() + if (!expectedType.isPrimitiveNumberOrNullableType()) { + continue + } for (argument in arguments.arguments) { val expression = KtPsiUtil.deparenthesize(argument.getArgumentExpression()) ?: continue val compileTimeValue = @@ -50,7 +55,7 @@ object NewSchemeOfIntegerOperatorResolutionChecker : CallChecker { val valueTypeConstructor = compileTimeValue.unknownIntegerType.constructor as? IntegerLiteralTypeConstructor ?: continue val approximatedType = valueTypeConstructor.getApproximatedType() - if (approximatedType != expectedType) { + if (approximatedType.constructor != expectedType.constructor) { context.trace.report(Errors.INTEGER_OPERATOR_RESOLVE_WILL_CHANGE.on(expression, approximatedType)) } } diff --git a/compiler/testData/diagnostics/tests/annotations/parameters/expressions/maxValueInt.kt b/compiler/testData/diagnostics/tests/annotations/parameters/expressions/maxValueInt.kt index 8260cf979a8..de017572ec4 100644 --- a/compiler/testData/diagnostics/tests/annotations/parameters/expressions/maxValueInt.kt +++ b/compiler/testData/diagnostics/tests/annotations/parameters/expressions/maxValueInt.kt @@ -11,8 +11,8 @@ annotation class Ann( @Ann( p1 = java.lang.Integer.MAX_VALUE + 1, p2 = 1 + 1, - p3 = java.lang.Integer.MAX_VALUE + 1, - p4 = 1.toInt() + 1.toInt(), + p3 = java.lang.Integer.MAX_VALUE + 1, + p4 = 1.toInt() + 1.toInt(), p5 = 1.toInt() + 1.toInt() ) class MyClass diff --git a/compiler/testData/diagnostics/tests/numbers/kt47729.fir.kt b/compiler/testData/diagnostics/tests/numbers/kt47729.fir.kt new file mode 100644 index 00000000000..260e2439cff --- /dev/null +++ b/compiler/testData/diagnostics/tests/numbers/kt47729.fir.kt @@ -0,0 +1,22 @@ +// LANGUAGE: -ApproximateIntegerLiteralTypesInReceiverPosition +// ISSUE: Kt-47447, KT-47729 + +fun takeLong(value : Long) {} +fun takeInt(value : Int) {} +fun takeAny(value : Any) {} +fun takeLongX(value : Long?) {} +fun takeIntX(value : Int?) {} +fun takeAnyX(value : Any?) {} +fun takeGeneric(value : A) {} +fun takeGenericX(value : A?) {} + +fun test_1() { + takeLong(1 + 1) // warning + takeInt(1 + 1) // ok + takeAny(1 + 1) // ok + takeLongX(1 + 1) // warning + takeIntX(1 + 1) // ok + takeAnyX(1 + 1) // ok + takeGeneric(1 + 1) // ok + takeGenericX(1 + 1) // ok +} diff --git a/compiler/testData/diagnostics/tests/numbers/kt47729.kt b/compiler/testData/diagnostics/tests/numbers/kt47729.kt new file mode 100644 index 00000000000..1c1af8f4819 --- /dev/null +++ b/compiler/testData/diagnostics/tests/numbers/kt47729.kt @@ -0,0 +1,22 @@ +// LANGUAGE: -ApproximateIntegerLiteralTypesInReceiverPosition +// ISSUE: Kt-47447, KT-47729 + +fun takeLong(value : Long) {} +fun takeInt(value : Int) {} +fun takeAny(value : Any) {} +fun takeLongX(value : Long?) {} +fun takeIntX(value : Int?) {} +fun takeAnyX(value : Any?) {} +fun takeGeneric(value : A) {} +fun takeGenericX(value : A?) {} + +fun test_1() { + takeLong(1 + 1) // warning + takeInt(1 + 1) // ok + takeAny(1 + 1) // ok + takeLongX(1 + 1) // warning + takeIntX(1 + 1) // ok + takeAnyX(1 + 1) // ok + takeGeneric(1 + 1) // ok + takeGenericX(1 + 1) // ok +} diff --git a/compiler/testData/diagnostics/tests/numbers/kt47729.txt b/compiler/testData/diagnostics/tests/numbers/kt47729.txt new file mode 100644 index 00000000000..16895c50b1e --- /dev/null +++ b/compiler/testData/diagnostics/tests/numbers/kt47729.txt @@ -0,0 +1,11 @@ +package + +public fun takeAny(/*0*/ value: kotlin.Any): kotlin.Unit +public fun takeAnyX(/*0*/ value: kotlin.Any?): kotlin.Unit +public fun takeGeneric(/*0*/ value: A): kotlin.Unit +public fun takeGenericX(/*0*/ value: A?): kotlin.Unit +public fun takeInt(/*0*/ value: kotlin.Int): kotlin.Unit +public fun takeIntX(/*0*/ value: kotlin.Int?): kotlin.Unit +public fun takeLong(/*0*/ value: kotlin.Long): kotlin.Unit +public fun takeLongX(/*0*/ value: kotlin.Long?): kotlin.Unit +public fun test_1(): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/unsignedTypes/conversions/signedToUnsignedConversionWithExpectedType.kt b/compiler/testData/diagnostics/tests/unsignedTypes/conversions/signedToUnsignedConversionWithExpectedType.kt index b3514dc6dfa..bf30f8146d0 100644 --- a/compiler/testData/diagnostics/tests/unsignedTypes/conversions/signedToUnsignedConversionWithExpectedType.kt +++ b/compiler/testData/diagnostics/tests/unsignedTypes/conversions/signedToUnsignedConversionWithExpectedType.kt @@ -33,7 +33,7 @@ fun test() { takeUInt(Int.MAX_VALUE * 2) takeUInt(4294967294) - takeUBytes(1, 2, 255, 256, 0, -1, 40 + 2) + takeUBytes(1, 2, 255, 256, 0, -1, 40 + 2) takeUInt(1.myPlus(2)) diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/jsCode/argumentIsLiteral.kt b/compiler/testData/diagnostics/testsWithJsStdLib/jsCode/argumentIsLiteral.kt index eadb4f2ac9b..827b60773ac 100644 --- a/compiler/testData/diagnostics/testsWithJsStdLib/jsCode/argumentIsLiteral.kt +++ b/compiler/testData/diagnostics/testsWithJsStdLib/jsCode/argumentIsLiteral.kt @@ -9,7 +9,7 @@ fun test() { js((b)) js(("c")) js(3) - js(3 + 2) + js(3 + 2) js(1.0f) js(true) js("$a") 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 369b91952f5..3e626689ae6 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 @@ -19883,6 +19883,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/numbers/kt47447.kt"); } + @Test + @TestMetadata("kt47729.kt") + public void testKt47729() throws Exception { + runTest("compiler/testData/diagnostics/tests/numbers/kt47729.kt"); + } + @Test @TestMetadata("kt47729_parenthesis.kt") public void testKt47729_parenthesis() throws Exception { diff --git a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java index 0e704459e3c..b2157653312 100644 --- a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java +++ b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java @@ -19877,6 +19877,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/numbers/kt47447.kt"); } + @Test + @TestMetadata("kt47729.kt") + public void testKt47729() throws Exception { + runTest("compiler/testData/diagnostics/tests/numbers/kt47729.kt"); + } + @Test @TestMetadata("kt47729_parenthesis.kt") public void testKt47729_parenthesis() throws Exception {