p2.cpp
3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// RUN: %clang_cc1 -std=c++1z -verify %s
// RUN: %clang_cc1 -std=c++1z -verify %s -DUNDEFINED
#ifdef UNDEFINED
// "used but not defined" errors don't get produced if we have more interesting
// errors.
namespace std_example {
template <typename T, typename... Rest> void g(T &&p, Rest &&... rs) {
// use p
if constexpr(sizeof...(rs) > 0)
g(rs...);
}
void use_g() {
g(1, 2, 3);
}
static int x(); // no definition of x required
int f() {
if constexpr (true)
return 0;
else if (x())
return x();
else
return -x();
}
}
namespace odr_use_in_selected_arm {
static int x(); // expected-warning {{is not defined}}
int f() {
if constexpr (false)
return 0;
else if (x()) // expected-note {{here}}
return x();
else
return -x();
}
}
#else
namespace ccce {
void f() {
if (5) {}
if constexpr (5) {} // expected-error {{cannot be narrowed}}
}
template<int N> void g() {
if constexpr (N) {} // expected-error {{cannot be narrowed}}
}
template void g<5>(); // expected-note {{instantiation of}}
void h() {
if constexpr (4.3) {} // expected-error{{conversion from 'double' to 'bool' is not allowed in a converted constant expression}}
constexpr void *p = nullptr;
if constexpr (p) {} // expected-error{{conversion from 'void *const' to 'bool' is not allowed in a converted constant expression}}
}
}
namespace generic_lambda {
// Substituting for T produces a hard error here, even if substituting for
// the type of x would remove the error.
template<typename T> void f() {
[](auto x) {
if constexpr (sizeof(T) == 1 && sizeof(x) == 1)
T::error(); // expected-error 2{{'::'}}
} (0);
}
template<typename T> void g() {
[](auto x) {
if constexpr (sizeof(T) == 1)
if constexpr (sizeof(x) == 1)
T::error(); // expected-error {{'::'}}
} (0);
}
void use() {
f<int>(); // expected-note {{instantiation of}}
f<char>(); // expected-note {{instantiation of}}
g<int>(); // ok
g<char>(); // expected-note {{instantiation of}}
}
}
namespace potentially_discarded_branch_target {
void in_switch(int n) {
switch (n)
case 4: if constexpr(sizeof(n) == 4) return;
if constexpr(sizeof(n) == 4)
switch (n) case 4: return;
switch (n) {
if constexpr (sizeof(n) == 4) // expected-note 2{{constexpr if}}
case 4: return; // expected-error {{cannot jump}}
else
default: break; // expected-error {{cannot jump}}
}
}
template<typename T>
void in_switch_tmpl(int n) {
switch (n) {
if constexpr (sizeof(T) == 4) // expected-note 2{{constexpr if}}
case 4: return; // expected-error {{cannot jump}}
else
default: break; // expected-error {{cannot jump}}
}
}
void goto_scope(int n) {
goto foo; // expected-error {{cannot jump}}
if constexpr(sizeof(n) == 4) // expected-note {{constexpr if}}
foo: return;
bar:
if constexpr(sizeof(n) == 4)
goto bar; // ok
}
template<typename T>
void goto_scope(int n) {
goto foo; // expected-error {{cannot jump}}
if constexpr(sizeof(n) == 4) // expected-note {{constexpr if}}
foo: return;
bar:
if constexpr(sizeof(n) == 4)
goto bar; // ok
}
void goto_redef(int n) {
a: if constexpr(sizeof(n) == 4) // expected-error {{redefinition}} expected-note {{constexpr if}}
a: goto a; // expected-note 2{{previous}}
else
a: goto a; // expected-error {{redefinition}} expected-error {{cannot jump}}
}
void evil_things() {
goto evil_label; // expected-error {{cannot jump}}
if constexpr (true || ({evil_label: false;})) {} // expected-note {{constexpr if}}
if constexpr (true) // expected-note {{constexpr if}}
goto surprise; // expected-error {{cannot jump}}
else
surprise: {}
}
}
#endif