<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/84172>84172</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Expressions in _Generic Misinterpret the Standard
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
IGJoshua
</td>
</tr>
</table>
<pre>
The _Generic keyword introduced in C11 is very useful for defining type generic code, but its capabilities are hamstrung by a narrow interpretation of the spec which I don't think is supported by the text therein.
Follows is an example program which fails to compile, but which I believe should compile under the spec:
```c
struct s1 {
int x;
};
struct s2 {
int y;
};
int f1(struct s1 s);
int f2(struct s2 s);
int main(void) {
struct s1 s = {0};
return _Generic( (s),
struct s1 : f1(s),
struct s2 : f2(s) );
}
```
This program is rejected by clang with the error
```
main.c:15:37: error: passing 'struct s1' to parameter of incompatible type 'struct s2'
struct s2 : f2(s) );
^
```
I believe this is in error, as the branch which the error occurs in is never evaluated, and this is known at compile time.
In ISO 9899:2023 section 6.5.1.1 paragraph 3, it says
> The controlling expression of a generic selection is not evaluated. If a generic selection has a generic association with a type name that is compatible with the type of the controlling expression, then the result expression of the generic selection is the expression in that generic association. Otherwise, the result expression of the generic selection is the expression in the default generic association. None of the expressions from any other generic association of the generic selection is evaluated.
I may be mistaken, but I believe that saying that the other generic associations are not evaluated means that they should not be rejected for being invalid as long as they parse as expressions.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVs2Om0gQfpr2pRQEDR7MwYfJzDialXZzSO6rAsrQO0036m7s8duvChiME2-Uw1oIQ9ffV_-g96oxRHux_Sy2zxscQmvd_vXLH9a3A25KW1_231uCv7-QIacqeKPL2boalAnO1kNF_AhPSQLKw4ncBQZPx0HD0Tqo6aiMMg2ES0_QzCoqW5OQT1AOAVTwUGGPpdIqKPKAjqDFzgc3mAbKCyAYdM6e2SK53lHAoKwBe4TQEvieKji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