From d218b88106830f4ab0683bcc49e543b1d49f6114 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Mon, 2 Nov 2020 16:40:10 +0300 Subject: [PATCH] Forbid subclasses of NativePointed to have backing fields (this crashes the compiler otherwise) --- .../konan/lower/SpecialBackendChecksTraversal.kt | 11 +++++++++++ .../backend.native/tests/compilerChecks/t60.kt | 10 ++++++++++ 2 files changed, 21 insertions(+) create mode 100644 kotlin-native/backend.native/tests/compilerChecks/t60.kt diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/SpecialBackendChecksTraversal.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/SpecialBackendChecksTraversal.kt index d82f3c04f3b..e17c5790fd4 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/SpecialBackendChecksTraversal.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/SpecialBackendChecksTraversal.kt @@ -321,6 +321,17 @@ private class BackendChecker(val context: Context, val irFile: IrFile) : IrEleme reportError(expression, "Native interop types constructors must not be called directly") } + override fun visitField(declaration: IrField) { + if (declaration.isFakeOverride) return // Can't happen now, just trying to be future-proof here. + + val parent = declaration.parent + + if (parent is IrClass && parent.defaultType.isNativePointed(symbols) && parent.symbol != symbols.nativePointed) { + val nativePointed = symbols.nativePointed.owner.name + reportError(declaration, "Subclasses of $nativePointed cannot have properties with backing fields") + } + } + override fun visitCall(expression: IrCall) { expression.acceptChildrenVoid(this) diff --git a/kotlin-native/backend.native/tests/compilerChecks/t60.kt b/kotlin-native/backend.native/tests/compilerChecks/t60.kt new file mode 100644 index 00000000000..0b234eac4d6 --- /dev/null +++ b/kotlin-native/backend.native/tests/compilerChecks/t60.kt @@ -0,0 +1,10 @@ +import kotlinx.cinterop.* + +class Vertex constructor(rawPtr: NativePtr) : CStructVar(rawPtr) { + var x: Float = 0f + var y: Float = 0f + var r: Float = 0f + var g: Float = 0f + var b: Float = 0f + companion object : CStructVar.Type(40, 8) +}