Bug 11645 – struct with union considered unsafe only in second struct literal

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-11-30T01:44:46Z
Last change time
2024-12-13T18:14:40Z
Keywords
accepts-invalid
Assigned to
No Owner
Creator
Rainer Schuetze
Moved to GitHub: dmd#18728 →

Comments

Comment #0 by r.sagitario — 2013-11-30T01:44:46Z
Compiling: /////////////// struct S { union { int[] a; int[] b; } } void foo()() { S s1 = S(); S s2 = S(); // line 19: compiles without this line } void main() @safe { foo(); } /////////////// yields: test2.d(19): Error: safe function 'D main' cannot call system function 'test2.foo!().foo' Please note, that the error does not happen with only s1 being declared. std.regex makes use of the buggy behaviour, otherwise std.regex.replace would not infer as @safe because struct Captures contains a similar union. Note: https://github.com/D-Programming-Language/dmd/pull/2480 contains a fix for this, but causes the unittests of std.regex to fail.
Comment #1 by b2.temp — 2020-07-04T17:30:32Z
Now safe is infered for foo and the test always compiles. Should this be closed ?
Comment #2 by r.sagitario — 2020-07-04T18:00:49Z
(In reply to Basile-z from comment #1) > Now safe is infered for foo and the test always compiles. Hmm, that's pretty bad. This compiles and runs but produces a memory corruption: struct S { union { int[] a; long[] b; } } void foo()() { S s1 = S(); s1.a = [1]; s1.b[0] = 1; } void main() @safe { foo(); } Broken since 2.072 according to run.dlang.io
Comment #3 by robert.schadek — 2024-12-13T18:14:40Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18728 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB