aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/cpp23/consteval-if2.C
blob: 3b258711ce64f1c6c6edef791e7e2054791f24ad (plain)
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
// P1938R3
// { dg-do compile { target c++20 } }
// { dg-options "" }

constexpr bool f()
{
  if consteval (true) {}	// { dg-error "'if consteval' requires compound statement" }
				// { dg-error "expected" "" { target *-*-* } .-1 }
				// { dg-warning "'if consteval' only available with" "" { target c++20_only } .-2 }
  if not consteval (false) {}	// { dg-error "'if consteval' requires compound statement" }
				// { dg-error "expected" "" { target *-*-* } .-1 }
				// { dg-warning "'if consteval' only available with" "" { target c++20_only } .-2 }
  if consteval if (true) {}	// { dg-error "'if consteval' requires compound statement" }
				// { dg-warning "'if consteval' only available with" "" { target c++20_only } .-1 }
  if ! consteval {} else ;	// { dg-error "'if consteval' requires compound statement" }
				// { dg-warning "'if consteval' only available with" "" { target c++20_only } .-1 }
  if consteval {} else if (true) {}	// { dg-error "'if consteval' requires compound statement" }
				// { dg-warning "'if consteval' only available with" "" { target c++20_only } .-1 }
  if (true)
    if consteval		// { dg-warning "'if consteval' only available with" "" { target c++20_only } }
      {
      }
    else ;			// { dg-error "'if consteval' requires compound statement" }
  return false;
}

consteval int foo (int x) { return x; }
consteval int bar () { return 2; }

constexpr int
baz (int x)
{
  int r = 0;
  if not consteval	// { dg-warning "'if consteval' only available with" "" { target c++20_only } }
    {
      r += foo (x);	// { dg-error "not a constant expression" }
    }
  else
    {
      r += bar ();
    }
  if consteval		// { dg-warning "'if consteval' only available with" "" { target c++20_only } }
    {
      r += 2 * bar ();
    }
  else
    {
      r += foo (8 * x);	// { dg-error "is not a constant expression" }
    }
  if ! consteval	// { dg-warning "'if consteval' only available with" "" { target c++20_only } }
    {
      r += foo (32 * x);// { dg-error "not a constant expression" }
    }
  if consteval		// { dg-warning "'if consteval' only available with" "" { target c++20_only } }
    {
      r += 32 * bar ();
    }
  return r;
}

// This function is not instantiated so NDR.
template <typename T>
constexpr int
qux (int x)
{
  int r = 0;
  if not consteval	// { dg-warning "'if consteval' only available with" "" { target c++20_only } }
    {
      r += foo (x);	// { dg-error "'x' is not a constant expression" "" { xfail *-*-* } }
    }
  else
    {
      r += bar ();
    }
  if consteval		// { dg-warning "'if consteval' only available with" "" { target c++20_only } }
    {
      r += 2 * bar ();
    }
  else
    {
      r += foo (8 * x);	// { dg-error "is not a constant expression" "" { xfail *-*-* } }
    }
  if ! consteval	// { dg-warning "'if consteval' only available with" "" { target c++20_only } }
    {
      r += foo (32 * x);// { dg-error "is not a constant expression" "" { xfail *-*-* } }
    }
  if consteval		// { dg-warning "'if consteval' only available with" "" { target c++20_only } }
    {
      r += 32 * bar ();
    }
  return r;
}

template <typename T>
constexpr T
corge (T x)
{
  T r = 0;
  if not consteval	// { dg-warning "'if consteval' only available with" "" { target c++20_only } }
    {
      r += foo (x);
    }
  else
    {
      r += bar ();
    }
  if consteval		// { dg-warning "'if consteval' only available with" "" { target c++20_only } }
    {
      r += 2 * bar ();
    }
  else
    {
      r += foo (8 * x);
    }
  if ! consteval	// { dg-warning "'if consteval' only available with" "" { target c++20_only } }
    {
      r += foo (32 * x);
    }
  if consteval		// { dg-warning "'if consteval' only available with" "" { target c++20_only } }
    {
      r += 32 * bar ();
    }
  return r;
}

int
garply (int x)
{
  return corge (x); // { dg-error "is not a constant expression" }
}