www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Cannot access frame pointer of a struct with member function

reply Andrey Zherikov <andrey.zherikov gmail.com> writes:
Compilation of this code:
```d
auto foo(T)()
{
     return T();   // Error: cannot access frame pointer of 
`onlineapp.main.T`
}

void main()
{
     struct T
     {
         int a=1;
         void argsFunc(int a) {} // (1)
     }

     pragma(msg,foo!T());
}
```
fails with this error:
```
onlineapp.d(3): Error: cannot access frame pointer of 
`onlineapp.main.T`
onlineapp.d(14): Error: template instance `onlineapp.foo!(T)` 
error instantiating
onlineapp.d(14):        while evaluating `pragma(msg, foo!(T)())`
```
But when I comment out argsFunc function on line (1), it compiles 
successfully meaning that there is no issue to access frame 
pointer.

Is this a bug?
May 09
next sibling parent evilrat <evilrat666 gmail.com> writes:
On Sunday, 9 May 2021 at 17:37:40 UTC, Andrey Zherikov wrote:
 Compilation of this code:
 ```d
 auto foo(T)()
 {
     return T();   // Error: cannot access frame pointer of 
 `onlineapp.main.T`
 }

 void main()
 {
     struct T
     {
         int a=1;
         void argsFunc(int a) {} // (1)
     }

     pragma(msg,foo!T());
 }
 ```
 fails with this error:
 ```
 onlineapp.d(3): Error: cannot access frame pointer of 
 `onlineapp.main.T`
 onlineapp.d(14): Error: template instance `onlineapp.foo!(T)` 
 error instantiating
 onlineapp.d(14):        while evaluating `pragma(msg, 
 foo!(T)())`
 ```
 But when I comment out argsFunc function on line (1), it 
 compiles successfully meaning that there is no issue to access 
 frame pointer.

 Is this a bug?
Most likely because function member types have access to the scope, it complains because doing so will escape the scope of that function. If you make struct static this erorr should go away.
May 09
prev sibling next sibling parent reply SealabJaster <sealabjaster gmail.com> writes:
On Sunday, 9 May 2021 at 17:37:40 UTC, Andrey Zherikov wrote:
 Is this a bug?
My best guess is that, since the struct doesn't have any member functions the compiler has decided that the struct doesn't need any access to the main function's context/frame pointer, so the struct has implicitly become a `static struct`. Marking the struct `static` yourself while keeping the member function, will show that it can then compile. You can also move your `foo` function into the main function and it'll compile. Oddly the output ends up becoming: `T(1, null)` which I didn't really expect. Just a guess though.
May 09
parent Andrey Zherikov <andrey.zherikov gmail.com> writes:
On Sunday, 9 May 2021 at 17:53:07 UTC, SealabJaster wrote:
 On Sunday, 9 May 2021 at 17:37:40 UTC, Andrey Zherikov wrote:
 Is this a bug?
My best guess is that, since the struct doesn't have any member functions the compiler has decided that the struct doesn't need any access to the main function's context/frame pointer, so the struct has implicitly become a `static struct`. Marking the struct `static` yourself while keeping the member function, will show that it can then compile. You can also move your `foo` function into the main function and it'll compile. Oddly the output ends up becoming: `T(1, null)` which I didn't really expect. Just a guess though.
That's a point! Thanks you!
May 09
prev sibling parent SealabJaster <sealabjaster gmail.com> writes:
On Sunday, 9 May 2021 at 17:37:40 UTC, Andrey Zherikov wrote:
 ...
https://run.dlang.io/is/sCUdXe shows this a bit more clearly.
May 09