https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66618
Bug ID: 66618 Summary: Failure to diagnose non-constant initializer for static object with -O1 Product: gcc Version: 4.8.4 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: Keith.S.Thompson at gmail dot com Target Milestone: --- $ uname -a Linux m5 3.13.0-37-generic #64-Ubuntu SMP Mon Sep 22 21:28:38 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux $ gcc --version | head -n 1 gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4 $ cat bug.c int main(void) { const int not_a_constant = 0; static int n = not_a_constant; /* constraint violation */ return n; /* avoid unused variable warning */ } $ gcc -O0 -c -std=c11 -pedantic-errors -Wall -Wextra bug.c bug.c: In function ‘main’: bug.c:3:5: error: initializer element is not constant static int n = not_a_constant; /* constraint violation */ ^ $ gcc -O1 -c -std=c11 -pedantic-errors -Wall -Wextra bug.c $ In the test program "bug.c", the object "not_a_constant" is defined as "const", but its name is not a constant expression. The initializer for a static expression must be a constant expression; using a non-constant expression is a constraint violation, requiring a diagnostic (N1570 6.7.9p4). With "-O0", gcc correctly diagnoses this. I see the same problem with "-O1", "-O2", and "-O3", and with "-std=c90", "-std=c99", and "-std=c11". This is based on a question posted to Stack Overflow: http://stackoverflow.com/q/30962512/827263 by user "meet". Grzegorz Szpetkowski gets the credit for realizing that the "-O" option triggers the bug.