[Issue 17284] Template function attribute inference wrongly infers @safe for accessing overlapping pointer fields in unions

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed Mar 29 14:20:43 PDT 2017


https://issues.dlang.org/show_bug.cgi?id=17284

--- Comment #6 from ZombineDev <petar.p.kirov at gmail.com> ---
> Looks like attribute inference is a free license to violate @safety. :-D

Yeah with templates one enters 'god mode' in :D

Though the situation isn't very clear-cut. Take this code for example:

```
struct S { int* ptr; }
void func1()() { S s; s.ptr++; }
pragma (msg, "typeof(&func1!()): ", typeof(&func1!()));

class C { }
union U { C c; int i; }
void func2()() { U u; u.c = null; }
pragma (msg, "typeof(&func2!()): ", typeof(&func2!()));

int[] func3()() { int[5] sa; return sa; }
pragma (msg, "typeof(&func3!()): ", typeof(&func3!()));

int[] func4a(int[] a) @safe { return a; }
int[] func4b()() { int[5] sa; return func4a(sa); }
pragma (msg, "typeof(&func4b!()): ", typeof(&func4b!()));

void main() {}
```

$ dmd safe_infer_test1.d
typeof(&func1!()): void function() pure nothrow @nogc @system
typeof(&func2!()): void function() pure nothrow @nogc @safe
typeof(&func3!()): safe_infer_test1.d(10): Error: escaping reference to local
variable sa
int[] function() pure nothrow @nogc @safe
typeof(&func4b!()): int[] function() @safe

So, depending on the code in question, sometimes inferences works correctly,
and sometimes not.

Also note that the attribute inference of func4b!() changes depending on if you
compile with -dip1000 or not:

$ dmd -dip1000 safe_infer_test1.d
typeof(&func1!()): void function() pure nothrow @nogc @system
typeof(&func2!()): void function() pure nothrow @nogc @safe
typeof(&func3!()): safe_infer_test1.d(10): Error: escaping reference to local
variable sa
int[] function() pure nothrow @nogc @safe
typeof(&func4b!()): int[] function() @system

--


More information about the Digitalmars-d-bugs mailing list