Skip to content

Commit b82c7e7

Browse files
authored
[C2y] Claim conformance to WG14 N3525 (llvm#166824)
The paper is ensuring that a static_assert operand can not be deferred until runtime; it must accept an integer constant expression which is resolved at compile time. Note, Clang extends what it considers to be a valid integer constant expression, so this also verifies the expected extension diagnostics.
1 parent 96806a7 commit b82c7e7

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

clang/test/C/C2y/n3525.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %clang_cc1 -verify -std=c2y -Wall -pedantic %s
2+
// RUN: %clang_cc1 -verify -std=c23 -Wall -pedantic %s
3+
4+
/* WG14 N3525: Yes
5+
* static_assert without UB
6+
*
7+
* Ensures that a static_assert declaration cannot defer to runtime; it must
8+
* take an integer constant expression that is resolved at compile time.
9+
*
10+
* Note: implementations are free to extend what is a valid integer constant
11+
* expression, and Clang (and GCC) does so. So this test is validating that
12+
* we quietly accept a pasing assertion, loudly reject a failing assertion, and
13+
* issue a pedantic diagnostic for the extension case.
14+
*/
15+
16+
static_assert(1); // Okay
17+
18+
static_assert(0); // expected-error {{static assertion failed}}
19+
20+
extern int a;
21+
static_assert(1 || a); // expected-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
22+
23+
static_assert(a); // expected-error {{static assertion expression is not an integral constant expression}}
24+
static_assert(0 || a); // expected-error {{static assertion expression is not an integral constant expression}}
25+
26+
// Note, there is no CodeGen test for this; we have existing tests for the ICE
27+
// extension, so the pedantic warning is sufficient to verify we're not
28+
// emitting code which reads 'a' in '1 || a' because of the folding, and
29+
// there's no way to generate code for reading 'a' in '0 || a' because of the
30+
// error.

clang/www/c_status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ <h2 id="c2y">C2y implementation status</h2>
344344
<tr>
345345
<td>static_assert without UB</td>
346346
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3525.htm">N3525</a></td>
347-
<td class="unknown" align="center">Unknown</td>
347+
<td class="full" align="center">Yes</td>
348348
</tr>
349349
<tr>
350350
<td>Allow calling static inline within extern inline</td>

0 commit comments

Comments
 (0)