From f7665ae2f8c7f1e212106d4a29fdbe93ab5e45e0 Mon Sep 17 00:00:00 2001 From: Martynas Gurskas Date: Fri, 27 Sep 2024 12:08:00 +0300 Subject: [PATCH] Fix invalid `allocation_size` code generation when dealing with empty records (#45) Signed-off-by: Martynas Gurskas --- Cargo.lock | 8 ++++++++ bindgen/src/bindings/cpp/templates/rec.cpp | 6 +++++- cpp-tests/CMakeLists.txt | 2 ++ .../empty_type/lib_empty_type.cpp | 9 +++++++++ .../empty_type/lib_empty_type.hpp | 11 +++++++++++ cpp-tests/tests/empty_type/main.cpp | 11 +++++++++++ fixtures/Cargo.toml | 1 + fixtures/empty-type/Cargo.toml | 14 ++++++++++++++ fixtures/empty-type/build.rs | 3 +++ fixtures/empty-type/src/empty_type.udl | 6 ++++++ fixtures/empty-type/src/lib.rs | 9 +++++++++ fixtures/src/lib.rs | 1 + 12 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 cpp-tests/scaffolding_tests/empty_type/lib_empty_type.cpp create mode 100644 cpp-tests/scaffolding_tests/empty_type/lib_empty_type.hpp create mode 100644 cpp-tests/tests/empty_type/main.cpp create mode 100644 fixtures/empty-type/Cargo.toml create mode 100644 fixtures/empty-type/build.rs create mode 100644 fixtures/empty-type/src/empty_type.udl create mode 100644 fixtures/empty-type/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index c3ead5d..7eb8819 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -871,6 +871,7 @@ name = "uniffi-bindgen-cpp-fixtures" version = "1.0.0" dependencies = [ "uniffi-custom-types-builtin", + "uniffi-empty-type", "uniffi-enum-style-test", "uniffi-example-arithmetic", "uniffi-example-callbacks", @@ -897,6 +898,13 @@ dependencies = [ "uniffi", ] +[[package]] +name = "uniffi-empty-type" +version = "0.1.0" +dependencies = [ + "uniffi", +] + [[package]] name = "uniffi-enum-style-test" version = "0.1.0" diff --git a/bindgen/src/bindings/cpp/templates/rec.cpp b/bindgen/src/bindings/cpp/templates/rec.cpp index 6a0f231..1423408 100644 --- a/bindgen/src/bindings/cpp/templates/rec.cpp +++ b/bindgen/src/bindings/cpp/templates/rec.cpp @@ -33,7 +33,11 @@ void {{ ffi_converter_name }}::write(RustStream &stream, const {{ class_name }} } int32_t {{ ffi_converter_name }}::allocation_size(const {{ class_name }} &val) { + {% if rec.fields().is_empty() %} + return 0; + {% else %} return {% for field in rec.fields() %} {{ field|allocation_size_fn}}(val.{{ field.name()|var_name() }}){% if !loop.last %} +{% else -%};{%- endif %} {%- endfor %} -} \ No newline at end of file + {% endif %} +} diff --git a/cpp-tests/CMakeLists.txt b/cpp-tests/CMakeLists.txt index d37b9f1..d83edf3 100644 --- a/cpp-tests/CMakeLists.txt +++ b/cpp-tests/CMakeLists.txt @@ -87,6 +87,7 @@ test_case(uniffi_docstring) test_case(trait_methods) test_case(custom_types_builtin) test_case(enum_style_test) +test_case(empty_type) scaffolding_test_case(arithmetic) scaffolding_test_case(callbacks) @@ -101,6 +102,7 @@ scaffolding_test_case(traits) scaffolding_test_case(coverall) scaffolding_test_case(custom_types_builtin) scaffolding_test_case(enum_style_test) +scaffolding_test_case(empty_type) add_library(uniffi_fixtures SHARED ${SCAFFOLDING_LIB_FILES}) diff --git a/cpp-tests/scaffolding_tests/empty_type/lib_empty_type.cpp b/cpp-tests/scaffolding_tests/empty_type/lib_empty_type.cpp new file mode 100644 index 0000000..3e994df --- /dev/null +++ b/cpp-tests/scaffolding_tests/empty_type/lib_empty_type.cpp @@ -0,0 +1,9 @@ +#include + +empty_type::Empty empty_type::get_empty_type() { + return Empty {}; +} + +void empty_type::send_empty_type(Empty e) {} + +#include diff --git a/cpp-tests/scaffolding_tests/empty_type/lib_empty_type.hpp b/cpp-tests/scaffolding_tests/empty_type/lib_empty_type.hpp new file mode 100644 index 0000000..c30b17a --- /dev/null +++ b/cpp-tests/scaffolding_tests/empty_type/lib_empty_type.hpp @@ -0,0 +1,11 @@ +#include + +namespace { + namespace empty_type { + struct Empty {}; + + Empty get_empty_type(); + + void send_empty_type(Empty e); + } +} diff --git a/cpp-tests/tests/empty_type/main.cpp b/cpp-tests/tests/empty_type/main.cpp new file mode 100644 index 0000000..446a4f2 --- /dev/null +++ b/cpp-tests/tests/empty_type/main.cpp @@ -0,0 +1,11 @@ +#include "test_common.hpp" + +#include + +int main() { + auto empty = empty_type::get_empty_type(); + + empty_type::send_empty_type(empty); + + return 0; +} diff --git a/fixtures/Cargo.toml b/fixtures/Cargo.toml index b76ddb9..8e336fe 100644 --- a/fixtures/Cargo.toml +++ b/fixtures/Cargo.toml @@ -22,3 +22,4 @@ uniffi-fixture-time = { git = "https://github.com/NordSecurity/uniffi-rs.git", t uniffi-fixture-trait-methods = { git = "https://github.com/NordSecurity/uniffi-rs.git", tag = "v0.3.1+v0.25.0" } uniffi-custom-types-builtin = { path = "custom-types-builtin" } uniffi-enum-style-test = { path = "enum-style-test" } +uniffi-empty-type = { path = "empty-type" } diff --git a/fixtures/empty-type/Cargo.toml b/fixtures/empty-type/Cargo.toml new file mode 100644 index 0000000..f82d91d --- /dev/null +++ b/fixtures/empty-type/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "uniffi-empty-type" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["lib", "cdylib"] +name = "uniffi_empty_type" + +[dependencies] +uniffi = { workspace = true } + +[build-dependencies] +uniffi = { workspace = true, features = ["build"] } diff --git a/fixtures/empty-type/build.rs b/fixtures/empty-type/build.rs new file mode 100644 index 0000000..e698baa --- /dev/null +++ b/fixtures/empty-type/build.rs @@ -0,0 +1,3 @@ +fn main() { + uniffi::generate_scaffolding("src/empty_type.udl").unwrap(); +} diff --git a/fixtures/empty-type/src/empty_type.udl b/fixtures/empty-type/src/empty_type.udl new file mode 100644 index 0000000..da304ee --- /dev/null +++ b/fixtures/empty-type/src/empty_type.udl @@ -0,0 +1,6 @@ +dictionary Empty {}; + +namespace empty_type{ + Empty get_empty_type(); + void send_empty_type(Empty e); +}; diff --git a/fixtures/empty-type/src/lib.rs b/fixtures/empty-type/src/lib.rs new file mode 100644 index 0000000..e3a4f99 --- /dev/null +++ b/fixtures/empty-type/src/lib.rs @@ -0,0 +1,9 @@ +pub struct Empty {} + +pub fn get_empty_type() -> Empty { + Empty {} +} + +pub fn send_empty_type(empty: Empty) {} + +uniffi::include_scaffolding!("empty_type"); diff --git a/fixtures/src/lib.rs b/fixtures/src/lib.rs index c03d159..a50a4f4 100644 --- a/fixtures/src/lib.rs +++ b/fixtures/src/lib.rs @@ -15,4 +15,5 @@ mod uniffi_fixtures { uniffi_cpp_custom_types_builtin::uniffi_reexport_scaffolding!(); uniffi_enum_style_test::uniffi_reexport_scaffolding!(); + uniffi_empty_type::uniffi_reexport_scaffolding!(); }