diff --git a/DRAFT.md b/DRAFT.md index 9be125c..9f953ee 100644 --- a/DRAFT.md +++ b/DRAFT.md @@ -405,20 +405,23 @@ C++26. The member functions generated from `I` share a scope with the members `protocol` declares itself: `allocator_type`, `get_allocator` and `swap`. An -interface with a non-static member function of one of these names is -ill-formed. If it were allowed, the member of `protocol` would hide the -generated one without a diagnostic, and `protocol` would differ from -`protocol_view`, which declares no named members and would still expose the -function from `I`. +interface with a public member of one of these names is ill-formed, whatever +kind of member it is. If a non-static member function of that name were +allowed, the member of `protocol` would hide the generated one without a +diagnostic, and `protocol` would differ from `protocol_view`, which +declares no named members and would still expose the function from `I`. A +static member function, a data member or a nested type hides nothing, as only +non-static member functions are forwarded; it is ill-formed too so that the +rule does not depend on which members are forwarded. `get_allocator` and `swap` are members for consistency with `polymorphic` and the containers. An operation that need not be a member, such as `valueless_after_move`, is a hidden friend so that it reserves no name. -An interface member function named `protocol` or `protocol_view` is also -ill-formed: the class's own name is found in its scope, so it would hide the -generated member. Names an implementation uses internally are reserved -identifiers and cannot collide with an interface. +A public interface member named `protocol` or `protocol_view` is also +ill-formed: the class's own name is found in its scope, so it would hide a +generated member of that name. Names an implementation uses internally are +reserved identifiers and cannot collide with an interface. ### Function-like examples diff --git a/xyz/detail/conformance.hh b/xyz/detail/conformance.hh index c6b76d7..2b25cb1 100644 --- a/xyz/detail/conformance.hh +++ b/xyz/detail/conformance.hh @@ -237,6 +237,39 @@ consteval bool has_reserved_member_name(std::meta::info member) { std::ranges::contains(reserved_class_names, identifier_of(member))); } +// Throws if `type` declares a public member with a reserved name, whatever +// the member's kind. Only a non-static member function is forwarded and so +// hidden; rejecting every kind keeps the rule independent of what is +// forwarded. `members_of` returns neither the enumerators of an unscoped +// enumeration nor the members of an anonymous union, which are members of +// `type` too, so those are visited here. Not a template, for the reason given +// on `named_member_function_infos`. +consteval void reject_reserved_member_names(std::meta::info type) { + // GCC 16 Workaround: iterating the vector as a range-for temporary in this + // recursion reads it outside its lifetime during constant evaluation. + std::vector members = + members_of(type, std::meta::access_context::unprivileged()); + for (std::meta::info member : members) { + if (has_reserved_member_name(member)) { + reject_interface_member("reserved member name", member); + } + if (is_type(member) && !is_type_alias(member) && is_enum_type(member) && + !is_scoped_enum_type(member)) { + for (std::meta::info enumerator : enumerators_of(member)) { + if (has_reserved_member_name(enumerator)) { + reject_interface_member("reserved member name", enumerator); + } + } + } + // A non-static data member with no name that is not a bit-field is an + // anonymous union. + if (is_nonstatic_data_member(member) && !has_identifier(member) && + !is_bit_field(member)) { + reject_reserved_member_names(type_of(member)); + } + } +} + // Throws if `type` has a non-static member function template, operator // template or conversion function template. A template cannot be forwarded // through a vtable, and `named_member_function_infos` does not return one, so @@ -302,14 +335,15 @@ consteval void reject_placeholder_conversion_functions() { // The named, non-static, non-special member functions, operators and // conversion functions of `Type`, in declaration order. -// A `Type` with a base class is unsupported. -// Functions with a name in `reserved_member_names`, ref-qualified functions, -// explicit-object member functions, member function templates, conversion -// functions to `auto` or `decltype(auto)`, defaulted comparison operators, -// comparisons with `Type` and the operators `is_unsupported_operator` names -// are unsupported on protocol interfaces. -// Static member functions of `Type` are ignored; a static member function of -// a candidate can still conform, see `member_function_conforms_to`. The +// A `Type` with a base class, or with a public member of any kind that has a +// reserved name, is unsupported. +// Ref-qualified functions, explicit-object member functions, member function +// templates, conversion functions to `auto` or `decltype(auto)`, defaulted +// comparison operators, comparisons with `Type` and the operators +// `is_unsupported_operator` names are unsupported on protocol interfaces. +// Other static member functions of `Type` are ignored; a static member +// function of a candidate can still conform, see +// `member_function_conforms_to`. The // ignored functions include `operator new`, `operator delete` and their array // forms, which are static without being declared so: like `std::polymorphic`, // `protocol` allocates only through its allocator, so a class-specific @@ -317,14 +351,12 @@ consteval void reject_placeholder_conversion_functions() { template consteval std::vector protocol_interface_function_infos() { reject_base_classes(Type); + reject_reserved_member_names(Type); reject_member_function_templates(Type); reject_placeholder_conversion_functions(); std::vector result; for (std::meta::info member : conformance_candidates_of) { if (is_static_member(member)) continue; - if (has_reserved_member_name(member)) { - reject_interface_member("reserved member name", member); - } if (is_lvalue_reference_qualified(member) || is_rvalue_reference_qualified(member)) { reject_interface_member("ref-qualified member function", member); diff --git a/xyz/tests/conformance_tests.cc b/xyz/tests/conformance_tests.cc index a2cdee6..3113ffa 100644 --- a/xyz/tests/conformance_tests.cc +++ b/xyz/tests/conformance_tests.cc @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -644,6 +645,47 @@ struct InterfaceWithNamedMemberFunctionTemplate { void named_template(T); }; +// At namespace scope because a local class cannot declare a static data +// member. `[[maybe_unused]]` because Clang warns about an unused variable with +// internal linkage. +struct InterfaceWithReservedStaticDataMember { + [[maybe_unused]] static const int get_allocator; +}; + +// At namespace scope because a local class cannot declare a member template. +struct InterfaceWithReservedStaticMemberFunctionTemplate { + template + static void swap(T&, T&); +}; + +namespace outside { + +// NOLINTBEGIN(cppcoreguidelines-use-enum-class): an unscoped enumeration is +// what's under test. +enum Enumeration : std::uint8_t { swap }; + +// NOLINTEND(cppcoreguidelines-use-enum-class) + +} // namespace outside + +// The next three are at namespace scope because GCC warns about an unused +// type alias in a local class. +struct InterfaceWithReservedTypeAlias { + using allocator_type = int; + int f() const; +}; + +struct InterfaceWithReservedClassNameTypeAlias { + using protocol_view = int; + int f() const; +}; + +// The enumerators of an enumeration declared elsewhere are not members. +struct InterfaceWithEnumerationAlias { + using Kind = outside::Enumeration; + int f() const; +}; + TEST(ConformsToTest, ReservedMemberNamesAreRejected) { struct SwapInterface { void swap(SwapInterface&) noexcept; @@ -765,9 +807,47 @@ TEST(ConformsToTest, CandidateWithBaseClassIsNotRejected) { static_assert(is_protocol_conformant_v); } -TEST(ConformsToTest, StaticInterfaceMemberWithReservedNameIsIgnored) { - struct Interface { - static void swap(Interface&, Interface&) noexcept; +// Only a non-static member function would be hidden, but the rule does not +// depend on the kind of member. +TEST(ConformsToTest, ReservedNameIsRejectedWhateverTheMemberKind) { + struct StaticMemberFunctionInterface { + static void swap(StaticMemberFunctionInterface&, + StaticMemberFunctionInterface&) noexcept; + int f() const; + }; + + struct DataMemberInterface { + int swap; + int f() const; + }; + + struct NestedClassInterface { + struct get_allocator {}; + + int f() const; + }; + + struct ScopedEnumerationInterface { + enum class swap : std::uint8_t {}; + int f() const; + }; + + // NOLINTBEGIN(cppcoreguidelines-use-enum-class): an unscoped enumeration is + // what's under test. + struct EnumeratorInterface { + enum : std::uint8_t { first, swap }; + + int f() const; + }; + + // NOLINTEND(cppcoreguidelines-use-enum-class) + + struct AnonymousUnionInterface { + union { + int first; + int swap; + }; + int f() const; }; @@ -775,7 +855,81 @@ TEST(ConformsToTest, StaticInterfaceMemberWithReservedNameIsIgnored) { int f() const { return 0; } }; - static_assert(is_protocol_conformant()); +#ifdef __cpp_constexpr_exceptions + static_assert( + conformance_check_rejects()); + static_assert(conformance_check_rejects()); + static_assert( + conformance_check_rejects()); + static_assert(conformance_check_rejects()); + static_assert( + conformance_check_rejects()); + static_assert(conformance_check_rejects()); + static_assert( + conformance_check_rejects()); + static_assert( + conformance_check_rejects()); + static_assert(conformance_check_rejects()); + static_assert( + conformance_check_rejects< + InterfaceWithReservedStaticMemberFunctionTemplate, Candidate>()); + static_assert( + conformance_rejection_message_contains( + "reserved member name 'allocator_type'")); + static_assert( + conformance_rejection_message_contains( + "reserved member name 'swap'")); + static_assert(conformance_rejection_message_contains( + "reserved member name 'swap'")); +#endif // __cpp_constexpr_exceptions +} + +// A reserved name that is not the name of a public member of the interface +// is not rejected. +TEST(ConformsToTest, ReservedNameThatIsNotAPublicMemberNameIsNotRejected) { + class PrivateMemberInterface { + static void swap(PrivateMemberInterface&, PrivateMemberInterface&) noexcept; + int get_allocator() const; + + public: + int f() const; + }; + + struct ScopedEnumeratorInterface { + enum class Kind : std::uint8_t { swap }; + int f() const; + }; + + // A union with a name is not anonymous, so its members are not members of + // the interface. + struct NamedUnionInterface { + union { + int swap; + } storage; + + int f() const; + }; + + struct BitFieldInterface { + int flag : 1; + int : 3; + int f() const; + }; + + struct Candidate { + int f() const { return 0; } + }; + + static_assert(is_protocol_conformant()); + static_assert(is_protocol_conformant()); + static_assert( + is_protocol_conformant()); + static_assert(is_protocol_conformant()); + static_assert(is_protocol_conformant()); } // clang-p2996 Workaround: the fork's `members_of` returns private type