From 95a8060946352db8cfe31aa728e42a9458079ceb Mon Sep 17 00:00:00 2001 From: Pavel Kirpichenkov Date: Thu, 6 Feb 2020 12:07:13 +0300 Subject: [PATCH] [NI] Don't report uninferred type parameter error on special functions Type parameters can't be specified explicitly for special constructions. Reporting this error does not help fixing the cause of it and needlessly reveals implementation details. ^KT-36342 Fixed --- ...irOldFrontendDiagnosticsTestGenerated.java | 10 ++++++++ .../DiagnosticReporterByTrackingStrategy.kt | 12 +++++++++ .../incompleteCode/controlStructuresErrors.kt | 4 +-- .../inference/regressions/kt36342.fir.kt | 25 +++++++++++++++++++ .../tests/inference/regressions/kt36342.kt | 25 +++++++++++++++++++ .../tests/inference/regressions/kt36342.txt | 4 +++ .../inference/regressions/kt36342_2.fir.kt | 14 +++++++++++ .../tests/inference/regressions/kt36342_2.kt | 14 +++++++++++ .../tests/inference/regressions/kt36342_2.txt | 5 ++++ .../tests/inference/regressions/kt948.kt | 2 +- .../tests/resolve/callableReferenceInCST.kt | 2 +- .../checkers/DiagnosticsTestGenerated.java | 10 ++++++++ .../DiagnosticsUsingJavacTestGenerated.java | 10 ++++++++ 13 files changed, 133 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/inference/regressions/kt36342.fir.kt create mode 100644 compiler/testData/diagnostics/tests/inference/regressions/kt36342.kt create mode 100644 compiler/testData/diagnostics/tests/inference/regressions/kt36342.txt create mode 100644 compiler/testData/diagnostics/tests/inference/regressions/kt36342_2.fir.kt create mode 100644 compiler/testData/diagnostics/tests/inference/regressions/kt36342_2.kt create mode 100644 compiler/testData/diagnostics/tests/inference/regressions/kt36342_2.txt diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java index d1c01d3c1fe..bd590ca4ce0 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java @@ -11310,6 +11310,16 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte runTest("compiler/testData/diagnostics/tests/inference/regressions/kt35943.kt"); } + @TestMetadata("kt36342.kt") + public void testKt36342() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/regressions/kt36342.kt"); + } + + @TestMetadata("kt36342_2.kt") + public void testKt36342_2() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/regressions/kt36342_2.kt"); + } + @TestMetadata("kt4420.kt") public void testKt4420() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/regressions/kt4420.kt"); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/DiagnosticReporterByTrackingStrategy.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/DiagnosticReporterByTrackingStrategy.kt index 12212c1991b..74c425eab43 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/DiagnosticReporterByTrackingStrategy.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/DiagnosticReporterByTrackingStrategy.kt @@ -32,6 +32,7 @@ import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluat import org.jetbrains.kotlin.resolve.descriptorUtil.module import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver import org.jetbrains.kotlin.types.KotlinType +import org.jetbrains.kotlin.types.expressions.ControlStructureTypingUtils import org.jetbrains.kotlin.utils.addToStdlib.cast import org.jetbrains.kotlin.utils.addToStdlib.safeAs @@ -391,6 +392,9 @@ class DiagnosticReporterByTrackingStrategy( } ) return + if (isSpecialFunction(error.resolvedAtom)) + return + val expression = when (val atom = error.resolvedAtom.atom) { is PSIKotlinCall -> atom.psiCall.calleeExpression is PSIKotlinCallArgument -> atom.valueArgument.getArgumentExpression() @@ -407,6 +411,14 @@ class DiagnosticReporterByTrackingStrategy( } } + private fun isSpecialFunction(atom: ResolvedAtom): Boolean { + if (atom !is ResolvedCallAtom) return false + + return ControlStructureTypingUtils.ResolveConstruct.values().any { specialFunction -> + specialFunction.specialFunctionName == atom.candidateDescriptor.name + } + } + private fun reportConstantTypeMismatch(constraintError: NewConstraintError, expression: KtExpression): Boolean { if (expression is KtConstantExpression) { val module = context.scope.ownerDescriptor.module diff --git a/compiler/testData/diagnostics/tests/incompleteCode/controlStructuresErrors.kt b/compiler/testData/diagnostics/tests/incompleteCode/controlStructuresErrors.kt index c2d9240da61..7f21b25d13d 100644 --- a/compiler/testData/diagnostics/tests/incompleteCode/controlStructuresErrors.kt +++ b/compiler/testData/diagnostics/tests/incompleteCode/controlStructuresErrors.kt @@ -1,7 +1,7 @@ // !WITH_NEW_INFERENCE fun test1() { - if (rr) { + if (rr) { if (l) { a.q() } @@ -16,7 +16,7 @@ fun test1() { else { a.u() } - } + } } fun test2(l: List<AA>) { diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt36342.fir.kt b/compiler/testData/diagnostics/tests/inference/regressions/kt36342.fir.kt new file mode 100644 index 00000000000..0c05a63971e --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt36342.fir.kt @@ -0,0 +1,25 @@ +// !WITH_NEW_INFERENCE + +import java.lang.Exception + +fun id(arg: K): K = arg + +fun test() { + id(unresolved)!! + unresolved!!!! + try { + id(unresolved) + } catch (e: Exception) { + id(unresolved) + } + + if (true) + id(unresolved) + else + id(unresolved) + + when { + true -> id(unresolved) + } + id(unresolved) ?: id(unresolved) +} diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt36342.kt b/compiler/testData/diagnostics/tests/inference/regressions/kt36342.kt new file mode 100644 index 00000000000..a47c7211b08 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt36342.kt @@ -0,0 +1,25 @@ +// !WITH_NEW_INFERENCE + +import java.lang.Exception + +fun id(arg: K): K = arg + +fun test() { + id(unresolved)!! + unresolved!!!! + try { + id(unresolved) + } catch (e: Exception) { + id(unresolved) + } + + if (true) + id(unresolved) + else + id(unresolved) + + when { + true -> id(unresolved) + } + id(unresolved) ?: id(unresolved) +} diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt36342.txt b/compiler/testData/diagnostics/tests/inference/regressions/kt36342.txt new file mode 100644 index 00000000000..805e26c2432 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt36342.txt @@ -0,0 +1,4 @@ +package + +public fun id(/*0*/ arg: K): K +public fun test(): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt36342_2.fir.kt b/compiler/testData/diagnostics/tests/inference/regressions/kt36342_2.fir.kt new file mode 100644 index 00000000000..5e8254c4013 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt36342_2.fir.kt @@ -0,0 +1,14 @@ +// !WITH_NEW_INFERENCE + +fun id(arg: K): K = arg +fun materialize(): M = TODO() + +fun test(b: Boolean) { + id(if (b) { + id(unresolved) + } else { + id( + materialize() + ) + }) +} diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt36342_2.kt b/compiler/testData/diagnostics/tests/inference/regressions/kt36342_2.kt new file mode 100644 index 00000000000..d2625902a26 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt36342_2.kt @@ -0,0 +1,14 @@ +// !WITH_NEW_INFERENCE + +fun id(arg: K): K = arg +fun materialize(): M = TODO() + +fun test(b: Boolean) { + id(if (b) { + id(unresolved) + } else { + id( + materialize() + ) + }) +} diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt36342_2.txt b/compiler/testData/diagnostics/tests/inference/regressions/kt36342_2.txt new file mode 100644 index 00000000000..e1d8ed4db69 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt36342_2.txt @@ -0,0 +1,5 @@ +package + +public fun id(/*0*/ arg: K): K +public fun materialize(): M +public fun test(/*0*/ b: kotlin.Boolean): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt948.kt b/compiler/testData/diagnostics/tests/inference/regressions/kt948.kt index 42480796d15..88507dd8e6e 100644 --- a/compiler/testData/diagnostics/tests/inference/regressions/kt948.kt +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt948.kt @@ -12,7 +12,7 @@ fun emptyList() : List? = ArrayList() fun foo() { // type arguments shouldn't be required val l : List = emptyList()!! - val l1 = emptyList()!! + val l1 = emptyList()!! checkSubtype>(emptyList()!!) checkSubtype?>(emptyList()) diff --git a/compiler/testData/diagnostics/tests/resolve/callableReferenceInCST.kt b/compiler/testData/diagnostics/tests/resolve/callableReferenceInCST.kt index 1c3e3498679..6152c6d22c4 100644 --- a/compiler/testData/diagnostics/tests/resolve/callableReferenceInCST.kt +++ b/compiler/testData/diagnostics/tests/resolve/callableReferenceInCST.kt @@ -28,7 +28,7 @@ fun testElvis(x: Any?) { } fun testExclExcl() { - val y = :: unresolved!! + val y = :: unresolved!! } fun testTry() { diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 1dd1b7eb251..f4e9f61dddf 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -11317,6 +11317,16 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { runTest("compiler/testData/diagnostics/tests/inference/regressions/kt35943.kt"); } + @TestMetadata("kt36342.kt") + public void testKt36342() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/regressions/kt36342.kt"); + } + + @TestMetadata("kt36342_2.kt") + public void testKt36342_2() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/regressions/kt36342_2.kt"); + } + @TestMetadata("kt4420.kt") public void testKt4420() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/regressions/kt4420.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index e1dd1da2081..9edec4e8c6f 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -11312,6 +11312,16 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/inference/regressions/kt35943.kt"); } + @TestMetadata("kt36342.kt") + public void testKt36342() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/regressions/kt36342.kt"); + } + + @TestMetadata("kt36342_2.kt") + public void testKt36342_2() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/regressions/kt36342_2.kt"); + } + @TestMetadata("kt4420.kt") public void testKt4420() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/regressions/kt4420.kt");