Bug 43114

Summary: initializing constants from each other
Product: [Retired] Red Hat Linux Reporter: roystgnr
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED NOTABUG QA Contact: David Lawrence <dkl>
Severity: low Docs Contact:
Priority: medium    
Version: 7.1   
Target Milestone: ---   
Target Release: ---   
Hardware: All   
OS: Linux   
URL: http://gcc.gnu.org/ml/gcc-bugs/2000-01/msg00941.html
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2001-05-31 22:02:44 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Attachments:
Description Flags
minimal demonstration source code none

Description roystgnr 2001-05-31 22:01:12 UTC
From Bugzilla Helper:
User-Agent: Mozilla/5.0 (X11; U; Linux 2.4.2-2smp i686; en-US; 0.8.1)
Gecko/20010430

Description of problem:
gcc-2.96-81 refuses to initialize one const int with an expression
containing the value of another constant integer, exiting with the error
"initializer element is not constant".

How reproducible:
Always

Steps to Reproduce:
1.Save attached file as "testconst.c"
2."gcc -o testconst testconst.c"
3.
	

Actual Results:  testconst.c:3: initializer element is not constant

Expected Results:  Successful compilation

Additional info:

egcs-1.1.2 exits with the same error.  According to the (year old) gcc-bugs
post archived at the above URL, gcc-2.95.2 compiles this code as expected.
 I'm not 100% certain that initializing one const from another is valid
ANSI C (my copy of K doesn't bring up the problem), but I can't see why it
wouldn't be allowed.

Comment 1 roystgnr 2001-05-31 22:02:40 UTC
Created attachment 20050 [details]
minimal demonstration source code

Comment 2 Jakub Jelinek 2001-06-01 08:21:28 UTC
From my testing gcc-2.95.2 does not accept it either.
Some legalesse:
6.6.6
An integer constant expression shall have integer type and shall only have
operands that are integer constants, enumeration constants, character
constants, sizeof expressions whose results are integer constants, and
floating constants that are the immediate operands of casts. Cast operators
in an integer constant expression shall only convert arithmetic types to
integer types, except as part of an operand to the sizeof operator.
6.6.7
More latitude is permitted for constant expressions in initializers. Such a
constant expression shall be, or evaluate to, one of the following:
- an arithmetic constant expression,
- a null pointer constant,
- an address constant, or
- an address constant for an object type plus or minus an integer constant
  expression.
6.6.10
An implementation may accept other forms of constant expressions.
6.7.8.4
All the expressions in an initializer for an object that has static storage
duration shall be constant expressions or string literals.

(only included the relevant paragraphs).
As you can see, your program is not valid ISO C, but according to 6.6.10 gcc
could decide to accept it as constant expression, but it does not have to
(unlike in C++).
None of egcs 1.1.x, gcc 2.95.x, gcc-2.96-RH, gcc 3.0 or gcc 3.1 accept this.