llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.16k stars 12.03k forks source link

[clang] initializer element is not a compile-time constant error when trying to initialize static array with elemment of type static const struct #61737

Open listout opened 1 year ago

listout commented 1 year ago

I came across this while building gnome-remote-desktop on Gentoo with clang 16. Relevant bug: https://bugs.gentoo.org/885875.

Minimal reproducible code:

struct AUDIO_FORMAT
{
    char* data;
};

typedef struct AUDIO_FORMAT AUDIO_FORMAT;

static const AUDIO_FORMAT audio_format_aac = { 0 };

static AUDIO_FORMAT server_formats[] =
{
  audio_format_aac,
};

With Clang, I get the following output

test.c:12:3: error: initializer element is not a compile-time constant
  audio_format_aac,
  ^~~~~~~~~~~~~~~~
1 error generated.

While with GCC, it builds successfully.

llvmbot commented 1 year ago

@llvm/issue-subscribers-clang-frontend

shafik commented 1 year ago

As far as I understand 6.6 Constant expressions this is a gcc extension and clang is strictly correct to reject this but I would have expected them to diagnose this in pedantic mode. gcc does diagnose this is version 7.5 though.

CC @AaronBallman

AaronBallman commented 1 year ago

Yeah, that is an extension even in C2x (though the constexpr feature in C2x could be used to make this code compile, but that won't help for C17 and earlier). So this isn't a bug, but is a reasonable feature request.

llvmbot commented 1 year ago

@llvm/issue-subscribers-c