1a5728872c7702ddd09537c95bc3cbd20e1f2fb09Daniel Dunbar// RUN: %clang_cc1 -fsyntax-only -verify %s
21f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor
33ef84e157574b52ff8cbf6f49bf92047eb3f2f86Mike Stumpvoid abort() __attribute__((noreturn));
41f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor
51f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregorclass Okay {
61f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  int a_;
71f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor};
81f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor
91f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregorclass Virtual {
10772afb15e9602de562ef740d4d91c083a31e1162John McCall  virtual void foo() { abort(); } // expected-note 4 {{because type 'Virtual' has a virtual member function}}
111f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor};
121f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor
13772afb15e9602de562ef740d4d91c083a31e1162John McCallclass VirtualBase : virtual Okay { // expected-note 4 {{because type 'VirtualBase' has a virtual base class}}
141f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor};
151f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor
161f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregorclass Ctor {
17772afb15e9602de562ef740d4d91c083a31e1162John McCall  Ctor() { abort(); } // expected-note 4 {{because type 'Ctor' has a user-declared constructor}}
181f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor};
1938fd4d001182b0b125a42c3b7e83e9dbac50e8a5Sebastian Redlclass Ctor2 {
207c2342dd4c9947806842e5aca3d2bb2e542853c9John McCall  Ctor2(); // expected-note 3 {{because type 'Ctor2' has a user-declared constructor}}
2138fd4d001182b0b125a42c3b7e83e9dbac50e8a5Sebastian Redl};
221f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor
231f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregorclass CopyCtor {
24772afb15e9602de562ef740d4d91c083a31e1162John McCall  CopyCtor(CopyCtor &cc) { abort(); } // expected-note 4 {{because type 'CopyCtor' has a user-declared copy constructor}}
251f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor};
261f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor
271f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor// FIXME: this should eventually trigger on the operator's declaration line
28772afb15e9602de562ef740d4d91c083a31e1162John McCallclass CopyAssign { // expected-note 4 {{because type 'CopyAssign' has a user-declared copy assignment operator}}
291f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  CopyAssign& operator=(CopyAssign& CA) { abort(); }
301f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor};
311f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor
321f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregorclass Dtor {
33772afb15e9602de562ef740d4d91c083a31e1162John McCall  ~Dtor() { abort(); } // expected-note 4 {{because type 'Dtor' has a user-declared destructor}}
341f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor};
351f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor
361f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregorunion U1 {
371f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  Virtual v; // expected-error {{union member 'v' has a non-trivial copy constructor}}
381f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  VirtualBase vbase; // expected-error {{union member 'vbase' has a non-trivial copy constructor}}
391f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  Ctor ctor; // expected-error {{union member 'ctor' has a non-trivial constructor}}
4038fd4d001182b0b125a42c3b7e83e9dbac50e8a5Sebastian Redl  Ctor2 ctor2; // expected-error {{union member 'ctor2' has a non-trivial constructor}}
411f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  CopyCtor copyctor; // expected-error {{union member 'copyctor' has a non-trivial copy constructor}}
421f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  CopyAssign copyassign; // expected-error {{union member 'copyassign' has a non-trivial copy assignment operator}}
431f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  Dtor dtor; // expected-error {{union member 'dtor' has a non-trivial destructor}}
441f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  Okay okay;
451f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor};
461f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor
471f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregorunion U2 {
481f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  struct {
4973061d054128e486e70e0f2874b23d6eca067e5bJohn McCall    Virtual v; // expected-note {{because type 'U2::<anonymous struct}}
501f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  } m1; // expected-error {{union member 'm1' has a non-trivial copy constructor}}
511f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  struct {
5273061d054128e486e70e0f2874b23d6eca067e5bJohn McCall    VirtualBase vbase; // expected-note {{because type 'U2::<anonymous struct}}
531f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  } m2; // expected-error {{union member 'm2' has a non-trivial copy constructor}}
541f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  struct {
5573061d054128e486e70e0f2874b23d6eca067e5bJohn McCall    Ctor ctor; // expected-note {{because type 'U2::<anonymous struct}}
561f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  } m3; // expected-error {{union member 'm3' has a non-trivial constructor}}
571f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  struct {
5873061d054128e486e70e0f2874b23d6eca067e5bJohn McCall    Ctor2 ctor2; // expected-note {{because type 'U2::<anonymous struct}}
5938fd4d001182b0b125a42c3b7e83e9dbac50e8a5Sebastian Redl  } m3a; // expected-error {{union member 'm3a' has a non-trivial constructor}}
6038fd4d001182b0b125a42c3b7e83e9dbac50e8a5Sebastian Redl  struct {
6173061d054128e486e70e0f2874b23d6eca067e5bJohn McCall    CopyCtor copyctor; // expected-note {{because type 'U2::<anonymous struct}}
621f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  } m4; // expected-error {{union member 'm4' has a non-trivial copy constructor}}
631f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  struct {
6473061d054128e486e70e0f2874b23d6eca067e5bJohn McCall    CopyAssign copyassign; // expected-note {{because type 'U2::<anonymous struct}}
651f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  } m5; // expected-error {{union member 'm5' has a non-trivial copy assignment operator}}
661f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  struct {
6773061d054128e486e70e0f2874b23d6eca067e5bJohn McCall    Dtor dtor; // expected-note {{because type 'U2::<anonymous struct}}
681f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  } m6; // expected-error {{union member 'm6' has a non-trivial destructor}}
691f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  struct {
701f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor    Okay okay;
711f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  } m7;
721f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor};
731f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor
741f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregorunion U3 {
757c2342dd4c9947806842e5aca3d2bb2e542853c9John McCall  struct s1 : Virtual { // expected-note {{because type 'U3::s1' has a base class with a non-trivial copy constructor}}
761f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  } m1; // expected-error {{union member 'm1' has a non-trivial copy constructor}}
777c2342dd4c9947806842e5aca3d2bb2e542853c9John McCall  struct s2 : VirtualBase { // expected-note {{because type 'U3::s2' has a base class with a non-trivial copy constructor}}
781f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  } m2; // expected-error {{union member 'm2' has a non-trivial copy constructor}}
797c2342dd4c9947806842e5aca3d2bb2e542853c9John McCall  struct s3 : Ctor { // expected-note {{because type 'U3::s3' has a base class with a non-trivial constructor}}
801f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  } m3; // expected-error {{union member 'm3' has a non-trivial constructor}}
817c2342dd4c9947806842e5aca3d2bb2e542853c9John McCall  struct s3a : Ctor2 { // expected-note {{because type 'U3::s3a' has a base class with a non-trivial constructor}}
8238fd4d001182b0b125a42c3b7e83e9dbac50e8a5Sebastian Redl  } m3a; // expected-error {{union member 'm3a' has a non-trivial constructor}}
837c2342dd4c9947806842e5aca3d2bb2e542853c9John McCall  struct s4 : CopyCtor { // expected-note {{because type 'U3::s4' has a base class with a non-trivial copy constructor}}
841f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  } m4; // expected-error {{union member 'm4' has a non-trivial copy constructor}}
857c2342dd4c9947806842e5aca3d2bb2e542853c9John McCall  struct s5 : CopyAssign { // expected-note {{because type 'U3::s5' has a base class with a non-trivial copy assignment operator}}
861f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  } m5; // expected-error {{union member 'm5' has a non-trivial copy assignment operator}}
877c2342dd4c9947806842e5aca3d2bb2e542853c9John McCall  struct s6 : Dtor { // expected-note {{because type 'U3::s6' has a base class with a non-trivial destructor}}
881f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  } m6; // expected-error {{union member 'm6' has a non-trivial destructor}}
891f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  struct s7 : Okay {
901f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  } m7;
911f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor};
921f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor
93dfdfc584f2a8d9f1eebd6e6eaa9b1bbff519d8f9Anders Carlssonunion U4 {
94dfdfc584f2a8d9f1eebd6e6eaa9b1bbff519d8f9Anders Carlsson  static int i1; // expected-error {{static data member 'i1' not allowed in union}}
95dfdfc584f2a8d9f1eebd6e6eaa9b1bbff519d8f9Anders Carlsson};
96dfdfc584f2a8d9f1eebd6e6eaa9b1bbff519d8f9Anders Carlsson
97dfdfc584f2a8d9f1eebd6e6eaa9b1bbff519d8f9Anders Carlssonunion U5 {
98dfdfc584f2a8d9f1eebd6e6eaa9b1bbff519d8f9Anders Carlsson  int& i1; // expected-error {{union member 'i1' has reference type 'int &'}}
99dfdfc584f2a8d9f1eebd6e6eaa9b1bbff519d8f9Anders Carlsson};
100dfdfc584f2a8d9f1eebd6e6eaa9b1bbff519d8f9Anders Carlsson
1011f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregortemplate <class A, class B> struct Either {
1021f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  bool tag;
103772afb15e9602de562ef740d4d91c083a31e1162John McCall  union { // expected-note 6 {{in instantiation of member class}}
1041f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor    A a;
105772afb15e9602de562ef740d4d91c083a31e1162John McCall    B b; // expected-error 6 {{non-trivial}}
1061f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  };
1071f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor
108772afb15e9602de562ef740d4d91c083a31e1162John McCall  Either(const A& a) : tag(true), a(a) {}
109772afb15e9602de562ef740d4d91c083a31e1162John McCall  Either(const B& b) : tag(false), b(b) {}
1101f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor};
1111f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor
1121f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregorvoid fred() {
113772afb15e9602de562ef740d4d91c083a31e1162John McCall  Either<int,Virtual> virt(0); // expected-note {{in instantiation of template}}
114772afb15e9602de562ef740d4d91c083a31e1162John McCall  Either<int,VirtualBase> vbase(0); // expected-note {{in instantiation of template}}
115772afb15e9602de562ef740d4d91c083a31e1162John McCall  Either<int,Ctor> ctor(0); // expected-note {{in instantiation of template}}
116772afb15e9602de562ef740d4d91c083a31e1162John McCall  Either<int,CopyCtor> copyctor(0); // expected-note {{in instantiation of template}}
117772afb15e9602de562ef740d4d91c083a31e1162John McCall  Either<int,CopyAssign> copyassign(0); // expected-note {{in instantiation of template}}
118772afb15e9602de562ef740d4d91c083a31e1162John McCall  Either<int,Dtor> dtor(0); // expected-note {{in instantiation of template}}
1191f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor  Either<int,Okay> okay(0);
1201f2023ab8b35e0f665eb6c0f11dd6d9b9bca12b8Douglas Gregor}
121