fix: add guard for GCC <10.3 on C++20 (#5205)

* Update CI

* update define guard

* style: pre-commit fixes

* updated define guard

* style: pre-commit fixes

* update guard

* testing new guards

* update guards

* surely this time

* style: pre-commit fixes

* Define PYBIND11_TYPING_H_HAS_STRING_LITERAL to avoid repeating a complex expression.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
This commit is contained in:
Michael Carlstrom 2024-06-28 01:20:28 -04:00 committed by Henry Schreiner
parent 3074608ebc
commit 65afa13eb0
No known key found for this signature in database
GPG Key ID: B9D0E45146A241E8
4 changed files with 13 additions and 8 deletions

View File

@ -501,7 +501,9 @@ jobs:
- { gcc: 7, std: 17 } - { gcc: 7, std: 17 }
- { gcc: 8, std: 14 } - { gcc: 8, std: 14 }
- { gcc: 8, std: 17 } - { gcc: 8, std: 17 }
- { gcc: 9, std: 20 }
- { gcc: 10, std: 17 } - { gcc: 10, std: 17 }
- { gcc: 10, std: 20 }
- { gcc: 11, std: 20 } - { gcc: 11, std: 20 }
- { gcc: 12, std: 20 } - { gcc: 12, std: 20 }
- { gcc: 13, std: 20 } - { gcc: 13, std: 20 }

View File

@ -98,7 +98,10 @@ class Never : public none {
using none::none; using none::none;
}; };
#if defined(__cpp_nontype_template_parameter_class) #if defined(__cpp_nontype_template_parameter_class) \
&& (/* See #5201 */ !defined(__GNUC__) \
|| (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3)))
# define PYBIND11_TYPING_H_HAS_STRING_LITERAL
template <size_t N> template <size_t N>
struct StringLiteral { struct StringLiteral {
constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); } constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); }
@ -222,7 +225,7 @@ struct handle_type_name<typing::Never> {
static constexpr auto name = const_name("Never"); static constexpr auto name = const_name("Never");
}; };
#if defined(__cpp_nontype_template_parameter_class) #if defined(PYBIND11_TYPING_H_HAS_STRING_LITERAL)
template <typing::StringLiteral... Literals> template <typing::StringLiteral... Literals>
struct handle_type_name<typing::Literal<Literals...>> { struct handle_type_name<typing::Literal<Literals...>> {
static constexpr auto name = const_name("Literal[") static constexpr auto name = const_name("Literal[")

View File

@ -109,7 +109,7 @@ void m_defs(py::module_ &m) {
} // namespace handle_from_move_only_type_with_operator_PyObject } // namespace handle_from_move_only_type_with_operator_PyObject
#if defined(__cpp_nontype_template_parameter_class) #if defined(PYBIND11_TYPING_H_HAS_STRING_LITERAL)
namespace literals { namespace literals {
enum Color { RED = 0, BLUE = 1 }; enum Color { RED = 0, BLUE = 1 };
@ -905,7 +905,7 @@ TEST_SUBMODULE(pytypes, m) {
m.def("annotate_optional_to_object", m.def("annotate_optional_to_object",
[](py::typing::Optional<int> &o) -> py::object { return o; }); [](py::typing::Optional<int> &o) -> py::object { return o; });
#if defined(__cpp_nontype_template_parameter_class) #if defined(PYBIND11_TYPING_H_HAS_STRING_LITERAL)
py::enum_<literals::Color>(m, "Color") py::enum_<literals::Color>(m, "Color")
.value("RED", literals::Color::RED) .value("RED", literals::Color::RED)
.value("BLUE", literals::Color::BLUE); .value("BLUE", literals::Color::BLUE);
@ -919,8 +919,8 @@ TEST_SUBMODULE(pytypes, m) {
m.def("annotate_listT_to_T", m.def("annotate_listT_to_T",
[](const py::typing::List<typevar::TypeVarT> &l) -> typevar::TypeVarT { return l[0]; }); [](const py::typing::List<typevar::TypeVarT> &l) -> typevar::TypeVarT { return l[0]; });
m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; }); m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; });
m.attr("if_defined__cpp_nontype_template_parameter_class") = true; m.attr("defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL") = true;
#else #else
m.attr("if_defined__cpp_nontype_template_parameter_class") = false; m.attr("defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL") = false;
#endif #endif
} }

View File

@ -1025,7 +1025,7 @@ def test_optional_object_annotations(doc):
@pytest.mark.skipif( @pytest.mark.skipif(
not m.if_defined__cpp_nontype_template_parameter_class, not m.defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL,
reason="C++20 feature not available.", reason="C++20 feature not available.",
) )
def test_literal(doc): def test_literal(doc):
@ -1036,7 +1036,7 @@ def test_literal(doc):
@pytest.mark.skipif( @pytest.mark.skipif(
not m.if_defined__cpp_nontype_template_parameter_class, not m.defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL,
reason="C++20 feature not available.", reason="C++20 feature not available.",
) )
def test_typevar(doc): def test_typevar(doc):