diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/diagnostics/DefaultErrorMessagesJvm.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/diagnostics/DefaultErrorMessagesJvm.java index c8e66770559..7ca7e8030a2 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/diagnostics/DefaultErrorMessagesJvm.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/diagnostics/DefaultErrorMessagesJvm.java @@ -48,6 +48,7 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension { MAP.put(ErrorsJvm.OVERRIDE_CANNOT_BE_STATIC, "Override cannot be 'platformStatic' in object"); MAP.put(ErrorsJvm.PLATFORM_STATIC_ILLEGAL_USAGE, "This declaration does not support ''platformStatic''", DescriptorRenderer.SHORT_NAMES_IN_TYPES); MAP.put(ErrorsJvm.NATIVE_DECLARATION_CANNOT_BE_ABSTRACT, "Native declaration can not be abstract"); + MAP.put(ErrorsJvm.NATIVE_DECLARATION_CANNOT_HAVE_BODY, "Native declaration can not have a body"); } diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/diagnostics/ErrorsJvm.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/diagnostics/ErrorsJvm.java index 43b60039bd9..5cd89d420a1 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/diagnostics/ErrorsJvm.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/diagnostics/ErrorsJvm.java @@ -38,6 +38,7 @@ public interface ErrorsJvm { DiagnosticFactory1 PLATFORM_STATIC_ILLEGAL_USAGE = DiagnosticFactory1.create(ERROR, DECLARATION_SIGNATURE); DiagnosticFactory0 NATIVE_DECLARATION_CANNOT_BE_ABSTRACT = DiagnosticFactory0.create(ERROR, ABSTRACT_MODIFIER); + DiagnosticFactory0 NATIVE_DECLARATION_CANNOT_HAVE_BODY = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE); @SuppressWarnings("UnusedDeclaration") Object _initializer = new Object() { diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/kotlin/native.kt b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/kotlin/native.kt index 7e20a45ad8f..11623d325a4 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/kotlin/native.kt +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/kotlin/native.kt @@ -28,6 +28,7 @@ import org.jetbrains.jet.lang.resolve.name.FqName import org.jetbrains.jet.lang.descriptors.CallableMemberDescriptor import org.jetbrains.jet.lang.descriptors.Modality import org.jetbrains.jet.lang.resolve.java.diagnostics.ErrorsJvm +import org.jetbrains.jet.lang.psi.JetDeclarationWithBody private val NATIVE_ANNOTATION_CLASS_NAME = FqName("kotlin.jvm.native") @@ -52,10 +53,14 @@ class SuppressNoBodyErrorsForNativeDeclarations : DiagnosticsWithSuppression.Sup public class NativeFunChecker : AnnotationChecker { override fun check(declaration: JetDeclaration, descriptor: DeclarationDescriptor, diagnosticHolder: DiagnosticSink) { - if (descriptor.hasNativeAnnotation() && - descriptor is CallableMemberDescriptor && + if (!descriptor.hasNativeAnnotation()) return + if (descriptor is CallableMemberDescriptor && descriptor.getModality() == Modality.ABSTRACT) { diagnosticHolder.report(ErrorsJvm.NATIVE_DECLARATION_CANNOT_BE_ABSTRACT.on(declaration)) } + + if (declaration is JetDeclarationWithBody && declaration.hasBody()) { + diagnosticHolder.report(ErrorsJvm.NATIVE_DECLARATION_CANNOT_HAVE_BODY.on(declaration)) + } } } diff --git a/compiler/testData/codegen/boxWithStdlib/native/simpleNative.kt b/compiler/testData/codegen/boxWithStdlib/native/simpleNative.kt index eddc3553c70..e928c3d6506 100644 --- a/compiler/testData/codegen/boxWithStdlib/native/simpleNative.kt +++ b/compiler/testData/codegen/boxWithStdlib/native/simpleNative.kt @@ -3,7 +3,7 @@ package foo import kotlin.jvm.* class WithNative { - native fun foo() {} + native fun foo() } fun box(): String { diff --git a/compiler/testData/codegen/boxWithStdlib/native/staticNative.kt b/compiler/testData/codegen/boxWithStdlib/native/staticNative.kt index f95864575bb..4f9ca83e2f3 100644 --- a/compiler/testData/codegen/boxWithStdlib/native/staticNative.kt +++ b/compiler/testData/codegen/boxWithStdlib/native/staticNative.kt @@ -5,12 +5,12 @@ import kotlin.platform.* class WithNative { class object { - platformStatic native fun bar(l: Long, s: String): Double = null!! + platformStatic native fun bar(l: Long, s: String): Double } } object ObjWithNative { - platformStatic native fun bar(l: Long, s: String): Double = null!! + platformStatic native fun bar(l: Long, s: String): Double } fun box(): String { diff --git a/compiler/testData/diagnostics/testsWithStdLib/native/body.kt b/compiler/testData/diagnostics/testsWithStdLib/native/body.kt new file mode 100644 index 00000000000..c68c023d6ed --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/native/body.kt @@ -0,0 +1,25 @@ +import kotlin.jvm.* + +native fun foo() {} + +class C { + native fun foo() {} + + class object { + native fun foo() {} + } +} + +object O { + native fun foo() {} +} + +fun test() { + class Local { + native fun foo() {} + } + + object { + native fun foo() {} + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithStdLib/native/body.txt b/compiler/testData/diagnostics/testsWithStdLib/native/body.txt new file mode 100644 index 00000000000..b6459d3140c --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/native/body.txt @@ -0,0 +1,36 @@ +package + +kotlin.jvm.native() internal fun foo(): kotlin.Unit +internal fun test(): kotlin.Unit + +internal final class C { + public constructor C() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + kotlin.jvm.native() internal final fun foo(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + internal class object { + private constructor () + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + kotlin.jvm.native() internal final fun foo(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } +} + +internal object O { + private constructor O() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + kotlin.jvm.native() internal final fun foo(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + public class object : O { + private constructor () + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + kotlin.jvm.native() internal final override /*1*/ /*fake_override*/ fun foo(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } +} diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestWithStdLibGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestWithStdLibGenerated.java index f97e693742a..9d767611715 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestWithStdLibGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestWithStdLibGenerated.java @@ -662,6 +662,12 @@ public class JetDiagnosticsTestWithStdLibGenerated extends AbstractJetDiagnostic JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib/native"), Pattern.compile("^(.+)\\.kt$"), true); } + @TestMetadata("body.kt") + public void testBody() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/native/body.kt"); + doTest(fileName); + } + @TestMetadata("noBody.kt") public void testNoBody() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/native/noBody.kt");