<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/121694>121694</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [Clang][C23] constexpr initialized with compound literal incorrectly allows non-null pointers
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Cydox
      </td>
    </tr>
</table>

<pre>
    @Fznamznon 

Compiler Explorer: https://godbolt.org/z/oTvq738fa

```C
typedef struct {
        int *a;
} foo;

int b = 42;

constexpr foo bar1 = (foo){.a = &b}; // compiles without warning (even with -Wpedantic)
constexpr foo bar2 = {.a = &b};      // error: constexpr pointer initializer is not null
```

Initializing with a compound literal is a GNU extension [1]:
> "As a GNU extension, GCC allows initialization of objects with static storage duration by compound literals (which is not possible in ISO C99 because the initializer is not a constant). It is handled as if the object were initialized only with the brace-enclosed list if the types of the compound literal and the object match. The elements of the compound literal must be constant. If the object being initialized has array type of unknown size, the size is determined by the size of the compound literal."

But it should be treated like the variable was initialized with an initializer list. So the same constraints should be enforced in the `constexpr` case. Meaning no [2]:
- Pointers (except that null pointers can be constexpr),
- Variably modified types,
- Atomic types,
- volatile types,
- restrict pointers.

The behavior of the generated code appears to be correct and what you'd expect though (in the limited testing I've done).

`gcc` behaves very similarly, however it does provide a warning with `-Wpedantic` for the `bar1` initialization (`bar2` correctly errors).


The problem appears to be that initialization with compound literal bypasses this check:
https://github.com/llvm/llvm-project/blob/2adcec7780f3d3027f76c80dbd73085fdee144d7/clang/lib/Sema/SemaInit.cpp#L8245-L8249
and only hits this one:
https://github.com/llvm/llvm-project/blob/2adcec7780f3d3027f76c80dbd73085fdee144d7/clang/lib/Sema/SemaDecl.cpp#L14507-L14519

[1] https://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html
[2] https://en.cppreference.com/w/c/language/constexpr
</pre>
<img width="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