According to the discussion here:
https://forum.dlang.org/thread/[email protected]
the following code should compile, but it does not.
void main() // line 1
{
auto s = S();
auto t = T!s(); // line 4
t.fun;
}
struct S { void fun(){} }
struct T(alias s){ static fun() { s.fun; } } // line 8
The error is:
source/app.d(8,35): Error: static function app.main.T!(s).T.fun cannot access frame of function D main
source/app.d(4,14): Error: template instance app.main.T!(s) error instantiating
Comment #1 by aurelien.fredouelle+dlang — 2019-05-13T16:27:08Z
Ran into the same issue, here is some extra sample code to better understand the problem:
template A(alias fun)
{
void A() {
fun();
}
}
struct B(alias fun)
{
static void opCall() {
fun();
}
}
void foo() {}
void main() {
static void bar() {}
void hun() {}
A!foo(); // OK
B!foo(); // OK
A!bar(); // OK
B!bar(); // OK
A!hun(); // OK
B!hun(); // ERROR! static function app.main.B!(hun).B.opCall cannot access frame of function D main
}
The issue happens when instantiating the templated struct with an object that lives on the stack of the calling function (such as the `hun` function). But it looks like this case is not so different from the previous one (`A!hun()`), using an eponymous function template rather than a struct with a static opCall. Is it really expected for one case to succeed and the other to fail?
Comment #2 by razvan.nitu1305 — 2023-05-05T13:37:45Z
This is not a bug. The alias parameter can be seen as a pointer that is stored inside the declaring struct. Since fun is static it should have no context pointer, therefore it cannot access the alias parameter.
> Is it really expected for one case to succeed and the other to fail?
Yes, because your opCall is static and cannot access anything in B's context. If you declare `A()` as static you will end up with the same error message.