Clang Project

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