https://issues.dlang.org/show_bug.cgi?id=18568
Issue ID: 18568 Summary: partially overlapping assignments have undefined behavior but are accepted in @safe code Product: D Version: D2 Hardware: All OS: All Status: NEW Keywords: safe Severity: normal Priority: P1 Component: dmd Assignee: nob...@puremagic.com Reporter: ag0ae...@gmail.com Prompted by this forum post: https://forum.dlang.org/post/kslpmklgrgwaynlbk...@forum.dlang.org On assignments, the spec says [1]: > Undefined Behavior: > 1. if the lvalue and rvalue have partially overlapping storage > 2. if the lvalue and rvalue's storage overlaps exactly but the types are > different But DMD accepts this: ---- struct S { union { int i; byte b; float f; struct { byte b2; align(1) int i2; } } } void main() @safe { S s; s.i = s.b; /* Partially overlapping, different types. */ s.f = s.i; /* Exactly overlapping, different types. */ s.i = s.i2; /* Partially overlapping, same type. */ } ---- According to the spec, all those assignments have undefined behavior. So they shouldn't be allowed in @safe code. (As always, this can be fixed by letting DMD reject the code, or by changing the spec to give the code defined behavior.) [1] https://dlang.org/spec/expression.html#assign_expressions --