From d410799d57c41fe3989d2c0079794f567c8a7d02 Mon Sep 17 00:00:00 2001 From: Ellen Arteca Date: Thu, 5 Dec 2024 18:21:10 -0800 Subject: [PATCH 1/9] Adding support for returning opaques as the error type of a result, as a custom Kotlin exception type --- .../kotlin/dev/diplomattest/somelib/DataProvider.kt | 2 +- .../kotlin/dev/diplomattest/somelib/FixedDecimal.kt | 2 +- .../dev/diplomattest/somelib/FixedDecimalFormatter.kt | 2 +- .../src/main/kotlin/dev/diplomattest/somelib/Lib.kt | 6 +++++- .../main/kotlin/dev/diplomattest/somelib/Locale.kt | 2 +- .../kotlin/dev/diplomattest/somelib/AttrOpaque1.kt | 2 +- .../kotlin/dev/diplomattest/somelib/AttrOpaque2.kt | 2 +- .../src/main/kotlin/dev/diplomattest/somelib/Bar.kt | 2 +- .../kotlin/dev/diplomattest/somelib/Float64Vec.kt | 2 +- .../src/main/kotlin/dev/diplomattest/somelib/Foo.kt | 2 +- .../src/main/kotlin/dev/diplomattest/somelib/Lib.kt | 6 +++++- .../main/kotlin/dev/diplomattest/somelib/MyIndexer.kt | 2 +- .../kotlin/dev/diplomattest/somelib/MyIterable.kt | 2 +- .../kotlin/dev/diplomattest/somelib/MyIterator.kt | 2 +- .../main/kotlin/dev/diplomattest/somelib/MyString.kt | 2 +- .../main/kotlin/dev/diplomattest/somelib/MyStruct.kt | 4 ++-- .../src/main/kotlin/dev/diplomattest/somelib/One.kt | 2 +- .../main/kotlin/dev/diplomattest/somelib/Opaque.kt | 2 +- .../kotlin/dev/diplomattest/somelib/OpaqueIterable.kt | 2 +- .../kotlin/dev/diplomattest/somelib/OpaqueIterator.kt | 2 +- .../dev/diplomattest/somelib/OpaqueMutexedString.kt | 2 +- .../kotlin/dev/diplomattest/somelib/OptionOpaque.kt | 2 +- .../dev/diplomattest/somelib/OptionOpaqueChar.kt | 2 +- .../kotlin/dev/diplomattest/somelib/OptionString.kt | 2 +- .../main/kotlin/dev/diplomattest/somelib/RefList.kt | 2 +- .../dev/diplomattest/somelib/RefListParameter.kt | 2 +- .../kotlin/dev/diplomattest/somelib/ResultOpaque.kt | 10 +++++----- .../src/main/kotlin/dev/diplomattest/somelib/Two.kt | 2 +- .../kotlin/dev/diplomattest/somelib/Unnamespaced.kt | 2 +- .../main/kotlin/dev/diplomattest/somelib/Utf16Wrap.kt | 2 +- tool/src/kotlin/mod.rs | 11 +++++++++-- .../diplomat_tool__kotlin__test__opaque_gen.snap | 4 ++-- ...l__kotlin__test__opaque_gen_multiple_ref_args.snap | 4 ++-- ...ool__kotlin__test__opaque_gen_with_finalizers.snap | 4 ++-- tool/templates/kotlin/Opaque.kt.jinja | 2 +- tool/templates/kotlin/init.kt.jinja | 6 +++++- 36 files changed, 64 insertions(+), 45 deletions(-) diff --git a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/DataProvider.kt b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/DataProvider.kt index 8397c3886..001ac7558 100644 --- a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/DataProvider.kt +++ b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/DataProvider.kt @@ -20,7 +20,7 @@ class DataProvider internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -) { +): Exception("Rust error result for DataProvider") { internal class DataProviderCleaner(val handle: Pointer, val lib: DataProviderLib) : Runnable { override fun run() { diff --git a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimal.kt b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimal.kt index f3b6749d8..95b3e8862 100644 --- a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimal.kt +++ b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimal.kt @@ -19,7 +19,7 @@ class FixedDecimal internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -) { +): Exception("Rust error result for FixedDecimal") { internal class FixedDecimalCleaner(val handle: Pointer, val lib: FixedDecimalLib) : Runnable { override fun run() { diff --git a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatter.kt b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatter.kt index 3b616a121..649515b8b 100644 --- a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatter.kt +++ b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatter.kt @@ -20,7 +20,7 @@ class FixedDecimalFormatter internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -) { +): Exception("Rust error result for FixedDecimalFormatter") { internal class FixedDecimalFormatterCleaner(val handle: Pointer, val lib: FixedDecimalFormatterLib) : Runnable { override fun run() { diff --git a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Lib.kt b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Lib.kt index a04049934..482c104f2 100644 --- a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Lib.kt +++ b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Lib.kt @@ -331,10 +331,14 @@ internal fun T.ok(): Result { return Result.success(this) } -internal fun E.err(): Result { +internal fun E.primitive_err(): Result { return Result.failure(RuntimeException("Received error $this")) } +internal fun Throwable.err(): Result { + return Result.failure(this) +} + internal class ResultPointerUnitUnion: Union() { @JvmField internal var ok: Pointer = Pointer(0) diff --git a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Locale.kt b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Locale.kt index 9fac83be9..aa4eb8a6d 100644 --- a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Locale.kt +++ b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Locale.kt @@ -19,7 +19,7 @@ class Locale internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -) { +): Exception("Rust error result for Locale") { internal class LocaleCleaner(val handle: Pointer, val lib: LocaleLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/AttrOpaque1.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/AttrOpaque1.kt index 59113d09c..5033abc5e 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/AttrOpaque1.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/AttrOpaque1.kt @@ -20,7 +20,7 @@ class AttrOpaque1 internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -) { +): Exception("Rust error result for AttrOpaque1") { internal class AttrOpaque1Cleaner(val handle: Pointer, val lib: AttrOpaque1Lib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/AttrOpaque2.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/AttrOpaque2.kt index 5c4e200e8..9c960e650 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/AttrOpaque2.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/AttrOpaque2.kt @@ -15,7 +15,7 @@ class AttrOpaque2 internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -) { +): Exception("Rust error result for AttrOpaque2") { internal class AttrOpaque2Cleaner(val handle: Pointer, val lib: AttrOpaque2Lib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Bar.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Bar.kt index 5da4f1657..88786d663 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Bar.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Bar.kt @@ -18,7 +18,7 @@ class Bar internal constructor ( internal val selfEdges: List, internal val bEdges: List, internal val aEdges: List, -) { +): Exception("Rust error result for Bar") { internal class BarCleaner(val handle: Pointer, val lib: BarLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Float64Vec.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Float64Vec.kt index 90bc70ce3..3b1e180a9 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Float64Vec.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Float64Vec.kt @@ -28,7 +28,7 @@ class Float64Vec internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -) { +): Exception("Rust error result for Float64Vec") { internal class Float64VecCleaner(val handle: Pointer, val lib: Float64VecLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Foo.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Foo.kt index 01498fdad..5aa12ee8f 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Foo.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Foo.kt @@ -22,7 +22,7 @@ class Foo internal constructor ( // up by the garbage collector. internal val selfEdges: List, internal val aEdges: List, -) { +): Exception("Rust error result for Foo") { internal class FooCleaner(val handle: Pointer, val lib: FooLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Lib.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Lib.kt index 04522f675..281b67de2 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Lib.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Lib.kt @@ -331,10 +331,14 @@ internal fun T.ok(): Result { return Result.success(this) } -internal fun E.err(): Result { +internal fun E.primitive_err(): Result { return Result.failure(RuntimeException("Received error $this")) } +internal fun Throwable.err(): Result { + return Result.failure(this) +} + internal class ResultIntUnitUnion: Union() { @JvmField internal var ok: Int = 0 diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIndexer.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIndexer.kt index f1c4f7d56..bf6d8940a 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIndexer.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIndexer.kt @@ -16,7 +16,7 @@ class MyIndexer internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -) { +): Exception("Rust error result for MyIndexer") { internal class MyIndexerCleaner(val handle: Pointer, val lib: MyIndexerLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterable.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterable.kt index a230f021d..67f17c050 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterable.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterable.kt @@ -17,7 +17,7 @@ class MyIterable internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Iterable { +): Exception("Rust error result for MyIterable"): Iterable { internal class MyIterableCleaner(val handle: Pointer, val lib: MyIterableLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterator.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterator.kt index 13dab0793..202a08f46 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterator.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterator.kt @@ -18,7 +18,7 @@ class MyIterator internal constructor ( // up by the garbage collector. internal val selfEdges: List, internal val aEdges: List, -): Iterator { +): Exception("Rust error result for MyIterator"): Iterator { internal class MyIteratorCleaner(val handle: Pointer, val lib: MyIteratorLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyString.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyString.kt index 9badd69b9..c9b5521ee 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyString.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyString.kt @@ -23,7 +23,7 @@ class MyString internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -) { +): Exception("Rust error result for MyString") { internal class MyStringCleaner(val handle: Pointer, val lib: MyStringLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyStruct.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyStruct.kt index 4a6643322..b6f97fbb4 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyStruct.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyStruct.kt @@ -64,7 +64,7 @@ class MyStruct internal constructor ( if (returnVal.isOk == 1.toByte()) { return Unit.ok() } else { - return MyZst().err() + return MyZst().primitive_err() } } @@ -74,7 +74,7 @@ class MyStruct internal constructor ( if (returnVal.isOk == 1.toByte()) { return Unit.ok() } else { - return MyZst().err() + return MyZst().primitive_err() } } } diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/One.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/One.kt index 54a7a5e88..72a40b4d1 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/One.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/One.kt @@ -27,7 +27,7 @@ class One internal constructor ( // up by the garbage collector. internal val selfEdges: List, internal val aEdges: List, -) { +): Exception("Rust error result for One") { internal class OneCleaner(val handle: Pointer, val lib: OneLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Opaque.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Opaque.kt index 6270b33aa..fcb1f928f 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Opaque.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Opaque.kt @@ -23,7 +23,7 @@ class Opaque internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -) { +): Exception("Rust error result for Opaque") { internal class OpaqueCleaner(val handle: Pointer, val lib: OpaqueLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterable.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterable.kt index 3eb4da941..f47464daf 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterable.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterable.kt @@ -16,7 +16,7 @@ class OpaqueIterable internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Iterable { +): Exception("Rust error result for OpaqueIterable"): Iterable { internal class OpaqueIterableCleaner(val handle: Pointer, val lib: OpaqueIterableLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterator.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterator.kt index f869f06ac..1334211f2 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterator.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterator.kt @@ -18,7 +18,7 @@ class OpaqueIterator internal constructor ( // up by the garbage collector. internal val selfEdges: List, internal val aEdges: List, -): Iterator { +): Exception("Rust error result for OpaqueIterator"): Iterator { internal class OpaqueIteratorCleaner(val handle: Pointer, val lib: OpaqueIteratorLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueMutexedString.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueMutexedString.kt index dc2158d81..333bbfe57 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueMutexedString.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueMutexedString.kt @@ -23,7 +23,7 @@ class OpaqueMutexedString internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -) { +): Exception("Rust error result for OpaqueMutexedString") { internal class OpaqueMutexedStringCleaner(val handle: Pointer, val lib: OpaqueMutexedStringLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionOpaque.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionOpaque.kt index 71c8b93ab..2f9e5de41 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionOpaque.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionOpaque.kt @@ -26,7 +26,7 @@ class OptionOpaque internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -) { +): Exception("Rust error result for OptionOpaque") { internal class OptionOpaqueCleaner(val handle: Pointer, val lib: OptionOpaqueLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionOpaqueChar.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionOpaqueChar.kt index 1867aba60..439f0ae56 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionOpaqueChar.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionOpaqueChar.kt @@ -16,7 +16,7 @@ class OptionOpaqueChar internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -) { +): Exception("Rust error result for OptionOpaqueChar") { internal class OptionOpaqueCharCleaner(val handle: Pointer, val lib: OptionOpaqueCharLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionString.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionString.kt index 6e799de9c..f20e54fd8 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionString.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionString.kt @@ -18,7 +18,7 @@ class OptionString internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -) { +): Exception("Rust error result for OptionString") { internal class OptionStringCleaner(val handle: Pointer, val lib: OptionStringLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/RefList.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/RefList.kt index 4cac78c46..2d7481ae9 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/RefList.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/RefList.kt @@ -17,7 +17,7 @@ class RefList internal constructor ( // up by the garbage collector. internal val selfEdges: List, internal val aEdges: List, -) { +): Exception("Rust error result for RefList") { internal class RefListCleaner(val handle: Pointer, val lib: RefListLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/RefListParameter.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/RefListParameter.kt index 09e53d128..b1efbf1a2 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/RefListParameter.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/RefListParameter.kt @@ -15,7 +15,7 @@ class RefListParameter internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -) { +): Exception("Rust error result for RefListParameter") { internal class RefListParameterCleaner(val handle: Pointer, val lib: RefListParameterLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ResultOpaque.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ResultOpaque.kt index c9ab4e2cf..6c78f60d4 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ResultOpaque.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ResultOpaque.kt @@ -24,7 +24,7 @@ class ResultOpaque internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -) { +): Exception("Rust error result for ResultOpaque") { internal class ResultOpaqueCleaner(val handle: Pointer, val lib: ResultOpaqueLib) : Runnable { override fun run() { @@ -46,7 +46,7 @@ class ResultOpaque internal constructor ( CLEANER.register(returnOpaque, ResultOpaque.ResultOpaqueCleaner(handle, ResultOpaque.lib)); return returnOpaque.ok() } else { - return ErrorEnum.fromNative(returnVal.union.err).err() + return ErrorEnum.fromNative(returnVal.union.err).primitive_err() } } @@ -60,7 +60,7 @@ class ResultOpaque internal constructor ( CLEANER.register(returnOpaque, ResultOpaque.ResultOpaqueCleaner(handle, ResultOpaque.lib)); return returnOpaque.ok() } else { - return ErrorEnum.fromNative(returnVal.union.err).err() + return ErrorEnum.fromNative(returnVal.union.err).primitive_err() } } @@ -74,7 +74,7 @@ class ResultOpaque internal constructor ( CLEANER.register(returnOpaque, ResultOpaque.ResultOpaqueCleaner(handle, ResultOpaque.lib)); return returnOpaque.ok() } else { - return ErrorEnum.fromNative(returnVal.union.err).err() + return ErrorEnum.fromNative(returnVal.union.err).primitive_err() } } @@ -104,7 +104,7 @@ class ResultOpaque internal constructor ( } else { val returnStruct = ErrorStruct(returnVal.union.err) - return returnStruct.err() + return returnStruct.primitive_err() } } diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Two.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Two.kt index 3b101adfc..a2e557159 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Two.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Two.kt @@ -17,7 +17,7 @@ class Two internal constructor ( internal val selfEdges: List, internal val aEdges: List, internal val bEdges: List, -) { +): Exception("Rust error result for Two") { internal class TwoCleaner(val handle: Pointer, val lib: TwoLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Unnamespaced.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Unnamespaced.kt index fa1cdab32..44d49c8db 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Unnamespaced.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Unnamespaced.kt @@ -17,7 +17,7 @@ class Unnamespaced internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -) { +): Exception("Rust error result for Unnamespaced") { internal class UnnamespacedCleaner(val handle: Pointer, val lib: UnnamespacedLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Utf16Wrap.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Utf16Wrap.kt index 0f12309c3..7e3dcd68b 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Utf16Wrap.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Utf16Wrap.kt @@ -18,7 +18,7 @@ class Utf16Wrap internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -) { +): Exception("Rust error result for Utf16Wrap") { internal class Utf16WrapCleaner(val handle: Pointer, val lib: Utf16WrapLib) : Runnable { override fun run() { diff --git a/tool/src/kotlin/mod.rs b/tool/src/kotlin/mod.rs index 771c93ecd..996e6556a 100644 --- a/tool/src/kotlin/mod.rs +++ b/tool/src/kotlin/mod.rs @@ -850,17 +850,24 @@ val intermediateOption = {val_name}.option() ?: return null let err_path = err .as_ref() .map(|err| { + let err_converter = + if let OutType::Opaque(OpaquePath { tcx_id: _my_id, .. }) = err { + ".err()" + } else { + ".primitive_err()" + }; + self.gen_out_type_return_conversion( method, &method_lifetimes_map, cleanups, "returnVal.union.err", - ".err()", + err_converter, err, use_finalizers_not_cleaners, ) }) - .unwrap_or_else(|| "return Unit.err()".into()); + .unwrap_or_else(|| "return Unit.primitive_err()".into()); #[derive(Template)] #[template(path = "kotlin/ResultReturn.kt.jinja", escape = "none")] diff --git a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen.snap b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen.snap index 5f8583db2..9056a8d4a 100644 --- a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen.snap +++ b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen.snap @@ -1,6 +1,6 @@ --- source: tool/src/kotlin/mod.rs -assertion_line: 1995 +assertion_line: 2326 expression: result --- package dev.gigapixel.somelib; @@ -22,7 +22,7 @@ class BorrowWrapper internal constructor ( internal val selfEdges: List, internal val aEdges: List, internal val bEdges: List, -) { +): Exception("Rust error result for BorrowWrapper") { internal class BorrowWrapperCleaner(val handle: Pointer, val lib: BorrowWrapperLib) : Runnable { override fun run() { diff --git a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen_multiple_ref_args.snap b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen_multiple_ref_args.snap index 37b3f5184..0eaea4039 100644 --- a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen_multiple_ref_args.snap +++ b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen_multiple_ref_args.snap @@ -1,6 +1,6 @@ --- source: tool/src/kotlin/mod.rs -assertion_line: 2174 +assertion_line: 2217 expression: result --- package dev.gigapixel.somelib; @@ -21,7 +21,7 @@ class AnotherOpaque internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -) { +): Exception("Rust error result for AnotherOpaque") { internal class AnotherOpaqueCleaner(val handle: Pointer, val lib: AnotherOpaqueLib) : Runnable { override fun run() { diff --git a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen_with_finalizers.snap b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen_with_finalizers.snap index 6158e8ae0..06f25e2a2 100644 --- a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen_with_finalizers.snap +++ b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen_with_finalizers.snap @@ -1,6 +1,6 @@ --- source: tool/src/kotlin/mod.rs -assertion_line: 2328 +assertion_line: 2377 expression: result --- package dev.gigapixel.somelib; @@ -24,7 +24,7 @@ class MyOpaqueStruct internal constructor ( internal val selfEdges: List, internal val bEdges: List, internal var finalizer_registered: Boolean = false, -) { +): Exception("Rust error result for MyOpaqueStruct") { fun registerFinalizer() { this.finalizer_registered = true } diff --git a/tool/templates/kotlin/Opaque.kt.jinja b/tool/templates/kotlin/Opaque.kt.jinja index d90d72b48..fe7409b04 100644 --- a/tool/templates/kotlin/Opaque.kt.jinja +++ b/tool/templates/kotlin/Opaque.kt.jinja @@ -41,7 +41,7 @@ class {{type_name}} internal constructor ( {%- if use_finalizers_not_cleaners %} internal var finalizer_registered: Boolean = false, {%- endif %} -) +): Exception("Rust error result for {{type_name}}") {%- if special_methods.interfaces.is_empty() %} {% else %}: {% for interface in special_methods.interfaces %} {%- if loop.first %}{% else %}, {% endif %}{{interface}} {%- endfor %} diff --git a/tool/templates/kotlin/init.kt.jinja b/tool/templates/kotlin/init.kt.jinja index 072ed77c6..34f508ecb 100644 --- a/tool/templates/kotlin/init.kt.jinja +++ b/tool/templates/kotlin/init.kt.jinja @@ -333,10 +333,14 @@ internal fun T.ok(): Result { return Result.success(this) } -internal fun E.err(): Result { +internal fun E.primitive_err(): Result { return Result.failure(RuntimeException("Received error $this")) } +internal fun Throwable.err(): Result { + return Result.failure(this) +} + {% for native_result in native_results -%} {{native_result}} {% endfor %} From b42dea56811d0341a26aabcb29ba3dbedd77860b Mon Sep 17 00:00:00 2001 From: Ellen Arteca Date: Thu, 5 Dec 2024 18:31:25 -0800 Subject: [PATCH 2/9] adding same support for throwable structs --- .../main/kotlin/dev/diplomattest/somelib/DataProvider.kt | 2 +- .../main/kotlin/dev/diplomattest/somelib/FixedDecimal.kt | 2 +- .../dev/diplomattest/somelib/FixedDecimalFormatter.kt | 2 +- .../diplomattest/somelib/FixedDecimalFormatterOptions.kt | 2 +- .../main/kotlin/dev/diplomattest/somelib/BorrowedFields.kt | 2 +- .../dev/diplomattest/somelib/BorrowedFieldsReturning.kt | 2 +- .../dev/diplomattest/somelib/BorrowedFieldsWithBounds.kt | 2 +- .../dev/diplomattest/somelib/CallbackTestingStruct.kt | 2 +- .../kotlin/dev/diplomattest/somelib/CallbackWrapper.kt | 2 +- .../main/kotlin/dev/diplomattest/somelib/CyclicStructA.kt | 2 +- .../main/kotlin/dev/diplomattest/somelib/CyclicStructB.kt | 2 +- .../main/kotlin/dev/diplomattest/somelib/CyclicStructC.kt | 2 +- .../main/kotlin/dev/diplomattest/somelib/ErrorStruct.kt | 2 +- .../main/kotlin/dev/diplomattest/somelib/ImportedStruct.kt | 2 +- .../src/main/kotlin/dev/diplomattest/somelib/MyStruct.kt | 6 +++--- .../src/main/kotlin/dev/diplomattest/somelib/MyZst.kt | 2 +- .../dev/diplomattest/somelib/NestedBorrowedFields.kt | 2 +- .../main/kotlin/dev/diplomattest/somelib/OptionString.kt | 2 +- .../main/kotlin/dev/diplomattest/somelib/OptionStruct.kt | 2 +- .../main/kotlin/dev/diplomattest/somelib/ResultOpaque.kt | 6 +++--- .../kotlin/dev/diplomattest/somelib/TraitTestingStruct.kt | 2 +- .../main/kotlin/dev/diplomattest/somelib/TraitWrapper.kt | 2 +- tool/src/kotlin/mod.rs | 2 +- .../snapshots/diplomat_tool__kotlin__test__struct.snap | 7 ++++--- tool/templates/kotlin/Struct.kt.jinja | 2 +- 25 files changed, 32 insertions(+), 31 deletions(-) diff --git a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/DataProvider.kt b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/DataProvider.kt index 001ac7558..aff58008d 100644 --- a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/DataProvider.kt +++ b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/DataProvider.kt @@ -52,7 +52,7 @@ class DataProvider internal constructor ( if (returnVal.isOk == 1.toByte()) { return Unit.ok() } else { - return Unit.err() + return Unit.primitive_err() } } } diff --git a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimal.kt b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimal.kt index 95b3e8862..35f2006bd 100644 --- a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimal.kt +++ b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimal.kt @@ -66,7 +66,7 @@ class FixedDecimal internal constructor ( val returnString = DW.writeToString(write) return returnString.ok() } else { - return Unit.err() + return Unit.primitive_err() } } diff --git a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatter.kt b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatter.kt index 649515b8b..d157b4038 100644 --- a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatter.kt +++ b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatter.kt @@ -46,7 +46,7 @@ class FixedDecimalFormatter internal constructor ( CLEANER.register(returnOpaque, FixedDecimalFormatter.FixedDecimalFormatterCleaner(handle, FixedDecimalFormatter.lib)); return returnOpaque.ok() } else { - return Unit.err() + return Unit.primitive_err() } } } diff --git a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatterOptions.kt b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatterOptions.kt index 6cb390c02..482d67ee1 100644 --- a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatterOptions.kt +++ b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatterOptions.kt @@ -23,7 +23,7 @@ internal class FixedDecimalFormatterOptionsNative: Structure(), Structure.ByValu } class FixedDecimalFormatterOptions internal constructor ( - internal val nativeStruct: FixedDecimalFormatterOptionsNative) { + internal val nativeStruct: FixedDecimalFormatterOptionsNative): Exception("Rust error result for FixedDecimalFormatterOptions") { val groupingStrategy: FixedDecimalGroupingStrategy = FixedDecimalGroupingStrategy.fromNative(nativeStruct.groupingStrategy) val someOtherConfig: Boolean = nativeStruct.someOtherConfig > 0 diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFields.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFields.kt index d950aebbb..d6f2dd3b2 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFields.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFields.kt @@ -27,7 +27,7 @@ internal class BorrowedFieldsNative: Structure(), Structure.ByValue { class BorrowedFields internal constructor ( internal val nativeStruct: BorrowedFieldsNative, internal val aEdges: List - ) { + ): Exception("Rust error result for BorrowedFields") { val a: String = PrimitiveArrayTools.getUtf16(nativeStruct.a) val b: String = PrimitiveArrayTools.getUtf8(nativeStruct.b) val c: String = PrimitiveArrayTools.getUtf8(nativeStruct.c) diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFieldsReturning.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFieldsReturning.kt index 86d0708bc..46d3cc341 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFieldsReturning.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFieldsReturning.kt @@ -22,7 +22,7 @@ internal class BorrowedFieldsReturningNative: Structure(), Structure.ByValue { class BorrowedFieldsReturning internal constructor ( internal val nativeStruct: BorrowedFieldsReturningNative, internal val aEdges: List - ) { + ): Exception("Rust error result for BorrowedFieldsReturning") { val bytes: String = PrimitiveArrayTools.getUtf8(nativeStruct.bytes) companion object { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFieldsWithBounds.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFieldsWithBounds.kt index 921527f71..02b479b83 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFieldsWithBounds.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFieldsWithBounds.kt @@ -29,7 +29,7 @@ class BorrowedFieldsWithBounds internal constructor ( internal val aEdges: List, internal val bEdges: List, internal val cEdges: List - ) { + ): Exception("Rust error result for BorrowedFieldsWithBounds") { val fieldA: String = PrimitiveArrayTools.getUtf16(nativeStruct.fieldA) val fieldB: String = PrimitiveArrayTools.getUtf8(nativeStruct.fieldB) val fieldC: String = PrimitiveArrayTools.getUtf8(nativeStruct.fieldC) diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CallbackTestingStruct.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CallbackTestingStruct.kt index 568ace7c7..010b98a36 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CallbackTestingStruct.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CallbackTestingStruct.kt @@ -22,7 +22,7 @@ internal class CallbackTestingStructNative: Structure(), Structure.ByValue { } class CallbackTestingStruct internal constructor ( - internal val nativeStruct: CallbackTestingStructNative) { + internal val nativeStruct: CallbackTestingStructNative): Exception("Rust error result for CallbackTestingStruct") { val x: Int = nativeStruct.x val y: Int = nativeStruct.y diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CallbackWrapper.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CallbackWrapper.kt index f95e51c18..0a00ae343 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CallbackWrapper.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CallbackWrapper.kt @@ -270,7 +270,7 @@ internal class DiplomatCallback_CallbackWrapper_test_multiple_cb_args_diplomatCa } } class CallbackWrapper internal constructor ( - internal val nativeStruct: CallbackWrapperNative) { + internal val nativeStruct: CallbackWrapperNative): Exception("Rust error result for CallbackWrapper") { val cantBeEmpty: Boolean = nativeStruct.cantBeEmpty > 0 companion object { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructA.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructA.kt index 43db22176..a660974eb 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructA.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructA.kt @@ -22,7 +22,7 @@ internal class CyclicStructANative: Structure(), Structure.ByValue { } class CyclicStructA internal constructor ( - internal val nativeStruct: CyclicStructANative) { + internal val nativeStruct: CyclicStructANative): Exception("Rust error result for CyclicStructA") { val a: CyclicStructB = CyclicStructB(nativeStruct.a) companion object { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructB.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructB.kt index 87bb2a3ff..b898cde1c 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructB.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructB.kt @@ -21,7 +21,7 @@ internal class CyclicStructBNative: Structure(), Structure.ByValue { } class CyclicStructB internal constructor ( - internal val nativeStruct: CyclicStructBNative) { + internal val nativeStruct: CyclicStructBNative): Exception("Rust error result for CyclicStructB") { val field: UByte = nativeStruct.field.toUByte() companion object { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructC.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructC.kt index b55f7c053..bc12d2f2d 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructC.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructC.kt @@ -21,7 +21,7 @@ internal class CyclicStructCNative: Structure(), Structure.ByValue { } class CyclicStructC internal constructor ( - internal val nativeStruct: CyclicStructCNative) { + internal val nativeStruct: CyclicStructCNative): Exception("Rust error result for CyclicStructC") { val a: CyclicStructA = CyclicStructA(nativeStruct.a) companion object { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ErrorStruct.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ErrorStruct.kt index 3bbd89b73..f524466d5 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ErrorStruct.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ErrorStruct.kt @@ -22,7 +22,7 @@ internal class ErrorStructNative: Structure(), Structure.ByValue { } class ErrorStruct internal constructor ( - internal val nativeStruct: ErrorStructNative) { + internal val nativeStruct: ErrorStructNative): Exception("Rust error result for ErrorStruct") { val i: Int = nativeStruct.i val j: Int = nativeStruct.j diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ImportedStruct.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ImportedStruct.kt index 27a704a49..0abfb0685 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ImportedStruct.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ImportedStruct.kt @@ -22,7 +22,7 @@ internal class ImportedStructNative: Structure(), Structure.ByValue { } class ImportedStruct internal constructor ( - internal val nativeStruct: ImportedStructNative) { + internal val nativeStruct: ImportedStructNative): Exception("Rust error result for ImportedStruct") { val foo: UnimportedEnum = UnimportedEnum.fromNative(nativeStruct.foo) val count: UByte = nativeStruct.count.toUByte() diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyStruct.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyStruct.kt index b6f97fbb4..c03442e8f 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyStruct.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyStruct.kt @@ -36,7 +36,7 @@ internal class MyStructNative: Structure(), Structure.ByValue { } class MyStruct internal constructor ( - internal val nativeStruct: MyStructNative) { + internal val nativeStruct: MyStructNative): Exception("Rust error result for MyStruct") { val a: UByte = nativeStruct.a.toUByte() val b: Boolean = nativeStruct.b > 0 val c: UByte = nativeStruct.c.toUByte() @@ -64,7 +64,7 @@ class MyStruct internal constructor ( if (returnVal.isOk == 1.toByte()) { return Unit.ok() } else { - return MyZst().primitive_err() + return MyZst().err() } } @@ -74,7 +74,7 @@ class MyStruct internal constructor ( if (returnVal.isOk == 1.toByte()) { return Unit.ok() } else { - return MyZst().primitive_err() + return MyZst().err() } } } diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyZst.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyZst.kt index 182df4327..e2d76ccac 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyZst.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyZst.kt @@ -7,6 +7,6 @@ import com.sun.jna.Pointer import com.sun.jna.Structure class MyZst internal constructor ( - ) { + ): Exception("Rust error result for MyZst") { } diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/NestedBorrowedFields.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/NestedBorrowedFields.kt index c686c467d..16b2484ef 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/NestedBorrowedFields.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/NestedBorrowedFields.kt @@ -29,7 +29,7 @@ class NestedBorrowedFields internal constructor ( internal val xEdges: List, internal val yEdges: List, internal val zEdges: List - ) { + ): Exception("Rust error result for NestedBorrowedFields") { val fields: BorrowedFields = BorrowedFields(nativeStruct.fields, xEdges) val bounds: BorrowedFieldsWithBounds = BorrowedFieldsWithBounds(nativeStruct.bounds, xEdges, yEdges, yEdges) val bounds2: BorrowedFieldsWithBounds = BorrowedFieldsWithBounds(nativeStruct.bounds2, zEdges, zEdges, zEdges) diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionString.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionString.kt index f20e54fd8..9601cb879 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionString.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionString.kt @@ -51,7 +51,7 @@ class OptionString internal constructor ( val returnString = DW.writeToString(write) return returnString.ok() } else { - return Unit.err() + return Unit.primitive_err() } } diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionStruct.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionStruct.kt index aa0395c1b..7652c3261 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionStruct.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionStruct.kt @@ -26,7 +26,7 @@ internal class OptionStructNative: Structure(), Structure.ByValue { } class OptionStruct internal constructor ( - internal val nativeStruct: OptionStructNative) { + internal val nativeStruct: OptionStructNative): Exception("Rust error result for OptionStruct") { val a: OptionOpaque? = if (nativeStruct.a == null) { null } else { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ResultOpaque.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ResultOpaque.kt index 6c78f60d4..83e665b14 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ResultOpaque.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ResultOpaque.kt @@ -88,7 +88,7 @@ class ResultOpaque internal constructor ( CLEANER.register(returnOpaque, ResultOpaque.ResultOpaqueCleaner(handle, ResultOpaque.lib)); return returnOpaque.ok() } else { - return Unit.err() + return Unit.primitive_err() } } @@ -104,7 +104,7 @@ class ResultOpaque internal constructor ( } else { val returnStruct = ErrorStruct(returnVal.union.err) - return returnStruct.primitive_err() + return returnStruct.err() } } @@ -128,7 +128,7 @@ class ResultOpaque internal constructor ( if (returnVal.isOk == 1.toByte()) { return (returnVal.union.ok).ok() } else { - return Unit.err() + return Unit.primitive_err() } } diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/TraitTestingStruct.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/TraitTestingStruct.kt index bf96466be..9c8f9b44b 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/TraitTestingStruct.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/TraitTestingStruct.kt @@ -22,7 +22,7 @@ internal class TraitTestingStructNative: Structure(), Structure.ByValue { } class TraitTestingStruct internal constructor ( - internal val nativeStruct: TraitTestingStructNative) { + internal val nativeStruct: TraitTestingStructNative): Exception("Rust error result for TraitTestingStruct") { val x: Int = nativeStruct.x val y: Int = nativeStruct.y diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/TraitWrapper.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/TraitWrapper.kt index 7baeb3986..47be186f7 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/TraitWrapper.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/TraitWrapper.kt @@ -22,7 +22,7 @@ internal class TraitWrapperNative: Structure(), Structure.ByValue { } class TraitWrapper internal constructor ( - internal val nativeStruct: TraitWrapperNative) { + internal val nativeStruct: TraitWrapperNative): Exception("Rust error result for TraitWrapper") { val cantBeEmpty: Boolean = nativeStruct.cantBeEmpty > 0 companion object { diff --git a/tool/src/kotlin/mod.rs b/tool/src/kotlin/mod.rs index 996e6556a..b8eca8348 100644 --- a/tool/src/kotlin/mod.rs +++ b/tool/src/kotlin/mod.rs @@ -851,7 +851,7 @@ val intermediateOption = {val_name}.option() ?: return null .as_ref() .map(|err| { let err_converter = - if let OutType::Opaque(OpaquePath { tcx_id: _my_id, .. }) = err { + if let OutType::Opaque(..) | OutType::Struct(..) = err { ".err()" } else { ".primitive_err()" diff --git a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__struct.snap b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__struct.snap index 03edb42e7..c793ca2b2 100644 --- a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__struct.snap +++ b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__struct.snap @@ -1,5 +1,6 @@ --- source: tool/src/kotlin/mod.rs +assertion_line: 2165 expression: struct_code --- package dev.gigapixel.somelib @@ -111,7 +112,7 @@ internal class DiplomatCallback_MyNativeStruct_test_multi_arg_callback_diplomatC class MyNativeStruct internal constructor ( internal val nativeStruct: MyNativeStructNative, internal val bEdges: List - ) { + ): Exception("Rust error result for MyNativeStruct") { val a: Boolean = nativeStruct.a > 0 val b: Byte = nativeStruct.b val c: UByte = nativeStruct.c.toUByte() @@ -159,7 +160,7 @@ class MyNativeStruct internal constructor ( if (returnVal.isOk == 1.toByte()) { return (returnVal.union.ok > 0).ok() } else { - return Unit.err() + return Unit.primitive_err() } } @@ -169,7 +170,7 @@ class MyNativeStruct internal constructor ( if (returnVal.isOk == 1.toByte()) { return (returnVal.union.ok.toUByte()).ok() } else { - return Unit.err() + return Unit.primitive_err() } } } diff --git a/tool/templates/kotlin/Struct.kt.jinja b/tool/templates/kotlin/Struct.kt.jinja index 19730feca..08990fae2 100644 --- a/tool/templates/kotlin/Struct.kt.jinja +++ b/tool/templates/kotlin/Struct.kt.jinja @@ -50,7 +50,7 @@ class {{type_name}} internal constructor ( internal val {{lt}}Edges: List{% if !loop.last%},{% endif %} {%- endfor %} {% endif -%} - ) { + ): Exception("Rust error result for {{type_name}}") { {%- for field in fields %} val {{field.name}}: {{field.field_type}} = {{field.native_to_kt}} From 4b00a85e5c8c19da34b4bb89e560fdc42c3d6a38 Mon Sep 17 00:00:00 2001 From: Ellen Arteca Date: Thu, 5 Dec 2024 18:40:27 -0800 Subject: [PATCH 3/9] clippy --- tool/src/kotlin/mod.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tool/src/kotlin/mod.rs b/tool/src/kotlin/mod.rs index b8eca8348..6f743071d 100644 --- a/tool/src/kotlin/mod.rs +++ b/tool/src/kotlin/mod.rs @@ -850,12 +850,11 @@ val intermediateOption = {val_name}.option() ?: return null let err_path = err .as_ref() .map(|err| { - let err_converter = - if let OutType::Opaque(..) | OutType::Struct(..) = err { - ".err()" - } else { - ".primitive_err()" - }; + let err_converter = if let OutType::Opaque(..) | OutType::Struct(..) = err { + ".err()" + } else { + ".primitive_err()" + }; self.gen_out_type_return_conversion( method, From 221d8626df04bee2f08508e0b7202c9dc1e1e19b Mon Sep 17 00:00:00 2001 From: Ellen Arteca Date: Thu, 5 Dec 2024 18:47:58 -0800 Subject: [PATCH 4/9] fixing multiple inheritance --- core/src/hir/type_context.rs | 4 ++-- .../src/main/kotlin/dev/diplomattest/somelib/MyIterable.kt | 2 +- .../src/main/kotlin/dev/diplomattest/somelib/MyIterator.kt | 2 +- .../main/kotlin/dev/diplomattest/somelib/OpaqueIterable.kt | 2 +- .../main/kotlin/dev/diplomattest/somelib/OpaqueIterator.kt | 2 +- tool/templates/kotlin/Opaque.kt.jinja | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/core/src/hir/type_context.rs b/core/src/hir/type_context.rs index 747261783..7c7fc3c41 100644 --- a/core/src/hir/type_context.rs +++ b/core/src/hir/type_context.rs @@ -103,7 +103,7 @@ impl TypeContext { ) } - pub fn all_traits<'tcx>(&'tcx self) -> impl Iterator { + pub fn all_traits<'tcx>(&'tcx self) -> impl Iterator { self.traits .iter() .enumerate() @@ -203,7 +203,7 @@ impl TypeContext { pub(super) fn from_ast_without_validation<'ast>( env: &'ast Env, attr_validator: impl AttributeValidator + 'static, - ) -> Result<(LoweringContext, Self), Vec> { + ) -> Result<(LoweringContext<'ast>, Self), Vec> { let mut ast_out_structs = SmallVec::<[_; 16]>::new(); let mut ast_structs = SmallVec::<[_; 16]>::new(); let mut ast_opaques = SmallVec::<[_; 16]>::new(); diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterable.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterable.kt index 67f17c050..50e77e50c 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterable.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterable.kt @@ -17,7 +17,7 @@ class MyIterable internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for MyIterable"): Iterable { +): Exception("Rust error result for MyIterable"), Iterable { internal class MyIterableCleaner(val handle: Pointer, val lib: MyIterableLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterator.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterator.kt index 202a08f46..f702ae464 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterator.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterator.kt @@ -18,7 +18,7 @@ class MyIterator internal constructor ( // up by the garbage collector. internal val selfEdges: List, internal val aEdges: List, -): Exception("Rust error result for MyIterator"): Iterator { +): Exception("Rust error result for MyIterator"), Iterator { internal class MyIteratorCleaner(val handle: Pointer, val lib: MyIteratorLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterable.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterable.kt index f47464daf..e157aa904 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterable.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterable.kt @@ -16,7 +16,7 @@ class OpaqueIterable internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for OpaqueIterable"): Iterable { +): Exception("Rust error result for OpaqueIterable"), Iterable { internal class OpaqueIterableCleaner(val handle: Pointer, val lib: OpaqueIterableLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterator.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterator.kt index 1334211f2..c81d0e19c 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterator.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterator.kt @@ -18,7 +18,7 @@ class OpaqueIterator internal constructor ( // up by the garbage collector. internal val selfEdges: List, internal val aEdges: List, -): Exception("Rust error result for OpaqueIterator"): Iterator { +): Exception("Rust error result for OpaqueIterator"), Iterator { internal class OpaqueIteratorCleaner(val handle: Pointer, val lib: OpaqueIteratorLib) : Runnable { override fun run() { diff --git a/tool/templates/kotlin/Opaque.kt.jinja b/tool/templates/kotlin/Opaque.kt.jinja index fe7409b04..1fd4274c9 100644 --- a/tool/templates/kotlin/Opaque.kt.jinja +++ b/tool/templates/kotlin/Opaque.kt.jinja @@ -42,7 +42,7 @@ class {{type_name}} internal constructor ( internal var finalizer_registered: Boolean = false, {%- endif %} ): Exception("Rust error result for {{type_name}}") -{%- if special_methods.interfaces.is_empty() %} {% else %}: {% for interface in special_methods.interfaces %} +{%- if special_methods.interfaces.is_empty() %} {% else %}, {% for interface in special_methods.interfaces %} {%- if loop.first %}{% else %}, {% endif %}{{interface}} {%- endfor %} {%- endif %} { From 0e8a2478c3300e5a08c0a5736d600e75203d4af5 Mon Sep 17 00:00:00 2001 From: Ellen Arteca Date: Thu, 5 Dec 2024 18:55:10 -0800 Subject: [PATCH 5/9] moooore clippy --- tool/src/c/ty.rs | 2 +- tool/src/cpp/ty.rs | 2 +- tool/src/dart/mod.rs | 2 +- tool/src/demo_gen/terminus.rs | 2 +- tool/src/js/converter.rs | 2 +- tool/src/js/gen.rs | 2 +- tool/src/kotlin/mod.rs | 2 +- tool/src/lib.rs | 4 ++-- 8 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tool/src/c/ty.rs b/tool/src/c/ty.rs index be3e16929..744838f2c 100644 --- a/tool/src/c/ty.rs +++ b/tool/src/c/ty.rs @@ -78,7 +78,7 @@ pub struct TyGenContext<'cx, 'tcx> { pub impl_header_path: &'cx String, } -impl<'cx, 'tcx> TyGenContext<'cx, 'tcx> { +impl<'tcx> TyGenContext<'_, 'tcx> { pub fn gen_enum_def(&self, def: &'tcx hir::EnumDef) -> Header { let mut decl_header = Header::new(self.decl_header_path.clone(), self.is_for_cpp); let ty_name = self.formatter.fmt_type_name(self.id.try_into().unwrap()); diff --git a/tool/src/cpp/ty.rs b/tool/src/cpp/ty.rs index 73af006a5..92e774e6e 100644 --- a/tool/src/cpp/ty.rs +++ b/tool/src/cpp/ty.rs @@ -61,7 +61,7 @@ pub(super) struct TyGenContext<'ccx, 'tcx, 'header> { pub generating_struct_fields: bool, } -impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> { +impl<'ccx, 'tcx: 'ccx> TyGenContext<'ccx, 'tcx, '_> { /// Adds an enum definition to the current decl and impl headers. /// /// The enum is defined in C++ using a `class` with a single private field that is the diff --git a/tool/src/dart/mod.rs b/tool/src/dart/mod.rs index dc1baeff6..c4a524422 100644 --- a/tool/src/dart/mod.rs +++ b/tool/src/dart/mod.rs @@ -146,7 +146,7 @@ struct TyGenContext<'a, 'cx> { helper_classes: &'a mut BTreeMap, } -impl<'a, 'cx> TyGenContext<'a, 'cx> { +impl<'cx> TyGenContext<'_, 'cx> { fn gen(&mut self, id: TypeId) -> (String, String) { let ty = self.tcx.resolve_type(id); diff --git a/tool/src/demo_gen/terminus.rs b/tool/src/demo_gen/terminus.rs index 1169b2f07..e9e621cda 100644 --- a/tool/src/demo_gen/terminus.rs +++ b/tool/src/demo_gen/terminus.rs @@ -144,7 +144,7 @@ pub(super) struct TerminusInfo { pub imports: BTreeSet, } -impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> { +impl RenderTerminusContext<'_, '_> { /// See [`TerminusInfo`] for more information on termini. /// /// Right now, we only check for the existence of `&mut DiplomatWrite` in the function parameters to determine a valid render termini. diff --git a/tool/src/js/converter.rs b/tool/src/js/converter.rs index 7a3b74b2d..616d59262 100644 --- a/tool/src/js/converter.rs +++ b/tool/src/js/converter.rs @@ -58,7 +58,7 @@ pub(super) enum JsToCConversionContext { WriteToBuffer(&'static str, usize), } -impl<'jsctx, 'tcx> TyGenContext<'jsctx, 'tcx> { +impl<'tcx> TyGenContext<'_, 'tcx> { // #region C to JS /// Given a type from Rust, convert it into something Typescript will understand. /// We use this to double-check our Javascript work as well. diff --git a/tool/src/js/gen.rs b/tool/src/js/gen.rs index ae3bb1001..12413817d 100644 --- a/tool/src/js/gen.rs +++ b/tool/src/js/gen.rs @@ -33,7 +33,7 @@ pub(super) struct TyGenContext<'ctx, 'tcx> { pub imports: RefCell>, } -impl<'ctx, 'tcx> TyGenContext<'ctx, 'tcx> { +impl<'tcx> TyGenContext<'_, 'tcx> { /// Generates the code at the top of every `.d.ts` and `.mjs` file. /// /// This could easily be an [inherited template](https://djc.github.io/askama/template_syntax.html#template-inheritance), if you want to be a little more strict about how templates are used. diff --git a/tool/src/kotlin/mod.rs b/tool/src/kotlin/mod.rs index 6f743071d..f3b2e7ae2 100644 --- a/tool/src/kotlin/mod.rs +++ b/tool/src/kotlin/mod.rs @@ -261,7 +261,7 @@ struct TyGenContext<'a, 'cx> { callback_params: &'a mut Vec, } -impl<'a, 'cx> TyGenContext<'a, 'cx> { +impl<'cx> TyGenContext<'_, 'cx> { fn gen_infallible_return_type_name(&self, success_type: &SuccessType) -> Cow<'cx, str> { match success_type { SuccessType::Unit => self.formatter.fmt_void().into(), diff --git a/tool/src/lib.rs b/tool/src/lib.rs index dbf7b714d..5dae5355e 100644 --- a/tool/src/lib.rs +++ b/tool/src/lib.rs @@ -211,7 +211,7 @@ struct ErrorContext<'tcx> { method: Option>, } -impl<'tcx> fmt::Display for ErrorContext<'tcx> { +impl fmt::Display for ErrorContext<'_> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let ty = &self.ty; if let Some(ref method) = self.method { @@ -226,7 +226,7 @@ impl<'tcx> fmt::Display for ErrorContext<'tcx> { #[must_use] pub struct ErrorContextGuard<'a, 'tcx, E>(&'a ErrorStore<'tcx, E>, ErrorContext<'tcx>); -impl<'a, 'tcx, E> Drop for ErrorContextGuard<'a, 'tcx, E> { +impl Drop for ErrorContextGuard<'_, '_, E> { fn drop(&mut self) { let _ = mem::replace(&mut *self.0.context.borrow_mut(), mem::take(&mut self.1)); } From 8e99fb06fe7a9bac67442f15fe20c7c1154d8703 Mon Sep 17 00:00:00 2001 From: Ellen Arteca Date: Thu, 5 Dec 2024 18:57:21 -0800 Subject: [PATCH 6/9] test fix --- .../kotlin/dev/diplomattest/somelib/ResultOpaqueTest.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/feature_tests/kotlin/somelib/src/test/kotlin/dev/diplomattest/somelib/ResultOpaqueTest.kt b/feature_tests/kotlin/somelib/src/test/kotlin/dev/diplomattest/somelib/ResultOpaqueTest.kt index fc795dbab..6171fb216 100644 --- a/feature_tests/kotlin/somelib/src/test/kotlin/dev/diplomattest/somelib/ResultOpaqueTest.kt +++ b/feature_tests/kotlin/somelib/src/test/kotlin/dev/diplomattest/somelib/ResultOpaqueTest.kt @@ -16,7 +16,7 @@ class ResultOpaqueTest { assert(resultOpaque2.isFailure) val result2 = resultOpaque2.exceptionOrNull()?.message - val shouldRes: Result = ErrorEnum.Bar.err() + val shouldRes: Result = ErrorEnum.Bar.primitive_err() assertEquals(result2, shouldRes.exceptionOrNull()?.message) @@ -24,7 +24,7 @@ class ResultOpaqueTest { val resultOpaque3 = ResultOpaque.newFailingFoo() assert(resultOpaque3.isFailure) val result3 = resultOpaque3.exceptionOrNull()?.message - val shouldRes3: Result = ErrorEnum.Foo.err() + val shouldRes3: Result = ErrorEnum.Foo.primitive_err() assertEquals(result3, shouldRes3.exceptionOrNull()?.message) val resultOpaque4 = ResultOpaque.newInErr(8) @@ -34,4 +34,4 @@ class ResultOpaqueTest { assert(assertion == true) } -} \ No newline at end of file +} From 4e87935d5fa16a44f1406c89898771514cae6d1b Mon Sep 17 00:00:00 2001 From: Ellen Arteca Date: Thu, 5 Dec 2024 19:04:27 -0800 Subject: [PATCH 7/9] new error log --- .../test/kotlin/dev/diplomattest/somelib/ResultOpaqueTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/feature_tests/kotlin/somelib/src/test/kotlin/dev/diplomattest/somelib/ResultOpaqueTest.kt b/feature_tests/kotlin/somelib/src/test/kotlin/dev/diplomattest/somelib/ResultOpaqueTest.kt index 6171fb216..66e8860f7 100644 --- a/feature_tests/kotlin/somelib/src/test/kotlin/dev/diplomattest/somelib/ResultOpaqueTest.kt +++ b/feature_tests/kotlin/somelib/src/test/kotlin/dev/diplomattest/somelib/ResultOpaqueTest.kt @@ -30,7 +30,7 @@ class ResultOpaqueTest { val resultOpaque4 = ResultOpaque.newInErr(8) assert(resultOpaque4.isFailure) val result4 = resultOpaque4.exceptionOrNull()?.message - val assertion = result4?.startsWith("Received error dev.diplomattest.somelib.ResultOpaque", true) + val assertion = result4?.startsWith("Rust error result for ResultOpaque", true) assert(assertion == true) } From c4309dabce610a3bd8c19cecd4d9efd6a0063757 Mon Sep 17 00:00:00 2001 From: Ellen Arteca Date: Fri, 6 Dec 2024 23:30:54 -0800 Subject: [PATCH 8/9] making custom `error` attribute --- core/src/hir/attrs.rs | 32 +++++++++++++++++-- ...ir__elision__tests__elision_in_struct.snap | 4 +++ ...core__hir__elision__tests__simple_mod.snap | 11 +++++++ .../dev/diplomattest/somelib/DataProvider.kt | 2 +- .../dev/diplomattest/somelib/FixedDecimal.kt | 2 +- .../somelib/FixedDecimalFormatter.kt | 2 +- .../somelib/FixedDecimalFormatterOptions.kt | 2 +- .../kotlin/dev/diplomattest/somelib/Locale.kt | 2 +- .../dev/diplomattest/somelib/AttrOpaque1.kt | 2 +- .../dev/diplomattest/somelib/AttrOpaque2.kt | 2 +- .../kotlin/dev/diplomattest/somelib/Bar.kt | 2 +- .../diplomattest/somelib/BorrowedFields.kt | 2 +- .../somelib/BorrowedFieldsReturning.kt | 2 +- .../somelib/BorrowedFieldsWithBounds.kt | 2 +- .../somelib/CallbackTestingStruct.kt | 2 +- .../diplomattest/somelib/CallbackWrapper.kt | 2 +- .../dev/diplomattest/somelib/CyclicStructA.kt | 2 +- .../dev/diplomattest/somelib/CyclicStructB.kt | 2 +- .../dev/diplomattest/somelib/CyclicStructC.kt | 2 +- .../dev/diplomattest/somelib/Float64Vec.kt | 2 +- .../kotlin/dev/diplomattest/somelib/Foo.kt | 2 +- .../diplomattest/somelib/ImportedStruct.kt | 2 +- .../dev/diplomattest/somelib/MyIndexer.kt | 2 +- .../dev/diplomattest/somelib/MyIterable.kt | 2 +- .../dev/diplomattest/somelib/MyIterator.kt | 2 +- .../dev/diplomattest/somelib/MyString.kt | 2 +- .../dev/diplomattest/somelib/MyStruct.kt | 2 +- .../somelib/NestedBorrowedFields.kt | 2 +- .../kotlin/dev/diplomattest/somelib/One.kt | 2 +- .../kotlin/dev/diplomattest/somelib/Opaque.kt | 2 +- .../diplomattest/somelib/OpaqueIterable.kt | 2 +- .../diplomattest/somelib/OpaqueIterator.kt | 2 +- .../somelib/OpaqueMutexedString.kt | 2 +- .../dev/diplomattest/somelib/OptionOpaque.kt | 2 +- .../diplomattest/somelib/OptionOpaqueChar.kt | 2 +- .../dev/diplomattest/somelib/OptionString.kt | 2 +- .../dev/diplomattest/somelib/OptionStruct.kt | 2 +- .../dev/diplomattest/somelib/RefList.kt | 2 +- .../diplomattest/somelib/RefListParameter.kt | 2 +- .../somelib/TraitTestingStruct.kt | 2 +- .../dev/diplomattest/somelib/TraitWrapper.kt | 2 +- .../kotlin/dev/diplomattest/somelib/Two.kt | 2 +- .../dev/diplomattest/somelib/Unnamespaced.kt | 2 +- .../dev/diplomattest/somelib/Utf16Wrap.kt | 2 +- feature_tests/src/result.rs | 2 ++ feature_tests/src/structs.rs | 1 + tool/src/c/mod.rs | 1 + tool/src/cpp/mod.rs | 1 + tool/src/js/mod.rs | 1 + tool/src/kotlin/mod.rs | 28 +++++++++++++++- ...plomat_tool__kotlin__test__opaque_gen.snap | 4 +-- ...n__test__opaque_gen_multiple_ref_args.snap | 4 +-- ...lin__test__opaque_gen_with_finalizers.snap | 4 +-- .../diplomat_tool__kotlin__test__struct.snap | 4 +-- tool/templates/kotlin/Opaque.kt.jinja | 4 +-- tool/templates/kotlin/Struct.kt.jinja | 2 +- 56 files changed, 130 insertions(+), 55 deletions(-) diff --git a/core/src/hir/attrs.rs b/core/src/hir/attrs.rs index c7c226190..9aa18ec7b 100644 --- a/core/src/hir/attrs.rs +++ b/core/src/hir/attrs.rs @@ -41,6 +41,8 @@ pub struct Attrs { /// This attribute does not participate in inheritance and must always /// be specified on individual methods pub special_method: Option, + /// This user-defined type can be used as the error type in a Result. + pub custom_errors: bool, /// From #[diplomat::demo()]. Created from [`crate::ast::attrs::Attrs::demo_attrs`]. /// List of attributes specific to automatic demo generation. @@ -349,9 +351,16 @@ impl Attrs { continue; } } + } else if path == "error" { + if !support.custom_errors { + maybe_error_unsupported(auto_found, "error", backend, errors); + continue; + } + auto_used = true; + this.custom_errors = true; } else { errors.push(LoweringError::Other(format!( - "Unknown diplomat attribute {path}: expected one of: `disable, rename, namespace, constructor, stringifier, comparison, named_constructor, getter, setter, indexer`" + "Unknown diplomat attribute {path}: expected one of: `disable, rename, namespace, constructor, stringifier, comparison, named_constructor, getter, setter, indexer, error`" ))); } if auto_found && !auto_used { @@ -361,7 +370,7 @@ impl Attrs { } } else { errors.push(LoweringError::Other(format!( - "Unknown diplomat attribute {path:?}: expected one of: `disable, rename, namespace, constructor, stringifier, comparison, named_constructor, getter, setter, indexer`" + "Unknown diplomat attribute {path:?}: expected one of: `disable, rename, namespace, constructor, stringifier, comparison, named_constructor, getter, setter, indexer, error`" ))); } } @@ -458,6 +467,7 @@ impl Attrs { rename, abi_rename, special_method, + custom_errors, demo_attrs: _, } = &self; @@ -745,6 +755,17 @@ impl Attrs { ))); } } + + if *custom_errors + && !matches!( + context, + AttributeContext::Type(..) | AttributeContext::Trait(..) + ) + { + errors.push(LoweringError::Other( + "`error` can only be used on types".to_string(), + )); + } } pub(crate) fn for_inheritance(&self, context: AttrInheritContext) -> Attrs { @@ -773,6 +794,8 @@ impl Attrs { abi_rename: Default::default(), // Never inherited special_method: None, + // Not inherited + custom_errors: false, demo_attrs: Default::default(), } } @@ -849,6 +872,8 @@ pub struct BackendAttrSupport { pub callbacks: bool, /// Allowing traits pub traits: bool, + /// Marking a user-defined type as being a valid error result type. + pub custom_errors: bool, } impl BackendAttrSupport { @@ -875,6 +900,7 @@ impl BackendAttrSupport { option: true, callbacks: true, traits: true, + custom_errors: true, } } } @@ -1007,6 +1033,7 @@ impl AttributeValidator for BasicAttributeValidator { option, callbacks, traits, + custom_errors, } = self.support; match value { "namespacing" => namespacing, @@ -1029,6 +1056,7 @@ impl AttributeValidator for BasicAttributeValidator { "option" => option, "callbacks" => callbacks, "traits" => traits, + "custom_errors" => custom_errors, _ => { return Err(LoweringError::Other(format!( "Unknown supports = value found: {value}" diff --git a/core/src/hir/snapshots/diplomat_core__hir__elision__tests__elision_in_struct.snap b/core/src/hir/snapshots/diplomat_core__hir__elision__tests__elision_in_struct.snap index 3fec47a51..26944dd95 100644 --- a/core/src/hir/snapshots/diplomat_core__hir__elision__tests__elision_in_struct.snap +++ b/core/src/hir/snapshots/diplomat_core__hir__elision__tests__elision_in_struct.snap @@ -1,5 +1,6 @@ --- source: core/src/hir/elision.rs +assertion_line: 536 expression: method --- Method { @@ -44,6 +45,7 @@ Method { pattern: None, }, special_method: None, + custom_errors: false, demo_attrs: DemoInfo { generate: false, default_constructor: false, @@ -97,6 +99,7 @@ Method { pattern: None, }, special_method: None, + custom_errors: false, demo_attrs: DemoInfo { generate: false, default_constructor: false, @@ -123,6 +126,7 @@ Method { pattern: None, }, special_method: None, + custom_errors: false, demo_attrs: DemoInfo { generate: false, default_constructor: false, diff --git a/core/src/hir/snapshots/diplomat_core__hir__elision__tests__simple_mod.snap b/core/src/hir/snapshots/diplomat_core__hir__elision__tests__simple_mod.snap index a19cedd5f..eb5bbf6fa 100644 --- a/core/src/hir/snapshots/diplomat_core__hir__elision__tests__simple_mod.snap +++ b/core/src/hir/snapshots/diplomat_core__hir__elision__tests__simple_mod.snap @@ -1,5 +1,6 @@ --- source: core/src/hir/elision.rs +assertion_line: 473 expression: tcx --- TypeContext { @@ -47,6 +48,7 @@ TypeContext { pattern: None, }, special_method: None, + custom_errors: false, demo_attrs: DemoInfo { generate: false, default_constructor: false, @@ -104,6 +106,7 @@ TypeContext { pattern: None, }, special_method: None, + custom_errors: false, demo_attrs: DemoInfo { generate: false, default_constructor: false, @@ -149,6 +152,7 @@ TypeContext { pattern: None, }, special_method: None, + custom_errors: false, demo_attrs: DemoInfo { generate: false, default_constructor: false, @@ -172,6 +176,7 @@ TypeContext { pattern: None, }, special_method: None, + custom_errors: false, demo_attrs: DemoInfo { generate: false, default_constructor: false, @@ -236,6 +241,7 @@ TypeContext { pattern: None, }, special_method: None, + custom_errors: false, demo_attrs: DemoInfo { generate: false, default_constructor: false, @@ -295,6 +301,7 @@ TypeContext { pattern: None, }, special_method: None, + custom_errors: false, demo_attrs: DemoInfo { generate: false, default_constructor: false, @@ -333,6 +340,7 @@ TypeContext { pattern: None, }, special_method: None, + custom_errors: false, demo_attrs: DemoInfo { generate: false, default_constructor: false, @@ -372,6 +380,7 @@ TypeContext { pattern: None, }, special_method: None, + custom_errors: false, demo_attrs: DemoInfo { generate: false, default_constructor: false, @@ -395,6 +404,7 @@ TypeContext { pattern: None, }, special_method: None, + custom_errors: false, demo_attrs: DemoInfo { generate: false, default_constructor: false, @@ -441,6 +451,7 @@ TypeContext { pattern: None, }, special_method: None, + custom_errors: false, demo_attrs: DemoInfo { generate: false, default_constructor: false, diff --git a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/DataProvider.kt b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/DataProvider.kt index aff58008d..0bf7fc61a 100644 --- a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/DataProvider.kt +++ b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/DataProvider.kt @@ -20,7 +20,7 @@ class DataProvider internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for DataProvider") { +) { internal class DataProviderCleaner(val handle: Pointer, val lib: DataProviderLib) : Runnable { override fun run() { diff --git a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimal.kt b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimal.kt index 35f2006bd..eb13db45a 100644 --- a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimal.kt +++ b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimal.kt @@ -19,7 +19,7 @@ class FixedDecimal internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for FixedDecimal") { +) { internal class FixedDecimalCleaner(val handle: Pointer, val lib: FixedDecimalLib) : Runnable { override fun run() { diff --git a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatter.kt b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatter.kt index d157b4038..d995f9303 100644 --- a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatter.kt +++ b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatter.kt @@ -20,7 +20,7 @@ class FixedDecimalFormatter internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for FixedDecimalFormatter") { +) { internal class FixedDecimalFormatterCleaner(val handle: Pointer, val lib: FixedDecimalFormatterLib) : Runnable { override fun run() { diff --git a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatterOptions.kt b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatterOptions.kt index 482d67ee1..6cb390c02 100644 --- a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatterOptions.kt +++ b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/FixedDecimalFormatterOptions.kt @@ -23,7 +23,7 @@ internal class FixedDecimalFormatterOptionsNative: Structure(), Structure.ByValu } class FixedDecimalFormatterOptions internal constructor ( - internal val nativeStruct: FixedDecimalFormatterOptionsNative): Exception("Rust error result for FixedDecimalFormatterOptions") { + internal val nativeStruct: FixedDecimalFormatterOptionsNative) { val groupingStrategy: FixedDecimalGroupingStrategy = FixedDecimalGroupingStrategy.fromNative(nativeStruct.groupingStrategy) val someOtherConfig: Boolean = nativeStruct.someOtherConfig > 0 diff --git a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Locale.kt b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Locale.kt index aa4eb8a6d..9fac83be9 100644 --- a/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Locale.kt +++ b/example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Locale.kt @@ -19,7 +19,7 @@ class Locale internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for Locale") { +) { internal class LocaleCleaner(val handle: Pointer, val lib: LocaleLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/AttrOpaque1.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/AttrOpaque1.kt index 5033abc5e..59113d09c 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/AttrOpaque1.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/AttrOpaque1.kt @@ -20,7 +20,7 @@ class AttrOpaque1 internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for AttrOpaque1") { +) { internal class AttrOpaque1Cleaner(val handle: Pointer, val lib: AttrOpaque1Lib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/AttrOpaque2.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/AttrOpaque2.kt index 9c960e650..5c4e200e8 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/AttrOpaque2.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/AttrOpaque2.kt @@ -15,7 +15,7 @@ class AttrOpaque2 internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for AttrOpaque2") { +) { internal class AttrOpaque2Cleaner(val handle: Pointer, val lib: AttrOpaque2Lib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Bar.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Bar.kt index 88786d663..5da4f1657 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Bar.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Bar.kt @@ -18,7 +18,7 @@ class Bar internal constructor ( internal val selfEdges: List, internal val bEdges: List, internal val aEdges: List, -): Exception("Rust error result for Bar") { +) { internal class BarCleaner(val handle: Pointer, val lib: BarLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFields.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFields.kt index d6f2dd3b2..d950aebbb 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFields.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFields.kt @@ -27,7 +27,7 @@ internal class BorrowedFieldsNative: Structure(), Structure.ByValue { class BorrowedFields internal constructor ( internal val nativeStruct: BorrowedFieldsNative, internal val aEdges: List - ): Exception("Rust error result for BorrowedFields") { + ) { val a: String = PrimitiveArrayTools.getUtf16(nativeStruct.a) val b: String = PrimitiveArrayTools.getUtf8(nativeStruct.b) val c: String = PrimitiveArrayTools.getUtf8(nativeStruct.c) diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFieldsReturning.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFieldsReturning.kt index 46d3cc341..86d0708bc 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFieldsReturning.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFieldsReturning.kt @@ -22,7 +22,7 @@ internal class BorrowedFieldsReturningNative: Structure(), Structure.ByValue { class BorrowedFieldsReturning internal constructor ( internal val nativeStruct: BorrowedFieldsReturningNative, internal val aEdges: List - ): Exception("Rust error result for BorrowedFieldsReturning") { + ) { val bytes: String = PrimitiveArrayTools.getUtf8(nativeStruct.bytes) companion object { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFieldsWithBounds.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFieldsWithBounds.kt index 02b479b83..921527f71 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFieldsWithBounds.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/BorrowedFieldsWithBounds.kt @@ -29,7 +29,7 @@ class BorrowedFieldsWithBounds internal constructor ( internal val aEdges: List, internal val bEdges: List, internal val cEdges: List - ): Exception("Rust error result for BorrowedFieldsWithBounds") { + ) { val fieldA: String = PrimitiveArrayTools.getUtf16(nativeStruct.fieldA) val fieldB: String = PrimitiveArrayTools.getUtf8(nativeStruct.fieldB) val fieldC: String = PrimitiveArrayTools.getUtf8(nativeStruct.fieldC) diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CallbackTestingStruct.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CallbackTestingStruct.kt index 010b98a36..568ace7c7 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CallbackTestingStruct.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CallbackTestingStruct.kt @@ -22,7 +22,7 @@ internal class CallbackTestingStructNative: Structure(), Structure.ByValue { } class CallbackTestingStruct internal constructor ( - internal val nativeStruct: CallbackTestingStructNative): Exception("Rust error result for CallbackTestingStruct") { + internal val nativeStruct: CallbackTestingStructNative) { val x: Int = nativeStruct.x val y: Int = nativeStruct.y diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CallbackWrapper.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CallbackWrapper.kt index 0a00ae343..f95e51c18 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CallbackWrapper.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CallbackWrapper.kt @@ -270,7 +270,7 @@ internal class DiplomatCallback_CallbackWrapper_test_multiple_cb_args_diplomatCa } } class CallbackWrapper internal constructor ( - internal val nativeStruct: CallbackWrapperNative): Exception("Rust error result for CallbackWrapper") { + internal val nativeStruct: CallbackWrapperNative) { val cantBeEmpty: Boolean = nativeStruct.cantBeEmpty > 0 companion object { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructA.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructA.kt index a660974eb..43db22176 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructA.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructA.kt @@ -22,7 +22,7 @@ internal class CyclicStructANative: Structure(), Structure.ByValue { } class CyclicStructA internal constructor ( - internal val nativeStruct: CyclicStructANative): Exception("Rust error result for CyclicStructA") { + internal val nativeStruct: CyclicStructANative) { val a: CyclicStructB = CyclicStructB(nativeStruct.a) companion object { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructB.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructB.kt index b898cde1c..87bb2a3ff 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructB.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructB.kt @@ -21,7 +21,7 @@ internal class CyclicStructBNative: Structure(), Structure.ByValue { } class CyclicStructB internal constructor ( - internal val nativeStruct: CyclicStructBNative): Exception("Rust error result for CyclicStructB") { + internal val nativeStruct: CyclicStructBNative) { val field: UByte = nativeStruct.field.toUByte() companion object { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructC.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructC.kt index bc12d2f2d..b55f7c053 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructC.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/CyclicStructC.kt @@ -21,7 +21,7 @@ internal class CyclicStructCNative: Structure(), Structure.ByValue { } class CyclicStructC internal constructor ( - internal val nativeStruct: CyclicStructCNative): Exception("Rust error result for CyclicStructC") { + internal val nativeStruct: CyclicStructCNative) { val a: CyclicStructA = CyclicStructA(nativeStruct.a) companion object { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Float64Vec.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Float64Vec.kt index 3b1e180a9..90bc70ce3 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Float64Vec.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Float64Vec.kt @@ -28,7 +28,7 @@ class Float64Vec internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for Float64Vec") { +) { internal class Float64VecCleaner(val handle: Pointer, val lib: Float64VecLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Foo.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Foo.kt index 5aa12ee8f..01498fdad 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Foo.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Foo.kt @@ -22,7 +22,7 @@ class Foo internal constructor ( // up by the garbage collector. internal val selfEdges: List, internal val aEdges: List, -): Exception("Rust error result for Foo") { +) { internal class FooCleaner(val handle: Pointer, val lib: FooLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ImportedStruct.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ImportedStruct.kt index 0abfb0685..27a704a49 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ImportedStruct.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/ImportedStruct.kt @@ -22,7 +22,7 @@ internal class ImportedStructNative: Structure(), Structure.ByValue { } class ImportedStruct internal constructor ( - internal val nativeStruct: ImportedStructNative): Exception("Rust error result for ImportedStruct") { + internal val nativeStruct: ImportedStructNative) { val foo: UnimportedEnum = UnimportedEnum.fromNative(nativeStruct.foo) val count: UByte = nativeStruct.count.toUByte() diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIndexer.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIndexer.kt index bf6d8940a..f1c4f7d56 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIndexer.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIndexer.kt @@ -16,7 +16,7 @@ class MyIndexer internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for MyIndexer") { +) { internal class MyIndexerCleaner(val handle: Pointer, val lib: MyIndexerLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterable.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterable.kt index 50e77e50c..a230f021d 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterable.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterable.kt @@ -17,7 +17,7 @@ class MyIterable internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for MyIterable"), Iterable { +): Iterable { internal class MyIterableCleaner(val handle: Pointer, val lib: MyIterableLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterator.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterator.kt index f702ae464..13dab0793 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterator.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyIterator.kt @@ -18,7 +18,7 @@ class MyIterator internal constructor ( // up by the garbage collector. internal val selfEdges: List, internal val aEdges: List, -): Exception("Rust error result for MyIterator"), Iterator { +): Iterator { internal class MyIteratorCleaner(val handle: Pointer, val lib: MyIteratorLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyString.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyString.kt index c9b5521ee..9badd69b9 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyString.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyString.kt @@ -23,7 +23,7 @@ class MyString internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for MyString") { +) { internal class MyStringCleaner(val handle: Pointer, val lib: MyStringLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyStruct.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyStruct.kt index c03442e8f..4a6643322 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyStruct.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/MyStruct.kt @@ -36,7 +36,7 @@ internal class MyStructNative: Structure(), Structure.ByValue { } class MyStruct internal constructor ( - internal val nativeStruct: MyStructNative): Exception("Rust error result for MyStruct") { + internal val nativeStruct: MyStructNative) { val a: UByte = nativeStruct.a.toUByte() val b: Boolean = nativeStruct.b > 0 val c: UByte = nativeStruct.c.toUByte() diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/NestedBorrowedFields.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/NestedBorrowedFields.kt index 16b2484ef..c686c467d 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/NestedBorrowedFields.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/NestedBorrowedFields.kt @@ -29,7 +29,7 @@ class NestedBorrowedFields internal constructor ( internal val xEdges: List, internal val yEdges: List, internal val zEdges: List - ): Exception("Rust error result for NestedBorrowedFields") { + ) { val fields: BorrowedFields = BorrowedFields(nativeStruct.fields, xEdges) val bounds: BorrowedFieldsWithBounds = BorrowedFieldsWithBounds(nativeStruct.bounds, xEdges, yEdges, yEdges) val bounds2: BorrowedFieldsWithBounds = BorrowedFieldsWithBounds(nativeStruct.bounds2, zEdges, zEdges, zEdges) diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/One.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/One.kt index 72a40b4d1..54a7a5e88 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/One.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/One.kt @@ -27,7 +27,7 @@ class One internal constructor ( // up by the garbage collector. internal val selfEdges: List, internal val aEdges: List, -): Exception("Rust error result for One") { +) { internal class OneCleaner(val handle: Pointer, val lib: OneLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Opaque.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Opaque.kt index fcb1f928f..6270b33aa 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Opaque.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Opaque.kt @@ -23,7 +23,7 @@ class Opaque internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for Opaque") { +) { internal class OpaqueCleaner(val handle: Pointer, val lib: OpaqueLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterable.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterable.kt index e157aa904..3eb4da941 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterable.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterable.kt @@ -16,7 +16,7 @@ class OpaqueIterable internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for OpaqueIterable"), Iterable { +): Iterable { internal class OpaqueIterableCleaner(val handle: Pointer, val lib: OpaqueIterableLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterator.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterator.kt index c81d0e19c..f869f06ac 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterator.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueIterator.kt @@ -18,7 +18,7 @@ class OpaqueIterator internal constructor ( // up by the garbage collector. internal val selfEdges: List, internal val aEdges: List, -): Exception("Rust error result for OpaqueIterator"), Iterator { +): Iterator { internal class OpaqueIteratorCleaner(val handle: Pointer, val lib: OpaqueIteratorLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueMutexedString.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueMutexedString.kt index 333bbfe57..dc2158d81 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueMutexedString.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OpaqueMutexedString.kt @@ -23,7 +23,7 @@ class OpaqueMutexedString internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for OpaqueMutexedString") { +) { internal class OpaqueMutexedStringCleaner(val handle: Pointer, val lib: OpaqueMutexedStringLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionOpaque.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionOpaque.kt index 2f9e5de41..71c8b93ab 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionOpaque.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionOpaque.kt @@ -26,7 +26,7 @@ class OptionOpaque internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for OptionOpaque") { +) { internal class OptionOpaqueCleaner(val handle: Pointer, val lib: OptionOpaqueLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionOpaqueChar.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionOpaqueChar.kt index 439f0ae56..1867aba60 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionOpaqueChar.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionOpaqueChar.kt @@ -16,7 +16,7 @@ class OptionOpaqueChar internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for OptionOpaqueChar") { +) { internal class OptionOpaqueCharCleaner(val handle: Pointer, val lib: OptionOpaqueCharLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionString.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionString.kt index 9601cb879..e3bb88989 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionString.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionString.kt @@ -18,7 +18,7 @@ class OptionString internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for OptionString") { +) { internal class OptionStringCleaner(val handle: Pointer, val lib: OptionStringLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionStruct.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionStruct.kt index 7652c3261..aa0395c1b 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionStruct.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/OptionStruct.kt @@ -26,7 +26,7 @@ internal class OptionStructNative: Structure(), Structure.ByValue { } class OptionStruct internal constructor ( - internal val nativeStruct: OptionStructNative): Exception("Rust error result for OptionStruct") { + internal val nativeStruct: OptionStructNative) { val a: OptionOpaque? = if (nativeStruct.a == null) { null } else { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/RefList.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/RefList.kt index 2d7481ae9..4cac78c46 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/RefList.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/RefList.kt @@ -17,7 +17,7 @@ class RefList internal constructor ( // up by the garbage collector. internal val selfEdges: List, internal val aEdges: List, -): Exception("Rust error result for RefList") { +) { internal class RefListCleaner(val handle: Pointer, val lib: RefListLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/RefListParameter.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/RefListParameter.kt index b1efbf1a2..09e53d128 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/RefListParameter.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/RefListParameter.kt @@ -15,7 +15,7 @@ class RefListParameter internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for RefListParameter") { +) { internal class RefListParameterCleaner(val handle: Pointer, val lib: RefListParameterLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/TraitTestingStruct.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/TraitTestingStruct.kt index 9c8f9b44b..bf96466be 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/TraitTestingStruct.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/TraitTestingStruct.kt @@ -22,7 +22,7 @@ internal class TraitTestingStructNative: Structure(), Structure.ByValue { } class TraitTestingStruct internal constructor ( - internal val nativeStruct: TraitTestingStructNative): Exception("Rust error result for TraitTestingStruct") { + internal val nativeStruct: TraitTestingStructNative) { val x: Int = nativeStruct.x val y: Int = nativeStruct.y diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/TraitWrapper.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/TraitWrapper.kt index 47be186f7..7baeb3986 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/TraitWrapper.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/TraitWrapper.kt @@ -22,7 +22,7 @@ internal class TraitWrapperNative: Structure(), Structure.ByValue { } class TraitWrapper internal constructor ( - internal val nativeStruct: TraitWrapperNative): Exception("Rust error result for TraitWrapper") { + internal val nativeStruct: TraitWrapperNative) { val cantBeEmpty: Boolean = nativeStruct.cantBeEmpty > 0 companion object { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Two.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Two.kt index a2e557159..3b101adfc 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Two.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Two.kt @@ -17,7 +17,7 @@ class Two internal constructor ( internal val selfEdges: List, internal val aEdges: List, internal val bEdges: List, -): Exception("Rust error result for Two") { +) { internal class TwoCleaner(val handle: Pointer, val lib: TwoLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Unnamespaced.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Unnamespaced.kt index 44d49c8db..fa1cdab32 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Unnamespaced.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Unnamespaced.kt @@ -17,7 +17,7 @@ class Unnamespaced internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for Unnamespaced") { +) { internal class UnnamespacedCleaner(val handle: Pointer, val lib: UnnamespacedLib) : Runnable { override fun run() { diff --git a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Utf16Wrap.kt b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Utf16Wrap.kt index 7e3dcd68b..0f12309c3 100644 --- a/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Utf16Wrap.kt +++ b/feature_tests/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Utf16Wrap.kt @@ -18,7 +18,7 @@ class Utf16Wrap internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for Utf16Wrap") { +) { internal class Utf16WrapCleaner(val handle: Pointer, val lib: Utf16WrapLib) : Runnable { override fun run() { diff --git a/feature_tests/src/result.rs b/feature_tests/src/result.rs index 0786a2b86..1012b6d78 100644 --- a/feature_tests/src/result.rs +++ b/feature_tests/src/result.rs @@ -2,6 +2,7 @@ pub mod ffi { #[diplomat::opaque] + #[diplomat::attr(auto, error)] pub struct ResultOpaque(i32); #[derive(PartialEq, Eq, Debug)] @@ -11,6 +12,7 @@ pub mod ffi { } #[derive(Debug)] + #[diplomat::attr(auto, error)] pub struct ErrorStruct { i: i32, j: i32, diff --git a/feature_tests/src/structs.rs b/feature_tests/src/structs.rs index 8cb1515a8..855871df2 100644 --- a/feature_tests/src/structs.rs +++ b/feature_tests/src/structs.rs @@ -45,6 +45,7 @@ pub mod ffi { g: MyEnum, } + #[diplomat::attr(auto, error)] pub struct MyZst; impl Opaque { diff --git a/tool/src/c/mod.rs b/tool/src/c/mod.rs index 322f65021..03498c6de 100644 --- a/tool/src/c/mod.rs +++ b/tool/src/c/mod.rs @@ -34,6 +34,7 @@ pub(crate) fn attr_support() -> BackendAttrSupport { a.option = true; a.callbacks = true; a.traits = true; + a.custom_errors = false; a } diff --git a/tool/src/cpp/mod.rs b/tool/src/cpp/mod.rs index e8a63f9b4..8c6f542f0 100644 --- a/tool/src/cpp/mod.rs +++ b/tool/src/cpp/mod.rs @@ -30,6 +30,7 @@ pub(crate) fn attr_support() -> BackendAttrSupport { a.option = true; a.callbacks = false; a.traits = false; + a.custom_errors = false; a } diff --git a/tool/src/js/mod.rs b/tool/src/js/mod.rs index d7b36da7f..ec92c5206 100644 --- a/tool/src/js/mod.rs +++ b/tool/src/js/mod.rs @@ -56,6 +56,7 @@ pub(crate) fn attr_support() -> BackendAttrSupport { a.callbacks = false; a.option = true; a.traits = false; + a.custom_errors = false; // TODO a } diff --git a/tool/src/kotlin/mod.rs b/tool/src/kotlin/mod.rs index f3b2e7ae2..9a376bffe 100644 --- a/tool/src/kotlin/mod.rs +++ b/tool/src/kotlin/mod.rs @@ -4,7 +4,7 @@ use diplomat_core::hir::{ self, BackendAttrSupport, Borrow, Callback, DocsUrlGenerator, InputOnly, Lifetime, LifetimeEnv, Lifetimes, MaybeOwn, MaybeStatic, Method, Mutability, OpaquePath, Optional, OutType, Param, PrimitiveType, ReturnableStructDef, SelfType, Slice, SpecialMethod, StringEncoding, - StructField, StructPath, StructPathLike, TraitIdGetter, TyPosition, Type, TypeContext, TypeDef, + StructField, StructPath, StructPathLike, TraitIdGetter, TyPosition, Type, TypeContext, TypeDef, ReturnableStructPath }; use diplomat_core::hir::{ReturnType, SuccessType}; @@ -42,6 +42,7 @@ pub(crate) fn attr_support() -> BackendAttrSupport { a.indexing = true; a.callbacks = true; a.traits = true; + a.custom_errors = true; a } @@ -851,6 +852,27 @@ val intermediateOption = {val_name}.option() ?: return null .as_ref() .map(|err| { let err_converter = if let OutType::Opaque(..) | OutType::Struct(..) = err { + match err { + OutType::Opaque(OpaquePath{tcx_id: id, ..}) => { + let resolved = self.tcx.resolve_opaque(*id); + if !resolved.attrs.custom_errors { + panic!("Opaque type {:?} must have the `error` attribute to be used as an error result", resolved.name); + } + }, + OutType::Struct(ReturnableStructPath::Struct(path)) => { + let resolved = self.tcx.resolve_struct(path.tcx_id); + if !resolved.attrs.custom_errors { + panic!("Struct type {:?} must have the `error` attribute to be used as an error result", resolved.name); + } + }, + OutType::Struct(ReturnableStructPath::OutStruct(path)) => { + let resolved = self.tcx.resolve_out_struct(path.tcx_id); + if !resolved.attrs.custom_errors { + panic!("Struct type {:?} must have the `error` attribute to be used as an error result", resolved.name); + } + } + _ => {} + } ".err()" } else { ".primitive_err()" @@ -1361,6 +1383,7 @@ returnVal.option() ?: return null callback_params: &'a [CallbackParamInfo], use_finalizers_not_cleaners: bool, docs: String, + is_custom_error: bool, } ( @@ -1378,6 +1401,7 @@ returnVal.option() ?: return null callback_params: self.callback_params.as_ref(), use_finalizers_not_cleaners, docs: self.formatter.fmt_docs(&ty.docs), + is_custom_error: ty.attrs.custom_errors, } .render() .expect("failed to generate struct"), @@ -1468,6 +1492,7 @@ returnVal.option() ?: return null callback_params: &'a [CallbackParamInfo], lifetimes: Vec>, docs: String, + is_custom_error: bool, } let fields = ty @@ -1504,6 +1529,7 @@ returnVal.option() ?: return null callback_params: self.callback_params.as_ref(), lifetimes, docs: self.formatter.fmt_docs(&ty.docs), + is_custom_error: ty.attrs.custom_errors, } .render() .expect("Failed to render struct template"), diff --git a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen.snap b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen.snap index 9056a8d4a..dfff7f8bd 100644 --- a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen.snap +++ b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen.snap @@ -1,6 +1,6 @@ --- source: tool/src/kotlin/mod.rs -assertion_line: 2326 +assertion_line: 2352 expression: result --- package dev.gigapixel.somelib; @@ -22,7 +22,7 @@ class BorrowWrapper internal constructor ( internal val selfEdges: List, internal val aEdges: List, internal val bEdges: List, -): Exception("Rust error result for BorrowWrapper") { +) { internal class BorrowWrapperCleaner(val handle: Pointer, val lib: BorrowWrapperLib) : Runnable { override fun run() { diff --git a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen_multiple_ref_args.snap b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen_multiple_ref_args.snap index 0eaea4039..3116d7fc4 100644 --- a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen_multiple_ref_args.snap +++ b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen_multiple_ref_args.snap @@ -1,6 +1,6 @@ --- source: tool/src/kotlin/mod.rs -assertion_line: 2217 +assertion_line: 2243 expression: result --- package dev.gigapixel.somelib; @@ -21,7 +21,7 @@ class AnotherOpaque internal constructor ( // These ensure that anything that is borrowed is kept alive and not cleaned // up by the garbage collector. internal val selfEdges: List, -): Exception("Rust error result for AnotherOpaque") { +) { internal class AnotherOpaqueCleaner(val handle: Pointer, val lib: AnotherOpaqueLib) : Runnable { override fun run() { diff --git a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen_with_finalizers.snap b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen_with_finalizers.snap index 06f25e2a2..16d3d1f94 100644 --- a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen_with_finalizers.snap +++ b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__opaque_gen_with_finalizers.snap @@ -1,6 +1,6 @@ --- source: tool/src/kotlin/mod.rs -assertion_line: 2377 +assertion_line: 2403 expression: result --- package dev.gigapixel.somelib; @@ -24,7 +24,7 @@ class MyOpaqueStruct internal constructor ( internal val selfEdges: List, internal val bEdges: List, internal var finalizer_registered: Boolean = false, -): Exception("Rust error result for MyOpaqueStruct") { +) { fun registerFinalizer() { this.finalizer_registered = true } diff --git a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__struct.snap b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__struct.snap index c793ca2b2..d91788193 100644 --- a/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__struct.snap +++ b/tool/src/kotlin/snapshots/diplomat_tool__kotlin__test__struct.snap @@ -1,6 +1,6 @@ --- source: tool/src/kotlin/mod.rs -assertion_line: 2165 +assertion_line: 2191 expression: struct_code --- package dev.gigapixel.somelib @@ -112,7 +112,7 @@ internal class DiplomatCallback_MyNativeStruct_test_multi_arg_callback_diplomatC class MyNativeStruct internal constructor ( internal val nativeStruct: MyNativeStructNative, internal val bEdges: List - ): Exception("Rust error result for MyNativeStruct") { + ) { val a: Boolean = nativeStruct.a > 0 val b: Byte = nativeStruct.b val c: UByte = nativeStruct.c.toUByte() diff --git a/tool/templates/kotlin/Opaque.kt.jinja b/tool/templates/kotlin/Opaque.kt.jinja index 1fd4274c9..bac524a3f 100644 --- a/tool/templates/kotlin/Opaque.kt.jinja +++ b/tool/templates/kotlin/Opaque.kt.jinja @@ -41,8 +41,8 @@ class {{type_name}} internal constructor ( {%- if use_finalizers_not_cleaners %} internal var finalizer_registered: Boolean = false, {%- endif %} -): Exception("Rust error result for {{type_name}}") -{%- if special_methods.interfaces.is_empty() %} {% else %}, {% for interface in special_methods.interfaces %} +){%- if is_custom_error %}: Exception("Rust error result for {{type_name}}") {%- endif %} +{%- if special_methods.interfaces.is_empty() %} {% else %}{%- if is_custom_error %},{% else %}:{% endif %} {% for interface in special_methods.interfaces %} {%- if loop.first %}{% else %}, {% endif %}{{interface}} {%- endfor %} {%- endif %} { diff --git a/tool/templates/kotlin/Struct.kt.jinja b/tool/templates/kotlin/Struct.kt.jinja index 08990fae2..c011afa78 100644 --- a/tool/templates/kotlin/Struct.kt.jinja +++ b/tool/templates/kotlin/Struct.kt.jinja @@ -50,7 +50,7 @@ class {{type_name}} internal constructor ( internal val {{lt}}Edges: List{% if !loop.last%},{% endif %} {%- endfor %} {% endif -%} - ): Exception("Rust error result for {{type_name}}") { + ){%- if is_custom_error %}: Exception("Rust error result for {{type_name}}") {%- endif %} { {%- for field in fields %} val {{field.name}}: {{field.field_type}} = {{field.native_to_kt}} From 1b08ce52221d0ae26550dbca8202478b05703601 Mon Sep 17 00:00:00 2001 From: Ellen Arteca Date: Fri, 6 Dec 2024 23:33:18 -0800 Subject: [PATCH 9/9] clippy --- tool/src/kotlin/mod.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tool/src/kotlin/mod.rs b/tool/src/kotlin/mod.rs index 9a376bffe..e1d57bb09 100644 --- a/tool/src/kotlin/mod.rs +++ b/tool/src/kotlin/mod.rs @@ -3,8 +3,9 @@ use diplomat_core::hir::borrowing_param::{BorrowedLifetimeInfo, ParamBorrowInfo} use diplomat_core::hir::{ self, BackendAttrSupport, Borrow, Callback, DocsUrlGenerator, InputOnly, Lifetime, LifetimeEnv, Lifetimes, MaybeOwn, MaybeStatic, Method, Mutability, OpaquePath, Optional, OutType, Param, - PrimitiveType, ReturnableStructDef, SelfType, Slice, SpecialMethod, StringEncoding, - StructField, StructPath, StructPathLike, TraitIdGetter, TyPosition, Type, TypeContext, TypeDef, ReturnableStructPath + PrimitiveType, ReturnableStructDef, ReturnableStructPath, SelfType, Slice, SpecialMethod, + StringEncoding, StructField, StructPath, StructPathLike, TraitIdGetter, TyPosition, Type, + TypeContext, TypeDef, }; use diplomat_core::hir::{ReturnType, SuccessType};