Bug 83222 - [8 regression] Inconsistent "initializer element is not constant" error
Summary: [8 regression] Inconsistent "initializer element is not constant" error
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: 8.0
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2017-11-30 01:06 UTC by Ian Lance Taylor
Modified: 2021-09-12 21:32 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
gcc8-pr83222.patch (1.30 KB, patch)
2017-11-30 12:57 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ian Lance Taylor 2017-11-30 01:06:59 UTC
The handling of this C program recently changed:

const char _expA = 0x42;
void __cgo_f_1_4(void) { static const double x = (double)(_expA); }

With a GCC built on October 10, 2017, I get this:

foo.c: In function ‘__cgo_f_1_4’:
foo.c:2:50: error: initializer element is not constant
 void __cgo_f_1_4(void) { static const double x = (double)(_expA); }
                                                  ^
With this morning's sources (November 29, 2017) the program compiles without error.

Perhaps that change is OK.  But this similar program still fails:

const char _expA = 0x42;
double __cgodebug_floats[] = {
  _expA,
  1
};

foo1.c:3:3: error: initializer element is not constant
   _expA,
   ^~~~~
foo1.c:3:3: note: (near initialization for ‘__cgodebug_floats[0]’)


I think that if we are going to change the behavior for the first program, we need to change the behavior for the second one.
Comment 1 Ian Lance Taylor 2017-11-30 01:12:59 UTC
This may be caused by the recent fix for PR 69960.
Comment 2 Jakub Jelinek 2017-11-30 12:52:52 UTC
Well, even GCC 7 and older were doing it, but only with -O1 and above.
Comment 3 Jakub Jelinek 2017-11-30 12:57:50 UTC
Created attachment 42752 [details]
gcc8-pr83222.patch

Perhaps we can do something like this and in initializers try to behave more consistently even when outside of functions.  Or in the other direction, handle static var initializers even in functions like those outside of functions.
Comment 4 Jakub Jelinek 2017-11-30 23:43:41 UTC
Author: jakub
Date: Thu Nov 30 23:43:10 2017
New Revision: 255285

URL: https://gcc.gnu.org/viewcvs?rev=255285&root=gcc&view=rev
Log:
	PR c/83222
	* c-tree.h (decl_constant_value_1): Declare.
	* c-typeck.c (decl_constant_value_1): New function.
	(decl_constant_value): Use it.
	* c-fold.c (c_fully_fold_internal): If in_init, use
	decl_constant_value_1 instead of decl_constant_value.

	* gcc.c-torture/compile/pr83222.c: New test.

Added:
    trunk/gcc/testsuite/gcc.c-torture/compile/pr83222.c
Modified:
    trunk/gcc/c/ChangeLog
    trunk/gcc/c/c-fold.c
    trunk/gcc/c/c-tree.h
    trunk/gcc/c/c-typeck.c
    trunk/gcc/testsuite/ChangeLog
Comment 5 Jakub Jelinek 2017-11-30 23:54:55 UTC
Should be fixed now.