From 269dfc61c8f8384b40b7a0d80d5d800d3e200d82 Mon Sep 17 00:00:00 2001 From: Brian Norman Date: Thu, 15 Jun 2023 08:25:22 -0500 Subject: [PATCH] [FIR] Display actual type for argument type mismatch error Previously the argument type was being used for the actual type error display. However, safe-call arguments are unwrapped which causes nullable types to be displayed as non-null. Change to use the actual type provided by the diagnostic instead of extracting the type from the argument. #KT-58844 Fixed --- ...gnosticCompilerTestFE10TestdataTestGenerated.java | 6 ++++++ ...dDiagnosticCompilerFE10TestDataTestGenerated.java | 6 ++++++ ...LightTreeOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ .../FirPsiOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ .../diagnostics/coneDiagnosticToFirDiagnostic.kt | 2 +- .../cli/jvm/instanceAccessBeforeSuperCall.out | 2 +- .../annotations/annotationRenderingInTypes.fir.kt | 2 +- .../diagnostics/tests/nullableTypes/kt58844.fir.kt | 12 ++++++++++++ .../diagnostics/tests/nullableTypes/kt58844.kt | 12 ++++++++++++ .../kotlin/test/runners/DiagnosticTestGenerated.java | 6 ++++++ 10 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/nullableTypes/kt58844.fir.kt create mode 100644 compiler/testData/diagnostics/tests/nullableTypes/kt58844.kt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java index 33b4338e7c6..d76770d1b2b 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java @@ -22744,6 +22744,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia runTest("compiler/testData/diagnostics/tests/nullableTypes/inferenceFlexibleTToNullable.kt"); } + @Test + @TestMetadata("kt58844.kt") + public void testKt58844() throws Exception { + runTest("compiler/testData/diagnostics/tests/nullableTypes/kt58844.kt"); + } + @Test @TestMetadata("nullAssertOnTypeWithNullableUpperBound.kt") public void testNullAssertOnTypeWithNullableUpperBound() throws Exception { diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java index b939fe2f6c3..bea3a833e95 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java @@ -22744,6 +22744,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated runTest("compiler/testData/diagnostics/tests/nullableTypes/inferenceFlexibleTToNullable.kt"); } + @Test + @TestMetadata("kt58844.kt") + public void testKt58844() throws Exception { + runTest("compiler/testData/diagnostics/tests/nullableTypes/kt58844.kt"); + } + @Test @TestMetadata("nullAssertOnTypeWithNullableUpperBound.kt") public void testNullAssertOnTypeWithNullableUpperBound() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java index 59aeb55cd58..5ab6d5c4fe3 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java @@ -22744,6 +22744,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir runTest("compiler/testData/diagnostics/tests/nullableTypes/inferenceFlexibleTToNullable.kt"); } + @Test + @TestMetadata("kt58844.kt") + public void testKt58844() throws Exception { + runTest("compiler/testData/diagnostics/tests/nullableTypes/kt58844.kt"); + } + @Test @TestMetadata("nullAssertOnTypeWithNullableUpperBound.kt") public void testNullAssertOnTypeWithNullableUpperBound() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java index 5988ae78115..a5d25fc1f4e 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java @@ -22750,6 +22750,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia runTest("compiler/testData/diagnostics/tests/nullableTypes/inferenceFlexibleTToNullable.kt"); } + @Test + @TestMetadata("kt58844.kt") + public void testKt58844() throws Exception { + runTest("compiler/testData/diagnostics/tests/nullableTypes/kt58844.kt"); + } + @Test @TestMetadata("nullAssertOnTypeWithNullableUpperBound.kt") public void testNullAssertOnTypeWithNullableUpperBound() throws Exception { diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/coneDiagnosticToFirDiagnostic.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/coneDiagnosticToFirDiagnostic.kt index afe6d4f69ef..5b17e40547a 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/coneDiagnosticToFirDiagnostic.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/coneDiagnosticToFirDiagnostic.kt @@ -223,7 +223,7 @@ private fun mapInapplicableCandidateError( FirErrors.ARGUMENT_TYPE_MISMATCH.createOn( rootCause.argument.source ?: source, rootCause.expectedType.removeTypeVariableTypes(typeContext), - rootCause.argument.typeRef.coneType.removeTypeVariableTypes(typeContext), + rootCause.actualType.removeTypeVariableTypes(typeContext), rootCause.isMismatchDueToNullability ) } diff --git a/compiler/testData/cli/jvm/instanceAccessBeforeSuperCall.out b/compiler/testData/cli/jvm/instanceAccessBeforeSuperCall.out index 91b887e04d8..29163b87408 100644 --- a/compiler/testData/cli/jvm/instanceAccessBeforeSuperCall.out +++ b/compiler/testData/cli/jvm/instanceAccessBeforeSuperCall.out @@ -11,7 +11,7 @@ compiler/testData/cli/jvm/instanceAccessBeforeSuperCall.kt:2:74: error: cannot a compiler/testData/cli/jvm/instanceAccessBeforeSuperCall.kt:7:20: error: 'this' is not defined in this context class B(other: B = this) ^ -compiler/testData/cli/jvm/instanceAccessBeforeSuperCall.kt:10:32: error: argument type mismatch: actual type is kotlin/Function0'' before the instance has been initialized> but kotlin/Int was expected +compiler/testData/cli/jvm/instanceAccessBeforeSuperCall.kt:10:32: error: argument type mismatch: actual type is kotlin/Function0 but kotlin/Int was expected constructor(x: Int) : this({ ^ compiler/testData/cli/jvm/instanceAccessBeforeSuperCall.kt:12:9: error: cannot access '' before the instance has been initialized diff --git a/compiler/testData/diagnostics/tests/annotations/annotationRenderingInTypes.fir.kt b/compiler/testData/diagnostics/tests/annotations/annotationRenderingInTypes.fir.kt index 96cfc58038a..1e5c44e7840 100644 --- a/compiler/testData/diagnostics/tests/annotations/annotationRenderingInTypes.fir.kt +++ b/compiler/testData/diagnostics/tests/annotations/annotationRenderingInTypes.fir.kt @@ -12,7 +12,7 @@ annotation class Ann fun <@Ann R : @Ann Any> f3(a: Array<@Ann R>): Array<@Ann R?> = null!! fun test2(a: @Ann Array) { - val r: Array = f3(; @R|Ann|() kotlin/Array")!>a) + val r: Array = f3(; @R|Ann|() kotlin/Array")!>a) } diff --git a/compiler/testData/diagnostics/tests/nullableTypes/kt58844.fir.kt b/compiler/testData/diagnostics/tests/nullableTypes/kt58844.fir.kt new file mode 100644 index 00000000000..f0ec03ad5c5 --- /dev/null +++ b/compiler/testData/diagnostics/tests/nullableTypes/kt58844.fir.kt @@ -0,0 +1,12 @@ +// !RENDER_DIAGNOSTICS_MESSAGES + +fun intFun(i: Int) {} +fun byteFun(i: Byte) {} + +fun main(args: Array) { + var intVar: Int? = 1 + var byteVar: Byte? = 1 + + intFun(intVar?.toInt()) + byteFun(byteVar?.toByte()) +} diff --git a/compiler/testData/diagnostics/tests/nullableTypes/kt58844.kt b/compiler/testData/diagnostics/tests/nullableTypes/kt58844.kt new file mode 100644 index 00000000000..39f2f104211 --- /dev/null +++ b/compiler/testData/diagnostics/tests/nullableTypes/kt58844.kt @@ -0,0 +1,12 @@ +// !RENDER_DIAGNOSTICS_MESSAGES + +fun intFun(i: Int) {} +fun byteFun(i: Byte) {} + +fun main(args: Array) { + var intVar: Int? = 1 + var byteVar: Byte? = 1 + + intFun(intVar?.toInt()) + byteFun(byteVar?.toByte()) +} 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 b30bdcfef18..e9ac5a6328b 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 @@ -23570,6 +23570,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/nullableTypes/inferenceFlexibleTToNullable.kt"); } + @Test + @TestMetadata("kt58844.kt") + public void testKt58844() throws Exception { + runTest("compiler/testData/diagnostics/tests/nullableTypes/kt58844.kt"); + } + @Test @TestMetadata("nullAssertOnTypeWithNullableUpperBound.kt") public void testNullAssertOnTypeWithNullableUpperBound() throws Exception {