D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 17473 - foreach on delegates incorrectly rejected
Summary: foreach on delegates incorrectly rejected
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL: http://dlang.org/
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2017-06-06 22:43 UTC by Eyal
Modified: 2024-12-13 18:52 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Eyal 2017-06-06 22:43:12 UTC
Here is an incorrect rejection of a valid program:

struct S {
    int f(int delegate(char a) dlg) {
        char c = cast(char)50;
        return dlg(c);
    }
}

unittest {
    S s;
    pragma(msg, typeof(&s.f));  // int delegate(int delegate(char a) dlg)
    foreach(x; &s.f) {}

    int delegate(int delegate(char a) dlg) g = &s.f;
    pragma(msg, typeof(g));     // int delegate(int delegate(char a) dlg)
    foreach(x; g) {}            // FAILS
}
Comment 1 Vladimir Panteleev 2017-06-07 13:26:14 UTC
Your example works here. Could you specify the compiler version and compilation flags?
Comment 2 Eyal 2017-06-07 13:40:36 UTC
(In reply to Vladimir Panteleev from comment #1)
> Your example works here. Could you specify the compiler version and
> compilation flags?

dmd --version
DMD64 D Compiler v2.074.0
Copyright (c) 1999-2017 by Digital Mars written by Walter Bright

dmd -unittest testdlg.d
int delegate(int delegate(char a) dlg)
int delegate(int delegate(char a) dlg)
testdlg.d(15): Error: delegate g (int delegate(char a) dlg) is not callable using argument types (int delegate(ref char __applyArg0) pure nothrow @nogc @safe)


Perhaps you didn't pass -unittest?
Comment 3 Vladimir Panteleev 2017-06-07 13:42:50 UTC
(In reply to Eyal from comment #2)
> Perhaps you didn't pass -unittest?

Yep, my bad
Comment 4 Vladimir Panteleev 2017-06-07 13:44:24 UTC
Adding `ref` to `char a` fixes compilation, but it's still weird that `ref`'s presence is inconsistently needed.
Comment 5 Eyal 2017-06-07 22:04:35 UTC
(In reply to Vladimir Panteleev from comment #4)
> Adding `ref` to `char a` fixes compilation, but it's still weird that
> `ref`'s presence is inconsistently needed.

It also changes the meaning of the program in an undesired way.
Comment 6 Stanislav Blinov 2021-12-08 19:15:52 UTC
This probably should be changed to accepts-invalid instead. Foreach is documented as taking delegates with a ref parameter:

https://dlang.org/spec/statement.html#foreach-statement

...therefore that the line `foreach(x; &s.f);` compiles should be an error, shouldn't it?
Comment 7 dlangBugzillaToGithub 2024-12-13 18:52:24 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/19256

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB