Bug 392166 - fields of struct into anonymous union into struct not visible as most external struct fields
Summary: fields of struct into anonymous union into struct not visible as most externa...
Status: NEW
Alias: None
Product: CDT
Classification: Tools
Component: cdt-parser (show other bugs)
Version: 8.0.2   Edit
Hardware: PC Windows XP
: P3 normal with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact: Jonah Graham CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-10-17 09:44 EDT by Yves Guerte CLA
Modified: 2020-09-04 15:20 EDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Yves Guerte CLA 2012-10-17 09:44:06 EDT
/* this file compiles with xlc */

typedef struct info
{
    unsigned int flags;
    union {
        struct other_project
        {
            unsigned int : 0;
        };
        struct current_project
        {
            int objectType;
        };
    };
}info_t;

int main(int argc, char *argv[])
{
    info_t test;
    test.flags = 0;

    /* completion works fine for next line */
    test.objectType = 1;  /* Eclipse shows: "Field 'objectType' could not be resolved" */

    return 0;
}
Comment 1 AK CLA 2013-09-27 03:08:05 EDT
I do not know if your code is correct and anonymous structures in a union are allowed. Change your program to get named structures and your error will vanish:

#include <iostream>
using namespace std;

typedef struct info
{
    unsigned int flags;
    union {
        struct other_project
        {
            unsigned int : 0;
        } op;
        struct current_project
        {
            int objectType;
        } cp;
    };
}info_t;

int main(int argc, char *argv[])
{
    info_t test;
    test.flags = 0;

    test.cp.objectType = 1;
    return 0;
}
Comment 2 AK CLA 2013-09-27 03:12:08 EDT
Sorry, I was to fast. :-) You need also a name for your union.


typedef struct info
{
    unsigned int flags;
    union {
        struct other_project
        {
            unsigned int : 0;
        } op;
        struct current_project
        {
            int objectType;
        } cp;
    } union_t;
} info_t;

int main(int argc, char *argv[])
{
    info_t test;
    test.flags = 0;

    test.union_t.cp.objectType = 1;
    return 0;
}
Comment 3 AK CLA 2013-09-27 08:01:54 EDT
In C++ an union is a class. According C++11 draft chapter 9.5 paragraph 5 "[...] Nested types and functions cannot be declared within an anonymous union.[...]". But this is what you did. You can change your code in this way to stay with an anonymous union:


#include <iostream>
using namespace std;

struct other_project
{
    unsigned int :0;
};
struct current_project
{
    int objectType;
};

typedef struct info
{
    unsigned int flags;
    union
    {
        struct other_project op;
        struct current_project cp;
    };
} info_t;

int main( int argc, char *argv[] )
{
    info_t test;
    test.flags = 0;

    test.cp.objectType = 1;

    return 0;
}
Comment 4 AK CLA 2013-09-27 09:16:41 EDT
Of course you can define (not declare) structs inside your union

typedef struct info
{
    unsigned int flags;
    union
    {
        struct
        {
            unsigned int :0;
        } other_project;
        struct
        {
            int objectType;
        } current_project;
    };
} info_t;



Now you can access the union members by

    test.current_project.objectType = 1;
Comment 5 Nathan Ridge CLA 2013-12-15 02:32:09 EST
I agree with AK that the code in comment #0 is not valid C or C++. Since Yves says it compiles with Xlc, I guess it's an Xlc extension.

I don't know whether we accept patches implementing Xlc extensions into our C++ parser. If so, we should leave this bug open and mark it as being a request for implementing an Xlc extension. If not, we should close this bug as invalid.