llvm-project/clang/test/Sema/const-init.c
Sirraide 2cf2bc472d
[Clang] [CodeGen] Fix codegen bug in constant initialisation in C23 mode (#84981)
Consider the following code:
```c
bool const inf =  (1.0/0.0);
``` 

When trying to emit the initialiser of this variable in C23, we end up
hitting a code path in codegen in `VarDecl::evaluateValueImpl()` where
we check for `IsConstantInitialization && (Ctx.getLangOpts().CPlusPlus
|| Ctx.getLangOpts().C23)`, and if that is the case and we emitted any
notes, constant evaluation fails, and as a result, codegen issues this
error:
```
<source>:1:12: error: cannot compile this static initializer yet
    1 | bool const inf =  (1.0/0.0);
      |        
```

As a fix, only fail in C23 mode if we’re initialising a `constexpr`
variable.

This fixes #84784.
2024-03-13 14:59:55 +01:00

7 lines
385 B
C

// RUN: %clang_cc1 -fsyntax-only -verify -std=c23 %s
// Division by 0 here is an error iff the variable is 'constexpr'.
const _Bool inf1 = (1.0/0.0 == __builtin_inf());
constexpr _Bool inf2 = (1.0/0.0 == __builtin_inf()); // expected-error {{must be initialized by a constant expression}} expected-note {{division by zero}}
constexpr _Bool inf3 = __builtin_inf() == __builtin_inf();