D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7362 - forward referenced inner struct with align(1) doesn't add to offset of next field when used inside a union of another struct, that is nested in the same outer struct
Summary: forward referenced inner struct with align(1) doesn't add to offset of next f...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All Linux
: P2 minor
Assignee: No Owner
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2012-01-24 10:49 UTC by Marco Leise
Modified: 2015-06-09 04:41 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Marco Leise 2012-01-24 10:49:06 UTC
This happened to me when I designed data structures for CD sectors, and used an outer struct to wrap all the different sector layouts (Mode1/2 CDs, ...). The short code below is more suited for a unittest:

    struct Outer {
        struct Inner1 {
            union { 
                Inner2 a;
            } 
            byte I_AM_AT_THE_WRONG_OFFSET;
        }
    
        align(1) struct Inner2 {
            ubyte x;
        }
    }
    
    void main() {
        writeln(Outer.Inner1.I_AM_AT_THE_WRONG_OFFSET.offsetof);
    }

This prints '0', which means that field 'I_AM_AT_THE_WRONG_OFFSET' wasn't correctly offset behind field 'a'. It works correctly when
a) align(1) is removed
b) Inner2 is declared before Inner1
c) 'a' is not inside a union block
Comment 1 SomeDude 2012-04-19 14:44:01 UTC
Returns 1 on Win32 2.059
Comment 2 Marco Leise 2012-04-20 08:55:45 UTC
Yes, it has been fixed in the mean time. Add this to the solved bugs for 2.059!