brintos

brintos / llvm-project-archived public Read only

0
0
Text · 11.2 KiB · e0c5305 Raw
219 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 -Wno-deprecated-builtins -Wno-defaulted-function-deleted2 3struct DefaultedDefCtor1 {};4struct DefaultedDefCtor2 { DefaultedDefCtor2() = default; };5struct DefaultedDefCtorUninitialized1 { int x; };6struct DefaultedDefCtorUninitialized2 { int x; DefaultedDefCtorUninitialized2() = default; };7struct DeletedDefCtor { DeletedDefCtor() = delete; DeletedDefCtor(int); }; // expected-note {{explicitly marked deleted here}}8class PrivateDefCtor { PrivateDefCtor() = default; public: PrivateDefCtor(int); };9struct DeletedDtor { ~DeletedDtor() = delete; }; // expected-note 4{{explicitly marked deleted here}}10class PrivateDtor { ~PrivateDtor() = default; };11class Friend {12  Friend() = default; ~Friend() = default;13  friend struct NotDeleted6c;14  friend struct NotDeleted7i;15  friend struct NotDeleted7j;16  friend struct NotDeleted7k;17};18struct UserProvidedDefCtor { UserProvidedDefCtor() {} };19int n;20 21 22// A defaulted default constructor for a class X is defined as deleted if:23 24// - X is a union-like class that has a variant member with a non-trivial25// default constructor,26union Deleted1a { UserProvidedDefCtor u; }; // expected-note {{default constructor of 'Deleted1a' is implicitly deleted because variant field 'u' has a non-trivial default constructor}}27Deleted1a d1a; // expected-error {{implicitly-deleted default constructor}}28union NotDeleted1a { DefaultedDefCtor1 nu; };29NotDeleted1a nd1a;30union NotDeleted1b { DefaultedDefCtor2 nu; };31NotDeleted1b nd1b;32 33// - any non-static data member with no brace-or-equal-initializer is of34// reference type,35class Deleted2a {36  Deleted2a() = default;  // expected-note 4{{implicitly deleted here}}37  int &a; // expected-note 4{{because field 'a' of reference type 'int &' would not be initialized}}38};39Deleted2a d2a; // expected-error {{implicitly-deleted default constructor}}40struct Deleted2b {41  int &&b; // expected-note {{default constructor of 'Deleted2b' is implicitly deleted because field 'b' of reference type 'int &&' would not be initialized}}42};43Deleted2b d2b; // expected-error {{deleted default constructor}}44class NotDeleted2a { int &a = n; };45NotDeleted2a nd2a;46class NotDeleted2b { int &a = error; }; // expected-error {{undeclared identifier}}47NotDeleted2b nd2b;48class NotDeleted2c { int &&a = static_cast<int&&>(n); };49NotDeleted2c nd2c;50// Note: this one does not have a deleted default constructor even though the51// implicit default constructor is ill-formed!52class NotDeleted2d { int &&a = 0; }; // expected-error {{reference member 'a' binds to a temporary object}} expected-note {{default member init}}53NotDeleted2d nd2d; // expected-note {{first required here}}54 55// - any non-variant non-static data member of const qualified type (or array56// thereof) with no brace-or-equal-initializer is not const-default-constructible57class Deleted3a { const int a; }; // expected-note {{because field 'a' of const-qualified type 'const int' would not be initialized}} \58                                     expected-warning {{does not declare any constructor}} \59                                     expected-note {{will never be initialized}}60Deleted3a d3a; // expected-error {{implicitly-deleted default constructor}}61class Deleted3b { const DefaultedDefCtorUninitialized1 a[42]; }; // expected-note {{because field 'a' of const-qualified type 'const DefaultedDefCtorUninitialized1[42]' would not be initialized}}62Deleted3b d3b; // expected-error {{implicitly-deleted default constructor}}63class Deleted3c { const DefaultedDefCtorUninitialized2 a; }; // expected-note {{because field 'a' of const-qualified type 'const DefaultedDefCtorUninitialized2' would not be initialized}}64Deleted3c d3c; // expected-error {{implicitly-deleted default constructor}}65class NotDeleted3a { const int a = 0; };66NotDeleted3a nd3a;67class NotDeleted3b { const DefaultedDefCtorUninitialized1 a[42] = {}; };68NotDeleted3b nd3b;69class NotDeleted3c { const DefaultedDefCtorUninitialized2 a = DefaultedDefCtorUninitialized2(); };70NotDeleted3c nd3c;71union NotDeleted3d { const int a; int b; };72NotDeleted3d nd3d;73union NotDeleted3e { const DefaultedDefCtor1 a[42]; int b; };74NotDeleted3e nd3e;75union NotDeleted3f { const DefaultedDefCtor2 a; int b; };76NotDeleted3f nd3f;77struct NotDeleted3g { union { const int a; int b; }; };78NotDeleted3g nd3g;79struct NotDeleted3h { const DefaultedDefCtor1 a[42]; };80NotDeleted3h nd3h;81struct NotDeleted3i { const DefaultedDefCtor2 a; };82NotDeleted3i nd3i;83 84// - X is a union and all of its variant members are of const-qualified type (or85// array thereof),86union Deleted4a {87  const int a;88  const int b;89  const UserProvidedDefCtor c; // expected-note {{because variant field 'c' has a non-trivial default constructor}}90};91Deleted4a d4a; // expected-error {{implicitly-deleted default constructor}}92union NotDeleted4a { const int a; int b; };93NotDeleted4a nd4a;94 95// - X is a non-union class and all members of any anonymous union member are of96// const-qualified type (or array thereof),97struct Deleted5a {98  union { const int a; }; // expected-note {{because all data members of an anonymous union member are const-qualified}}99  union { int b; };100};101Deleted5a d5a; // expected-error {{implicitly-deleted default constructor}}102struct NotDeleted5a { union { const int a; int b; }; union { const int c; int d; }; };103NotDeleted5a nd5a;104 105// - any direct or virtual base class, or non-static data member with no106// brace-or-equal-initializer, has class type M (or array thereof) and either107// M has no default constructor or overload resolution as applied to M's default108// constructor results in an ambiguity or in a function that is deleted or109// inaccessible from the defaulted default constructor, or110struct Deleted6a : Deleted2a {}; // expected-note {{because base class 'Deleted2a' has a deleted default constructor}}111Deleted6a d6a; // expected-error {{implicitly-deleted default constructor}}112struct Deleted6b : virtual Deleted2a {}; // expected-note {{because base class 'Deleted2a' has a deleted default constructor}}113Deleted6b d6b; // expected-error {{implicitly-deleted default constructor}}114struct Deleted6c { Deleted2a a; }; // expected-note {{because field 'a' has a deleted default constructor}}115Deleted6c d6c; // expected-error {{implicitly-deleted default constructor}}116struct Deleted6d { DeletedDefCtor a; }; // expected-note {{because field 'a' has a deleted default constructor}}117Deleted6d d6d; // expected-error {{implicitly-deleted default constructor}}118struct NotDeleted6a { DeletedDefCtor a = 0; };119NotDeleted6a nd6a;120struct Deleted6e { PrivateDefCtor a; }; // expected-note {{because field 'a' has an inaccessible default constructor}}121Deleted6e d6e; // expected-error {{implicitly-deleted default constructor}}122struct NotDeleted6b { PrivateDefCtor a = 0; };123NotDeleted6b nd6b;124struct NotDeleted6c { Friend a; };125NotDeleted6c nd6c;126 127// - any direct or virtual base class or non-static data member has a type with128// a destructor that is deleted or inaccessible from the defaulted default129// constructor.130struct Deleted7a : DeletedDtor {}; // expected-note {{because base class 'DeletedDtor' has a deleted destructor}}131Deleted7a d7a; // expected-error {{implicitly-deleted default constructor}}132struct Deleted7b : virtual DeletedDtor {}; // expected-note {{because base class 'DeletedDtor' has a deleted destructor}}133Deleted7b d7b; // expected-error {{implicitly-deleted default constructor}}134struct Deleted7c { DeletedDtor a; }; // expected-note {{because field 'a' has a deleted destructor}}135Deleted7c d7c; // expected-error {{implicitly-deleted default constructor}}136struct Deleted7d { DeletedDtor a = {}; }; // expected-note {{because field 'a' has a deleted destructor}}137Deleted7d d7d; // expected-error {{implicitly-deleted default constructor}}138struct Deleted7e : PrivateDtor {}; // expected-note {{base class 'PrivateDtor' has an inaccessible destructor}}139Deleted7e d7e; // expected-error {{implicitly-deleted default constructor}}140struct Deleted7f : virtual PrivateDtor {}; // expected-note {{base class 'PrivateDtor' has an inaccessible destructor}}141Deleted7f d7f; // expected-error {{implicitly-deleted default constructor}}142struct Deleted7g { PrivateDtor a; }; // expected-note {{field 'a' has an inaccessible destructor}}143Deleted7g d7g; // expected-error {{implicitly-deleted default constructor}}144struct Deleted7h { PrivateDtor a = {}; }; // expected-note {{field 'a' has an inaccessible destructor}}145Deleted7h d7h; // expected-error {{implicitly-deleted default constructor}}146struct NotDeleted7i : Friend {};147NotDeleted7i d7i;148struct NotDeleted7j : virtual Friend {};149NotDeleted7j d7j;150struct NotDeleted7k { Friend a; };151NotDeleted7k d7k;152 153 154class Trivial { static const int n = 42; };155static_assert(__has_trivial_constructor(Trivial), "Trivial is nontrivial");156 157// A default constructor is trivial if it is not user-provided and if:158class NonTrivialDefCtor1 { NonTrivialDefCtor1(); };159static_assert(!__has_trivial_constructor(NonTrivialDefCtor1), "NonTrivialDefCtor1 is trivial");160 161#define ASSERT_NONTRIVIAL_IMPL(Class, Bases, Body) \162  class Class Bases { Body }; \163  static_assert(!__has_trivial_constructor(Class), "");164#define ASSERT_NONTRIVIAL(Class, Bases, Body) \165  ASSERT_NONTRIVIAL_IMPL(Class, Bases, Body) \166  ASSERT_NONTRIVIAL_IMPL(Def ## Class, Bases, Def ## Class() = default; Body) \167  ASSERT_NONTRIVIAL_IMPL(Del ## Class, Bases, Del ## Class() = delete; Body)168 169// - its class has no virtual functions (10.3) and no virtual base classes (10.1), and170ASSERT_NONTRIVIAL(NonTrivialDefCtor2, , virtual void f();)171ASSERT_NONTRIVIAL(NonTrivialDefCtor3, : virtual Trivial, )172 173// - no non-static data member of its class has a brace-or-equal-initializer, and174ASSERT_NONTRIVIAL(NonTrivialDefCtor4, , int m = 52;)175 176// - all the direct base classes of its class have trivial default constructors, and177ASSERT_NONTRIVIAL(NonTrivialDefCtor5, : NonTrivialDefCtor1, )178 179// - for all the non-static data members of its class that are of class type (or array thereof), each such class180// has a trivial default constructor.181ASSERT_NONTRIVIAL(NonTrivialDefCtor6, , NonTrivialDefCtor1 t;)182 183// FIXME: No core issue number yet.184// - its parameter-declaration-clause is equivalent to that of an implicit declaration.185struct NonTrivialDefCtor7 {186  NonTrivialDefCtor7(...) = delete;187};188static_assert(!__has_trivial_constructor(NonTrivialDefCtor7), "");189struct NonTrivialDefCtor8 {190  NonTrivialDefCtor8(int = 0) = delete;191};192static_assert(!__has_trivial_constructor(NonTrivialDefCtor8), "");193 194// Otherwise, the default constructor is non-trivial.195 196class Trivial2 { Trivial2() = delete; };197static_assert(__has_trivial_constructor(Trivial2), "Trivial2 is trivial");198 199class Trivial3 { Trivial3() = default; };200static_assert(__has_trivial_constructor(Trivial3), "Trivial3 is trivial");201 202template<typename T> class Trivial4 { Trivial4() = default; };203static_assert(__has_trivial_constructor(Trivial4<int>), "Trivial4 is trivial");204 205template<typename T> class Trivial5 { Trivial5() = delete; };206static_assert(__has_trivial_constructor(Trivial5<int>), "Trivial5 is trivial");207 208namespace PR14558 {209  // Ensure we determine whether an explicitly-defaulted or deleted special210  // member is trivial before we return to parsing the containing class.211  struct A {212    struct B { B() = default; } b;213    struct C { C() = delete; } c;214  };215 216  static_assert(__has_trivial_constructor(A), "");217  static_assert(__has_trivial_constructor(A::B), "");218}219