Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions DRAFT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<I>` would differ from
`protocol_view<I>`, 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<I>` would differ from `protocol_view<I>`, 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

Expand Down
54 changes: 43 additions & 11 deletions xyz/detail/conformance.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::meta::info> 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
Expand Down Expand Up @@ -302,29 +335,28 @@ 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
// allocation function has nothing to do.
template <std::meta::info Type>
consteval std::vector<std::meta::info> protocol_interface_function_infos() {
reject_base_classes(Type);
reject_reserved_member_names(Type);
reject_member_function_templates(Type);
reject_placeholder_conversion_functions<typename[:Type:]>();
std::vector<std::meta::info> result;
for (std::meta::info member : conformance_candidates_of<Type>) {
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);
Expand Down
162 changes: 158 additions & 4 deletions xyz/tests/conformance_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <algorithm>
#include <compare>
#include <cstddef>
#include <cstdint>
#include <meta>
#include <span>
#include <stdexcept>
Expand Down Expand Up @@ -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 <typename T>
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;
Expand Down Expand Up @@ -765,17 +807,129 @@ TEST(ConformsToTest, CandidateWithBaseClassIsNotRejected) {
static_assert(is_protocol_conformant_v<Interface, Candidate>);
}

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;
};

struct Candidate {
int f() const { return 0; }
};

static_assert(is_protocol_conformant<Interface, Candidate>());
#ifdef __cpp_constexpr_exceptions
static_assert(
conformance_check_rejects<StaticMemberFunctionInterface, Candidate>());
static_assert(conformance_check_rejects<DataMemberInterface, Candidate>());
static_assert(
conformance_check_rejects<InterfaceWithReservedTypeAlias, Candidate>());
static_assert(conformance_check_rejects<NestedClassInterface, Candidate>());
static_assert(
conformance_check_rejects<ScopedEnumerationInterface, Candidate>());
static_assert(conformance_check_rejects<EnumeratorInterface, Candidate>());
static_assert(
conformance_check_rejects<AnonymousUnionInterface, Candidate>());
static_assert(
conformance_check_rejects<InterfaceWithReservedClassNameTypeAlias,
Candidate>());
static_assert(conformance_check_rejects<InterfaceWithReservedStaticDataMember,
Candidate>());
static_assert(
conformance_check_rejects<
InterfaceWithReservedStaticMemberFunctionTemplate, Candidate>());
static_assert(
conformance_rejection_message_contains<InterfaceWithReservedTypeAlias,
Candidate>(
"reserved member name 'allocator_type'"));
static_assert(
conformance_rejection_message_contains<EnumeratorInterface, Candidate>(
"reserved member name 'swap'"));
static_assert(conformance_rejection_message_contains<AnonymousUnionInterface,
Candidate>(
"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<PrivateMemberInterface, Candidate>());
static_assert(is_protocol_conformant<ScopedEnumeratorInterface, Candidate>());
static_assert(
is_protocol_conformant<InterfaceWithEnumerationAlias, Candidate>());
static_assert(is_protocol_conformant<NamedUnionInterface, Candidate>());
static_assert(is_protocol_conformant<BitFieldInterface, Candidate>());
}

// clang-p2996 Workaround: the fork's `members_of` returns private type
Expand Down
Loading