From 9623b0eedbace5b91c7dd289de127cd8d1a05a55 Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Fri, 24 Jan 2020 12:36:34 +0100 Subject: [PATCH] Report error instead of assertion when property is used as operator #KT-34857 fixed --- .../fir/FirOldFrontendDiagnosticsTestGenerated.java | 5 +++++ .../src/org/jetbrains/kotlin/diagnostics/Errors.java | 2 ++ .../diagnostics/rendering/DefaultErrorMessages.java | 2 ++ .../kotlin/resolve/calls/checkers/OperatorCallChecker.kt | 5 ++++- compiler/testData/diagnostics/tests/kt34857.fir.kt | 8 ++++++++ compiler/testData/diagnostics/tests/kt34857.kt | 8 ++++++++ compiler/testData/diagnostics/tests/kt34857.txt | 4 ++++ .../kotlin/checkers/DiagnosticsTestGenerated.java | 5 +++++ .../javac/DiagnosticsUsingJavacTestGenerated.java | 5 +++++ 9 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/tests/kt34857.fir.kt create mode 100644 compiler/testData/diagnostics/tests/kt34857.kt create mode 100644 compiler/testData/diagnostics/tests/kt34857.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 071b9951aaf..e8e6cdb3926 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java @@ -328,6 +328,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte runTest("compiler/testData/diagnostics/tests/kt310.kt"); } + @TestMetadata("kt34857.kt") + public void testKt34857() throws Exception { + runTest("compiler/testData/diagnostics/tests/kt34857.kt"); + } + @TestMetadata("kt435.kt") public void testKt435() throws Exception { runTest("compiler/testData/diagnostics/tests/kt435.kt"); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java index 58a4e739636..c24073924a1 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java @@ -867,6 +867,8 @@ public interface Errors { DiagnosticFactory2 OPERATOR_MODIFIER_REQUIRED = DiagnosticFactory2.create(ERROR); DiagnosticFactory2 INFIX_MODIFIER_REQUIRED = DiagnosticFactory2.create(ERROR); + DiagnosticFactory2 PROPERTY_AS_OPERATOR = DiagnosticFactory2.create(ERROR); + DiagnosticFactory2 INAPPLICABLE_MODIFIER = DiagnosticFactory2.create(ERROR); DiagnosticFactory1 DSL_SCOPE_VIOLATION = DiagnosticFactory1.create(ERROR); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java index 9c9fd69d981..f3a131903cd 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -504,6 +504,8 @@ public class DefaultErrorMessages { MAP.put(OPERATOR_MODIFIER_REQUIRED, "''operator'' modifier is required on ''{0}'' in ''{1}''", NAME, STRING); MAP.put(INFIX_MODIFIER_REQUIRED, "''infix'' modifier is required on ''{0}'' in ''{1}''", NAME, STRING); + MAP.put(PROPERTY_AS_OPERATOR, "Properties cannot be used in operator conventions: ''{0}'' in ''{1}''", NAME, STRING); + MAP.put(INAPPLICABLE_MODIFIER, "''{0}'' modifier is inapplicable. The reason is that {1}", TO_STRING, STRING); MAP.put(DSL_SCOPE_VIOLATION, "''{0}'' can''t be called in this context by implicit receiver. " + diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/OperatorCallChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/OperatorCallChecker.kt index ec1eeb6f4c9..beb528f59ae 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/OperatorCallChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/OperatorCallChecker.kt @@ -48,7 +48,10 @@ class OperatorCallChecker : CallChecker { if (resolvedCall is VariableAsFunctionResolvedCall && call is CallTransformer.CallForImplicitInvoke && call.itIsVariableAsFunctionCall) { val outerCall = call.outerCall - if (isConventionCall(outerCall) || isWrongCallWithExplicitTypeArguments(resolvedCall, outerCall)) { + if (isConventionCall(outerCall)) { + val containingDeclarationName = functionDescriptor.containingDeclaration.fqNameUnsafe.asString() + context.trace.report(Errors.PROPERTY_AS_OPERATOR.on(reportOn, functionDescriptor, containingDeclarationName)) + } else if (isWrongCallWithExplicitTypeArguments(resolvedCall, outerCall)) { throw AssertionError( "Illegal resolved call to variable with invoke for $outerCall. " + "Variable: ${resolvedCall.variableCall.resultingDescriptor}" + diff --git a/compiler/testData/diagnostics/tests/kt34857.fir.kt b/compiler/testData/diagnostics/tests/kt34857.fir.kt new file mode 100644 index 00000000000..a71121f8a49 --- /dev/null +++ b/compiler/testData/diagnostics/tests/kt34857.fir.kt @@ -0,0 +1,8 @@ +// !LANGUAGE: +NewInference + +val Int.plusAssign: (Int) -> Unit + get() = {} + +fun main() { + 1 += 2 +} diff --git a/compiler/testData/diagnostics/tests/kt34857.kt b/compiler/testData/diagnostics/tests/kt34857.kt new file mode 100644 index 00000000000..a71121f8a49 --- /dev/null +++ b/compiler/testData/diagnostics/tests/kt34857.kt @@ -0,0 +1,8 @@ +// !LANGUAGE: +NewInference + +val Int.plusAssign: (Int) -> Unit + get() = {} + +fun main() { + 1 += 2 +} diff --git a/compiler/testData/diagnostics/tests/kt34857.txt b/compiler/testData/diagnostics/tests/kt34857.txt new file mode 100644 index 00000000000..f0365d51108 --- /dev/null +++ b/compiler/testData/diagnostics/tests/kt34857.txt @@ -0,0 +1,4 @@ +package + +public val kotlin.Int.plusAssign: (kotlin.Int) -> kotlin.Unit +public fun main(): kotlin.Unit diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index d3d4395b93e..1f6f301b3b3 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -330,6 +330,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { runTest("compiler/testData/diagnostics/tests/kt310.kt"); } + @TestMetadata("kt34857.kt") + public void testKt34857() throws Exception { + runTest("compiler/testData/diagnostics/tests/kt34857.kt"); + } + @TestMetadata("kt435.kt") public void testKt435() throws Exception { runTest("compiler/testData/diagnostics/tests/kt435.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index aa544ca4c16..116b2394edd 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -330,6 +330,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/kt310.kt"); } + @TestMetadata("kt34857.kt") + public void testKt34857() throws Exception { + runTest("compiler/testData/diagnostics/tests/kt34857.kt"); + } + @TestMetadata("kt435.kt") public void testKt435() throws Exception { runTest("compiler/testData/diagnostics/tests/kt435.kt");