[Issue 6705] Bad codegen when passing fields as template alias params

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Sep 15 06:20:30 UTC 2020


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

Walter Bright <bugzilla at digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|backend, rejects-valid,     |accepts-invalid, safe
                   |wrong-code                  |

--- Comment #4 from Walter Bright <bugzilla at digitalmars.com> ---
Here's what's happening:

When C.a is passed as an alias argument to func(), then func becomes (for that
instantiation) a member of C. Being a member function, then it expects a 'this'
pointer. But when a(5) is called, there is no 'this' pointer, and the '5' is
interpreted as the 'this' pointer, and 'i' is garbage.

The same problem appears with this simpler example:

    class C
    {
        int a;
        int member(long);
    }

    void moon()
    {
        auto f = &C.member;  // creates `int function(long)`
        f();  // oops, no `this`
    }

It does need to be possible to take the address of a member function without
the 'this', and if we make that an error, we may break existing legitimate
code.

Note the following:

    int delegate(long) dg;
    pragma(msg, typeof(dg.funcptr));

prints:

    int function(long)

so changing the type of &C.member to `void*` isn't going to work, either.

meaning one can construct delegates by using &C.member for dg.funcptr and some
void* for dg.ptr.

What I can do is make taking the address of a member without `this` not allowed
in @safe code.

--


More information about the Digitalmars-d-bugs mailing list