cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[BUG] Can not declare functions that return function pointers

Open Xeverous opened this issue 7 months ago • 2 comments

Describe the bug I copy pasted an example from wiki (https://github.com/hsutter/cppfront/wiki/Design-note:-Postfix-operators) to play with and it does not parse.

To Reproduce Sample code:

f: (i: int) -> * (j: int) -> string = { return nullptr; }

Result:

main.cpp2...
main.cpp2(1,25): error: missing ';' at end of declaration or '=' at start of initializer (at '->')
main.cpp2(1,1): error: unexpected text at end of Cpp2 code section (at 'f')
main.cpp2(1,0): error: parse failed for section starting here

Steps to reproduce the behavior: Run as ./source/cppfront main.cpp2 -p with cppfront built from commit 5aa32aef7c74679994d6da39e6d0cf9b9714e1ee.

Additional context Would be nice if it got fixed with a unit test for something like this:

// C stdlib and POSIX function
void (*signal(int signum, void (*handler)(int)))(int)
// expected C++2 syntax
signal: (signum: int, handler: *(_: int) -> void) -> *(_: int) -> void

Xeverous avatar May 30 '25 13:05 Xeverous

I took a look at this and reproduced your problem. I'll dig a little deeper, but in the meantime, here is a workaround:

intfuncptr: type == *(_: int) -> void; signal: (signum: int, handler: intfuncptr) -> intfuncptr

My guess is that the compiler is getting confused by multiple -> operators, but that's just a guess for right now. (And it gets even more confused if you put *(_: int) -> void into parentheses.)

threeifbyair avatar Jun 21 '25 16:06 threeifbyair

In case I don't come back to this, we have three different cases:

intfuncptr: type == *(_: int) -> void;
//simple_signal: (signum: int, handler: *(_: int) -> void) -> *(_: int) -> void = {
//simple_signal: (signum: int, handler: *(_: int) -> void) -> *(_: int) = {
simple_signal: (signum: int, handler: *(_: int) -> void) -> intfuncptr = {
    return handler;
}

The first fails to parse. The second complains that the _ argument in the return value has not been initialized (?!). The third works fine.

threeifbyair avatar Jun 21 '25 17:06 threeifbyair