Bug 62116 - Allowing redeclaration of global variable x using ::x
Summary: Allowing redeclaration of global variable x using ::x
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.9.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2014-08-13 03:17 UTC by Shafik Yaghmour
Modified: 2021-07-23 12:14 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-07-22 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Shafik Yaghmour 2014-08-13 03:17:00 UTC
Given the following code:

int x;

int main() {
    int(::x); //does not compile
    int(::x + 2); //compiles
}

gcc 4.9 will compile it without an error while gcc 4.8.x and clang 3.4 will generate an error. For gcc 4.8.x the error is:

error: invalid use of qualified-name '::x'
     int(::x); //does not compile
            ^

and clang gives the following error:

error: definition or redeclaration of 'x' cannot name the global scope
    int(::x); //does not compile
        ~~^

as far as I can tell both gcc 4.8.x and clang are correct here based on my reading of section 8.3 paragraph 6:

int(::x) ;

is equivalent to:

int ::x ;

which is not valid.

The problem originally cam up in the following Stackoverflow question:

http://stackoverflow.com/questions/24623071/is-typex-valid
Comment 1 Jonathan Wakely 2014-08-13 11:31:21 UTC
(In reply to Shafik Yaghmour from comment #0)
> as far as I can tell both gcc 4.8.x and clang are correct here based on my
> reading of section 8.3 paragraph 6:
> 
> int(::x) ;
> 
> is equivalent to:
> 
> int ::x ;
> 
> which is not valid.

Current G++ and EDG both treat it as the valid expression (int)::x
Comment 2 Shafik Yaghmour 2014-08-13 15:29:38 UTC
I am happy to be mistaken here, but it seems like section 6.8 paragraph 1 applies, for example if we have the following:

int(y) = 10;

it is being treated as a declaration not a cast and further more section 6.8 comments on an ill-formed example and says:

This is of course ill-formed for semantic reasons, but that does not affect the syntactic analysis.

so even though:

int(::x);

would be ill-formed we are forced to treat it as a declaration and not a function style cast.
Comment 3 Richard Smith 2015-02-19 23:12:22 UTC
This is a GCC bug (or extension). Expression / declaration disambiguation is purely syntactic, and 'int(::x);' conforms to the grammar for a simple-declaration.

I've also reported the bug to EDG.
Comment 4 Jonathan Wakely 2015-02-20 09:51:24 UTC
Confirmed then. Thanks, Richard.
Comment 5 TC 2015-03-14 06:52:49 UTC
Somewhat related: http://stackoverflow.com/questions/28955859

struct Foo {
     enum { bar };
     explicit Foo(int){}
};

struct Baz { explicit Baz(Foo){} };

Baz b(Foo(Foo::bar)); // 1

Line #1 should be ill-formed because it fits the grammar for a function declaration and is disambiguated as such. GCC considers it a variable declaration instead. Clang rejects it - see https://llvm.org/bugs/show_bug.cgi?id=4594.