Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

foreach on delegates incorrectly rejected #19256

Open
dlangBugzillaToGithub opened this issue Jun 6, 2017 · 6 comments
Open

foreach on delegates incorrectly rejected #19256

dlangBugzillaToGithub opened this issue Jun 6, 2017 · 6 comments

Comments

@dlangBugzillaToGithub
Copy link

Eyal reported this on 2017-06-06T22:43:12Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=17473

CC List

  • ag0aep6g
  • Vladimir Panteleev (@CyberShadow)
  • Stanislav Blinov

Description

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
}
@dlangBugzillaToGithub
Copy link
Author

dlang-bugzilla (@CyberShadow) commented on 2017-06-07T13:26:14Z

Your example works here. Could you specify the compiler version and compilation flags?

@dlangBugzillaToGithub
Copy link
Author

eyal commented on 2017-06-07T13:40:36Z

(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?

@dlangBugzillaToGithub
Copy link
Author

dlang-bugzilla (@CyberShadow) commented on 2017-06-07T13:42:50Z

(In reply to Eyal from comment #2)
> Perhaps you didn't pass -unittest?

Yep, my bad

@dlangBugzillaToGithub
Copy link
Author

dlang-bugzilla (@CyberShadow) commented on 2017-06-07T13:44:24Z

Adding `ref` to `char a` fixes compilation, but it's still weird that `ref`'s presence is inconsistently needed.

@dlangBugzillaToGithub
Copy link
Author

eyal commented on 2017-06-07T22:04:35Z

(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.

@dlangBugzillaToGithub
Copy link
Author

stanislav.blinov commented on 2021-12-08T19:15:52Z

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant