llvm / llvm/llvm-project

clang -std=c23 incorrectly treats int (*)(struct { int i; }) and int (*)(struct { int i; }) as compatible types when they are types of struct members being compared for compatibility

Open
#186,196 23 comments 0 reactions 0 assignees View on GitHub
c23 clang:frontend confirmed
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Declaring `x` twice in the same compilation unit :

```c
struct s { int (*f)(struct { int i; } ); } x;
struct s { int (*f)(struct { int i; } ); } x;
```

Clang accepts ([CE link](https://gcc.godbolt.org/z/jqor3xson)). GCC rejects because the struct with the member `i` being tagless, its first declaration is not compatible with its second declaration. The above should be rejected for the same reason `struct { int i; } y; struct { int i; } y;` should be rejected (Clang correctly rejects this latter example).

The most relevant clause in C23 is https://cigix.me/c23#6.2.7.p1

EDIT: as noted by anematode, `clang -std=c23` also [fails to reject the simpler code below](https://gcc.godbolt.org/z/hYTM1T1W6):

```c
struct s { struct { int i; } b; } x;
struct s { struct { int i; } b; } x;
```

Also, since I wrote the program for another discussion, the problem is not just that Clang accepts programs that it should reject. It produces the wrong code for valid C23 programs, too: https://gcc.godbolt.org/z/36c4r8Y53

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.