Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ugly code for anonymous union inside struct #297

Open
Temtaime opened this issue Aug 22, 2021 · 3 comments
Open

Ugly code for anonymous union inside struct #297

Temtaime opened this issue Aug 22, 2021 · 3 comments

Comments

@Temtaime
Copy link

struct B
{
	union
	{
		int a;
		char c;
	};
};

Produces:

extern (C)
{
	struct B
	{
		static union _Anonymous_0
		{
			int a;
			char c;
		}

		_Anonymous_0 _anonymous_1;
		ref auto a() @property @nogc pure nothrow
		{
			return _anonymous_1.a;
		}

		void a(_T_)(auto ref _T_ val) @property @nogc pure nothrow
		{
			_anonymous_1.a = val;
		}

		ref auto c() @property @nogc pure nothrow
		{
			return _anonymous_1.c;
		}

		void c(_T_)(auto ref _T_ val) @property @nogc pure nothrow
		{
			_anonymous_1.c = val;
		}
	}
}

I do not see any reasons for those properties. Anonymous unions are allowed in D.

@atilaneves
Copy link
Owner

I just copied the definition into a D file and it compiled fine. I don't remember/know why it was done this way, but given the way I work (TDD) I doubt it was for no reason but who knows. Maybe there was a reason and there isn't anymore.

In any case, the translations aren't really meant to be read - and while this is apparently unneeded, it also doesn't cause any problems. If you feel like tackling it, then PRs welcome.

@Temtaime
Copy link
Author

Temtaime commented Sep 1, 2021

There are problems.

    B b;
    int* a = &b.a;

Error: cannot implicitly convert expression &b.a of type extern (C) int delegate() pure nothrow @nogc @property ref @safe to int*

@atilaneves
Copy link
Owner

There are problems.

I would have led with that instead of "ugly code" ;)

Adding a test now.

atilaneves added a commit that referenced this issue Sep 27, 2021
atilaneves added a commit that referenced this issue Sep 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants