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

Walter Bright <[email protected]> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |[email protected]

--- Comment #6 from Walter Bright <[email protected]> ---
The reason for the frame pointer is:

void test(int i)
{
    struct S
    {
        int x;
        int f() { return x + i; } // reference to `i`
    }
    pragma(msg, S.sizeof); // prints 16
}

so member functions can access variables in the function the struct is nested
in. To avoid this, use `static`:

void test(int i)
{
    static struct S
    {
        int x;
        int f() { return x + i; }
    }
    pragma(msg, S.sizeof); // prints 4
}

test2.d(7): Error: function `test2.test.S.f` cannot access variable `i` in
frame of function `test2.test`
test2.d(2):        `i` declared here

The compiler does a conservative guess as to whether a frame pointer is needed.
A non-static member function will trip that. The compiler does not look at the
body of the function to determine this, because the body may be defined
elsewhere.

The alias parameter also trips the "needs a frame pointer" for similar reasons.

To resolve the problem, declare the template as `static`:

static struct PostBlitted(alias al){
  this(this){}
  void x(){} //a member function so that an instance contains a frame pointer
}

static struct CopyConstructed(alias al){
  this(ref typeof(this)){};
  void x(){} //a member function so that an instance contains a frame pointer
}

The fix for this bug should be to point this out in the spec.

--

Reply via email to