Bug 22529 - [3.4/4.0/4.1 Regression] Rejects valid C99 address of C99 struct in static variable in function
Summary: [3.4/4.0/4.1 Regression] Rejects valid C99 address of C99 struct in static va...
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.1.0
: P2 normal
Target Milestone: 3.4.5
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2005-07-17 19:09 UTC by Andrew Pinski
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

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


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Pinski 2005-07-17 19:09:53 UTC
Take the following code:
struct f1
{
  int i;
};
void f(void)
{
 static struct f1 *f2 = &(struct f1){1};
}

This is valid C99 at least according to both ICC and Comeau.
And we acceptted it in 3.0.4 also.
Comment 1 jsm-csl@polyomino.org.uk 2005-07-17 20:13:53 UTC
Subject: Re:  New: [3.4/4.0/4.1 Regression] Rejects valid C99
 address of C99 struct in static variable in function

On Sun, 17 Jul 2005, pinskia at gcc dot gnu dot org wrote:

> Take the following code:
> struct f1
> {
>   int i;
> };
> void f(void)
> {
>  static struct f1 *f2 = &(struct f1){1};
> }
> 
> This is valid C99 at least according to both ICC and Comeau.

So report it as a bug in ICC and Comeau.  It is exactly as valid as

struct f1
{
  int i;
};
void f(void)
{
  struct f1 tmp = { 1 };
  static struct f1 *f2 = &tmp;
}

i.e. not at all.  The address of an object of automatic storage duration 
is not a constant.

Comment 2 Andrew Pinski 2005-07-17 20:18:51 UTC
That is interesting as if we move the variable declaration out of the function, it works in GCC.
Comment 3 Andrew Pinski 2005-07-17 20:56:47 UTC
I believe you that this is invalid code.
Comment 4 jsm-csl@polyomino.org.uk 2005-07-17 20:58:31 UTC
Subject: Re:  [3.4/4.0/4.1 Regression] Rejects valid C99 address
 of C99 struct in static variable in function

On Sun, 17 Jul 2005, pinskia at gcc dot gnu dot org wrote:

> That is interesting as if we move the variable declaration out of the 
> function, it works in GCC.

That's because of 6.5.2.5#6 which defines the storage duration of compound 
literals to depend on whether they are inside a function:

       [#6] The value of the compound literal is that of an unnamed
       object initialized by the initializer list.  If the compound
       literal occurs outside the body of a  function,  the  object
       has  static  storage  duration;  otherwise, it has automatic
       storage duration associated with the enclosing block.