type_caster_generic: fix compiler error when casting a T that is implicitly convertible from T* (#5873)

* type_caster_generic: fix compiler error when casting a T that is implicitly convertible from T*

* style: pre-commit fixes

* Placate clang-tidy

* Expand NOLINT to specify Clang-Tidy check names

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
This commit is contained in:
Joshua Oreman
2025-10-15 12:10:50 -04:00
committed by GitHub
parent a2c59711b2
commit cc36ac51a0
2 changed files with 22 additions and 3 deletions

View File

@@ -563,7 +563,7 @@ struct cast_sources {
// Use the given pointer with its compile-time type, possibly downcast
// via polymorphic_type_hook()
template <typename itype>
cast_sources(const itype *ptr); // NOLINT(google-explicit-constructor)
explicit cast_sources(const itype *ptr);
// Use the given pointer and type
// NOLINTNEXTLINE(google-explicit-constructor)
@@ -1621,8 +1621,7 @@ public:
// that's correct in this context, so you can't use type_caster_base<A>
// to convert an unrelated B* to Python.
struct cast_sources : detail::cast_sources {
// NOLINTNEXTLINE(google-explicit-constructor)
cast_sources(const itype *ptr) : detail::cast_sources(ptr) {}
explicit cast_sources(const itype *ptr) : detail::cast_sources(ptr) {}
};
static handle cast(const itype &src, return_value_policy policy, handle parent) {
@@ -1637,6 +1636,10 @@ public:
return cast(std::addressof(src), return_value_policy::move, parent);
}
static handle cast(const itype *src, return_value_policy policy, handle parent) {
return cast(cast_sources{src}, policy, parent);
}
static handle cast(const cast_sources &srcs, return_value_policy policy, handle parent) {
return type_caster_generic::cast(srcs,
policy,
@@ -1645,6 +1648,10 @@ public:
make_move_constructor((const itype *) nullptr));
}
static handle cast_holder(const itype *src, const void *holder) {
return cast_holder(cast_sources{src}, holder);
}
static handle cast_holder(const cast_sources &srcs, const void *holder) {
auto policy = return_value_policy::take_ownership;
return type_caster_generic::cast(srcs, policy, {}, nullptr, nullptr, holder);

View File

@@ -58,6 +58,13 @@ class ForwardClass;
class Args : public py::args {};
} // namespace pr5396_forward_declared_class
struct ConvertibleFromAnything {
ConvertibleFromAnything() = default;
template <class T>
// NOLINTNEXTLINE(bugprone-forwarding-reference-overload,google-explicit-constructor)
ConvertibleFromAnything(T &&) {}
};
} // namespace test_class
static_assert(py::detail::is_same_or_base_of<py::args, py::args>::value, "");
@@ -578,6 +585,11 @@ TEST_SUBMODULE(class_, m) {
});
test_class::pr4220_tripped_over_this::bind_empty0(m);
// Regression test for compiler error that showed up in #5866
m.def("return_universal_recipient", []() -> test_class::ConvertibleFromAnything {
return test_class::ConvertibleFromAnything{};
});
}
template <int N>