Skip to content

fix: returning a copyable type with a deleted move constructor - #6143

Open
henryiii wants to merge 1 commit into
pybind:masterfrom
henryiii:fix/function-ref-copy-only-return
Open

fix: returning a copyable type with a deleted move constructor#6143
henryiii wants to merge 1 commit into
pybind:masterfrom
henryiii:fix/function-ref-copy-only-return

Conversation

@henryiii

Copy link
Copy Markdown
Collaborator

🤖 AI text below 🤖

Description

Fixes #6142, a 3.1.0 regression from the call_impl outlining in #5887.

detail::function_ref's constructor SFINAE requires is_convertible<decltype(f(args...)), Ret>. For a copyable type whose move constructor is explicitly deleted, is_convertible<Ret, Ret> is false, because the trait tests conversion from an xvalue, which selects the deleted move constructor. In C++17 such a prvalue return is legal via guaranteed copy elision, so the constraint rejected code the function body compiles fine. The fix also accepts an exact type match (C++17 and later only; in C++14 such a type cannot be returned by value at all).

The regression test fails to compile without the fix and is guarded for C++17, since the C++14 CI builds cannot express the case.

Suggested changelog entry:

  • Fixed a 3.1.0 regression: functions returning a copyable type with an explicitly deleted move constructor failed to compile.

…d#6142)

detail::function_ref's constructor SFINAE required
is_convertible<Ret, Ret>, which is false for a copyable type whose move
constructor is explicitly deleted (the trait tests conversion from an
xvalue). In C++17, such a prvalue return is legal via guaranteed copy
elision, so also accept an exact type match. Regression introduced in
3.1.0 by the call_impl outlining (pybind#5887).

Fixes pybind#6142

Assisted-by: ClaudeCode:claude-fable-5
// copy elision. Accept that case explicitly. See issue #6142.
template <typename From, typename To>
using is_returnable_as = bool_constant<
#if defined(PYBIND11_CPP17)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guard with feature flag: "__cpp_guaranteed_copy_elision" :) seems more specific here and better expresses intent.

#include <iostream>

int main() {
#if defined(__cpp_guaranteed_copy_elision) && __cpp_guaranteed_copy_elision >= 201606L
    std::cout << "Guaranteed prvalue elision is supported!\n";
#else
    std::cout << "Old value categories apply (copies/moves may occur).\n";
#endif
}

Comment thread tests/test_copy_move.cpp
// #6142: returning a copy-constructible type with an explicitly deleted move
// constructor failed to compile inside detail::function_ref (3.1.0 regression).
// Returning such a type by value requires C++17 guaranteed copy elision.
#if defined(PYBIND11_CPP17)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Comment thread tests/test_copy_move.cpp
.def_static("get_one", &lacking_move_ctor::get_one, py::return_value_policy::move);

// test_copy_only_deleted_move (#6142)
#if defined(PYBIND11_CPP17)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Possible 3.1.0 regression when returning an object that has a copy constructor but no move constructor

2 participants