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 }
Your example works here. Could you specify the compiler version and compilation flags?
(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?
(In reply to Eyal from comment #2) > Perhaps you didn't pass -unittest? Yep, my bad
Adding `ref` to `char a` fixes compilation, but it's still weird that `ref`'s presence is inconsistently needed.
(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.
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?
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