Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getting: "error: initializer element is not a compile-time constant" Even when struct is inside function #87867

Open
aalmkainzi opened this issue Apr 6, 2024 · 6 comments
Labels
c23 clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party

Comments

@aalmkainzi
Copy link

int foo(int *a, int b)
{
    return 0;
}

int main()
{
    int x;
    (struct {
          typeof(foo(&(struct { int t; }){.t = x}.t, 0)) t;
        }){0};
}

the above code compiles with gcc and tcc. but fails with clang.

The error I get is:

err_test.c:10:48: error: initializer element is not a compile-time constant
          typeof(foo(&(struct { int t; }){.t = x}.t, 0)) t;
                                               ^
1 error generated.

This error seems to be for global or static variables, but for some reason is triggering even though the variable is defined inside a function and inside a typeof

@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed new issue labels Apr 6, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Apr 6, 2024

@llvm/issue-subscribers-clang-frontend

Author: Abdulmalek Almkainzi (aalmkainzi)

```C int foo(int *a, int b) { return 0; }

int main()
{
int x;
(struct {
typeof(foo(&(struct { int t; }){.t = x}.t, 0)) t;
}){0};
}


the above code compiles with gcc and tcc. but fails with clang.

The error I get is: 

err_test.c:10:48: error: initializer element is not a compile-time constant
typeof(foo(&(struct { int t; }){.t = x}.t, 0)) t;
^
1 error generated.


This error seems to be for global or static variables, but for some reason is triggering even though the variable is defined inside a function and inside a `typeof`
</details>

@shafik
Copy link
Collaborator

shafik commented Apr 6, 2024

x does not look like a constant expression in C, I think gcc is accepting that as an implementation defined constant expression.

However if we change x to be const or constexpr and initialize it then both accept it: https://godbolt.org/z/jT6Yzoje3

This makes a lot more sense, although the const case feels like an extension.

CC @AaronBallman @cor3ntin @Fznamznon

@aalmkainzi
Copy link
Author

x does not look like a constant expression in C, I think gcc is accepting that as an implementation defined constant expression.

However if we change x to be const or constexpr and initialize it then both accept it: https://godbolt.org/z/jT6Yzoje3

This makes a lot more sense, although the const case feels like an extension.

CC @AaronBallman @cor3ntin @Fznamznon

Why would x be required to be constant in this context?

@AaronBallman AaronBallman added c23 confirmed Verified by a second party labels Apr 8, 2024
@AaronBallman
Copy link
Collaborator

It's not required to be a constant in this case. The compound literal is at block scope and so the initializer does not need to be constant (C23 6.5.3.6p4).

The issue is that we're checking for file scope at

bool isFileScope = !CurContext->isFunctionOrMethod();
and because the context is "record scope" we assume it must be file scope.

@aalmkainzi
Copy link
Author

shouldn't also work for file scope? since the compound literal is inside a typeof

@AaronBallman
Copy link
Collaborator

shouldn't also work for file scope? since the compound literal is inside a typeof

There are no special allowances for initialization that occurs in an unevaluated context, so being inside of typeof (or sizeof, etc) makes no difference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c23 clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party
Projects
None yet
Development

No branches or pull requests

5 participants