https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80756
Bug ID: 80756 Summary: missing diagnostic on non-constant expression with function call such as fabs or fma in initializer Product: gcc Version: 6.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: vincent-gcc at vinc17 dot net Target Milestone: --- GCC misses a diagnostic when the fabs() or fma() function is used in an initializer. For instance, consider: ---------------------------------------- double fabs (double); double fma (double, double, double); double foo (double, double, double); double f (void) { static double x = fabs (3.0); static double y = fma (2.0, 3.0, 4.0); static double z = foo (2.0, 3.0, 4.0); return x + y + z; } ---------------------------------------- $ gcc-snapshot -std=c99 -c tst-cst.c tst-cst.c: In function 'f': tst-cst.c:9:21: error: initializer element is not constant static double z = foo (2.0, 3.0, 4.0); ^~~ where gcc-snapshot is: gcc version 8.0.0 20170512 (experimental) [trunk revision 247986] (Debian 20170512-1) I get the diagnostic as expected for foo(), but not for fabs() and fma(). Note that <math.h> is not included, so that fabs() and fma() must not be regarded as special. But even when these functions are regarded as ISO C's specified ones, the diagnostic should probably still be present (it seems that the C standard does not make an exception for such functions, unfortunately). Same problem with GCC 6.3.0. But GCC 5.4.1 gives: $ gcc-5 -std=c99 -c tst-cst.c tst-cst.c: In function âfâ: tst-cst.c:7:21: warning: initializer element is not a constant expression static double x = fabs (3.0); ^ tst-cst.c:8:21: warning: initializer element is not a constant expression static double y = fma (2.0, 3.0, 4.0); ^ tst-cst.c:9:21: error: initializer element is not constant static double z = foo (2.0, 3.0, 4.0); ^ As far as the C standard is concerned, there are no differences between warnings and errors (all diagnostics), so that this is much better. But this should really be an error in all cases. As a comparison, Clang gives 3 "error" diagnostics (tested 3.6 to 4.0 RC1): $ clang-4.0 -c tst-cst.c tst-cst.c:7:21: error: initializer element is not a compile-time constant static double x = fabs (3.0); ^~~~~~~~~~ tst-cst.c:8:21: error: initializer element is not a compile-time constant static double y = fma (2.0, 3.0, 4.0); ^~~~~~~~~~~~~~~~~~~ tst-cst.c:9:21: error: initializer element is not a compile-time constant static double z = foo (2.0, 3.0, 4.0); ^~~~~~~~~~~~~~~~~~~ 3 errors generated.