From 6b4174258d8c668650bd7555b73e4abe82644457 Mon Sep 17 00:00:00 2001 From: Xiaofei Wang <6218006+wangxf123456@users.noreply.github.com> Date: Mon, 27 Jun 2022 16:12:24 -0700 Subject: [PATCH 1/6] Support loading unique_ptr as unique_ptr. --- include/pybind11/detail/smart_holder_poc.h | 6 +++-- tests/pure_cpp/smart_holder_poc_test.cpp | 28 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/include/pybind11/detail/smart_holder_poc.h b/include/pybind11/detail/smart_holder_poc.h index 9794b1b382..04db9eae58 100644 --- a/include/pybind11/detail/smart_holder_poc.h +++ b/include/pybind11/detail/smart_holder_poc.h @@ -182,8 +182,10 @@ struct smart_holder { } ensure_vptr_is_using_builtin_delete(context); } else if (!(*rtti_requested == *rtti_uqp_del)) { - throw std::invalid_argument(std::string("Incompatible unique_ptr deleter (") + context - + ")."); + if (!(vptr_is_using_builtin_delete && is_std_default_delete(*rtti_requested))) { + throw std::invalid_argument(std::string("Incompatible unique_ptr deleter (") + context + + ")."); + } } } diff --git a/tests/pure_cpp/smart_holder_poc_test.cpp b/tests/pure_cpp/smart_holder_poc_test.cpp index cbdbc25f2d..e23abe5886 100644 --- a/tests/pure_cpp/smart_holder_poc_test.cpp +++ b/tests/pure_cpp/smart_holder_poc_test.cpp @@ -37,6 +37,16 @@ struct indestructible_int { ~indestructible_int() = default; }; +struct base { + virtual int get() { return 10; } + virtual ~base() { } +}; + +struct derived: public base { + int get() override { return 100; } + ~derived() override { } +}; + } // namespace helpers TEST_CASE("from_raw_ptr_unowned+as_raw_ptr_unowned", "[S]") { @@ -227,6 +237,24 @@ TEST_CASE("from_unique_ptr+as_shared_ptr", "[S]") { REQUIRE(*new_owner == 19); } +TEST_CASE("from_unique_ptr_derived+as_unique_ptr_base", "[E]") { + std::unique_ptr orig_owner(new helpers::derived()); + auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); + REQUIRE(orig_owner.get() == nullptr); + std::unique_ptr new_owner = hld.as_unique_ptr(); + REQUIRE(!hld.has_pointee()); + REQUIRE(new_owner->get() == 100); +} + +TEST_CASE("from_unique_ptr_derived+as_unique_ptr_base2", "[E]") { + std::unique_ptr> + orig_owner(new helpers::derived()); + auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); + REQUIRE(orig_owner.get() == nullptr); + REQUIRE_THROWS_WITH((hld.as_unique_ptr>()), + "Incompatible unique_ptr deleter (as_unique_ptr)."); +} + TEST_CASE("from_unique_ptr_with_deleter+as_lvalue_ref", "[S]") { std::unique_ptr> orig_owner(new int(19)); auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); From dd9708882f32931a6f6cabddb188f4c84b677aee Mon Sep 17 00:00:00 2001 From: Xiaofei Wang <6218006+wangxf123456@users.noreply.github.com> Date: Mon, 27 Jun 2022 17:49:54 -0700 Subject: [PATCH 2/6] Fix incorrect test --- tests/pure_cpp/smart_holder_poc_test.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/pure_cpp/smart_holder_poc_test.cpp b/tests/pure_cpp/smart_holder_poc_test.cpp index e23abe5886..474d5d1d2a 100644 --- a/tests/pure_cpp/smart_holder_poc_test.cpp +++ b/tests/pure_cpp/smart_holder_poc_test.cpp @@ -251,8 +251,9 @@ TEST_CASE("from_unique_ptr_derived+as_unique_ptr_base2", "[E]") { orig_owner(new helpers::derived()); auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); REQUIRE(orig_owner.get() == nullptr); - REQUIRE_THROWS_WITH((hld.as_unique_ptr>()), - "Incompatible unique_ptr deleter (as_unique_ptr)."); + REQUIRE_THROWS_WITH( + (hld.as_unique_ptr>()), + "Incompatible unique_ptr deleter (as_unique_ptr)."); } TEST_CASE("from_unique_ptr_with_deleter+as_lvalue_ref", "[S]") { From 1b983c652fa06e3afe61b85220c863ddecc50835 Mon Sep 17 00:00:00 2001 From: Xiaofei Wang <6218006+wangxf123456@users.noreply.github.com> Date: Mon, 27 Jun 2022 17:51:38 -0700 Subject: [PATCH 3/6] pre commit fix --- include/pybind11/detail/smart_holder_poc.h | 4 ++-- tests/pure_cpp/smart_holder_poc_test.cpp | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/pybind11/detail/smart_holder_poc.h b/include/pybind11/detail/smart_holder_poc.h index 04db9eae58..0b16e4117f 100644 --- a/include/pybind11/detail/smart_holder_poc.h +++ b/include/pybind11/detail/smart_holder_poc.h @@ -183,8 +183,8 @@ struct smart_holder { ensure_vptr_is_using_builtin_delete(context); } else if (!(*rtti_requested == *rtti_uqp_del)) { if (!(vptr_is_using_builtin_delete && is_std_default_delete(*rtti_requested))) { - throw std::invalid_argument(std::string("Incompatible unique_ptr deleter (") + context - + ")."); + throw std::invalid_argument(std::string("Incompatible unique_ptr deleter (") + + context + ")."); } } } diff --git a/tests/pure_cpp/smart_holder_poc_test.cpp b/tests/pure_cpp/smart_holder_poc_test.cpp index 474d5d1d2a..197e29fbf8 100644 --- a/tests/pure_cpp/smart_holder_poc_test.cpp +++ b/tests/pure_cpp/smart_holder_poc_test.cpp @@ -39,12 +39,12 @@ struct indestructible_int { struct base { virtual int get() { return 10; } - virtual ~base() { } + virtual ~base() {} }; -struct derived: public base { +struct derived : public base { int get() override { return 100; } - ~derived() override { } + ~derived() override {} }; } // namespace helpers @@ -247,8 +247,8 @@ TEST_CASE("from_unique_ptr_derived+as_unique_ptr_base", "[E]") { } TEST_CASE("from_unique_ptr_derived+as_unique_ptr_base2", "[E]") { - std::unique_ptr> - orig_owner(new helpers::derived()); + std::unique_ptr> orig_owner( + new helpers::derived()); auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); REQUIRE(orig_owner.get() == nullptr); REQUIRE_THROWS_WITH( From 0e2fa2eac969eb261c7f465ccb0da7606e8095d2 Mon Sep 17 00:00:00 2001 From: Xiaofei Wang <6218006+wangxf123456@users.noreply.github.com> Date: Mon, 27 Jun 2022 18:08:16 -0700 Subject: [PATCH 4/6] Fix clang tidy --- tests/pure_cpp/smart_holder_poc_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/pure_cpp/smart_holder_poc_test.cpp b/tests/pure_cpp/smart_holder_poc_test.cpp index 197e29fbf8..1ddf2c27f5 100644 --- a/tests/pure_cpp/smart_holder_poc_test.cpp +++ b/tests/pure_cpp/smart_holder_poc_test.cpp @@ -39,12 +39,12 @@ struct indestructible_int { struct base { virtual int get() { return 10; } - virtual ~base() {} + virtual ~base() = default; }; struct derived : public base { int get() override { return 100; } - ~derived() override {} + ~derived() override = default; }; } // namespace helpers From 511cbbfbfe1fd43ae3230466f20c72a77f0d921a Mon Sep 17 00:00:00 2001 From: Xiaofei Wang <6218006+wangxf123456@users.noreply.github.com> Date: Mon, 27 Jun 2022 18:12:56 -0700 Subject: [PATCH 5/6] Resolve comments --- tests/pure_cpp/smart_holder_poc_test.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/pure_cpp/smart_holder_poc_test.cpp b/tests/pure_cpp/smart_holder_poc_test.cpp index 1ddf2c27f5..aebd58818d 100644 --- a/tests/pure_cpp/smart_holder_poc_test.cpp +++ b/tests/pure_cpp/smart_holder_poc_test.cpp @@ -44,7 +44,6 @@ struct base { struct derived : public base { int get() override { return 100; } - ~derived() override = default; }; } // namespace helpers @@ -237,7 +236,7 @@ TEST_CASE("from_unique_ptr+as_shared_ptr", "[S]") { REQUIRE(*new_owner == 19); } -TEST_CASE("from_unique_ptr_derived+as_unique_ptr_base", "[E]") { +TEST_CASE("from_unique_ptr_derived+as_unique_ptr_base", "[S]") { std::unique_ptr orig_owner(new helpers::derived()); auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); REQUIRE(orig_owner.get() == nullptr); @@ -246,7 +245,7 @@ TEST_CASE("from_unique_ptr_derived+as_unique_ptr_base", "[E]") { REQUIRE(new_owner->get() == 100); } -TEST_CASE("from_unique_ptr_derived+as_unique_ptr_base2", "[E]") { +TEST_CASE("from_unique_ptr_derived+as_unique_ptr_base2", "[S]") { std::unique_ptr> orig_owner( new helpers::derived()); auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); From 85fd46ed48fdb755a90392501a0191354ea3cab3 Mon Sep 17 00:00:00 2001 From: Xiaofei Wang <6218006+wangxf123456@users.noreply.github.com> Date: Mon, 27 Jun 2022 18:54:06 -0700 Subject: [PATCH 6/6] Resolve comments --- include/pybind11/detail/smart_holder_poc.h | 10 +++++----- tests/pure_cpp/smart_holder_poc_test.cpp | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/pybind11/detail/smart_holder_poc.h b/include/pybind11/detail/smart_holder_poc.h index 0b16e4117f..de92f4156f 100644 --- a/include/pybind11/detail/smart_holder_poc.h +++ b/include/pybind11/detail/smart_holder_poc.h @@ -181,11 +181,11 @@ struct smart_holder { + ")."); } ensure_vptr_is_using_builtin_delete(context); - } else if (!(*rtti_requested == *rtti_uqp_del)) { - if (!(vptr_is_using_builtin_delete && is_std_default_delete(*rtti_requested))) { - throw std::invalid_argument(std::string("Incompatible unique_ptr deleter (") - + context + ")."); - } + } else if (!(*rtti_requested == *rtti_uqp_del) + && !(vptr_is_using_builtin_delete + && is_std_default_delete(*rtti_requested))) { + throw std::invalid_argument(std::string("Incompatible unique_ptr deleter (") + context + + ")."); } } diff --git a/tests/pure_cpp/smart_holder_poc_test.cpp b/tests/pure_cpp/smart_holder_poc_test.cpp index aebd58818d..8a625a5c15 100644 --- a/tests/pure_cpp/smart_holder_poc_test.cpp +++ b/tests/pure_cpp/smart_holder_poc_test.cpp @@ -245,7 +245,7 @@ TEST_CASE("from_unique_ptr_derived+as_unique_ptr_base", "[S]") { REQUIRE(new_owner->get() == 100); } -TEST_CASE("from_unique_ptr_derived+as_unique_ptr_base2", "[S]") { +TEST_CASE("from_unique_ptr_derived+as_unique_ptr_base2", "[E]") { std::unique_ptr> orig_owner( new helpers::derived()); auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));