llvm / llvm-project

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

`inner` declared `static` in `constexpr` context #41567

Open llvmbot opened 5 years ago

llvmbot commented 5 years ago
Bugzilla Link 42222
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor
CC @DougGregor,@zygoloid

Extended Description

My clang is 9.0.0, and the code is:

struct test { const int *addr; };

const test* setup()
{
 static constexpr test atest =
 {
 ({ static const int inner = 123; &inner; })
 };

 return &atest;
}

int main(){}

clang accepts it, but icc and gcc reject it.

gcc:

source>: In function 'const test* setup()':
<source>:7:22: error: 'inner' declared 'static' in 'constexpr' context
    7 |  ({ static const int inner = 123; &inner; })
      |                      ^~~~~
Compiler returned: 1

icc:

source>(7): error: expression must have a constant value
   ({ static const int inner = 123; &inner; })
    ^
compilation aborted for <source> (code 2)
Compiler returned: 2
ec04fc15-fa35-46f2-80e1-5d271f2ef708 commented 5 years ago

Modified example that demonstrates why it might be a good idea to reject this:

struct test { const int *addr; };
int get();               
const test* setup()
{
 static constexpr test atest =
 {
 ({ static int inner = get(); &inner; })
 };

 return &atest;
}

Note that the code generated for setup() does not include a call to get(): we're returning the address of an uninitialized static local.