xref: /llvm-project/clang/test/SemaCXX/cxx11-crashes.cpp (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1 // RUN: %clang_cc1 -std=c++11 -verify %s -Wno-deprecated-builtins
2 
3 namespace rdar12240916 {
4 
5 struct S2 {
6   S2(const S2&);
7   S2();
8 };
9 
10 struct S { // expected-note {{not complete}}
11   S x; // expected-error {{incomplete type}}
12   S2 y;
13 };
14 
foo()15 S foo() {
16   S s;
17   return s;
18 }
19 
20 struct S3; // expected-note {{forward declaration}}
21 
22 struct S4 {
23   S3 x; // expected-error {{incomplete type}}
24   S2 y;
25 };
26 
27 struct S3 {
28   S4 x;
29   S2 y;
30 };
31 
foo2()32 S4 foo2() {
33   S4 s;
34   return s;
35 }
36 
37 }
38 
39 namespace rdar12542261 {
40 
41 template <class _Tp>
42 struct check_complete
43 {
44   static_assert(sizeof(_Tp) > 0, "Type must be complete.");
45 };
46 
47 
48 template<class _Rp>
49 class function // expected-note 2 {{candidate}}
50 {
51 public:
52   template<class _Fp>
53   function(_Fp, typename check_complete<_Fp>::type* = 0);  // expected-note {{candidate}}
54 };
55 
foobar()56 void foobar()
57 {
58   auto LeftCanvas = new Canvas(); // expected-error {{unknown type name}}
59   function<void()> m_OnChange = [&, LeftCanvas]() { }; // expected-error {{no viable conversion}}
60 }
61 
62 }
63 
64 namespace b6981007 {
65   struct S {}; // expected-note 3{{candidate}}
f()66   void f() {
67     S s(1, 2, 3); // expected-error {{no matching}}
68     for (auto x : s) {
69       // We used to attempt to evaluate the initializer of this variable,
70       // and crash because it has an undeduced type.
71       const int &n(x);
72       constexpr int k = sizeof(x);
73     }
74   }
75 }
76 
77 namespace incorrect_auto_type_deduction_for_typo {
78 struct S {
Sincorrect_auto_type_deduction_for_typo::S79   template <typename T> S(T t) {
80     (void)sizeof(t);
81     (void)new auto(t);
82   }
83 };
84 
85 void Foo(S);
86 
test(int some_number)87 void test(int some_number) {  // expected-note {{'some_number' declared here}}
88   auto x = sum_number;  // expected-error {{use of undeclared identifier 'sum_number'; did you mean 'some_number'?}}
89   auto lambda = [x] {};
90   Foo(lambda);
91 }
92 }
93 
94 namespace pr29091 {
95   struct X{ X(const X &x); };
96   struct Y: X { using X::X; };
foo()97   bool foo() { return __has_nothrow_constructor(Y); }
bar()98   bool bar() { return __has_nothrow_copy(Y); }
99 
100   struct A { template <typename T> A(); };
101   struct B : A { using A::A; };
baz()102   bool baz() { return __has_nothrow_constructor(B); }
qux()103   bool qux() { return __has_nothrow_copy(B); }
104 }
105 
106 namespace undeduced_field {
107 template<class T>
108 struct Foo {
109   typedef T type;
110 };
111 
112 struct Bar {
113   Bar();
114   // The missing expression makes A undeduced.
115   static constexpr auto A = ;  // expected-error {{expected expression}}
116                                // expected-error@-1 {{declaration of variable 'A' with deduced type 'const auto' requires an initializer}}
117 
118   Foo<decltype(A)>::type B;  // The type of B is also undeduced (wrapped in Elaborated).
119 };
120 
121 // This used to crash when trying to get the layout of B.
122 Bar x;
123 }
124