D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3551 - nested struct => dmd adds a hidden pointer
Summary: nested struct => dmd adds a hidden pointer
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other All
: P2 regression
Assignee: No Owner
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2009-11-25 02:16 UTC by nfxjfg
Modified: 2015-06-09 01:27 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description nfxjfg 2009-11-25 02:16:13 UTC
Take this code:

void main() {
    struct X {
        int x;
        int bar() { return x; }
    }
    X s;
}

dmd2 adds a hidden pointer to struct X. That means s.sizeof==8. For extra buggyness, the hidden pointer appears in s.tupleof.

This silently breaks many uses of structs. Especially it breaks C compatibility, the bit-layout for asm code, and whatever you think of.

At the very least, dmd should never add a hidden pointer if that hidden pointer gets is not needed.

Actually, I consider this an anti-feature, because dmd should NEVER add (non-static) hidden fields to structs. The request to fix or remove this feature is part of this bug report. The language spec should also be fixed. Note that even if the hidden pointer is added only if needed, this is still bug-prone, because it requires complicated semantic analysis just to tell, whether the struct's size got messed up.

I propose to add a "dynamic" attribute for nested structs. If a struct is tagged with "dynamic", it can reference variables from its enclosing scope, and may need a hidden pointer.
Comment 1 Don 2009-11-25 02:21:53 UTC
(In reply to comment #0)
> Take this code:
> 
> void main() {
>     struct X {
>         int x;
>         int bar() { return x; }
>     }
>     X s;
> }
> 
> dmd2 adds a hidden pointer to struct X. That means s.sizeof==8. For extra
> buggyness, the hidden pointer appears in s.tupleof.
> 
> This silently breaks many uses of structs. Especially it breaks C
> compatibility, the bit-layout for asm code, and whatever you think of.
> 
> At the very least, dmd should never add a hidden pointer if that hidden pointer
> gets is not needed.
> 
> Actually, I consider this an anti-feature, because dmd should NEVER add
> (non-static) hidden fields to structs. The request to fix or remove this
> feature is part of this bug report. The language spec should also be fixed.
> Note that even if the hidden pointer is added only if needed, this is still
> bug-prone, because it requires complicated semantic analysis just to tell,
> whether the struct's size got messed up.
> 
> I propose to add a "dynamic" attribute for nested structs. If a struct is
> tagged with "dynamic", it can reference variables from its enclosing scope, and
> may need a hidden pointer.

In the spec, that's what static structs are for. Nested _anything_ has this behaviour. Declare it as a static struct if you don't want the hidden pointer. Please close this bug as invalid, and create a new one: "static nested structs are not implemented".
Comment 2 Walter Bright 2009-11-30 02:22:08 UTC
Declaring the struct as static should work, I'll make that fix.
Comment 3 Walter Bright 2009-12-06 00:55:04 UTC
Fixed dmd 2.037