[Issue 19371] Taking address of ref return in @safe code: compile-time checks fail

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Nov 10 20:56:17 UTC 2018


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

Nick Treleaven <nick at geany.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nick at geany.org

--- Comment #1 from Nick Treleaven <nick at geany.org> ---
Using __traits(compiles) works for the first two asserts:

    static assert(!__traits(compiles, &get()));
    static assert(!__traits(compiles, () @safe { return &get(); }));

is(typeof()) is not as strict, it is looking for a type rather than compiling.

>     static assert(!__traits(compiles, { auto p = &get(); }));

Here, the function literal is inferred as @system, and can even be assigned to
a variable in @safe code (but not called):

    auto f = { auto p = &get(); }; //ok
    f(); // Error: `@safe` function cannot call `@system` function pointer `f`

You can fix it by forcing @safe for the literal, or calling the literal:

    static assert(!__traits(compiles, () @safe { auto p = &get(); }));
    static assert(!__traits(compiles, { auto p = &get(); }()));

--


More information about the Digitalmars-d-bugs mailing list