[Issue 17174] can take address of member of struct parameter in @safe code

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Fri Feb 10 11:46:55 PST 2017


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

ag0aep6g at gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |accepts-invalid, safe
                 CC|                            |ag0aep6g at gmail.com
           Hardware|x86                         |All
            Summary|auto ref allows pointers to |can take address of member
                   |rvalues                     |of struct parameter in
                   |                            |@safe code
                 OS|Mac OS X                    |All

--- Comment #1 from ag0aep6g at gmail.com ---
Taking the address of a value parameter is allowed in @system code. Your
constructor is not marked as @safe, so it's @system and you're allowed to do
that.

However, if you add @safe, the code still compiles. That's a bug. It doesn't
seem to be related to `auto ref`, though. Rather, the compiler gets confused by
the struct. I'm changing the summary of this issue to be about this.

----
/* Shouldn't compile: */
void f(initStruct iS) @safe
{
    auto p = &iS.var; /* Should trigger the same error as below. */
}

struct initStruct
{
    size_t var;
}

/* Doesn't compile, as it should be: */
void g(size_t var) @safe
{
    auto p = &var; /* "Error: cannot take address of parameter" */
}
----

--


More information about the Digitalmars-d-bugs mailing list