<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/77186>77186</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[Clang] Insufficient diagnostics for dereferencing a null pointer in constant evaluation
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
frederick-vs-ja
</td>
</tr>
</table>
<pre>
Not sure whether there should be a separated issue, as it may be desired to track this as [CWG2823](https://cplusplus.github.io/CWG/issues/2823.html).
Currently Clang accepts this program with warnings ([Godbolt link](https://godbolt.org/z/9xhxezh8d)):
```C++
static_assert([](int&) { return true; }(*static_cast<int*>(nullptr)));
static_assert(([]() -> int& { return *static_cast<int*>(nullptr); }(), true));
int main() {}
```
```
<source>:2:38: warning: binding dereferenced null pointer to reference has undefined behavior [-Wnull-dereference]
2 | static_assert(([]() -> int& { return *static_cast<int*>(nullptr); }(), true));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
```
The first line should also be diagnosed.
Also, it seems that dereferencing a null pointer should be an error in constant evaluation instead of a warning. In the following program, Clang doesn't make the constant evaluation fail due to this kind of UB and thus selects the first overload, which is wrong. [Godbolt link](https://godbolt.org/z/s8x95hrq7).
```C++
#include <type_traits>
template<class T, bool = ([]() -> T& { return *static_cast<T*>(nullptr); }(), true)>
std::true_type fun(int);
template<class T>
std::false_type fun(...);
static_assert(!decltype(fun<int>(0))::value);
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEVU1z4jgQ_TXNpQvKSBjMgQM4Q2oue8pWjikhtbEmQmIlOSRz2N--1XaYwIbZr8u6HAWwut9z671ulZLde6IVlBso70aqy22IqyaSoWj18_gljb-p0S6Yt9UvIWPqIuGppdxSRF4IUxs6Z3BHqDDRUUWVyaBNqSMQNaqENuNBvfEOQ8lGMpgD5qj0M-bWJt4C5aZ-vBeVkFDegajanI8J5Br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