xref: /llvm-project/clang/test/SemaCXX/static-assert.cpp (revision 67b03de9a2d354d41eb88a801e16a7df193f1de2)
1 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 -triple=x86_64-linux-gnu
2 
3 int f(); // expected-note {{declared here}}
4 
5 static_assert(f(), "f"); // expected-error {{static_assert expression is not an integral constant expression}} expected-note {{non-constexpr function 'f' cannot be used in a constant expression}}
6 static_assert(true, "true is not false");
7 static_assert(false, "false is false"); // expected-error {{static_assert failed "false is false"}}
8 
9 void g() {
10     static_assert(false, "false is false"); // expected-error {{static_assert failed "false is false"}}
11 }
12 
13 class C {
14     static_assert(false, "false is false"); // expected-error {{static_assert failed "false is false"}}
15 };
16 
17 template<int N> struct T {
18     static_assert(N == 2, "N is not 2!"); // expected-error {{static_assert failed "N is not 2!"}}
19 };
20 
21 T<1> t1; // expected-note {{in instantiation of template class 'T<1>' requested here}}
22 T<2> t2;
23 
24 template<typename T> struct S {
25     static_assert(sizeof(T) > sizeof(char), "Type not big enough!"); // expected-error {{static_assert failed "Type not big enough!"}}
26 };
27 
28 S<char> s1; // expected-note {{in instantiation of template class 'S<char>' requested here}}
29 S<int> s2;
30 
31 static_assert(false, L"\xFFFFFFFF"); // expected-error {{static_assert failed L"\xFFFFFFFF"}}
32 static_assert(false, u"\U000317FF"); // expected-error {{static_assert failed u"\U000317FF"}}
33 // FIXME: render this as u8"\u03A9"
34 static_assert(false, u8"Ω"); // expected-error {{static_assert failed u8"\316\251"}}
35 static_assert(false, L"\u1234"); // expected-error {{static_assert failed L"\x1234"}}
36 static_assert(false, L"\x1ff" "0\x123" "fx\xfffff" "goop"); // expected-error {{static_assert failed L"\x1FF""0\x123""fx\xFFFFFgoop"}}
37 
38 template<typename T> struct AlwaysFails {
39   // Only give one error here.
40   static_assert(false, ""); // expected-error {{static_assert failed}}
41 };
42 AlwaysFails<int> alwaysFails;
43 
44 template<typename T> struct StaticAssertProtected {
45   static_assert(__is_literal(T), ""); // expected-error {{static_assert failed}}
46   static constexpr T t = {}; // no error here
47 };
48 struct X { ~X(); };
49 StaticAssertProtected<int> sap1;
50 StaticAssertProtected<X> sap2; // expected-note {{instantiation}}
51 
52 static_assert(true); // expected-warning {{C++17 extension}}
53 static_assert(false); // expected-error-re {{failed{{$}}}} expected-warning {{extension}}
54 
55 
56 // Diagnostics for static_assert with multiple conditions
57 template<typename T> struct first_trait {
58   static const bool value = false;
59 };
60 
61 template<>
62 struct first_trait<X> {
63   static const bool value = true;
64 };
65 
66 template<typename T> struct second_trait {
67   static const bool value = false;
68 };
69 
70 static_assert(first_trait<X>::value && second_trait<X>::value, "message"); // expected-error{{static_assert failed due to requirement 'second_trait<X>::value' "message"}}
71 
72 namespace std {
73 
74 template <class Tp, Tp v>
75 struct integral_constant {
76   static const Tp value = v;
77   typedef Tp value_type;
78   typedef integral_constant type;
79 };
80 
81 template <class Tp, Tp v>
82 const Tp integral_constant<Tp, v>::value;
83 
84 typedef integral_constant<bool, true> true_type;
85 typedef integral_constant<bool, false> false_type;
86 
87 template <class Tp>
88 struct is_const : public false_type {};
89 template <class Tp>
90 struct is_const<Tp const> : public true_type {};
91 
92 // We do not define is_same in terms of integral_constant to check that both implementations are supported.
93 template <typename T, typename U>
94 struct is_same {
95   static const bool value = false;
96 };
97 
98 template <typename T>
99 struct is_same<T, T> {
100   static const bool value = true;
101 };
102 
103 } // namespace std
104 
105 struct ExampleTypes {
106   using T = int;
107   using U = float;
108 };
109 
110 static_assert(std::is_same<ExampleTypes::T, ExampleTypes::U>::value, "message");
111 // expected-error@-1{{static_assert failed due to requirement 'std::is_same<int, float>::value' "message"}}
112 static_assert(std::is_const<ExampleTypes::T>::value, "message");
113 // expected-error@-1{{static_assert failed due to requirement 'std::is_const<int>::value' "message"}}
114 static_assert(!std::is_const<const ExampleTypes::T>::value, "message");
115 // expected-error@-1{{static_assert failed due to requirement '!std::is_const<int>::value' "message"}}
116 static_assert(!(std::is_const<const ExampleTypes::T>::value), "message");
117 // expected-error@-1{{static_assert failed due to requirement '!(std::is_const<int>::value)' "message"}}
118 
119 struct BI_tag {};
120 struct RAI_tag : BI_tag {};
121 struct MyIterator {
122   using tag = BI_tag;
123 };
124 struct MyContainer {
125   using iterator = MyIterator;
126 };
127 template <class Container>
128 void foo() {
129   static_assert(std::is_same<RAI_tag, typename Container::iterator::tag>::value, "message");
130   // expected-error@-1{{static_assert failed due to requirement 'std::is_same<RAI_tag, BI_tag>::value' "message"}}
131 }
132 template void foo<MyContainer>();
133 // expected-note@-1{{in instantiation of function template specialization 'foo<MyContainer>' requested here}}
134 
135 namespace ns {
136 template <typename T, int v>
137 struct NestedTemplates1 {
138   struct NestedTemplates2 {
139     template <typename U>
140     struct NestedTemplates3 : public std::is_same<T, U> {};
141   };
142 };
143 } // namespace ns
144 
145 template <typename T, typename U, int a>
146 void foo2() {
147   static_assert(::ns::NestedTemplates1<T, a>::NestedTemplates2::template NestedTemplates3<U>::value, "message");
148   // expected-error@-1{{static_assert failed due to requirement '::ns::NestedTemplates1<int, 3>::NestedTemplates2::NestedTemplates3<float>::value' "message"}}
149 }
150 template void foo2<int, float, 3>();
151 // expected-note@-1{{in instantiation of function template specialization 'foo2<int, float, 3>' requested here}}
152 
153 template <class T>
154 void foo3(T t) {
155   static_assert(std::is_const<T>::value, "message");
156   // expected-error-re@-1{{static_assert failed due to requirement 'std::is_const<(lambda at {{.*}}static-assert.cpp:{{[0-9]*}}:{{[0-9]*}})>::value' "message"}}
157   static_assert(std::is_const<decltype(t)>::value, "message");
158   // expected-error-re@-1{{static_assert failed due to requirement 'std::is_const<(lambda at {{.*}}static-assert.cpp:{{[0-9]*}}:{{[0-9]*}})>::value' "message"}}
159 }
160 void callFoo3() {
161   foo3([]() {});
162   // expected-note@-1{{in instantiation of function template specialization 'foo3<(lambda at }}
163 }
164 
165 template <class T>
166 void foo4(T t) {
167   static_assert(std::is_const<typename T::iterator>::value, "message");
168   // expected-error@-1{{type 'int' cannot be used prior to '::' because it has no members}}
169 }
170 void callFoo4() { foo4(42); }
171 // expected-note@-1{{in instantiation of function template specialization 'foo4<int>' requested here}}
172