Skip to content

unnecessary constraint enforcement within static_assert() within structure definition #143284

Open
@alejandro-colomar

Description

@alejandro-colomar

Here's a small reproducer:

alx@devuan:~/tmp$ cat cnt.c

#include <stdlib.h>
#include <assert.h>


#define must_be(e)                                                    \
(                                                                     \
        0 * (int) sizeof(                                             \
                struct {                                              \
                        static_assert(e, "");                         \
                        int ISO_C_forbids_a_struct_with_no_members_;  \
                }                                                     \
        )                                                             \
)

#define is_same_type(a, b)                                            \
(                                                                     \
        __builtin_types_compatible_p(a, b)                            \
)

#define is_same_typeof(a, b)                                          \
(                                                                     \
        is_same_type(typeof(a), typeof(b))                            \
)

#define is_array(a)                                                   \
(                                                                     \
        !is_same_typeof(a, &(a)[0])                                   \
)


int
main(void)
{
	return must_be(is_array(((int []) {rand(), rand()})));
}
alx@devuan:~/tmp$ gcc -Wall -Wextra -S cnt.c 
alx@devuan:~/tmp$
alx@devuan:~/tmp$ clang -Wall -Wextra -S cnt.c 
cnt.c:34:37: error: initializer element is not a compile-time constant
   34 |         return must_be(is_array(((int []) {rand(), rand()})));
      |                                            ^~~~~~
cnt.c:27:25: note: expanded from macro 'is_array'
   27 |         !is_same_typeof(a, &(a)[0])                                   \
      |                         ^
cnt.c:22:29: note: expanded from macro 'is_same_typeof'
   22 |         is_same_type(typeof(a), typeof(b))                            \
      |                             ^
cnt.c:17:38: note: expanded from macro 'is_same_type'
   17 |         __builtin_types_compatible_p(a, b)                            \
      |                                      ^
cnt.c:9:39: note: expanded from macro 'must_be'
    9 |                         static_assert(e, "");                         \
      |                                       ^
1 error generated.

Please include the following in an eventual fix:

Reported-by: "Basil L. Contovounesios" <[email protected]>
Reported-by: Alejandro Colomar <[email protected]>

Metadata

Metadata

Assignees

No one assigned

    Labels

    c23clang:frontendLanguage frontend issues, e.g. anything involving "Sema"

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions