D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4505 - Type literal of pure function pointer inside function signature
Summary: Type literal of pure function pointer inside function signature
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
: 10603 (view as issue list)
Depends on:
Blocks:
 
Reported: 2010-07-25 07:47 UTC by bearophile_hugs
Modified: 2020-08-06 14:30 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010-07-25 07:47:43 UTC
D2 code, foo4() shows that you can't define a pure function pointer in a function signature:


pure int sqr(int x) {
    return x * x;
}
pure int foo1(TF)(TF func, int x) { // OK
    return func(x);
}
pure int foo2(typeof(&sqr) func, int x) { // OK
    return func(x);
}
alias pure int function(int) FN;
pure foo3(FN func, int x) { // OK, from Simen kjaeraas
    return func(x);
}
pure int foo4(pure int function(int) func, int x) { // line 14, ERR
    return func(x);
}
void main() {
    assert(foo1(&sqr, 5) == 25);
    assert(foo2(&sqr, 5) == 25);
    assert(foo3(&sqr, 5) == 25);
    assert(foo4(&sqr, 5) == 25);
}


DMD 2.047 prints:
test.d(14): basic type expected, not pure
test.d(14): found 'pure' when expecting ')'
test.d(14): semicolon expected following function declaration
test.d(14): no identifier for declarator int function(int)
test.d(14): semicolon expected, not 'int'
test.d(14): semicolon expected, not ')'
test.d(14): Declaration expected, not ')'
test.d(16): unrecognized declaration


Also, it generates too many error messages.
Comment 1 Andrej Mitrovic 2010-08-29 19:11:49 UTC
I think this is a problem of the keyword pure trying to act as a type specifier for the return type ( See also my bug 4734 for a similar issue).

If you put pure after the function definition but before the identifier, like so:

pure int foo4(int function(int) pure func, int x) {

then your example compiles and all asserts pass.
Comment 2 Kenji Hara 2015-03-03 03:57:34 UTC
*** Issue 10603 has been marked as a duplicate of this issue. ***
Comment 3 Mathias LANG 2020-08-06 14:30:40 UTC
As mentioned by Andrej, it's possible if you put the keyword on the RHS, which is the recommended way to do it nowadays. I don't think that's an issue worth fixing, so marking as WONTFIX.