Bug 17473 – foreach on delegates incorrectly rejected

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-06-06T22:43:12Z
Last change time
2024-12-13T18:52:24Z
Keywords
rejects-valid
Assigned to
No Owner
Creator
Eyal
Moved to GitHub: dmd#19256 →

Comments

Comment #0 by eyal — 2017-06-06T22:43:12Z
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 by dlang-bugzilla — 2017-06-07T13:26:14Z
Your example works here. Could you specify the compiler version and compilation flags?
Comment #2 by eyal — 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?
Comment #3 by dlang-bugzilla — 2017-06-07T13:42:50Z
(In reply to Eyal from comment #2) > Perhaps you didn't pass -unittest? Yep, my bad
Comment #4 by dlang-bugzilla — 2017-06-07T13:44:24Z
Adding `ref` to `char a` fixes compilation, but it's still weird that `ref`'s presence is inconsistently needed.
Comment #5 by eyal — 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.
Comment #6 by stanislav.blinov — 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?
Comment #7 by robert.schadek — 2024-12-13T18:52:24Z
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