D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21580 - The D compiler adds an extra (void*) member in unittest
Summary: The D compiler adds an extra (void*) member in unittest
Status: RESOLVED DUPLICATE of issue 10276
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-01-25 08:56 UTC by cbleser
Modified: 2021-01-25 12:27 UTC (History)
1 user (show)

See Also:


Attachments
Example (469 bytes, application/x-dsrc)
2021-01-25 08:56 UTC, cbleser
Details

Note You need to log in before you can comment on or make changes to this issue.
Description cbleser 2021-01-25 08:56:05 UTC
Created attachment 1814 [details]
Example

I discovered something odd when I put a struct into an unittest.

If you place a struct inside an unittest with a mixin template the compiler adds an extract (void*) element to the struct.

carsten@spaceship:/tmp> dmd unittest_odd_void_ptr.d -unittest
(int, string)
(int, string, void*)
(int, string)
carsten@spaceship:/tmp> ldc2 unittest_odd_void_ptr.d -unittest
(int, string)
(int, string, void*)
(int, string)

See the attached code.

It is not a practical problem. I just found it a little odd and properly there is an explanation.

Keep up the good work.
Best Regards
Carsten
Comment 1 Simen Kjaeraas 2021-01-25 12:27:38 UTC
This is mostly expected. When a nested struct has a method, that method has access to the context of the function in which it is defined, so you can do things like this:

void fun() {
    int i = 0;
    struct S {
        void gun() { ++i; }
    }
    S s;
    s.gun();
    assert(i == 1);
}

Since the nested struct may be passed to other functions or even returned from the function that created it, this context pointer needs to be embedded in the struct.

Regardless, in your case the context pointer is strictly unnecessary, and could be elided. That makes this issue a duplicate of issue 10276.

The workaround for this issue is to mark the struct as static.

*** This issue has been marked as a duplicate of issue 10276 ***