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