Bug 43114 - initializing constants from each other
Summary: initializing constants from each other
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: gcc   
(Show other bugs)
Version: 7.1
Hardware: All Linux
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: David Lawrence
URL: http://gcc.gnu.org/ml/gcc-bugs/2000-0...
Depends On:
TreeView+ depends on / blocked
Reported: 2001-05-31 22:01 UTC by roystgnr
Modified: 2007-04-18 16:33 UTC (History)
0 users

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2001-05-31 22:02:44 UTC
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)
minimal demonstration source code (282 bytes, text/plain)
2001-05-31 22:02 UTC, roystgnr
no flags Details

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)

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:

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

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:
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.
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
An implementation may accept other forms of constant expressions.
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.

Note You need to log in before you can comment on or make changes to this bug.