Bug 11427 – anonymous unions break structs in @safe code

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-11-03T01:52:00Z
Last change time
2013-12-17T04:05:04Z
Keywords
pull, rejects-valid
Assigned to
nobody
Creator
r.sagitario

Comments

Comment #0 by r.sagitario — 2013-11-03T01:29:43Z
As far s I can see, the problem is that there is a check for pointers in unions in VarDeclaration::semantic, but that relies on AggregateDeclaration::hasUnions, which is set in StructLiteralExp::fill whenever there are overlapping fields. So in addition to the reported problem, a pointer in an anonymous union gets through unnoticed in @safe code: struct S { union { ubyte a; int x; void[] arr; } } int foo() @safe { S s2; return s2.x; }
Comment #1 by r.sagitario — 2013-11-03T01:32:10Z
Hmmm, something wrong with bugzilla? My comment replaced the description, and the initial description is now moved into the first comment.
Comment #2 by r.sagitario — 2013-11-03T01:52:10Z
Compile this code /////////////////// struct S { union { ubyte a; int x; } void[] arr; } int foo() @safe { S s1 = S(); S s2; return 0; } //////////////////// with "dmd -c test.d" fails with test.d(14): Error: variable test.foo.s2 unions containing pointers are not allowed in @safe functions Please note that the first variable s1 is ok! This is also triggered in std.format with this code: import std.format; import std.array; void foo() { FormatSpec!char spec = FormatSpec!char(); auto s = appender!string; formattedWrite(s, "%s", "hello"); }
Comment #3 by k.hara.pg — 2013-11-13T07:35:24Z
Comment #4 by github-bugzilla — 2013-12-16T11:21:17Z