File: p4.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (158 lines) | stat: -rw-r--r-- 3,857 bytes parent folder | download | duplicates (12)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// RUN: %clang_cc1 -std=c++23 -verify %s

void test_consteval() {
  if consteval ({(void)1;});  // expected-error {{expected { after consteval}}
  if consteval (void) 0; // expected-error {{expected { after consteval}}
  if consteval {
    (void)0;
  } else (void)0; // expected-error {{expected { after else}}

  static_assert([] {
    if consteval {
      return 0;
    }
    return 1;
  }() == 0);

  static_assert([] {
    if consteval {
      return 0;
    } else {
      return 1;
    }
  }() == 0);

  static_assert([] {
    if !consteval {
      return 0;
    } else {
      return 1;
    }
  }() == 1);

  static_assert([] {
    if not consteval {
      return 0;
    }
    return 1;
  }() == 1);

  if consteval [[likely]] { // expected-warning {{attribute 'likely' has no effect when annotating an 'if consteval' statement}}\
                            // expected-note 2{{annotating the 'if consteval' statement here}}


  }
  else [[unlikely]] { // expected-warning {{attribute 'unlikely' has no effect when annotating an 'if consteval' statement}}

  }

}

void test_consteval_jumps() {
  if consteval { // expected-note 4{{jump enters controlled statement of consteval if}}
    goto a;
    goto b; // expected-error {{cannot jump from this goto statement to its label}}
  a:;
  } else {
    goto b;
    goto a; // expected-error {{cannot jump from this goto statement to its label}}
  b:;
  }
  goto a; // expected-error {{cannot jump from this goto statement to its label}}
  goto b; // expected-error {{cannot jump from this goto statement to its label}}
}

void test_consteval_switch() {
  int x = 42;
  switch (x) {
    if consteval { // expected-note 2{{jump enters controlled statement of consteval if}}
    case 1:;       // expected-error {{cannot jump from switch statement to this case label}}
    default:;      // expected-error {{cannot jump from switch statement to this case label}}
    } else {
    }
  }
  switch (x) {
    if consteval { // expected-note 2{{jump enters controlled statement of consteval if}}
    } else {
    case 2:;  // expected-error {{cannot jump from switch statement to this case label}}
    default:; // expected-error {{cannot jump from switch statement to this case label}}
    }
  }
}

consteval int f(int i) { return i; }
constexpr int g(int i) {
  if consteval {
    return f(i);
  } else {
    return 42;
  }
}
static_assert(g(10) == 10);

constexpr int h(int i) { // expected-note {{declared here}}
  if !consteval {
    return f(i); // expected-error {{call to consteval function 'f' is not a constant expression}}\
                     // expected-note  {{cannot be used in a constant expression}}
  }
  return 0;
}

consteval void warn_in_consteval() {
  if consteval { // expected-warning {{consteval if is always true in an immediate context}}
    if consteval {} // expected-warning {{consteval if is always true in an immediate context}}
  }
}

constexpr void warn_in_consteval2() {
  if consteval {
    if consteval {} // expected-warning {{consteval if is always true in an immediate context}}
  }
}

auto y = []() consteval {
  if consteval { // expected-warning {{consteval if is always true in an immediate context}}
    if consteval {} // expected-warning {{consteval if is always true in an immediate context}}
  }
};

namespace test_transform {
int f(auto n) {
  if consteval {
    n.foo; //expected-error {{no member named}}
  }
  else {
  }

  if !consteval {
    n.foo; //expected-error {{no member named}}
  }
  else {
  }

  return 0;
}

constexpr int g(auto n) {
  if consteval {
  }
  else {
    n.foo; //expected-error {{no member named}}
  }

  if !consteval {
  }
  else {
     n.foo; //expected-error {{no member named}}
  }

  return 0;
}

struct S {};
void test() {
  f(S{}); //expected-note {{in instantiation}}
  g(S{}); //expected-note {{in instantiation}}
}

}