Bug 16093 – Trivial case of passing a template function to another template function doesn't compile

Status
NEW
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2016-05-29T12:54:07Z
Last change time
2024-12-13T18:48:08Z
Assigned to
No Owner
Creator
Max Samukha
Moved to GitHub: dmd#19135 →

Comments

Comment #0 by maxsamukha — 2016-05-29T12:54:07Z
void bar(alias f)() { f(); } void main() { void f()() { } bar!f(); } Error: function test.main.f!().f is a nested function and cannot be accessed from test.bar!(f).bar Non-template nested functions are accepted: void main() { void f() { } bar!f(); // ok }
Comment #1 by b2.temp — 2016-05-29T17:02:36Z
It's because f is local. When f is static (i.e like a free function) it works void bar(alias f)() { f(); } void main() { static void f()() { } bar!f(); } The doc is not clear about this case. So even if this is invalid the doc needs at least to list exactly what's supported or not. https://issues.dlang.org/show_bug.cgi?id=5108
Comment #2 by maxsamukha — 2016-05-29T18:08:36Z
(In reply to b2.temp from comment #1) > It's because f is local. When f is static (i.e like a free function) it works > void main() { int x = 1; void f() { x += 1; } bar!f(); // ok } 'f' above is local, and the compiled program works as expected. Can't see why making it a template should affect the semantics. Also, if the following didn't compile, the whole "pass functions by alias" business would go down the drain: void bar(alias f)() { f(1); } void main() { int y = 1; alias f = (x) { y += x; }; // local, equivalent to "void f(A)(A x) { y += x; }" bar!f(); // ok }
Comment #3 by maxsamukha — 2016-05-30T13:23:09Z
Raising severity because I've been unable to find an acceptable workaround. To make the absurdity of the current situation more obvious, it is possible to access a local template function if it is wrapped in a struct: void bar(alias f)() { f._f!(1, 2)(); } void main() { int y; void f(A...)() { y = A[0] + A[1]; } struct S { alias _f = f; } S s; bar!s(); assert(y == 3); // ok } This bug might be a regression as well because I am almost sure I didn't have this problem a couple of years ago.
Comment #4 by maxsamukha — 2016-05-31T10:50:13Z
(In reply to Max Samukha from comment #0) > void bar(alias f)() { > f(); > } > > void main() { > void f()() { > } > bar!f(); > } > > Error: function test.main.f!().f is a nested function and cannot be accessed > from test.bar!(f).bar > Note that it compiles ok with -inline.
Comment #5 by robert.schadek — 2024-12-13T18:48:08Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19135 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB