Issue 84172
Summary Expressions in _Generic Misinterpret the Standard
Labels new issue
Assignees
Reporter IGJoshua
    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.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to