<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/112434>112434</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Interception of explicit specification of implicit types is fragile, and easily broken
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ojhunt
</td>
</tr>
</table>
<pre>
This bug technically results in incorrect codegen, but in practice it is not a real issue as you only hit it in cases where you specify non-spec compliant definitions of core types.
There are a number of types that are needed to evaluate new and delete, that are declared implicitly if needed. However clang does need to track the explicit definitions provided by users or libraries, so that the same internal types are used.
The root of the problem is that we perform the interception during sema of the declaration, not when the declarations are inserted into the std namespace. This approach carries an implicit assumption that these types will be specified via the correct declaration, which it does not need to be, e.g:
```cpp
namespace std {
struct align_val_t {
align_val_t(__SIZE_TYPE__);
};
}
```
As clang assumes `align_val_t` is an enum it does not detect this user specified `align_val_t`, this becomes problematic because Sema ends up with multiple `align_val_t` types, and then as a result the calling conventions can become mismatched, e.g.
```cpp
// continued from above
void *operator new(__SIZE_TYPE__ size, std::align_val_t) {
return nullptr; // Totes fine
}
struct __attribute__((aligned(64))) S {
int i;
S();
};
void f() {
new S;
}
```
If we look at the codegen, we can see that despite the nonsense align_val_t struct, clang still passes the alignment parameter as the implicit enum type. This resolves as the implicit align_val_t matches the type used in the implicitly declared aligned operator new. If we try to force the use of our align_val_t via a placement argument
```cpp
struct Context;
void *operator new(__SIZE_TYPE__ size, std::align_val_t, Context&) {
return nullptr; // Totes fine
}
void f(Context& Ctx) {
new (Ctx) S;
}
```
Clang cannot resolve the operator new, as the candidate resolution is based on the implicit align_val_t, which does not match the explicit align_val_t used in the declaration.
To get the correct behavior here we should probably just be performing this interception at the point where declarations are inserted into scopes, not during validation of specific kinds of declaration.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVkFv4zoO_jXOhZjAcZK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