From d40906a69073359e2fc8402713dee51a155eef6f Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 18 Apr 2013 18:54:14 +0400 Subject: [PATCH] Disallow references to extensions in classes It's not clear in which order should the two receiver arguments be passed to them, and there's no corresponding K*FunctionN class for now #KT-1183 In Progress --- .../org/jetbrains/jet/lang/diagnostics/Errors.java | 4 ++++ .../rendering/DefaultErrorMessages.java | 3 +++ .../expressions/BasicExpressionTypingVisitor.java | 3 +-- .../extensionInClassDisallowed.kt | 14 ++++++++++++++ .../jet/checkers/JetDiagnosticsTestGenerated.java | 5 +++++ 5 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/callableReference/extensionInClassDisallowed.kt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java index c30f66c426a..9ea84d674d9 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java @@ -345,6 +345,10 @@ public interface Errors { DiagnosticFactory1 TYPE_INFERENCE_UPPER_BOUND_VIOLATED = DiagnosticFactory1.create(ERROR); DiagnosticFactory2 TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH = DiagnosticFactory2.create(ERROR); + // Callable references + + DiagnosticFactory1 EXTENSION_IN_CLASS_REFERENCE_NOT_ALLOWED = DiagnosticFactory1.create(ERROR); + // Multi-declarations DiagnosticFactory0 INITIALIZER_REQUIRED_FOR_MULTIDECLARATION = DiagnosticFactory0.create(ERROR, DEFAULT); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java index 6a79a12417d..fccf5119500 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java @@ -440,6 +440,9 @@ public class DefaultErrorMessages { MAP.put(AMBIGUOUS_ANONYMOUS_TYPE_INFERRED, "Right-hand side has anonymous type. Please specify type explicitly", TO_STRING); + MAP.put(EXTENSION_IN_CLASS_REFERENCE_NOT_ALLOWED, + "''{0}'' is a member and an extension at the same time. References to such elements are not allowed", TO_STRING); + MAP.setImmutable(); for (Field field : Errors.class.getFields()) { diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java index 2ebd303b539..85fed7c4f15 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java @@ -611,8 +611,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor { ReceiverParameterDescriptor receiverParameter = descriptor.getReceiverParameter(); ReceiverParameterDescriptor expectedThisObject = descriptor.getExpectedThisObject(); if (receiverParameter != null && expectedThisObject != null) { - // TODO: report "extensions in classes" are not supported - context.trace.report(UNRESOLVED_REFERENCE.on(reference, reference)); + context.trace.report(EXTENSION_IN_CLASS_REFERENCE_NOT_ALLOWED.on(reference, descriptor)); return null; } diff --git a/compiler/testData/diagnostics/tests/callableReference/extensionInClassDisallowed.kt b/compiler/testData/diagnostics/tests/callableReference/extensionInClassDisallowed.kt new file mode 100644 index 00000000000..7700ca2c22b --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/extensionInClassDisallowed.kt @@ -0,0 +1,14 @@ +class A { + fun Int.extInt() = 42 + fun A.extA(x: String) = x + + fun main() { + ::extInt + ::extA + } +} + +fun main() { + A::extInt + A::extA +} diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index 713b647ed1b..e19ae790e67 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -781,6 +781,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage doTest("compiler/testData/diagnostics/tests/callableReference/extensionFromTopLevel.kt"); } + @TestMetadata("extensionInClassDisallowed.kt") + public void testExtensionInClassDisallowed() throws Exception { + doTest("compiler/testData/diagnostics/tests/callableReference/extensionInClassDisallowed.kt"); + } + @TestMetadata("genericClassFromTopLevel.kt") public void testGenericClassFromTopLevel() throws Exception { doTest("compiler/testData/diagnostics/tests/callableReference/genericClassFromTopLevel.kt");