[Issue 22239] Can't migrate from postblits if they are used without frame pointer

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Apr 2 05:00:41 UTC 2024


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

Walter Bright <bugzilla at digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla at digitalmars.com

--- Comment #6 from Walter Bright <bugzilla at digitalmars.com> ---
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.

--


More information about the Digitalmars-d-bugs mailing list