https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90472

            Bug ID: 90472
           Summary: “extern int i;” declaration inside function is allowed
                    to shadow “static int i;” at file scope
           Product: gcc
           Version: 9.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pascal_cuoq at hotmail dot com
  Target Milestone: ---

Consider the two compilation units below, that differ only in the name of the
automatic variable inside function f:

____
int *p1, *p2;

static int i = 1;

void f(void) {
    p1 = &i;
    int i = 2;
    {
        extern int i;
        p2 = &i;
    }
}
____
int *p1, *p2;

static int i = 1;

void f(void) {
    p1 = &i;
    int j = 2;
    {
        extern int i;
        p2 = &i;
    }
}
____

Using GCC 9.1, the first file is accepted and the “obvious” assembly code is
generated (the function f assigns the same value to p1 and p2). The second file
is rejected with the error message:

<source>: In function 'f':
<source>:9:20: error: variable previously declared 'static' redeclared 'extern'
    9 |         extern int i;
      |                    ^


Compiler Explorer link: https://gcc.godbolt.org/z/wrvZ1d

I rather agree with the error message, and my understanding of C11 6.7:3
(https://port70.net/~nsz/c/c11/n1570.html#6.7p3 ) is that both compilation
units should be rejected. In any case, the two look equivalent, and they should
probably either be both accepted or both rejected.

(If you arrive to the conclusion that the C11 standard says they should both be
rejected, I will have a similar bug to report in Clang.)

Reply via email to