diff --git a/compiler/testData/codegen/boxWithStdlib/native/nativePropertyAccessors.kt b/compiler/testData/codegen/boxWithStdlib/native/nativePropertyAccessors.kt new file mode 100644 index 00000000000..d9b18bb1b4b --- /dev/null +++ b/compiler/testData/codegen/boxWithStdlib/native/nativePropertyAccessors.kt @@ -0,0 +1,49 @@ +class C { + class object { + val defaultGetter: Int = 1 + [native] get + + var defaultSetter: Int = 1 + [native] get + [native] set + } + + val defaultGetter: Int = 1 + [native] get + + var defaultSetter: Int = 1 + [native] get + [native] set +} + +val defaultGetter: Int = 1 + [native] get + +var defaultSetter: Int = 1 + [native] get + [native] set + +fun check(body: () -> Unit, signature: String): String? { + try { + body() + return "Link error expected" + } + catch (e: java.lang.UnsatisfiedLinkError) { + if (e.getMessage() != signature) return "Fail $signature: " + e.getMessage() + } + + return null +} + +fun box(): String { + return check({defaultGetter}, "_DefaultPackage.getDefaultGetter()I") + ?: check({defaultSetter = 1}, "_DefaultPackage.setDefaultSetter(I)V") + + ?: check({C.defaultGetter}, "C\$object.getDefaultGetter()I") + ?: check({C.defaultSetter = 1}, "C\$object.setDefaultSetter(I)V") + + ?: check({C().defaultGetter}, "C.getDefaultGetter()I") + ?: check({C().defaultSetter = 1}, "C.setDefaultSetter(I)V") + + ?: "OK" +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java index b568a419036..ec3a3e66fa2 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java @@ -1825,6 +1825,12 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode doTestWithStdlib(fileName); } + @TestMetadata("nativePropertyAccessors.kt") + public void testNativePropertyAccessors() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/native/nativePropertyAccessors.kt"); + doTestWithStdlib(fileName); + } + @TestMetadata("simpleNative.kt") public void testSimpleNative() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/native/simpleNative.kt"); diff --git a/spec-docs/jvm-native-flag-support.md b/spec-docs/jvm-native-flag-support.md index 81dfbc15a42..cbaf70d3c8a 100644 --- a/spec-docs/jvm-native-flag-support.md +++ b/spec-docs/jvm-native-flag-support.md @@ -50,10 +50,18 @@ A native member of package `p` yields one JVM method: ### Native Property Accessors A property can not be marked `native`. -A *property accessor* can be marked `native`. In this case the generated code is the same as for a native function defined in the same -context as the property. +A *property accessor* can be marked `native` if it has no body (i.e. it is a *default accessor*). +In this case the generated code is the same as for a native function defined in the same context as the property. + +Example: +``` kotlin +val foo: Int + [native] get +``` ## Not implemented (yet) - native property accessors + - frontend: when accessors are default and native, don't require an initializer, don't allow a backing field + - backend: when accessors are default and native, don't generate a backing field - applicability checks: only functions and property accessors \ No newline at end of file